TranslationLanguageCopy.java
42 KB
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.launches.api.Launch
* com.adobe.cq.launches.api.LaunchManager
* com.adobe.cq.launches.api.LaunchManager$CreateOptions
* com.adobe.cq.launches.api.LaunchSource
* com.day.cq.commons.LanguageUtil
* com.day.cq.wcm.api.LanguageManager
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.api.WCMException
* com.day.cq.workflow.WorkflowException
* com.day.text.Text
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.lang3.StringUtils
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.wcm.translation.impl;
import com.adobe.cq.launches.api.Launch;
import com.adobe.cq.launches.api.LaunchManager;
import com.adobe.cq.launches.api.LaunchSource;
import com.adobe.cq.wcm.translation.impl.I18nDictionary;
import com.adobe.cq.wcm.translation.impl.LaunchSourceImpl;
import com.adobe.cq.wcm.translation.impl.TranslationObjectImpl;
import com.adobe.cq.wcm.translation.impl.TranslationPodImpl;
import com.adobe.cq.wcm.translation.impl.TranslationRuleConfigurationFile;
import com.adobe.cq.wcm.translation.impl.TranslationUtils;
import com.day.cq.commons.LanguageUtil;
import com.day.cq.wcm.api.LanguageManager;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.workflow.WorkflowException;
import com.day.text.Text;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TranslationLanguageCopy {
private static final Logger log = LoggerFactory.getLogger(TranslationLanguageCopy.class);
public static final String[] DEFAULT_ROLLOUT_CONFIGS = new String[]{"/etc/msm/rolloutconfigs/pushonmodify"};
public static final String[] DEFAULT_PROMOTE_CONFIGS = new String[]{"/etc/msm/rolloutconfigs/launch"};
public static final String PN_SOURCE_ROOT_RESOURCE = "sourceRootResource";
public static final String PN_IS_DEEP = "isDeep";
public static final String PN_SOURCE_ROOT_RESOURCE_CHILD_FORMAT = "source_%d";
public List<String> createLanguageCopyIfRequired(TranslationPodImpl translationPodImpl, TranslationRuleConfigurationFile ruleFile, List<String> input, String strDestinationLanguage, Session userSession) throws RepositoryException, WCMException, WorkflowException {
ArrayList<String> inputPagesToAdd = new ArrayList<String>();
this.calculateinputPageList(ruleFile, input, inputPagesToAdd);
if (!StringUtils.isEmpty((CharSequence)strDestinationLanguage)) {
ArrayList<String> strFinalPageList = new ArrayList<String>();
ArrayList<String> strLanguageCopyList = new ArrayList<String>();
ArrayList<String> strLaunchCopyList = new ArrayList<String>();
ResourceResolver resourceResolver = ruleFile.getResourceResolver();
LanguageManager languageManager = ruleFile.getLanguageManager();
for (String payloadData : inputPagesToAdd) {
TranslationObjectImpl.TranslationObjectType inputObjectType = TranslationObjectImpl.getTranslationObjectType(resourceResolver, payloadData);
if (inputObjectType == TranslationObjectImpl.TranslationObjectType.PAGE) {
boolean bProcessPagePath = false;
String strPageLanguage = TranslationLanguageCopy.getResourceLanguage(resourceResolver, languageManager, resourceResolver.resolve(payloadData));
if (StringUtils.isEmpty((CharSequence)strPageLanguage) || strDestinationLanguage.compareToIgnoreCase(strPageLanguage) == 0) {
bProcessPagePath = false;
} else {
String langRootPagePath = LanguageUtil.getLanguageRoot((String)payloadData);
if (langRootPagePath != null) {
bProcessPagePath = true;
String contentPath = Text.getRelativeParent((String)langRootPagePath, (int)1);
String pagePath = langRootPagePath.equals(payloadData) ? "" : "" + '/' + payloadData.substring(langRootPagePath.length() + 1);
String destinationPath = contentPath + '/' + strDestinationLanguage + pagePath;
Resource destResource = resourceResolver.getResource(destinationPath);
if (destResource == null) {
strLanguageCopyList.add(payloadData);
} else {
strLaunchCopyList.add(payloadData);
}
} else {
bProcessPagePath = false;
}
}
if (bProcessPagePath) continue;
strFinalPageList.add(payloadData);
continue;
}
if (inputObjectType == TranslationObjectImpl.TranslationObjectType.ASSET || inputObjectType == TranslationObjectImpl.TranslationObjectType.CONTENTFRAGMENT) {
String strAssetPath = payloadData;
Node assetNode = ruleFile.getOrCreateAssetLanguageCopy(strAssetPath);
if (assetNode != null) {
strAssetPath = assetNode.getPath();
}
strFinalPageList.add(strAssetPath);
continue;
}
if (inputObjectType == TranslationObjectImpl.TranslationObjectType.I18NDICTIONARY) {
String strI18nDictPath = payloadData;
Resource i18DictResource = I18nDictionary.getOrCreateLanguageCopy(resourceResolver, strI18nDictPath, strDestinationLanguage);
if (i18DictResource != null) {
strI18nDictPath = ((Node)i18DictResource.adaptTo(Node.class)).getPath();
}
strFinalPageList.add(strI18nDictPath);
continue;
}
strFinalPageList.add(payloadData);
}
strFinalPageList.addAll(this.processLaunguageCopyList(ruleFile, strLanguageCopyList, strDestinationLanguage, userSession));
strFinalPageList.addAll(this.processLaunchCopyList(ruleFile, translationPodImpl.getProjectTitle(), strDestinationLanguage, strLaunchCopyList, userSession));
return strFinalPageList;
}
return inputPagesToAdd;
}
private void calculateinputPageList(TranslationRuleConfigurationFile ruleFile, List<String> input, List<String> outputList) throws RepositoryException {
if (input != null && !input.isEmpty()) {
ResourceResolver resourceResolver = ruleFile.getResourceResolver();
for (String strInput : input) {
Resource resource = resourceResolver.getResource(strInput);
if (TranslationUtils.isFolderNode(resource) && !I18nDictionary.isI18nDictionary(resource)) {
ArrayList<String> childPaths = new ArrayList<String>();
Iterable childList = resource.getChildren();
for (Resource child : childList) {
TranslationObjectImpl.TranslationObjectType inputObjectType = TranslationObjectImpl.getTranslationObjectType(resourceResolver, child.getPath());
if (inputObjectType != TranslationObjectImpl.TranslationObjectType.PAGE || TranslationUtils.isPageNode(child)) {
outputList.add(child.getPath());
}
if (!TranslationUtils.isFolderNode(child)) continue;
childPaths.add(child.getPath());
}
this.calculateinputPageList(ruleFile, childPaths, outputList);
continue;
}
outputList.add(strInput);
}
}
}
private ArrayList<String> processLaunchCopyList(TranslationRuleConfigurationFile ruleFile, String strProjectTitle, String strDestinationLanguage, ArrayList<String> strLaunchCopyList, Session userSession) throws WCMException, WorkflowException, RepositoryException {
if (strLaunchCopyList != null && !strLaunchCopyList.isEmpty()) {
HashMap contentMap = new HashMap();
for (String payloadData : strLaunchCopyList) {
String langRootPagePath = LanguageUtil.getLanguageRoot((String)payloadData);
String contentPath = Text.getRelativeParent((String)langRootPagePath, (int)1);
String pagePath = langRootPagePath.equals(payloadData) ? "" : "" + '/' + payloadData.substring(langRootPagePath.length() + 1);
String destinationPath = contentPath + '/' + strDestinationLanguage + pagePath;
if (contentMap.get(langRootPagePath) == null) {
contentMap.put(langRootPagePath, new ArrayList());
}
((ArrayList)contentMap.get(langRootPagePath)).add(destinationPath);
}
ArrayList<String> strRetVal = new ArrayList<String>();
for (String langRootPagePath : contentMap.keySet()) {
ArrayList strList = (ArrayList)contentMap.get(langRootPagePath);
String strMasterLanguage = Text.getName((String)langRootPagePath);
ArrayList<String> strUpdatedList = this.processContentSiteLaunchCopyList(ruleFile, strProjectTitle, strList, strDestinationLanguage, strMasterLanguage, userSession);
strRetVal.addAll(strUpdatedList);
}
return strRetVal;
}
return strLaunchCopyList;
}
private ArrayList<String> processContentSiteLaunchCopyList(TranslationRuleConfigurationFile ruleFile, String strProjectTitle, ArrayList<String> strLaunchCopyList, String strDestinationLanguage, String strMasterLanguage, Session userSession) throws WCMException, WorkflowException, RepositoryException {
if (strLaunchCopyList != null && !strLaunchCopyList.isEmpty()) {
ResourceResolver resourceResolver = ruleFile.getResourceResolver();
PageManager pageManager = (PageManager)resourceResolver.adaptTo(PageManager.class);
Collections.sort(strLaunchCopyList);
ArrayList<LaunchSource> launchSourceArrayList = new ArrayList<LaunchSource>();
for (String strPath : strLaunchCopyList) {
if (this.isPathParentPresent(launchSourceArrayList, strPath)) continue;
Resource currentResource = resourceResolver.resolve(strPath);
launchSourceArrayList.add(new LaunchSourceImpl(currentResource, true));
}
String strTitle = String.format("Launch Copy of %s", strProjectTitle);
LaunchManager.CreateOptions options = new LaunchManager.CreateOptions();
options.launchSourceList = launchSourceArrayList;
options.title = strTitle;
options.liveDate = null;
options.isDeep = true;
options.isLiveCopy = true;
options.sourceRolloutConfigs = DEFAULT_ROLLOUT_CONFIGS;
options.promoteRolloutConfigs = DEFAULT_PROMOTE_CONFIGS;
LaunchManager launchManager = ruleFile.getLaunchManager();
Launch launch = launchManager.createLaunch(options);
this.injectMasterLanguageIntoLaunch(ruleFile, pageManager, resourceResolver, launch, strMasterLanguage, userSession, true, true);
ArrayList<String> strOutput = new ArrayList<String>();
String strLaunchPath = launch.getResource().getPath();
for (LaunchSource launchSource : launch.getLaunchSources()) {
String launchBasedPath = launchSource.getSourceRootResource().getPath();
if (launchBasedPath.indexOf(strLaunchPath) == -1) {
launchBasedPath = strLaunchPath + launchBasedPath;
}
strOutput.add(launchBasedPath);
}
return strOutput;
}
return strLaunchCopyList;
}
private boolean isPathParentPresent(ArrayList<LaunchSource> launchSourceArrayList, String strPath) {
for (LaunchSource source : launchSourceArrayList) {
if (strPath.indexOf(source.getSourceRootResource().getPath()) != 0) continue;
return true;
}
return false;
}
private ArrayList<String> processLaunguageCopyList(TranslationRuleConfigurationFile ruleFile, ArrayList<String> strLanguageCopyList, String strDestinationLanguage, Session userSession) throws RepositoryException, WCMException {
if (strLanguageCopyList != null && !strLanguageCopyList.isEmpty()) {
Collections.sort(strLanguageCopyList);
ArrayList<String> strOutputList = new ArrayList<String>();
for (String payloadData : strLanguageCopyList) {
String langRootPagePath = LanguageUtil.getLanguageRoot((String)payloadData);
if (langRootPagePath == null) continue;
String contentPath = Text.getRelativeParent((String)langRootPagePath, (int)1);
String pagePath = langRootPagePath.equals(payloadData) ? "" : "" + '/' + payloadData.substring(langRootPagePath.length() + 1);
String destinationPath = contentPath + '/' + strDestinationLanguage + pagePath;
Resource destResource = ruleFile.getResourceResolver().getResource(destinationPath);
if (destResource != null) continue;
destResource = TranslationLanguageCopy.createLanguagePage(ruleFile.getResourceResolver(), userSession, payloadData, destinationPath, strDestinationLanguage, true, true);
strOutputList.add(destResource.getPath());
}
return strOutputList;
}
return strLanguageCopyList;
}
private static String getSourceForMissingDest(String childSourcePath, int missingDestLevel) {
String sourcePath;
sourcePath = "/";
if (StringUtils.isNotEmpty((CharSequence)childSourcePath) && missingDestLevel >= 0) {
String[] elements = Text.explode((String)childSourcePath, (int)47);
if (missingDestLevel >= elements.length) {
log.warn("Unable to determine child with level {} in path {}", (Object)missingDestLevel, (Object)childSourcePath);
return "";
}
for (int i = 0; i < elements.length; ++i) {
sourcePath = sourcePath + elements[i];
if (missingDestLevel != i) {
sourcePath = sourcePath + "/";
continue;
}
break;
}
} else {
log.warn("Unable to determine the missing ancestor source path (childSourcePath = {}, missingDestinationLevel = {})!", (Object)childSourcePath, (Object)missingDestLevel);
return "";
}
return sourcePath;
}
public static Resource createLanguagePage(ResourceResolver resourceResolver, Session session, String strSourcePath, String destinationPath, String destLanguage, boolean copyMissingAncestors, boolean deep) throws RepositoryException, WCMException {
String destLanguageTitle = LanguageUtil.getLanguage((String)destLanguage).getLocale().getDisplayName();
PageManager pageManager = (PageManager)resourceResolver.adaptTo(PageManager.class);
String[] elements = Text.explode((String)destinationPath, (int)47);
Node dstParent = session.getNode("" + '/' + elements[0]);
for (int i = 1; i < elements.length - 1; ++i) {
String name = elements[i];
if (dstParent.hasNode(name)) {
dstParent = dstParent.getNode(name);
continue;
}
if (copyMissingAncestors) {
Node ancestorSource;
String missingDestSourcePath = TranslationLanguageCopy.getSourceForMissingDest(strSourcePath, i);
if (StringUtils.isEmpty((CharSequence)missingDestSourcePath)) {
log.error("Could not identify the source path for creating a copy for missing ancestor {}!", (Object)strSourcePath);
}
if ((ancestorSource = session.getNode(missingDestSourcePath)).hasNode("jcr:content")) {
pageManager.copy(resourceResolver.getResource(missingDestSourcePath), dstParent.getPath() + "/" + name, null, true, true, true);
dstParent = dstParent.getNode(name);
if (!dstParent.getPath().equals(LanguageUtil.getLanguageRoot((String)destinationPath))) continue;
Node contentNode = dstParent.getNode("jcr:content");
if (StringUtils.isEmpty((CharSequence)destLanguageTitle)) continue;
contentNode.setProperty("jcr:title", destLanguageTitle);
log.info("Set Page Title {} to new Language Copy root language at {}", (Object)destLanguageTitle, (Object)dstParent.getPath());
continue;
}
log.warn("Cannot copy missing ancestor from source path {}! The source path page does not have a {} content node! An empty page will be created!!!", new Object[]{missingDestSourcePath, "jcr:content"});
dstParent = TranslationLanguageCopy.createEmptyPage(pageManager, dstParent, name, name.equals(destLanguage) ? destLanguageTitle : name);
continue;
}
dstParent = TranslationLanguageCopy.createEmptyPage(pageManager, dstParent, name, name.equals(destLanguage) ? destLanguageTitle : name);
}
Resource pageResource = resourceResolver.getResource(strSourcePath);
Resource destResource = pageManager.copy(pageResource, destinationPath, null, !deep, false, true);
if (destResource.getPath().equals(LanguageUtil.getLanguageRoot((String)destinationPath))) {
Node contentNode = (Node)destResource.getChild("jcr:content").adaptTo(Node.class);
if (!StringUtils.isEmpty((CharSequence)destLanguageTitle)) {
contentNode.setProperty("jcr:title", destLanguageTitle);
log.info("Set Page Title {} to new Language Copy root language at {}", (Object)destLanguageTitle, (Object)destResource.getPath());
}
}
session.save();
return destResource;
}
private static Node createEmptyPage(PageManager pageManager, Node parent, String childName, String childTitle) throws RepositoryException, WCMException {
return (Node)pageManager.create(parent.getPath(), childName, null, childTitle).adaptTo(Node.class);
}
public static String getResourceLanguage(ResourceResolver resourceResolver, LanguageManager languageManager, Resource resource) {
String strLanguage = null;
if (resource != null) {
strLanguage = I18nDictionary.isI18nDictionary(resource) ? TranslationUtils.getAttribute(resource, "jcr:language", String.class) : TranslationUtils.getResourceLanguage(resourceResolver, resource, languageManager);
}
if (strLanguage == null) {
strLanguage = "";
}
return strLanguage;
}
private void optimizeLaunchForTranslation(TranslationRuleConfigurationFile ruleFile, ResourceResolver resourceResolver, Session session, Launch launch, ArrayList<Resource> destinationResourceList, String masterLanguage) throws RepositoryException {
List launchSourceList = launch.getLaunchSources();
if (destinationResourceList.size() == launch.getLaunchSources().size()) {
ArrayList<LaunchSource> updatedLaunchSourceList = new ArrayList<LaunchSource>();
Resource launchResource = launch.getResource();
for (int index = 0; index < launchSourceList.size(); ++index) {
LaunchSource launchSource = (LaunchSource)launchSourceList.get(index);
Resource launchDestRes = destinationResourceList.get(index);
Resource launchSourceRootRes = launchSource.getSourceRootResource();
String launchSrcRootPath = launchSourceRootRes.getPath();
String launchLangRootPath = LanguageUtil.getLanguageRoot((String)launchSrcRootPath);
String contentPath = Text.getRelativeParent((String)launchLangRootPath, (int)1);
String pagePath = launchLangRootPath.equals(launchSrcRootPath) ? "" : "" + '/' + launchSrcRootPath.substring(launchLangRootPath.length() + 1);
String masterPagePath = contentPath + '/' + masterLanguage + pagePath;
Resource masterPageResource = resourceResolver.getResource(masterPagePath);
ArrayList<String> unmodifiedPageList = new ArrayList<String>();
ArrayList<String> modifiedPageList = new ArrayList<String>();
ArrayList<String> addedDeletedParentPageList = new ArrayList<String>();
this.calculateModifiedPageListBasedOnLastTranslation(resourceResolver, ruleFile, masterPageResource, launchSourceRootRes, launchDestRes, launchResource, unmodifiedPageList, modifiedPageList, session);
this.calculateDeletedPages(addedDeletedParentPageList, modifiedPageList, unmodifiedPageList, launch.getResource().getPath(), masterPageResource, launchSourceRootRes, launchSourceRootRes);
this.calculateNewAddedPages(addedDeletedParentPageList, modifiedPageList, unmodifiedPageList, launch.getResource().getPath(), masterPageResource, launchSourceRootRes, masterPageResource, launchDestRes);
this.calculateLaunchSourceList(launch, launchSourceRootRes, launchDestRes, modifiedPageList, unmodifiedPageList, updatedLaunchSourceList, addedDeletedParentPageList);
this.updateUnModifiedBasedOnLaunchSources(updatedLaunchSourceList, launch.getResource(), unmodifiedPageList, modifiedPageList, session);
this.addLastTranslationUpdateToModifiedList(launch.getResource(), modifiedPageList, session);
}
this.addLaunchSourceToLaunch(launch, updatedLaunchSourceList, session);
}
}
private void calculateNewAddedPages(ArrayList<String> addedDeletedParentPageList, ArrayList<String> modifiedPageList, ArrayList<String> unmodifiedPageList, String strLaunchRootPath, Resource sourceRootResource, Resource destinationRootResource, Resource currentDestinationResource, Resource launchDestRes) {
if (currentDestinationResource != null && TranslationUtils.isPageNode(currentDestinationResource)) {
Resource sourceResource;
String strRelativePath = currentDestinationResource.getPath().substring(destinationRootResource.getPath().length());
if (StringUtils.isEmpty((CharSequence)strRelativePath)) {
sourceResource = destinationRootResource;
} else {
strRelativePath = strRelativePath.substring(1);
sourceResource = destinationRootResource.getChild(strRelativePath);
}
if (sourceResource == null) {
Resource newAddedResource = launchDestRes;
if (!StringUtils.isEmpty((CharSequence)strRelativePath)) {
newAddedResource = launchDestRes.getChild(strRelativePath);
}
if (newAddedResource != null) {
modifiedPageList.add(newAddedResource.getPath());
String strParentPagePath = newAddedResource.getParent().getPath();
modifiedPageList.add(strParentPagePath);
unmodifiedPageList.remove(strParentPagePath);
addedDeletedParentPageList.add(strParentPagePath);
}
}
Iterable childList = currentDestinationResource.getChildren();
for (Resource child : childList) {
this.calculateNewAddedPages(addedDeletedParentPageList, modifiedPageList, unmodifiedPageList, strLaunchRootPath, sourceRootResource, destinationRootResource, child, launchDestRes);
}
}
}
private void calculateDeletedPages(ArrayList<String> addedDeletedParentPageList, ArrayList<String> modifiedPageList, ArrayList<String> unmodifiedPageList, String strLaunchRootPath, Resource sourceRootResource, Resource destinationRootResource, Resource currentDestinationResource) {
if (currentDestinationResource != null && TranslationUtils.isPageNode(currentDestinationResource)) {
Resource sourceResource;
String strRelativePath = currentDestinationResource.getPath().substring(destinationRootResource.getPath().length());
if (StringUtils.isEmpty((CharSequence)strRelativePath)) {
sourceResource = sourceRootResource;
} else {
strRelativePath = strRelativePath.substring(1);
sourceResource = sourceRootResource.getChild(strRelativePath);
}
if (sourceResource == null) {
String strDeletedPagePath = strLaunchRootPath + currentDestinationResource.getPath();
modifiedPageList.add(strDeletedPagePath);
String strParentPagePath = strLaunchRootPath + currentDestinationResource.getParent().getPath();
modifiedPageList.add(strParentPagePath);
unmodifiedPageList.remove(strParentPagePath);
addedDeletedParentPageList.add(strParentPagePath);
}
Iterable childList = currentDestinationResource.getChildren();
for (Resource child : childList) {
this.calculateDeletedPages(addedDeletedParentPageList, modifiedPageList, unmodifiedPageList, strLaunchRootPath, sourceRootResource, destinationRootResource, child);
}
}
}
private void addLastTranslationUpdateToModifiedList(Resource launchResource, ArrayList<String> modifiedPageList, Session session) throws RepositoryException {
for (String strPagePath : modifiedPageList) {
Resource pageResource = launchResource.getChild(strPagePath);
this.setLastTranslationUpdateToPage(pageResource);
}
session.save();
}
private void setLastTranslationUpdateToPage(Resource pageResource) throws RepositoryException {
Resource launchContent;
if (pageResource != null && (launchContent = pageResource.getChild("jcr:content")) != null) {
Node sourcePageContentNode = (Node)launchContent.adaptTo(Node.class);
Calendar lastModified = TranslationUtils.getCalendarAttribute(log, launchContent, "cq:lastModified");
if (lastModified == null) {
lastModified = Calendar.getInstance();
}
sourcePageContentNode.setProperty("cq:lastTranslationUpdate", lastModified);
}
}
private void updateUnModifiedBasedOnLaunchSources(ArrayList<LaunchSource> launchSourceArrayList, Resource launchResource, ArrayList<String> unmodifiedPageList, ArrayList<String> modifiedPageList, Session session) throws RepositoryException {
for (String strPagePath : unmodifiedPageList) {
LaunchSource nearestSource;
Resource pageResource = launchResource.getChild(strPagePath);
if (pageResource == null || (nearestSource = this.findNearestLaunchSource(launchSourceArrayList, strPagePath)) != null && nearestSource.isDeep()) continue;
this.updatePageSetLaunchNoContent(pageResource, modifiedPageList, session);
}
}
private LaunchSource findNearestLaunchSource(List<LaunchSource> launchSourceList, String strPagePath) {
LaunchSource startResourceSource = null;
for (LaunchSource source : launchSourceList) {
Resource sourceResource = source.getSourceRootResource();
if (strPagePath.indexOf(sourceResource.getPath()) == -1) continue;
if (startResourceSource == null) {
startResourceSource = source;
continue;
}
if (sourceResource.getPath().length() <= startResourceSource.getSourceRootResource().getPath().length()) continue;
startResourceSource = source;
}
return startResourceSource;
}
private void addLaunchSourceToLaunch(Launch launch, ArrayList<LaunchSource> launchSourceArrayList, Session session) throws RepositoryException {
Resource launchResource = launch.getResource();
String launchResourcePath = launchResource.getPath();
Resource sources = launchResource.getChild("jcr:content/sources");
if (sources != null) {
this.deleteAllChilds(sources, session);
int index = 0;
Node sourcesNode = (Node)sources.adaptTo(Node.class);
for (LaunchSource launchSource : launchSourceArrayList) {
String strNodeName = String.format("source_%d", index);
++index;
Node childNode = sourcesNode.addNode(strNodeName);
String strRootResourcePath = launchSource.getSourceRootResource().getPath();
String strPath = strRootResourcePath.substring(launchResourcePath.length());
childNode.setProperty("sourceRootResource", strPath);
childNode.setProperty("isDeep", launchSource.isDeep());
}
}
session.save();
}
private void deleteAllChilds(Resource parent, Session session) throws RepositoryException {
if (parent != null) {
Iterable childList = parent.getChildren();
for (Resource child : childList) {
Node childNode = (Node)child.adaptTo(Node.class);
childNode.remove();
}
}
session.save();
}
private void calculateLaunchSourceList(Launch launch, Resource destinationResourceRoot, Resource currentResource, ArrayList<String> modifiedPageList, ArrayList<String> unmodifiedPageList, ArrayList<LaunchSource> launchSourceArrayList, ArrayList<String> addedDeletedParentPageList) {
String strResourcePath;
boolean isDeep = false;
if (TranslationUtils.isPageNode(currentResource) && modifiedPageList.contains(strResourcePath = currentResource.getPath())) {
LaunchSource nearestSource = this.findNearestLaunchSource(launch.getLaunchSources(), strResourcePath);
boolean bl = isDeep = nearestSource != null ? nearestSource.isDeep() : true;
if (this.doesListContainsSubPath(unmodifiedPageList, strResourcePath) && this.allChildPathExists(destinationResourceRoot, launch.getResource(), currentResource)) {
isDeep = false;
}
if (addedDeletedParentPageList.contains(strResourcePath)) {
isDeep = true;
}
launchSourceArrayList.add(new LaunchSourceImpl(currentResource, isDeep));
}
if (!isDeep) {
Iterable childList = currentResource.getChildren();
for (Resource child : childList) {
this.calculateLaunchSourceList(launch, destinationResourceRoot, child, modifiedPageList, unmodifiedPageList, launchSourceArrayList, addedDeletedParentPageList);
}
}
}
private boolean allChildPathExists(Resource destinationResourceRoot, Resource launchResource, Resource currentResource) {
String strResourcePath;
boolean bRetVal = true;
if (TranslationUtils.isPageNode(currentResource) && destinationResourceRoot.getChild(strResourcePath = currentResource.getPath().substring(launchResource.getPath().length())) == null) {
bRetVal = false;
}
if (bRetVal) {
Iterable childList = currentResource.getChildren();
for (Resource child : childList) {
if (this.allChildPathExists(destinationResourceRoot, launchResource, child)) continue;
bRetVal = false;
break;
}
}
return bRetVal;
}
private boolean doesListContainsSubPath(ArrayList<String> pageList, String strResourcePath) {
boolean bRetVal = false;
for (String strPath : pageList) {
if (strPath.length() <= strResourcePath.length() || strPath.indexOf(strResourcePath) != 0) continue;
bRetVal = true;
break;
}
return bRetVal;
}
private void calculateModifiedPageListBasedOnLastTranslation(ResourceResolver resourceResolver, TranslationRuleConfigurationFile ruleFile, Resource masterPageResource, Resource launchSourceRootRes, Resource launchDestRes, Resource launchResource, ArrayList<String> unmodifiedPageList, ArrayList<String> modifiedPageList, Session session) throws RepositoryException {
if (launchDestRes != null) {
if (TranslationUtils.isPageNode(launchDestRes)) {
boolean bUpdated = false;
if (this.isResourceUpdatedSinceLastTranslation(masterPageResource, launchSourceRootRes, launchDestRes, launchResource)) {
bUpdated = true;
}
if (!bUpdated) {
bUpdated = this.isEmbeddedAssetsUpdatedSinceLastTranslation(resourceResolver, ruleFile, session, masterPageResource, launchSourceRootRes, launchDestRes, launchResource);
}
if (bUpdated) {
modifiedPageList.add(launchDestRes.getPath());
} else {
unmodifiedPageList.add(launchDestRes.getPath());
}
}
Iterable childList = launchDestRes.getChildren();
for (Resource child : childList) {
this.calculateModifiedPageListBasedOnLastTranslation(resourceResolver, ruleFile, masterPageResource, launchSourceRootRes, child, launchResource, unmodifiedPageList, modifiedPageList, session);
}
}
}
private void updatePageSetLaunchNoContent(Resource launchPageResource, ArrayList<String> modifiedPageList, Session session) throws RepositoryException {
Resource launchContent = launchPageResource.getChild("jcr:content");
if (launchContent != null) {
this.deleteAllChilds(launchContent, session);
Node contentNode = (Node)launchContent.adaptTo(Node.class);
if (contentNode != null) {
contentNode.setProperty("cq:template", "/libs/launches/templates/outofscope");
contentNode.setProperty("sling:resourceType", "launches/components/outofscope");
session.save();
}
this.deleteParentIfRequired(launchPageResource, modifiedPageList, session);
}
}
private void deleteParentIfRequired(Resource currentResource, ArrayList<String> modifiedPageList, Session session) throws RepositoryException {
int iChildCount;
if (currentResource != null && (iChildCount = this.getCurrentResoureChildCount(currentResource)) <= 1) {
Resource parentResource = currentResource.getParent();
Node currentNode = (Node)currentResource.adaptTo(Node.class);
if (currentNode != null) {
if (!modifiedPageList.contains(currentNode.getPath())) {
currentNode.remove();
session.save();
this.deleteParentIfRequired(parentResource, modifiedPageList, session);
} else {
log.info("Skipping the node as this is modified {}", (Object)currentNode.getPath());
}
}
}
}
private int getCurrentResoureChildCount(Resource currentResource) {
int iCount = 0;
if (currentResource != null) {
Iterable childList = currentResource.getChildren();
for (Resource child : childList) {
if (child == null) continue;
++iCount;
}
}
return iCount;
}
private boolean isEmbeddedAssetsUpdatedSinceLastTranslation(ResourceResolver resourceResolver, TranslationRuleConfigurationFile ruleFile, Session session, Resource sourceRootResource, Resource destinationRootResource, Resource launchDestRes, Resource launchResource) {
boolean bRetVal;
block3 : {
bRetVal = false;
try {
ArrayList<Node> nodeList = ruleFile.getAssetListFromPage((Node)launchDestRes.adaptTo(Node.class), session, true);
if (nodeList == null || nodeList.size() <= 0) break block3;
for (Node node : nodeList) {
if (!this.isNodeUpdatedSinceLastTranslation(resourceResolver, node)) continue;
bRetVal = true;
break;
}
}
catch (Exception ex) {
log.error("Error while checking for translation Update ", (Throwable)ex);
}
}
return bRetVal;
}
private boolean isNodeUpdatedSinceLastTranslation(ResourceResolver resourceResolver, Node node) throws RepositoryException {
boolean bRetVal = false;
if (node != null) {
Resource resource;
if (node.hasNode("jcr:content")) {
node = node.getNode("jcr:content");
}
if ((resource = resourceResolver.getResource(node.getPath())) != null) {
Calendar sourceLastModified = TranslationUtils.getCalendarAttribute(log, resource, "cq:lastModified");
Calendar lastTranslationUpdate = TranslationUtils.getCalendarAttribute(log, resource, "cq:lastTranslationUpdate");
if (sourceLastModified != null && lastTranslationUpdate != null) {
bRetVal = lastTranslationUpdate.getTimeInMillis() < sourceLastModified.getTimeInMillis();
}
}
}
return bRetVal;
}
private boolean isResourceUpdatedSinceLastTranslation(Resource sourceRootResource, Resource destinationRootResource, Resource launchDestRes, Resource launchResource) {
boolean bRetVal = true;
try {
Resource sourceResource;
Resource destResource;
String strResourcePath = launchDestRes.getPath().substring(launchResource.getPath().length());
String strRelativePath = strResourcePath.substring(destinationRootResource.getPath().length());
if (StringUtils.isEmpty((CharSequence)strRelativePath)) {
sourceResource = sourceRootResource;
destResource = destinationRootResource;
} else {
strRelativePath = strRelativePath.substring(1);
sourceResource = sourceRootResource.getChild(strRelativePath);
destResource = destinationRootResource.getChild(strRelativePath);
}
if (sourceResource != null && destResource != null) {
Resource sourceContentResource = sourceResource.getChild("jcr:content");
Resource destContentResource = destResource.getChild("jcr:content");
Calendar sourceLastModified = TranslationUtils.getCalendarAttribute(log, sourceContentResource, "cq:lastModified");
Calendar lastTranslationUpdate = TranslationUtils.getCalendarAttribute(log, destContentResource, "cq:lastTranslationUpdate");
if (sourceLastModified != null && lastTranslationUpdate != null) {
bRetVal = lastTranslationUpdate.getTimeInMillis() < sourceLastModified.getTimeInMillis();
}
}
}
catch (Exception ex) {
log.error("Error while checking for translation Update ", (Throwable)ex);
}
return bRetVal;
}
public ArrayList<Resource> injectMasterLanguageIntoLaunch(TranslationRuleConfigurationFile ruleFile, PageManager pageManager, ResourceResolver resourceResolver, Launch launch, String masterLanguage, Session session, boolean deep, boolean bOptimizeForTranslation) throws WCMException, WorkflowException, RepositoryException {
String launchPath = launch.getResource().getPath();
ArrayList<Resource> retVal = new ArrayList<Resource>();
for (LaunchSource launchSource : launch.getLaunchSources()) {
Resource launchSourceRootRes = launchSource.getSourceRootResource();
String launchSrcRootPath = launchSourceRootRes.getPath();
String launchLangRootPath = LanguageUtil.getLanguageRoot((String)launchSrcRootPath);
String contentPath = Text.getRelativeParent((String)launchLangRootPath, (int)1);
String pagePath = launchLangRootPath.equals(launchSrcRootPath) ? "" : "" + '/' + launchSrcRootPath.substring(launchLangRootPath.length() + 1);
String masterPagePath = contentPath + '/' + masterLanguage + pagePath;
String launchDest = launchPath + launchSrcRootPath;
Resource masterPageResource = resourceResolver.getResource(masterPagePath);
if (masterPageResource == null) {
throw new WorkflowException("Master page resource does not exist.");
}
Resource launchRootResource = launch.getRootResource();
String launchRootResourcePath = launchRootResource.getPath();
pageManager.delete(launchRootResource, false);
session.save();
if (launchRootResource != null && launchRootResource.getParent() != null && resourceResolver.resolve(launchRootResourcePath) != null) {
try {
String strNewPath = launchRootResource.getParent().getPath() + "/temp_delete";
session.move(launchRootResourcePath, strNewPath);
session.save();
}
catch (Exception ex) {
log.error("Error while moving delete path {}", (Throwable)ex);
}
}
Resource launchDestRes = pageManager.copy(masterPageResource, launchDest, null, !deep, true);
retVal.add(launchDestRes);
}
if (bOptimizeForTranslation) {
this.optimizeLaunchForTranslation(ruleFile, resourceResolver, session, launch, retVal, masterLanguage);
}
return retVal;
}
}