-
Notifications
You must be signed in to change notification settings - Fork 460
/
DependencyManager.java
1234 lines (1053 loc) · 47.1 KB
/
DependencyManager.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
package com.dotcms.publisher.util;
import com.dotcms.contenttype.transform.contenttype.StructureTransformer;
import com.dotcms.enterprise.rules.RulesAPI;
import com.dotcms.languagevariable.business.LanguageVariableAPI;
import com.dotcms.publisher.business.PublishQueueElement;
import com.dotcms.publisher.pusher.PushPublisherConfig;
import com.dotcms.publisher.pusher.PushPublisherConfig.AssetTypes;
import com.dotcms.publishing.DotBundleException;
import com.dotcms.publishing.PublisherConfig.Operation;
import com.dotmarketing.beans.ContainerStructure;
import com.dotmarketing.beans.Host;
import com.dotmarketing.beans.Identifier;
import com.dotmarketing.beans.MultiTree;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.CacheLocator;
import com.dotmarketing.business.DotIdentifierStateException;
import com.dotmarketing.business.DotStateException;
import com.dotmarketing.business.FactoryLocator;
import com.dotmarketing.business.IdentifierAPI;
import com.dotmarketing.business.PermissionAPI;
import com.dotmarketing.cache.FieldsCache;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.portlets.categories.model.Category;
import com.dotmarketing.portlets.containers.model.Container;
import com.dotmarketing.portlets.containers.model.FileAssetContainer;
import com.dotmarketing.portlets.contentlet.business.ContentletAPI;
import com.dotmarketing.portlets.contentlet.business.DotContentletStateException;
import com.dotmarketing.portlets.contentlet.business.HostAPI;
import com.dotmarketing.portlets.contentlet.model.Contentlet;
import com.dotmarketing.portlets.fileassets.business.FileAsset;
import com.dotmarketing.portlets.folders.business.FolderAPI;
import com.dotmarketing.portlets.folders.model.Folder;
import com.dotmarketing.portlets.htmlpageasset.model.IHTMLPage;
import com.dotmarketing.portlets.languagesmanager.model.Language;
import com.dotmarketing.portlets.links.model.Link;
import com.dotmarketing.portlets.rules.model.Rule;
import com.dotmarketing.portlets.structure.factories.StructureFactory;
import com.dotmarketing.portlets.structure.model.Field;
import com.dotmarketing.portlets.structure.model.Relationship;
import com.dotmarketing.portlets.structure.model.Structure;
import com.dotmarketing.portlets.templates.model.Template;
import com.dotmarketing.portlets.workflows.model.WorkflowScheme;
import com.dotmarketing.util.Config;
import com.dotmarketing.util.InodeUtils;
import com.dotmarketing.util.Logger;
import com.dotmarketing.util.UtilMethods;
import com.liferay.portal.model.User;
import com.liferay.util.StringPool;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.apache.commons.lang.StringUtils;
/**
* The main purpose of this class is to determine all possible content
* dependencies associated to the asset(s) the user wants to push via the Push
* Publish feature. Including the dependencies makes sure that pushing an asset
* will not fail when saved in the destination server(s).
* <p>
* Most of the assets in dotCMS <b>MUST</b> be associated to another parent
* structure. For example, pushing a Content Page requires the bundle to include
* information regarding the Site (host) in was created in, its folder,
* template, containers, contentlets, and so on. Whereas pushing a Language only
* needs the language information and the push will work correctly.
* <p>
* The Dependency Manager analyzes the type of each asset to push and includes
* dependent information in the bundle. This way, it can be seen by users
* exactly the same in both the sender and receiver servers.
*
* @author Daniel Silva
* @version 1.0
* @since Jan 11, 2013
*
*/
public class DependencyManager {
private DependencySet hosts;
private DependencySet folders;
private DependencySet htmlPages;
private DependencySet templates;
private DependencySet structures;
private DependencySet containers;
private DependencySet contents;
private DependencySet links;
private DependencySet relationships;
private DependencySet workflows;
private DependencySet languages;
private DependencySet rules;
private DependencySet categories;
private Set<String> hostsSet;
private Set<String> foldersSet;
private Set<String> htmlPagesSet;
private Set<String> templatesSet;
private Set<String> structuresSet;
private Set<String> containersSet;
private Set<String> fileAssetContainersSet;
private Set<String> contentsSet;
private Set<String> linksSet;
private Set<String> ruleSet;
private Set<String> solvedStructures;
private User user;
private PushPublisherConfig config;
/**
* Initializes the list of dependencies that this manager needs to satisfy,
* based on the {@link PushPublisherConfig} specified for the bundle.
*
* @param user
* - The user requesting the generation of the bundle.
* @param config
* - The main configuration values for the bundle we are trying
* to create.
*/
public DependencyManager(User user, PushPublisherConfig config) {
this.config = config;
// these ones store the assets that will be sent in the bundle
boolean isPublish=config.getOperation().equals(Operation.PUBLISH);
hosts = new DependencySet(config.getId(), "host", config.isDownloading(), isPublish, config.isStatic());
folders = new DependencySet(config.getId(), "folder", config.isDownloading(), isPublish, config.isStatic());
htmlPages = new DependencySet(config.getId(), "htmlpage", config.isDownloading(), isPublish, config.isStatic());
templates = new DependencySet(config.getId(), "template", config.isDownloading(), isPublish, config.isStatic());
structures = new DependencySet(config.getId(), "structure", config.isDownloading(), isPublish, config.isStatic());
containers = new DependencySet(config.getId(), "container", config.isDownloading(), isPublish, config.isStatic());
contents = new DependencySet(config.getId(), "content", config.isDownloading(), isPublish, config.isStatic());
relationships = new DependencySet(config.getId(), "relationship", config.isDownloading(), isPublish, config.isStatic());
links = new DependencySet(config.getId(),"links",config.isDownloading(), isPublish, config.isStatic());
workflows = new DependencySet(config.getId(),"workflows",config.isDownloading(), isPublish, config.isStatic());
languages = new DependencySet(config.getId(),"languages",config.isDownloading(), isPublish, config.isStatic());
this.rules = new DependencySet(config.getId(), PushPublisherConfig.AssetTypes.RULES.toString(), config.isDownloading(), isPublish, config.isStatic());
categories = new DependencySet(config.getId(), AssetTypes.CATEGORIES.toString(), config.isDownloading(), isPublish, config.isStatic());
// these ones are for being iterated over to solve the asset's dependencies
hostsSet = new HashSet<>();
foldersSet = new HashSet<>();
htmlPagesSet = new HashSet<>();
templatesSet = new HashSet<>();
structuresSet = new HashSet<>();
containersSet = new HashSet<>();
contentsSet = new HashSet<>();
fileAssetContainersSet = new HashSet<>();
linksSet = new HashSet<>();
this.ruleSet = new HashSet<>();
solvedStructures = new HashSet<>();
this.user = user;
}
/**
* Initializes the main mechanism to search for dependencies. It starts with
* the identification of the type of assets in the queue, and their
* dependencies will be searched and found base on those types.
*
* @throws DotSecurityException
* The specified user does not have the required permissions to
* perform the action.
* @throws DotDataException
* An error occurred when retrieving information from the
* database.
* @throws DotBundleException
* An error occurred when generating the bundle data.
*/
public void setDependencies() throws DotSecurityException, DotDataException, DotBundleException {
List<PublishQueueElement> assets = config.getAssets();
for (PublishQueueElement asset : assets) {
if(asset.getType().equals(PusheableAsset.CONTENT_TYPE.getType())) {
try {
Structure st = CacheLocator.getContentTypeCache().getStructureByInode(asset.getAsset());
if(st == null) {
Logger.warn(getClass(), "Structure id: "+ (asset.getAsset() != null ? asset.getAsset() : "N/A") +" does NOT have working or live version, not Pushed");
} else {
structures.add(asset.getAsset(), st.getModDate());
structuresSet.add(asset.getAsset());
}
} catch (Exception e) {
Logger.error(getClass(), "Couldn't add the Structure to the Bundle. Bundle ID: " + config.getId() + ", Structure ID: " + asset.getAsset(), e);
}
} else if(asset.getType().equals(PusheableAsset.TEMPLATE.getType())) {
try {
Template t = APILocator.getTemplateAPI().findLiveTemplate(asset.getAsset(), user, false);
if(t == null || !UtilMethods.isSet(t.getIdentifier())) {
t = APILocator.getTemplateAPI().findWorkingTemplate(asset.getAsset(), user, false);
}
if(t == null || !UtilMethods.isSet(t.getIdentifier())) {
Logger.warn(getClass(), "Template id: "+ (asset.getAsset() != null ? asset.getAsset() : "N/A") +" does NOT have working or live version, not Pushed");
} else {
templates.add(asset.getAsset(), t.getModDate());
templatesSet.add(asset.getAsset());
}
} catch (Exception e) {
Logger.error(getClass(), "Couldn't add the Template to the Bundle. Bundle ID: " + config.getId() + ", Template ID: " + asset.getAsset(), e);
}
} else if(asset.getType().equals(PusheableAsset.CONTAINER.getType())) {
try {
Container c = (Container) APILocator.getVersionableAPI().findLiveVersion(asset.getAsset(), user, false);
if(c == null) {
c = APILocator.getContainerAPI().getWorkingContainerById(asset.getAsset(), user, false);
}
if(c instanceof FileAssetContainer){
Logger.debug(getClass(), "FileAssetContainer id: "+ (asset.getAsset() != null ? asset.getAsset() : "N/A") +" will be ignored");
fileAssetContainersSet.add(asset.getAsset());
continue;
}
if(c == null) {
Logger.warn(getClass(), "Container id: "+ (asset.getAsset() != null ? asset.getAsset() : "N/A") +" does NOT have working or live version, not Pushed");
} else {
containers.add(asset.getAsset(), c.getModDate());
containersSet.add(asset.getAsset());
}
} catch (DotSecurityException e) {
Logger.error(getClass(), "Couldn't add the Container to the Bundle. Bundle ID: " + config.getId() + ", Container ID: " + asset.getAsset(), e);
}
} else if(asset.getType().equals(PusheableAsset.FOLDER.getType())) {
try {
Folder f = APILocator.getFolderAPI().find(asset.getAsset(), user, false);
if(f == null){
Logger.warn(getClass(), "Folder id: "+ (asset.getAsset() != null ? asset.getAsset() : "N/A") +" does NOT have working or live version, not Pushed");
} else {
folders.add(asset.getAsset(), f.getModDate());
foldersSet.add(asset.getAsset());
}
} catch (DotSecurityException e) {
Logger.error(getClass(), "Couldn't add the Folder to the Bundle. Bundle ID: " + config.getId() + ", Folder ID: " + asset.getAsset(), e);
}
} else if(asset.getType().equals(PusheableAsset.SITE.getType())) {
try {
Host h = APILocator.getHostAPI().find(asset.getAsset(), user, false);
if(h == null){
Logger.warn(getClass(), "Host id: "+ (asset.getAsset() != null ? asset.getAsset() : "N/A") +" does NOT have working or live version, not Pushed");
} else {
hosts.add(asset.getAsset(), h.getModDate());
hostsSet.add(asset.getAsset());
}
} catch (DotSecurityException e) {
Logger.error(getClass(), "Couldn't add the Host to the Bundle. Bundle ID: " + config.getId() + ", Host ID: " + asset.getAsset(), e);
}
} else if(asset.getType().equals(PusheableAsset.LINK.getType())) {
try {
Link link = (Link) APILocator.getVersionableAPI().findLiveVersion(asset.getAsset(), user, false);
if(link == null || !InodeUtils.isSet(link.getInode())) {
link = APILocator.getMenuLinkAPI().findWorkingLinkById(asset.getAsset(), user, false);
}
if(link == null || !InodeUtils.isSet(link.getInode())) {
Logger.warn(getClass(), "Link id: "+ (asset.getAsset() != null ? asset.getAsset() : "N/A") +" does NOT have working or live version, not Pushed");
} else {
links.add(asset.getAsset(),link.getModDate());
linksSet.add(asset.getAsset());
}
} catch (DotSecurityException e) {
Logger.error(getClass(), "Couldn't add the Host to the Bundle. Bundle ID: " + config.getId() + ", Host ID: " + asset.getAsset(), e);
}
} else if(asset.getType().equals(PusheableAsset.WORKFLOW.getType())) {
WorkflowScheme scheme = APILocator.getWorkflowAPI().findScheme(asset.getAsset());
if(scheme == null){
Logger.warn(getClass(), "WorkflowScheme id: "+ (asset.getAsset() != null ? asset.getAsset() : "N/A") +" does NOT have working or live version, not Pushed");
} else {
workflows.add(asset.getAsset(),scheme.getModDate());
}
} else if (asset.getType().equals(PusheableAsset.LANGUAGE.getType())) {
Language language = APILocator.getLanguageAPI()
.getLanguage(asset.getAsset());
if (language == null || !UtilMethods.isSet(language.getLanguage())) {
Logger.warn(getClass(), "Language id: "
+ (asset.getAsset() != null ? asset.getAsset()
: "N/A")
+ " is not present in the database, not Pushed");
} else {
languages.add(asset.getAsset());
}
} else if (asset.getType().equals(PusheableAsset.RULE.getType())) {
Rule rule = APILocator.getRulesAPI()
.getRuleById(asset.getAsset(), user, false);
if (rule != null && StringUtils.isNotBlank(rule.getId())) {
this.rules.add(rule.getId());
this.ruleSet.add(rule.getId());
} else {
Logger.warn(getClass(), "Rule id: "
+ (asset.getAsset() != null ? asset.getAsset()
: "N/A")
+ " is not present in the database, not Pushed.");
}
}
}
if(UtilMethods.isSet(config.getLuceneQueries())){
List<String> contentIds = PublisherUtil.getContentIds( config.getLuceneQueries());
for(String id : contentIds){
List<Contentlet> contentlets = APILocator.getContentletAPI().search("+identifier:"+id, 0, 0, "moddate", user, false);
for(Contentlet con : contentlets){
contents.add( con.getIdentifier(), con.getModDate());
contentsSet.add(con.getIdentifier());
}
}
}
setHostDependencies();
setFolderDependencies();
setHTMLPagesDependencies();
setTemplateDependencies();
setContainerDependencies();
setStructureDependencies();
setLinkDependencies();
setLanguageDependencies();
setContentDependencies();
setRuleDependencies();
config.setHostSet(hosts);
config.setFolders(folders);
config.setHTMLPages(htmlPages);
config.setTemplates(templates);
config.setContainers(containers);
config.setStructures(structures);
config.setContents(contents);
config.setLinks(links);
config.setRelationships(relationships);
config.setWorkflows(workflows);
config.setLanguages(languages);
config.setRules(this.rules);
config.setCategories(categories);
}
/**
* For given Links adds its dependencies:
* <ul>
* <li>Hosts</li>
* <li>Folders</li>
* </ul>
*/
private void setLinkDependencies() {
for(String linkId : linksSet) {
try {
Identifier ident=APILocator.getIdentifierAPI().find(linkId);
Folder ff = APILocator.getFolderAPI().findFolderByPath(ident.getParentPath(), ident.getHostId(), user, false);
folders.addOrClean( ff.getInode(), ff.getModDate());
foldersSet.add(ff.getInode());
Host hh=APILocator.getHostAPI().find(ident.getHostId(), user, false);
hosts.addOrClean( hh.getIdentifier(), hh.getModDate());
hostsSet.add(hh.getIdentifier());
Link link = APILocator.getMenuLinkAPI().findWorkingLinkById(linkId, user, false);
if(link!=null) {
if(link.getLinkType().equals(Link.LinkType.INTERNAL.toString())) {
Identifier id = APILocator.getIdentifierAPI().find(link.getInternalLinkIdentifier());
// add file/content dependencies. will also work with htmlpages as content
if (InodeUtils.isSet(id.getInode()) && id.getAssetType().equals("contentlet")) {
List<Contentlet> contentList = APILocator.getContentletAPI().search("+identifier:"+id.getId(), 0, 0, "moddate", user, false);
for (Contentlet contentlet : contentList) {
contents.addOrClean(contentlet.getIdentifier(), contentlet.getModDate());
contentsSet.add(contentlet.getIdentifier());
}
}
}
}
} catch (Exception e) {
Logger.error(this, "can't load menuLink deps "+linkId,e);
}
}
}
/**
* Collects the different dependent objects that are required for pushing
* {@link Host} objects. The required dependencies of a site are:
* <ul>
* <li>Templates.</li>
* <li>Containers.</li>
* <li>Contentlets.</li>
* <li>Content Types.</li>
* <li>Folders.</li>
* <li>Rules.</li>
* </ul>
*/
private void setHostDependencies () {
try {
for (String id : hostsSet) {
final Host h = APILocator.getHostAPI().find(id, user, false);
// Template dependencies
final List<Template> templateList = APILocator.getTemplateAPI().findTemplatesAssignedTo(h);
for (Template template : templateList) {
templates.addOrClean( template.getIdentifier(), template.getModDate());
templatesSet.add(template.getIdentifier());
}
// Container dependencies
final List<Container> containerList = APILocator.getContainerAPI().findContainersUnder(h);
for (Container container : containerList) {
if (container instanceof FileAssetContainer) {
fileAssetContainersSet.add(container.getIdentifier());
} else {
containers.addOrClean(container.getIdentifier(), container.getModDate());
containersSet.add(container.getIdentifier());
}
}
// Content dependencies
String luceneQuery = "+conHost:" + h.getIdentifier();
final List<Contentlet> contentList = APILocator.getContentletAPI().search(luceneQuery, 0, 0, null, user, false);
for (Contentlet contentlet : contentList) {
contents.addOrClean( contentlet.getIdentifier(), contentlet.getModDate());
contentsSet.add(contentlet.getIdentifier());
}
// Structure dependencies
final List<Structure> structuresList = StructureFactory.getStructuresUnderHost(h, user, false);
for (Structure structure : structuresList) {
structures.addOrClean( structure.getInode(), structure.getModDate());
structuresSet.add(structure.getInode());
}
// Folder dependencies
final List<Folder> folderList = APILocator.getFolderAPI().findFoldersByHost(h, user, false);
for (Folder folder : folderList) {
folders.addOrClean( folder.getInode(), folder.getModDate());
foldersSet.add(folder.getInode());
}
// Rule dependencies
final List<Rule> ruleList = APILocator.getRulesAPI().getAllRulesByParent(h, user, false);
for (final Rule rule : ruleList) {
this.rules.add(rule.getId());
this.ruleSet.add(rule.getId());
}
}
} catch (DotSecurityException e) {
Logger.error(this, e.getMessage(),e);
} catch (DotDataException e) {
Logger.error(this, e.getMessage(),e);
}
}
/**
* For given Folders adds its dependencies:
* <ul>
* <li>Hosts</li>
* <li>Contentlets</li>
* <li>Links</li>
* <li>Structures</li>
* <li>HTMLPages</li>
* </ul>
*/
private void setFolderDependencies() {
try {
List<Folder> folderList = new ArrayList<Folder>();
HashSet<String> parentFolders = new HashSet<String>();
for (String id : foldersSet) {
Folder f = APILocator.getFolderAPI().find(id, user, false);
// Parent folder
Folder parent = APILocator.getFolderAPI().findParentFolder(f, user, false);
if(UtilMethods.isSet(parent)) {
folders.addOrClean( parent.getInode(), parent.getModDate());
parentFolders.add(parent.getInode());
}
folderList.add(f);
}
foldersSet.addAll(parentFolders);
setFolderListDependencies(folderList);
} catch (DotSecurityException e) {
Logger.error(this, e.getMessage(),e);
} catch (DotDataException e) {
Logger.error(this, e.getMessage(),e);
}
}
/**
*
* @param folderList
* @throws DotIdentifierStateException
* @throws DotDataException
* @throws DotSecurityException
*/
private void setFolderListDependencies(List<Folder> folderList) throws DotIdentifierStateException, DotDataException, DotSecurityException {
for (Folder f : folderList) {
// Add folder even if empty
folders.addOrClean( f.getInode(), f.getModDate());
foldersSet.add(f.getInode());
// Host dependency
Host h = APILocator.getHostAPI().find(f.getHostId(), user, false);
hosts.addOrClean( f.getHostId(), h.getModDate());
hostsSet.add(f.getHostId());
// Content dependencies
String luceneQuery = "+conFolder:" + f.getInode();
List<Contentlet> contentList = APILocator.getContentletAPI().search(luceneQuery, 0, 0, null, user, false);
for (Contentlet contentlet : contentList) {
contents.addOrClean( contentlet.getIdentifier(), contentlet.getModDate());
contentsSet.add(contentlet.getIdentifier());
}
// Menu Link dependencies
List<Link> linkList = APILocator.getMenuLinkAPI().findFolderMenuLinks(f);
for (Link link : linkList) {
links.addOrClean( link.getIdentifier(), link.getModDate());
linksSet.add(link.getIdentifier());
}
// Structure dependencies
List<Structure> structureList = APILocator.getFolderAPI().getStructures(f, user, false);
for (Structure structure : structureList) {
structures.addOrClean( structure.getInode(), structure.getModDate());
structuresSet.add(structure.getInode());
}
//Add the default structure of this folder
if ( f.getDefaultFileType() != null ) {
Structure defaultStructure = CacheLocator.getContentTypeCache().getStructureByInode( f.getDefaultFileType() );
if ( (defaultStructure != null && InodeUtils.isSet( defaultStructure.getInode() ))
&& !structuresSet.contains( defaultStructure.getInode() ) ) {
structures.addOrClean( defaultStructure.getInode(), defaultStructure.getModDate() );
structuresSet.add( defaultStructure.getInode() );
}
}
setFolderListDependencies(APILocator.getFolderAPI().findSubFolders(f, user, false));
}
}
/**
* Traverses the list of selected contentlets that are being pushed and
* determines which of them are actually {@link IHTMLPage} objects. After
* that, the respective dependencies will be retrieved and added acordingly.
*/
private void setHTMLPagesDependencies() {
try {
Set<String> idsToWork = new HashSet<>();
idsToWork.addAll(htmlPagesSet);
for (String contId : contentsSet) {
List<Contentlet> c = APILocator.getContentletAPI().search("+identifier:" + contId, 0, 0, "moddate", user, false);
if (c != null && !c.isEmpty() && c.get(0).getStructure().getStructureType() == Structure.STRUCTURE_TYPE_HTMLPAGE) {
idsToWork.add(contId);
}
}
//Process the pages we found
setHTMLPagesDependencies(idsToWork);
} catch (DotSecurityException e) {
Logger.error(this, e.getMessage(), e);
} catch (DotDataException e) {
Logger.error(this, e.getMessage(), e);
}
}
/**
* Collects the different dependent objects that are required for pushing
* {@link IHTMLPage} objects. The required dependencies of a page are:
* <ul>
* <li>Host.</li>
* <li>Template.</li>
* <li>Containers.</li>
* <li>Content Types.</li>
* <li>Contentlets.</li>
* <li>Rules.</li>
* </ul>
*
* @param idsToWork
*/
private void setHTMLPagesDependencies(Set<String> idsToWork) {
try {
IdentifierAPI idenAPI = APILocator.getIdentifierAPI();
FolderAPI folderAPI = APILocator.getFolderAPI();
Set<Container> containerList = new HashSet<>();
for (String pageId : idsToWork) {
Identifier iden = idenAPI.find(pageId);
// Host dependency
Host h = APILocator.getHostAPI().find(iden.getHostId(), user, false);
hosts.addOrClean( iden.getHostId(), h.getModDate());
hostsSet.add(iden.getHostId());
Folder folder = folderAPI.findFolderByPath(iden.getParentPath(), iden.getHostId(), user, false);
folders.addOrClean( folder.getInode(), folder.getModDate());
foldersSet.add(folder.getInode());
// looking for working version (must exists)
IHTMLPage workingPage = null;
Contentlet contentlet = null;
try{
contentlet = APILocator.getContentletAPI().search("+identifier:"+pageId+" +working:true", 0, 0, "moddate", user, false).get(0);
} catch (DotContentletStateException e) {
// content not found message is already displayed on console
Logger.debug(this, e.getMessage(),e);
}
if(contentlet != null)
workingPage = APILocator.getHTMLPageAssetAPI().fromContentlet(contentlet);
// looking for live version (might not exists)
IHTMLPage livePage = null;
contentlet = null;
try{
List<Contentlet> result = APILocator.getContentletAPI().search("+identifier:"+pageId+" +live:true", 0, 0, "moddate", user, false);
if(!result.isEmpty()) {
contentlet = result.get(0);
}
} catch (DotContentletStateException e) {
// content not found message is already displayed on console
Logger.debug(this, e.getMessage(),e);
}
if(contentlet != null)
livePage = APILocator.getHTMLPageAssetAPI().fromContentlet(contentlet);
// working template working page
Template workingTemplateWP = null;
// live template working page
Template liveTemplateWP = null;
if(workingPage!=null) {
workingTemplateWP = APILocator.getTemplateAPI().findWorkingTemplate(workingPage.getTemplateId(), user, false);
liveTemplateWP = APILocator.getTemplateAPI().findLiveTemplate(workingPage.getTemplateId(), user, false);
// Templates dependencies
templates.addOrClean( workingPage.getTemplateId(), workingTemplateWP.getModDate());
templatesSet.add(workingPage.getTemplateId());
}
Template liveTemplateLP = null;
// live template live page
if(livePage!=null) {
liveTemplateLP = APILocator.getTemplateAPI().findLiveTemplate(livePage.getTemplateId(), user, false);
// Templates dependencies
templates.addOrClean( livePage.getTemplateId(), livePage.getModDate());
templatesSet.add(livePage.getTemplateId());
}
// Containers dependencies
containerList.clear();
if(workingTemplateWP!=null && InodeUtils.isSet(workingTemplateWP.getInode())){
containerList.addAll(APILocator.getTemplateAPI().getContainersInTemplate(workingTemplateWP, user, false));
}
if(liveTemplateWP!=null && InodeUtils.isSet(liveTemplateWP.getInode())){
containerList.addAll(APILocator.getTemplateAPI().getContainersInTemplate(liveTemplateWP, user, false));
}
if(liveTemplateLP!=null && InodeUtils.isSet(liveTemplateLP.getInode())){
containerList.addAll(APILocator.getTemplateAPI().getContainersInTemplate(liveTemplateLP, user, false));
}
for (Container container : containerList) {
if (container instanceof FileAssetContainer) {
fileAssetContainersSet.add(container.getIdentifier());
} else {
containers.addOrClean(container.getIdentifier(), container.getModDate());
containersSet.add(container.getIdentifier());
}
// Structure dependencies
List<ContainerStructure> csList = APILocator.getContainerAPI().getContainerStructures(container);
for (ContainerStructure containerStructure : csList) {
Structure st = CacheLocator.getContentTypeCache().getStructureByInode(containerStructure.getStructureId());
structures.addOrClean(containerStructure.getStructureId(), st.getModDate());
structuresSet.add(containerStructure.getStructureId());
}
List<MultiTree> treeList = APILocator.getMultiTreeAPI().getMultiTrees(workingPage,container);
for (MultiTree mt : treeList) {
String contentIdentifier = mt.getChild();
// Contents dependencies
List<Contentlet> contentList = APILocator.getContentletAPI().search( "+identifier:" + contentIdentifier, 0, 0, "moddate", user, false );
for ( Contentlet contentletI : contentList ) {
contents.addOrClean( contentletI.getIdentifier(), contentletI.getModDate() );
contentsSet.add( contentletI.getIdentifier() );
}
}
}
// Rule dependencies
final List<Rule> ruleList = APILocator.getRulesAPI().getAllRulesByParent(workingPage, user, false);
for (final Rule rule : ruleList) {
this.rules.add(rule.getId());
this.ruleSet.add(rule.getId());
}
}
} catch (DotSecurityException e) {
Logger.error(this, e.getMessage(),e);
} catch (DotDataException e) {
Logger.error(this, e.getMessage(),e);
}
}
/**
* For given Templates adds its dependencies:
* <ul>
* <li>Hosts</li>
* <li>Containers</li>
* </ul>
*/
private void setTemplateDependencies() {
try {
List<Container> containerList = new ArrayList<>();
FolderAPI folderAPI = APILocator.getFolderAPI();
for (String id : templatesSet) {
Template wkT = APILocator.getTemplateAPI().findWorkingTemplate(id, user, false);
Template lvT = APILocator.getTemplateAPI().findLiveTemplate(id, user, false);
// Host dependency
Host h = APILocator.getHostAPI().find(APILocator.getTemplateAPI().getTemplateHost(wkT).getIdentifier(), user, false);
hosts.addOrClean( APILocator.getTemplateAPI().getTemplateHost( wkT ).getIdentifier(), h.getModDate());
containerList.clear();
containerList.addAll(APILocator.getTemplateAPI().getContainersInTemplate(wkT, user, false));
if(lvT!=null && InodeUtils.isSet(lvT.getInode())) {
containerList.addAll(APILocator.getTemplateAPI().getContainersInTemplate(lvT, user, false));
}
for (Container container : containerList) {
if(container instanceof FileAssetContainer){
fileAssetContainersSet.add(container.getIdentifier());
continue;
}
// Container dependencies
containers.addOrClean( container.getIdentifier(), container.getModDate());
containersSet.add(container.getIdentifier());
}
//Adding theme
if(UtilMethods.isSet(wkT.getTheme())){
try{
Folder themeFolder = folderAPI.find(wkT.getTheme(), user, false);
if(themeFolder != null && InodeUtils.isSet(themeFolder.getInode())){
Folder parent = APILocator.getFolderAPI().findParentFolder(themeFolder, user, false);
if(UtilMethods.isSet(parent)) {
folders.addOrClean( parent.getInode(), parent.getModDate());
foldersSet.add(parent.getInode());
}
List<Folder> folderList = new ArrayList<Folder>();
folderList.add(themeFolder);
setFolderListDependencies(folderList);
}
}catch(DotDataException e1){
Logger.error(DependencyManager.class, "Error trying to add theme folder for template Id: "+id+". Theme folder ignored because: "+e1.getMessage(),e1);
}
}
}
} catch (DotSecurityException e) {
Logger.error(this, e.getMessage(),e);
} catch (DotDataException e) {
Logger.error(this, e.getMessage(),e);
}
}
/**
* For given Containers adds its dependencies:
* <ul>
* <li>Hosts</li>
* <li>Structures</li>
* </ul>
*/
private void setContainerDependencies() {
try {
List<Container> containerList = new ArrayList<>();
for (String id : containersSet) {
Container c = APILocator.getContainerAPI().getWorkingContainerById(id, user, false);
// Host Dependency
Host h = APILocator.getContainerAPI().getParentHost(c, user, false);
hosts.addOrClean( APILocator.getContainerAPI().getParentHost( c, user, false ).getIdentifier(), h.getModDate());
containerList.clear();
Container workingContainer = APILocator.getContainerAPI().getWorkingContainerById(id, user, false);
if ( workingContainer != null ) {
containerList.add( workingContainer );
}
Container liveContainer = APILocator.getContainerAPI().getLiveContainerById(id, user, false);
if ( liveContainer != null ) {
containerList.add( liveContainer );
}
for (Container container : containerList) {
// Structure dependencies
List<ContainerStructure> csList = APILocator.getContainerAPI()
.getContainerStructures(container);
for (ContainerStructure containerStructure : csList) {
Structure st = CacheLocator.getContentTypeCache()
.getStructureByInode(containerStructure.getStructureId());
structures.addOrClean(containerStructure.getStructureId(),
st.getModDate());
structuresSet.add(containerStructure.getStructureId());
}
}
}
// Process FileAssetContainer
final List<Folder> folders = collectFileAssetContainer().stream()
.map(this::collectFileAssetContainerDependencies).flatMap(Collection::stream)
.collect(Collectors.toList());
setFolderListDependencies(folders);
} catch (DotSecurityException e) {
Logger.error(this, e.getMessage(),e);
} catch (DotDataException e) {
Logger.error(this, e.getMessage(),e);
}
}
/**
* Function that wraps calling getWorkingContainerById
*/
private Function<String, Container> workingContainerById = id -> {
try {
return APILocator.getContainerAPI().getWorkingContainerById(id, user, false);
} catch (DotDataException | DotSecurityException e) {
Logger.error(this, e.getMessage(),e);
}
return null;
};
/**
* Aux Predicate to simplify filtering FileAssetContainer
*/
private Predicate<Container> fileAssetContainer = container -> container instanceof FileAssetContainer;
/**
* Utility method that takes a Container source function as param and returns FileAssetContainer
* @return
*/
private Set<FileAssetContainer> collectFileAssetContainer() {
return fileAssetContainersSet.stream().map(workingContainerById).filter(Objects::nonNull)
.filter(fileAssetContainer).map(FileAssetContainer.class::cast)
.collect(Collectors.toSet());
}
/**
* Given that a FileAssetContainer is defined by a bunch of vtl files we need to collect the folder that enclose'em
* @param fileAssetContainer
* @return
*/
private Set<Folder> collectFileAssetContainerDependencies(final FileAssetContainer fileAssetContainer){
final Set<Folder> collectedFolders = new HashSet<>();
final List<FileAsset> fileAssets = fileAssetContainer.getContainerStructuresAssets();
for(final FileAsset fileAsset:fileAssets){
try {
final Folder folder = APILocator.getFolderAPI().findFolderByPath(fileAsset.getPath(), fileAsset.getHost(),user, false);
if(UtilMethods.isSet(folder)) {
collectedFolders.add(folder);
}
} catch (DotSecurityException | DotDataException e) {
Logger.error(this, "Error collecting folders for FileAssetContainer " + fileAsset.getFileName() ,e);
}
}
return collectedFolders;
}
/**
* For given Structures adds its dependencies:
* <ul>
* <li>Hosts</li>
* <li>Folders</li>
* <li>Relationships</li>
* </ul>
*/
private void setStructureDependencies() throws DotDataException, DotSecurityException {
try {
Set<String> s = new HashSet<String>();
s.addAll(structuresSet);
for (String inode : s) {
structureDependencyHelper(inode);
}
} catch (DotDataException e) {
Logger.error(this, e.getMessage(),e);
}
}
/**
*
* @param stInode
* @throws DotDataException
* @throws DotSecurityException
*/
private void structureDependencyHelper(String stInode) throws DotDataException, DotSecurityException{
final Structure structure = CacheLocator.getContentTypeCache().getStructureByInode(stInode);
final Host host = APILocator.getHostAPI().find(structure.getHost(), user, false);
hosts.addOrClean(structure.getHost(), host.getModDate()); // add the host dependency
final Folder folder = APILocator.getFolderAPI().find(structure.getFolder(), user, false);
folders.addOrClean(structure.getFolder(), folder.getModDate()); // add the folder dependency
try {
final List<WorkflowScheme> schemes = APILocator.getWorkflowAPI().findSchemesForStruct(structure);
for (final WorkflowScheme scheme : schemes) {
workflows.addOrClean(scheme.getId(), scheme.getModDate());
}
} catch (DotDataException e) {
Logger.debug(getClass(), ()->"Could not get the Workflow Scheme Dependency for Structure ID: " + structure.getInode());
}
APILocator.getCategoryAPI().findCategories(new StructureTransformer(structure).from(), user).forEach(category -> {
this.categories.addOrClean(category.getCategoryId(), category.getModDate());
});
// Related structures
final List<Relationship> relations = FactoryLocator.getRelationshipFactory().byContentType(structure);
for (final Relationship relationship : relations) {
relationships.addOrClean( relationship.getInode(), relationship.getModDate());
if(!structures.contains(relationship.getChildStructureInode()) && config.getOperation().equals( Operation.PUBLISH) ){
Structure struct = CacheLocator.getContentTypeCache().getStructureByInode(relationship.getChildStructureInode());
solvedStructures.add(stInode);
structures.addOrClean( relationship.getChildStructureInode(), struct.getModDate());
if(!solvedStructures.contains(relationship.getChildStructureInode()))
structureDependencyHelper( relationship.getChildStructureInode() );
}
if(!structures.contains(relationship.getParentStructureInode()) && config.getOperation().equals( Operation.PUBLISH) ){
Structure struct = CacheLocator.getContentTypeCache().getStructureByInode(relationship.getParentStructureInode());
solvedStructures.add(stInode);
structures.addOrClean( relationship.getParentStructureInode(), struct.getModDate());
if(!solvedStructures.contains(relationship.getParentStructureInode()))
structureDependencyHelper( relationship.getParentStructureInode() );
}
}
}