This repository has been archived by the owner on Apr 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Mapper.java
1061 lines (897 loc) · 48.6 KB
/
Mapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (C) 2009 eXo Platform SAS.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.exoplatform.portal.pom.data;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Pattern;
import org.exoplatform.portal.config.NoSuchDataException;
import org.exoplatform.portal.config.StaleModelException;
import org.exoplatform.portal.config.UserACL;
import org.exoplatform.portal.config.model.ApplicationState;
import org.exoplatform.portal.config.model.ApplicationType;
import org.exoplatform.portal.config.model.CloneApplicationState;
import org.exoplatform.portal.config.model.Container;
import org.exoplatform.portal.config.model.PersistentApplicationState;
import org.exoplatform.portal.config.model.PortalConfig;
import org.exoplatform.portal.config.model.TransientApplicationState;
import org.exoplatform.portal.mop.Described;
import org.exoplatform.portal.mop.ProtectedContainer;
import org.exoplatform.portal.mop.ProtectedResource;
import org.exoplatform.portal.mop.redirects.Condition;
import org.exoplatform.portal.mop.redirects.DeviceProperty;
import org.exoplatform.portal.mop.redirects.Mappings;
import org.exoplatform.portal.mop.redirects.NodeMap;
import org.exoplatform.portal.mop.redirects.Redirect;
import org.exoplatform.portal.mop.redirects.Redirectable;
import org.exoplatform.portal.pom.config.POMSession;
import org.exoplatform.portal.pom.config.Utils;
import org.exoplatform.portal.pom.spi.portlet.Portlet;
import org.exoplatform.services.jcr.util.Text;
import org.gatein.common.logging.Logger;
import org.gatein.common.logging.LoggerFactory;
import org.gatein.mop.api.Attributes;
import org.gatein.mop.api.content.ContentType;
import org.gatein.mop.api.content.Customization;
import org.gatein.mop.api.workspace.ObjectType;
import org.gatein.mop.api.workspace.Site;
import org.gatein.mop.api.workspace.Templatized;
import org.gatein.mop.api.workspace.WorkspaceObject;
import org.gatein.mop.api.workspace.ui.UIBody;
import org.gatein.mop.api.workspace.ui.UIComponent;
import org.gatein.mop.api.workspace.ui.UIContainer;
import org.gatein.mop.api.workspace.ui.UIWindow;
import org.gatein.mop.core.util.Tools;
/**
* @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
* @version $Revision$
*/
public class Mapper {
/** . */
private static final Set<String> propertiesBlackList = Tools.set("jcr:uuid", "jcr:primaryType");
/** . */
private static final Set<String> portalPropertiesBlackList = Tools.set(MappedAttributes.LOCALE.getName(),
MappedAttributes.SKIN.getName());
/** . */
private static final Set<String> windowPropertiesBlackList = Tools.set(MappedAttributes.THEME.getName(),
MappedAttributes.TYPE.getName(), MappedAttributes.ICON.getName(), MappedAttributes.WIDTH.getName(),
MappedAttributes.HEIGHT.getName());
/** . */
private final POMSession session;
/** . */
private final Logger log = LoggerFactory.getLogger(Mapper.class);
public Mapper(POMSession session) {
this.session = session;
}
public PortalData load(Site src) {
String type = Mapper.getOwnerType(src.getObjectType());
Attributes attrs = src.getAttributes();
//
Templatized templarized = src.getRootNavigation().getTemplatized();
org.gatein.mop.api.workspace.Page template = templarized.getTemplate();
UIContainer srcLayout = template.getRootComponent();
//
Map<String, String> properties = new HashMap<String, String>();
load(attrs, properties, portalPropertiesBlackList);
//
List<ComponentData> layoutChildren = loadChildren(srcLayout);
ContainerData layout = load(srcLayout, layoutChildren);
//
List<String> accessPermissions = Collections.emptyList();
String editPermission = null;
if (src.isAdapted(ProtectedResource.class)) {
ProtectedResource pr = src.adapt(ProtectedResource.class);
accessPermissions = pr.getAccessPermissions();
editPermission = pr.getEditPermission();
}
//
List<RedirectData> redirects = null;
if (src.isAdapted(Redirectable.class)) {
Redirectable redirectAble = src.adapt(Redirectable.class);
redirects = loadRedirects(src, redirectAble.getRedirects());
}
Described described = src.adapt(Described.class);
//
return new PortalData(src.getObjectId(), src.getName(), type, attrs.getValue(MappedAttributes.LOCALE),
described.getName(), described.getDescription(), accessPermissions, editPermission,
Collections.unmodifiableMap(properties), attrs.getValue(MappedAttributes.SKIN), layout, redirects);
}
private List<RedirectData> loadRedirects(Site src, Map<String, Redirect> redirects) {
if (redirects == null || redirects.isEmpty()) {
return null;
} else {
List<RedirectData> redirectsData = new ArrayList<RedirectData>();
for (Redirect redirect : redirects.values()) {
RedirectData redirectDate = new RedirectData(src.getObjectId(), redirect.getSite(), redirect.getName(),
redirect.getEnabled(), buildConditionData(src, redirect.getConditions()), buildMappingsData(
src.getObjectId(), redirect.getMapping()));
redirectsData.add(redirectDate);
}
return redirectsData;
}
}
private List<RedirectConditionData> buildConditionData(Site src, Map<String, Condition> conditions) {
List<RedirectConditionData> conditionDatas = new ArrayList<RedirectConditionData>();
for (Condition condition : conditions.values()) {
RedirectConditionData conditionData = new RedirectConditionData(src.getObjectId(), null, condition.getName());
RedirectUserAgentConditionData userAgentConditionData = new RedirectUserAgentConditionData(src.getObjectId(), null);
userAgentConditionData.getUserAgentContains().addAll(condition.getUserAgentContains());
userAgentConditionData.getUserAgentDoesNotContain().addAll(condition.getUserAgentDoesNotContain());
conditionData.setUserAgentConditionData(userAgentConditionData);
for (DeviceProperty deviceProperty : condition.getDeviceProperties().values()) {
RedirectDevicePropertyConditionData propertyConditionData = new RedirectDevicePropertyConditionData(
src.getObjectId(), null, deviceProperty.getName());
if (deviceProperty.getEquals() != null) {
propertyConditionData.setEquals(deviceProperty.getEquals());
}
if (deviceProperty.getGreaterThan() != null) {
propertyConditionData.setGreaterThan(deviceProperty.getGreaterThan());
}
if (deviceProperty.getLessThan() != null) {
propertyConditionData.setLessThan(deviceProperty.getLessThan());
}
if (deviceProperty.getPattern() != null) {
propertyConditionData.setMatches(Pattern.compile(deviceProperty.getPattern()));
}
conditionData.getDevicePropertyConditionData().add(propertyConditionData);
}
conditionDatas.add(conditionData);
}
return conditionDatas;
}
private RedirectMappingsData buildMappingsData(String storageId, Mappings mappings) {
if (mappings != null) {
RedirectMappingsData redirectMappingsData = new RedirectMappingsData(storageId);
if (mappings.getUnresolvedNodeMatching() != null) {
redirectMappingsData.setUnresolvedNode(mappings.getUnresolvedNodeMatching());
}
if (mappings.getNodeNameMatching() != null) {
redirectMappingsData.setUseNodeNameMatching(mappings.getNodeNameMatching());
}
if (mappings.getNodeMap() != null) {
HashMap<String, String> map = new HashMap<String, String>();
for (NodeMap nodeMap : mappings.getNodeMap().values()) {
map.put(nodeMap.getOriginNode(), nodeMap.getRedirectNode());
}
redirectMappingsData.getMappings().putAll(map);
}
return redirectMappingsData;
} else {
return null;
}
}
public void save(PortalData src, Site dst) {
try {
if (src.getStorageId() != null && !src.getStorageId().equals(dst.getObjectId())) {
String msg = "Attempt to save a site " + src.getType() + "/" + src.getName() + " on the wrong target site "
+ dst.getObjectType() + "/" + dst.getName();
throw new IllegalArgumentException(msg);
}
//
Attributes attrs = dst.getAttributes();
attrs.setValue(MappedAttributes.LOCALE, src.getLocale());
attrs.setValue(MappedAttributes.SKIN, src.getSkin());
if (src.getProperties() != null) {
save(src.getProperties(), attrs, portalPropertiesBlackList);
}
ProtectedResource pr = dst.adapt(ProtectedResource.class);
pr.setAccessPermissions(src.getAccessPermissions());
pr.setEditPermission(src.getEditPermission());
Described described = dst.adapt(Described.class);
described.setName(src.getLabel());
described.setDescription(src.getDescription());
Redirectable redirectable = dst.adapt(Redirectable.class);
Map<String, Redirect> redirects = redirectable.getRedirects();
List<RedirectData> redirectsData = src.getRedirects();
redirects.clear(); // clear the redirects map since we need to rebuild it based on the new redirects
if (src.getRedirects() != null) {
for (RedirectData redirectData : redirectsData) {
Redirect redirect = redirectable.createRedirect();
redirects.put(redirectData.getRedirectName(), redirect);
redirect.setName(redirectData.getRedirectName());
redirect.setSite(redirectData.getRedirectSiteName());
redirect.setEnabled(redirectData.isEnabled());
if (redirectData.getConditions() != null) {
redirect.getConditions().clear(); // clear the map so that we can rebuild it
for (RedirectConditionData conditionData : redirectData.getConditions()) {
Condition condition = redirect.createCondition();
redirect.getConditions().put(conditionData.getRedirectName(), condition);
buildCondition(conditionData, condition);
}
}
if (redirectData.getMappings() != null) {
Mappings mappings = redirect.getMapping();
if (redirect.getMapping() == null) {
mappings = redirect.createMapping();
redirect.setMapping(mappings);
}
buildMappings(redirectData.getMappings(), mappings);
}
}
}
//
org.gatein.mop.api.workspace.Page templates = dst.getRootPage().getChild("templates");
org.gatein.mop.api.workspace.Page template = templates.getChild("default");
if (template == null) {
template = templates.addChild("default");
}
//
ContainerData srcContainer = src.getPortalLayout();
UIContainer dstContainer = template.getRootComponent();
// Workaround to have the real source container used as the model / UI layer lose this
// ID which lead to bugs
ContainerData realSrcContainer = new ContainerData(dstContainer.getObjectId(), srcContainer.getId(),
srcContainer.getName(), srcContainer.getIcon(), srcContainer.getTemplate(), srcContainer.getFactoryId(),
srcContainer.getTitle(), srcContainer.getDescription(), srcContainer.getWidth(), srcContainer.getHeight(),
srcContainer.getAccessPermissions(), srcContainer.getMoveAppsPermissions(),
srcContainer.getMoveContainersPermissions(), srcContainer.getChildren());
//
save(realSrcContainer, dstContainer);
saveChildren(realSrcContainer, dstContainer);
//
Templatized templatized = dst.getRootNavigation().getTemplatized();
if (templatized != null) {
templatized.setTemplate(template);
} else {
template.templatize(dst.getRootNavigation());
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
private void buildCondition(RedirectConditionData redirectConditionData, Condition condition) {
condition.setName(redirectConditionData.getRedirectName());
if (redirectConditionData.getUserAgentConditionData() != null) {
condition.setUserAgentContains(redirectConditionData.getUserAgentConditionData().getUserAgentContains());
}
if (redirectConditionData.getUserAgentConditionData() != null) {
condition
.setUserAgentDoesNotContain(redirectConditionData.getUserAgentConditionData().getUserAgentDoesNotContain());
}
// if the device property conditions is empty and there are currently no stored device properties, then skip
// otherwise we need to either remove or add device properties
if (redirectConditionData.getDevicePropertyConditionData() != null || !condition.getDeviceProperties().isEmpty()) {
// A list of stored redirect names which have not been found in the current RedirectConditionData list.
//
// This is because during a save we receive a list of RedirectData, and we need to keep track if an existing
// stored redirect has been deleted or not.
Set<String> notFoundProperties = new HashSet<String>(condition.getDeviceProperties().keySet());
for (RedirectDevicePropertyConditionData propertyConditionData : redirectConditionData
.getDevicePropertyConditionData()) {
DeviceProperty deviceProperty = condition.getDeviceProperties().get(propertyConditionData.getPropertyName());
// if not found, then create it
if (deviceProperty == null) {
deviceProperty = condition.createDeviceProperty();
condition.getDeviceProperties().put(propertyConditionData.getPropertyName(), deviceProperty);
} else {
// if found, remove from the nonFound list
notFoundProperties.remove(propertyConditionData.getPropertyName());
}
deviceProperty.setName(propertyConditionData.getPropertyName());
if (propertyConditionData.getGreaterThan() != null) {
deviceProperty.setGreaterThan(propertyConditionData.getGreaterThan());
}
if (propertyConditionData.getLessThan() != null) {
deviceProperty.setLessThan(propertyConditionData.getLessThan());
}
if (propertyConditionData.getEquals() != null) {
deviceProperty.setEquals(propertyConditionData.getEquals());
}
if (propertyConditionData.getMatches() != null) {
deviceProperty.setPattern(propertyConditionData.getMatches().toString());
}
}
// remove all the device conditions which were not in the RedirectConditionData list
for (String devicePropertyName : notFoundProperties) {
condition.getDeviceProperties().remove(devicePropertyName);
}
}
}
private void buildMappings(RedirectMappingsData mappingsData, Mappings mappings) {
mappings.setNodeNameMatching(mappingsData.isUseNodeNameMatching());
mappings.setUnresolvedNodeMatching(mappingsData.getUnresolvedNode());
// if the mappings conditions is empty and there are currently no stored mappings, then skip
// otherwise we need to either remove or add mappings
if (!mappingsData.getMappings().isEmpty() || !mappings.getNodeMap().isEmpty()) {
// A list of stored node mappings which have not been found in the current RedirectMappingsData list.
//
// This is because during a save we receive a list of nodes from RedirectMappingsData, and we need to keep track
// if an existing stored redirect node has been deleted or not.
Set<String> notFoundNodeMappings = new HashSet<String>(mappings.getNodeMap().keySet());
for (String key : mappingsData.getMappings().keySet()) {
NodeMap nodeMap = mappings.getNodeMap().get(Text.escapeIllegalJcrChars(key));
if (nodeMap == null) {
nodeMap = mappings.createNode();
mappings.getNodeMap().put(Text.escapeIllegalJcrChars(key), nodeMap);
} else {
notFoundNodeMappings.remove(Text.escapeIllegalJcrChars(key));
}
nodeMap.setOriginNode(key);
nodeMap.setRedirectNode(mappingsData.getMappings().get(key));
}
// remove all the device conditions which were not in the RedirectConditionData list
for (String nodeName : notFoundNodeMappings) {
mappings.getNodeMap().remove(nodeName);
}
}
}
public PageData load(org.gatein.mop.api.workspace.Page src) {
Site site = src.getSite();
String ownerType = getOwnerType(site.getObjectType());
String ownerId = site.getName();
String name = src.getName();
List<ComponentData> children = loadChildren(src.getRootComponent());
List<String> moveAppsPermissions = null;
List<String> moveContainersPermissions = null;
if (src.isAdapted(ProtectedContainer.class)) {
ProtectedContainer pc = src.adapt(ProtectedContainer.class);
moveAppsPermissions = pc.getMoveAppsPermissions();
moveContainersPermissions = pc.getMoveContainersPermissions();
} else {
moveAppsPermissions = Collections.emptyList();
moveContainersPermissions = Collections.emptyList();
}
//
return new PageData(src.getObjectId(), null, name, null, null, null, null, null, null, null,
Collections.<String> emptyList(), children, ownerType, ownerId, null, false, moveAppsPermissions,
moveContainersPermissions);
}
private ContainerData load(UIContainer src, List<ComponentData> children) {
//
List<String> accessPermissions = Collections.emptyList();
if (src.isAdapted(ProtectedResource.class)) {
ProtectedResource pr = src.adapt(ProtectedResource.class);
accessPermissions = pr.getAccessPermissions();
}
List<String> moveAppsPermissions = null;
List<String> moveContainersPermissions = null;
if (src.isAdapted(ProtectedContainer.class)) {
ProtectedContainer pc = src.adapt(ProtectedContainer.class);
moveAppsPermissions = pc.getMoveAppsPermissions();
moveContainersPermissions = pc.getMoveContainersPermissions();
} else {
/* legacy mode */
moveAppsPermissions = ProtectedContainer.DEFAULT_MOVE_APPLICATIONS_PERMISSIONS;
moveContainersPermissions = ProtectedContainer.DEFAULT_MOVE_CONTAINERS_PERMISSIONS;
}
//
Described described = src.adapt(Described.class);
Attributes attrs = src.getAttributes();
return new ContainerData(src.getObjectId(), attrs.getValue(MappedAttributes.ID), attrs.getValue(MappedAttributes.NAME),
attrs.getValue(MappedAttributes.ICON), attrs.getValue(MappedAttributes.TEMPLATE),
attrs.getValue(MappedAttributes.FACTORY_ID), described.getName(), described.getDescription(),
attrs.getValue(MappedAttributes.WIDTH), attrs.getValue(MappedAttributes.HEIGHT),
Utils.safeImmutableList(accessPermissions), Utils.safeImmutableList(moveAppsPermissions),
Utils.safeImmutableList(moveContainersPermissions), children);
}
private List<ComponentData> loadChildren(UIContainer src) {
if (src == null)
throw new NoSuchDataException("Can not load children");
ArrayList<ComponentData> children = new ArrayList<ComponentData>();
for (UIComponent component : src.getComponents()) {
// Obtain a model object from the ui component
ComponentData mo;
if (component instanceof UIContainer) {
UIContainer srcContainer = (UIContainer) component;
Attributes attrs = srcContainer.getAttributes();
String type = attrs.getValue(MappedAttributes.TYPE);
if ("dashboard".equals(type)) {
Site owner = src.getPage().getSite();
TransientApplicationState<Portlet> state = new TransientApplicationState<Portlet>(
"dashboard/DashboardPortlet", null, getOwnerType(owner.getObjectType()), owner.getName());
//
boolean showInfoBar = attrs.getValue(MappedAttributes.SHOW_INFO_BAR, false);
boolean showMode = attrs.getValue(MappedAttributes.SHOW_MODE, false);
boolean showWindowState = attrs.getValue(MappedAttributes.SHOW_WINDOW_STATE, false);
String theme = attrs.getValue(MappedAttributes.THEME, null);
Described described = srcContainer.adapt(Described.class);
String id = attrs.getValue(MappedAttributes.ID, null);
String icon = attrs.getValue(MappedAttributes.ICON, null);
String width = attrs.getValue(MappedAttributes.WIDTH, null);
String height = attrs.getValue(MappedAttributes.HEIGHT, null);
//
List<String> a = Collections.singletonList(UserACL.EVERYONE);
if (srcContainer.isAdapted(ProtectedResource.class)) {
ProtectedResource pr = srcContainer.adapt(ProtectedResource.class);
a = pr.getAccessPermissions();
}
//
mo = new ApplicationData<Portlet>(srcContainer.getObjectId(), component.getName(), ApplicationType.PORTLET,
state, id, described.getName(), icon, described.getDescription(), showInfoBar, showWindowState,
showMode, theme, width, height, Collections.<String, String> emptyMap(), a);
} else {
List<ComponentData> dstChildren = loadChildren(srcContainer);
mo = load(srcContainer, dstChildren);
}
} else if (component instanceof UIWindow) {
UIWindow window = (UIWindow) component;
ApplicationData<?> application = load(window);
mo = application;
} else if (component instanceof UIBody) {
mo = new BodyData(component.getObjectId(), BodyType.PAGE);
} else {
throw new AssertionError();
}
// Add among children
children.add(mo);
}
return children;
}
public List<ModelChange> save(PageData src, Site site, String name) throws IllegalStateException {
org.gatein.mop.api.workspace.Page root = site.getRootPage();
org.gatein.mop.api.workspace.Page pages = root.getChild("pages");
org.gatein.mop.api.workspace.Page dst = pages.getChild(name);
//
LinkedList<ModelChange> changes = new LinkedList<ModelChange>();
//
if (dst == null) {
throw new NoSuchDataException("The page " + name + " not found");
} else {
changes.add(new ModelChange.Update(src));
}
//
UIContainer rootContainer = dst.getRootComponent();
// We are creating a new Page with the root container id as this one is lost
// in the model / ui layer. Not doing this cause a class cast exception later
// so it's likely the best fix we can do at the moment
PageData src2 = new PageData(rootContainer.getObjectId(), src.getId(), src.getName(), src.getIcon(), src.getTemplate(),
null, null, null, src.getWidth(), src.getHeight(), Collections.<String> emptyList(), src.getChildren(),
src.getOwnerType(), src.getOwnerId(), null, false, src.getMoveAppsPermissions(),
src.getMoveContainersPermissions());
//
LinkedList<ModelChange> childrenChanges = saveChildren(src2, rootContainer);
//
changes.addAll(childrenChanges);
//
return changes;
}
private void save(ContainerData src, UIContainer dst) {
ProtectedResource pr = dst.adapt(ProtectedResource.class);
pr.setAccessPermissions(src.getAccessPermissions());
ProtectedContainer pc = dst.adapt(ProtectedContainer.class);
pc.setMoveAppsPermissions(src.getMoveAppsPermissions());
pc.setMoveContainersPermissions(src.getMoveContainersPermissions());
Described described = dst.adapt(Described.class);
described.setName(src.getTitle());
described.setDescription(src.getDescription());
Attributes dstAttrs = dst.getAttributes();
dstAttrs.setValue(MappedAttributes.ID, src.getId());
dstAttrs.setValue(MappedAttributes.TYPE, src instanceof DashboardData ? "dashboard" : null);
dstAttrs.setValue(MappedAttributes.ICON, src.getIcon());
dstAttrs.setValue(MappedAttributes.TEMPLATE, src.getTemplate());
dstAttrs.setValue(MappedAttributes.FACTORY_ID, src.getFactoryId());
dstAttrs.setValue(MappedAttributes.WIDTH, src.getWidth());
dstAttrs.setValue(MappedAttributes.HEIGHT, src.getHeight());
dstAttrs.setValue(MappedAttributes.NAME, src.getName());
}
/*
* Performs routing of the corresponding save method
*/
private void save(ModelData src, WorkspaceObject dst, LinkedList<ModelChange> changes,
Map<String, String> hierarchyRelationships) {
if (src instanceof ContainerData) {
save((ContainerData) src, (UIContainer) dst);
saveChildren((ContainerData) src, (UIContainer) dst, changes, hierarchyRelationships);
} else if (src instanceof ApplicationData) {
save((ApplicationData<?>) src, (UIWindow) dst);
} else if (src instanceof BodyData) {
// Stateless
} else {
throw new AssertionError("Was not expecting child " + src);
}
}
private LinkedList<ModelChange> saveChildren(final ContainerData src, UIContainer dst) {
LinkedList<ModelChange> changes = new LinkedList<ModelChange>();
// The relationship in the hierarchy
// basically it's a map of the relationships between parent/child nodes
// that is helpful to detect move operations
// that we make immutable to avoid any bug
Map<String, String> hierarchyRelationships = new HashMap<String, String>();
build(src, hierarchyRelationships);
hierarchyRelationships = Collections.unmodifiableMap(hierarchyRelationships);
//
saveChildren(src, dst, changes, hierarchyRelationships);
//
return changes;
}
private void build(ContainerData parent, Map<String, String> hierarchyRelationships) {
String parentId = parent.getStorageId();
for (ModelData child : parent.getChildren()) {
String childId = child.getStorageId();
if (childId != null) {
if (hierarchyRelationships.containsKey(childId)) {
throw new AssertionError("The same object is present two times in the object hierarchy");
}
// Note that we are aware that parent id may be null
hierarchyRelationships.put(childId, parentId);
}
if (child instanceof ContainerData) {
build((ContainerData) child, hierarchyRelationships);
}
}
}
private void saveChildren(final ContainerData src, UIContainer dst, LinkedList<ModelChange> changes,
Map<String, String> hierarchyRelationships) {
final List<String> orders = new ArrayList<String>();
final Map<String, ModelData> modelObjectMap = new HashMap<String, ModelData>();
//
for (ModelData srcChild : src.getChildren()) {
String srcChildId = srcChild.getStorageId();
// Flag variable, become non null if and only if we are saving a transient dashboard
ApplicationData<?> transientDashboardData = null;
// Replace dashboard application by container if needed
// this should be removed once we make the dashboard as first class
// citizen of the portal
if (srcChild instanceof ApplicationData) {
ApplicationData<?> app = (ApplicationData<?>) srcChild;
// todo julien: shouldn't we be checking for WSRP as well here?
if (app.getType() == ApplicationType.PORTLET && app.getState() instanceof TransientApplicationState) {
TransientApplicationState<?> state = (TransientApplicationState<?>) app.getState();
String contentId = state.getContentId();
if ("dashboard/DashboardPortlet".equals(contentId)) {
DashboardData data;
if (app.getStorageId() != null) {
UIContainer dstDashboard = session.findObjectById(ObjectType.CONTAINER, app.getStorageId());
data = loadDashboard(dstDashboard);
// Update those attributes as we have to do it now, they don't exist in a container
// but do exist in a dashboard container
Attributes attrs = dstDashboard.getAttributes();
attrs.setValue(MappedAttributes.SHOW_INFO_BAR, app.isShowInfoBar());
attrs.setValue(MappedAttributes.SHOW_MODE, app.isShowApplicationMode());
attrs.setValue(MappedAttributes.SHOW_WINDOW_STATE, app.isShowApplicationState());
attrs.setValue(MappedAttributes.THEME, app.getTheme());
} else {
data = DashboardData.INITIAL_DASHBOARD;
transientDashboardData = (ApplicationData<?>) srcChild;
}
//
String icon = data.getIcon();
if (icon == null)
icon = app.getIcon();
String title = data.getTitle();
if (title == null)
title = app.getTitle();
String description = data.getDescription();
if (description == null)
description = app.getDescription();
String width = data.getWidth();
if (width == null)
width = app.getWidth();
String height = data.getHeight();
if (height == null)
height = app.getHeight();
data = new DashboardData(data.getStorageId(), data.getId(), data.getName(), icon, data.getTemplate(),
data.getFactoryId(), title, description, width, height, app.getAccessPermissions(),
data.getMoveAppsPermissions(), data.getMoveContainersPermissions(), data.getChildren());
//
srcChild = data;
}
}
}
//
UIComponent dstChild;
if (srcChildId != null) {
dstChild = session.findObjectById(ObjectType.COMPONENT, srcChildId);
if (dstChild == null) {
throw new StaleModelException("Could not find supposed present child with id " + srcChildId);
}
// julien : this can fail due to a bug in chromattic not implementing equals method properly
// and is replaced with the foreach below
/*
* if (!dst.contains(dstChild)) { throw new IllegalArgumentException("Attempt for updating a ui component " +
* session.pathOf(dstChild) + "that is not present in the target ui container " + session.pathOf(dst)); }
*/
boolean found = false;
for (UIComponent child : dst.getComponents()) {
if (child.getObjectId().equals(srcChildId)) {
found = true;
break;
}
}
//
if (!found) {
if (hierarchyRelationships.containsKey(srcChildId)) {
String srcId = hierarchyRelationships.get(srcChildId);
// It's a move operation, so we move the node first
dst.getComponents().add(dstChild);
//
changes.add(new ModelChange.Move(srcId, dst.getObjectId(), srcChildId));
} else {
throw new IllegalArgumentException("Attempt for updating a ui component " + session.pathOf(dstChild)
+ " that is not present in the target ui container " + session.pathOf(dst));
}
}
//
changes.add(new ModelChange.Update(srcChild));
} else {
String name = srcChild.getStorageName();
if (name == null) {
// We manufacture one name
name = UUID.randomUUID().toString();
}
if (srcChild instanceof ContainerData) {
dstChild = dst.add(ObjectType.CONTAINER, name);
} else if (srcChild instanceof ApplicationData) {
dstChild = dst.add(ObjectType.WINDOW, name);
} else if (srcChild instanceof BodyData) {
dstChild = dst.add(ObjectType.BODY, name);
} else {
throw new StaleModelException("Was not expecting child " + srcChild);
}
changes.add(new ModelChange.Create(dst.getObjectId(), srcChild));
}
//
if (transientDashboardData != null) {
Attributes attrs = dstChild.getAttributes();
attrs.setValue(MappedAttributes.SHOW_INFO_BAR, transientDashboardData.isShowInfoBar());
attrs.setValue(MappedAttributes.SHOW_MODE, transientDashboardData.isShowApplicationMode());
attrs.setValue(MappedAttributes.SHOW_WINDOW_STATE, transientDashboardData.isShowApplicationState());
attrs.setValue(MappedAttributes.THEME, transientDashboardData.getTheme());
}
save(srcChild, dstChild, changes, hierarchyRelationships);
//
String dstChildId = dstChild.getObjectId();
modelObjectMap.put(dstChildId, srcChild);
orders.add(dstChildId);
}
// Take care of move operation that could be seen as a remove otherwise
for (UIComponent dstChild : dst.getComponents()) {
String dstChildId = dstChild.getObjectId();
if (!modelObjectMap.containsKey(dstChildId)) {
String parentId = hierarchyRelationships.get(dstChildId);
if (parentId != null) {
// Get the new parent
UIContainer parent = session.findObjectById(ObjectType.CONTAINER, parentId);
// Perform the move
parent.getComponents().add(dstChild);
//
changes.add(new ModelChange.Move(dst.getObjectId(), parentId, dstChildId));
// julien : we do not need to create an update operation
// as later the update operation will be created when the
// object
// will be processed
} else if (hierarchyRelationships.containsKey(dstChildId)) {
// The dstChild is placed under transient Chromattic entity whose storageId == null. However,
// the hierachyRelationships contains dstChildId in key set, so we have to mark dstChild as
// moved object
modelObjectMap.put(dstChildId, null);
}
}
}
// Delete removed children
for (Iterator<UIComponent> i = dst.getComponents().iterator(); i.hasNext();) {
UIComponent dstChild = i.next();
String dstChildId = dstChild.getObjectId();
if (!modelObjectMap.containsKey(dstChildId)) {
i.remove();
changes.add(new ModelChange.Destroy(dstChildId));
}
}
// Now sort children according to the order provided by the container
// need to replace that with Collections.sort once the set(int index, E element) is implemented in Chromattic lists
UIComponent[] a = dst.getComponents().toArray(new UIComponent[dst.getComponents().size()]);
Arrays.sort(a, new Comparator<UIComponent>() {
public int compare(UIComponent o1, UIComponent o2) {
int i1 = orders.indexOf(o1.getObjectId());
int i2 = orders.indexOf(o2.getObjectId());
return i1 - i2;
}
});
for (int j = 0; j < a.length; j++) {
dst.getComponents().add(j, a[j]);
}
}
public <S> ApplicationData<S> load(UIWindow src) {
Attributes attrs = src.getAttributes();
//
Customization<?> customization = src.getCustomization();
//
ContentType<?> contentType = customization.getType();
//
String customizationid = customization.getId();
// julien: should type check that
ApplicationType<S> type = (ApplicationType<S>) ApplicationType.getType(contentType);
//
PersistentApplicationState<S> instanceState = new PersistentApplicationState<S>(customizationid);
//
HashMap<String, String> properties = new HashMap<String, String>();
load(attrs, properties, windowPropertiesBlackList);
//
List<String> accessPermissions = Collections.emptyList();
if (src.isAdapted(ProtectedResource.class)) {
ProtectedResource pr = src.adapt(ProtectedResource.class);
accessPermissions = pr.getAccessPermissions();
}
//
Described described = src.adapt(Described.class);
//
boolean showInfoBar = attrs.getValue(MappedAttributes.SHOW_INFO_BAR, false);
boolean showWindowState = attrs.getValue(MappedAttributes.SHOW_WINDOW_STATE, false);
boolean showMode = attrs.getValue(MappedAttributes.SHOW_MODE, false);
String theme = attrs.getValue(MappedAttributes.THEME, null);
//
return new ApplicationData<S>(src.getObjectId(), src.getName(), type, instanceState, null, described.getName(),
attrs.getValue(MappedAttributes.ICON), described.getDescription(), showInfoBar, showWindowState, showMode,
theme, attrs.getValue(MappedAttributes.WIDTH), attrs.getValue(MappedAttributes.HEIGHT),
Utils.safeImmutableMap(properties), Utils.safeImmutableList(accessPermissions));
}
public <S> void save(ApplicationData<S> src, UIWindow dst) {
ProtectedResource pr = dst.adapt(ProtectedResource.class);
pr.setAccessPermissions(src.getAccessPermissions());
Described described = dst.adapt(Described.class);
described.setName(src.getTitle());
described.setDescription(src.getDescription());
//
Attributes attrs = dst.getAttributes();
attrs.setValue(MappedAttributes.SHOW_INFO_BAR, src.isShowInfoBar());
attrs.setValue(MappedAttributes.SHOW_WINDOW_STATE, src.isShowApplicationState());
attrs.setValue(MappedAttributes.SHOW_MODE, src.isShowApplicationMode());
attrs.setValue(MappedAttributes.THEME, src.getTheme());
attrs.setValue(MappedAttributes.ICON, src.getIcon());
attrs.setValue(MappedAttributes.WIDTH, src.getWidth());
attrs.setValue(MappedAttributes.HEIGHT, src.getHeight());
save(src.getProperties(), attrs, windowPropertiesBlackList);
//
ApplicationState<S> instanceState = src.getState();
// We modify only transient portlet state
// and we ignore any persistent portlet state
if (instanceState instanceof TransientApplicationState) {
//
TransientApplicationState<S> transientState = (TransientApplicationState<S>) instanceState;
// Attempt to get a site from the instance state
Site site = null;
if (transientState.getOwnerType() != null && transientState.getOwnerId() != null) {
ObjectType<Site> siteType = parseSiteType(transientState.getOwnerType());
site = session.getWorkspace().getSite(siteType, transientState.getOwnerId());
}
// The current site
Site currentSite = dst.getPage().getSite();
// If it is the same site than the current page
// set null
if (site == dst.getPage().getSite()) {
site = null;
}
// The content id
String contentId = transientState.getContentId();
ContentType<S> contentType = src.getType().getContentType();
// The customization that we will inherit from if not null
Customization<?> customization = null;
// Destroy existing window previous customization
if (dst.getCustomization() != null) {
dst.getCustomization().destroy();
}
// If the existing customization is not null and matches the content id
Customization<S> dstCustomization;
if (customization != null && customization.getType().equals(contentType)
&& customization.getContentId().equals(contentId)) {
// Cast is ok as content type matches
@SuppressWarnings("unchecked")
Customization<S> bilto = (Customization<S>) customization;
// If it's a customization of the current site we extend it
if (bilto.getContext() == currentSite) {
dstCustomization = dst.customize(bilto);
} else {
// Otherwise we clone it propertly
S state = bilto.getVirtualState();
dstCustomization = dst.customize(contentType, contentId, state);
}
} else {
// Otherwise we create an empty customization
dstCustomization = dst.customize(contentType, contentId, null);
}
// At this point we have customized the window
// now if we have any additional state payload we must merge it
// with the current state
S state = ((TransientApplicationState<S>) instanceState).getContentState();
if (state != null) {
dstCustomization.setState(state);
}
} else if (instanceState instanceof CloneApplicationState) {
CloneApplicationState cloneState = (CloneApplicationState) instanceState;
//
Customization<?> customization = session.findCustomizationById(cloneState.getStorageId());
//
dst.customize(customization);
} else if (instanceState instanceof PersistentApplicationState) {
// Do nothing
} else {
throw new IllegalArgumentException("Cannot save application with state " + instanceState);
}
}
public DashboardData loadDashboard(UIContainer container) {
List<String> accessPermissions = Collections.emptyList();
if (container.isAdapted(ProtectedResource.class)) {
ProtectedResource pr = container.adapt(ProtectedResource.class);
accessPermissions = pr.getAccessPermissions();
}
List<String> moveAppsPermissions = null;
List<String> moveContainersPermissions = null;
if (container.isAdapted(ProtectedContainer.class)) {
ProtectedContainer pc = container.adapt(ProtectedContainer.class);
moveAppsPermissions = pc.getMoveAppsPermissions();
moveContainersPermissions = pc.getMoveContainersPermissions();
} else {
moveAppsPermissions = Collections.emptyList();
moveContainersPermissions = Collections.emptyList();
}
//