TranslationObjectImpl.java
67.6 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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.launches.api.Launch
* com.adobe.cq.projects.api.Project
* com.adobe.granite.comments.Comment
* com.adobe.granite.comments.CommentCollection
* com.adobe.granite.comments.CommentManager
* com.adobe.granite.translation.api.TranslationConstants
* com.adobe.granite.translation.api.TranslationConstants$TranslationMethod
* com.adobe.granite.translation.api.TranslationConstants$TranslationStatus
* com.adobe.granite.translation.api.TranslationException
* com.adobe.granite.translation.api.TranslationException$ErrorCode
* com.adobe.granite.translation.api.TranslationMetadata
* com.adobe.granite.translation.api.TranslationObject
* com.adobe.granite.translation.api.TranslationService
* com.adobe.granite.translation.api.TranslationService$TranslationServiceInfo
* com.adobe.granite.translation.api.TranslationState
* com.adobe.granite.translation.api.xliff.TranslationXLIFFService
* com.adobe.granite.translation.api.xliff.TranslationXLIFFServiceException
* com.adobe.granite.translation.api.xliff.TranslationXLIFFServiceException$ErrorCode
* com.adobe.granite.translation.core.MachineTranslationCloudConfig
* com.day.cq.dam.api.Asset
* com.day.cq.dam.api.Rendition
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManagerFactory
* javax.jcr.Binary
* javax.jcr.Node
* javax.jcr.PathNotFoundException
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Value
* javax.jcr.ValueFactory
* javax.jcr.nodetype.NodeType
* org.apache.commons.lang3.StringUtils
* org.apache.sling.api.resource.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.wcm.translation.impl;
import com.adobe.cq.launches.api.Launch;
import com.adobe.cq.projects.api.Project;
import com.adobe.cq.wcm.translation.impl.CQPageHumanTranslator;
import com.adobe.cq.wcm.translation.impl.CQPageMachineTranslator;
import com.adobe.cq.wcm.translation.impl.CQPagePreviewGenerator;
import com.adobe.cq.wcm.translation.impl.ContentFragmentHumanTranslator;
import com.adobe.cq.wcm.translation.impl.ContentFragmentMachineTranslator;
import com.adobe.cq.wcm.translation.impl.I18nDictionary;
import com.adobe.cq.wcm.translation.impl.I18nDictionaryHumanTranslation;
import com.adobe.cq.wcm.translation.impl.I18nDictionaryMachineTranslation;
import com.adobe.cq.wcm.translation.impl.TagTranslator;
import com.adobe.cq.wcm.translation.impl.TranslationBaseObject;
import com.adobe.cq.wcm.translation.impl.TranslationMetadataImpl;
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.adobe.granite.comments.Comment;
import com.adobe.granite.comments.CommentCollection;
import com.adobe.granite.comments.CommentManager;
import com.adobe.granite.translation.api.TranslationConstants;
import com.adobe.granite.translation.api.TranslationException;
import com.adobe.granite.translation.api.TranslationMetadata;
import com.adobe.granite.translation.api.TranslationObject;
import com.adobe.granite.translation.api.TranslationService;
import com.adobe.granite.translation.api.TranslationState;
import com.adobe.granite.translation.api.xliff.TranslationXLIFFService;
import com.adobe.granite.translation.api.xliff.TranslationXLIFFServiceException;
import com.adobe.granite.translation.core.MachineTranslationCloudConfig;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManagerFactory;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipInputStream;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFactory;
import javax.jcr.nodetype.NodeType;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.xpath.XPathExpressionException;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class TranslationObjectImpl
extends TranslationBaseObject
implements TranslationObject,
Comparable<TranslationObjectImpl> {
private ResourceResolverFactory resolverFactory;
private PageManagerFactory pageManagerFactory = null;
private static final Logger logger = LoggerFactory.getLogger(TranslationObjectImpl.class);
private static final ArrayList<String> donotTranslatePropertyListName = new ArrayList();
private static int MAX_XLIFF_SNIFFER_BUF_LENGTH = 100;
private static final String NT_SLING_ORDEREDFOLDER = "sling:OrderedFolder";
private static final String ATTRIBUTE_EXTRACT_METADATA = "dam:extractMetadata";
private TranslationMetadataImpl metadata;
TranslationRuleConfigurationFile ruleFile;
TranslationPodImpl translationPod;
public ResourceResolverFactory getResolverFactory() {
return this.resolverFactory;
}
public void setResolverFactory(ResourceResolverFactory resolverFactory) {
this.resolverFactory = resolverFactory;
}
public PageManagerFactory getPageManagerFactory() {
return this.pageManagerFactory;
}
public void setPageManagerFactory(PageManagerFactory pageManagerFactory) {
this.pageManagerFactory = pageManagerFactory;
}
public TranslationObjectImpl(TranslationPodImpl translationPod, TranslationRuleConfigurationFile ruleFile, Node node, Resource resource, Session userSession) {
super(node, resource, userSession);
this.ruleFile = ruleFile;
this.translationPod = translationPod;
this.metadata = new TranslationMetadataImpl(this);
if (donotTranslatePropertyListName.size() == 0) {
donotTranslatePropertyListName.add("jcr:primaryType");
}
}
@Override
protected Logger getLogger() {
return logger;
}
public String getTitle() {
return this.getStringAttribute("jcr:title");
}
public String getTranslationObjectTargetPath() {
return this.getStringAttribute("targetPath");
}
public String getTranslationObjectSourcePath() {
String strSourcePath = this.getStringAttribute("sourcePath");
if (strSourcePath == null) {
strSourcePath = "";
}
return strSourcePath;
}
public String getSourceVersion() {
return this.getStringAttribute("sourceVersion");
}
private boolean isEmbeddedAsset() throws RepositoryException {
return TranslationUtils.isBinaryNode(this.getResourceResolver(), this.getTranslationObjectSourcePath());
}
private void setMimeType() throws RepositoryException {
String strMimeType = "text/html";
TranslationObjectType objType = this.getTranslationObjectType();
switch (objType) {
case PAGE: {
strMimeType = "text/html";
break;
}
case ASSET: {
Asset assetFile = (Asset)this.getTranslationSourceResource().adaptTo(Asset.class);
if (assetFile != null) {
strMimeType = assetFile.getMimeType();
break;
}
if (!this.isEmbeddedAsset()) break;
strMimeType = TranslationUtils.getMimeTypeAttribute(this.getResourceResolver(), this.getTranslationObjectSourcePath());
break;
}
case TAG:
case FILE:
case CONTENTFRAGMENT:
case ASSETMETADATA:
case TAGMETADATA:
case I18NDICTIONARY:
case I18NCOMPONENTSTRINGDICT: {
strMimeType = "text/xml";
}
}
this.setAttribute("mimeType", strMimeType);
}
public String getMimeType() {
return this.getStringAttribute("mimeType");
}
public String getId() {
return this.getTranslationObjectID();
}
public CommentCollection<Comment> getCommentCollection() {
CommentManager commentManager = this.ruleFile.getCommentManager();
CommentCollection commentCollection = null;
if (commentManager != null) {
commentCollection = commentManager.getCollection(this.getResource(), CommentCollection.class);
}
return commentCollection;
}
public Iterator<TranslationObject> getSupportingTranslationObjectsIterator() {
return null;
}
public int getSupportingTranslationObjectsCount() {
return this.getIntAttribute("supportedTranslationObjectCount");
}
public TranslationObject getTranslationObjectMetadata() {
return null;
}
public TranslationMetadata getTranslationJobMetadata() {
return this.metadata;
}
public InputStream getTranslationObjectInputStream() throws TranslationException {
return this.getTranslationObjectXMLInputStream();
}
public InputStream getTranslationObjectXMLInputStream() throws TranslationException {
InputStream inputStream;
if (this.getTranslationObjectType() == TranslationObjectType.ASSET) {
inputStream = this.getAssetInputStream();
} else {
try {
String strOutput = this.exportToXMLString();
inputStream = new ByteArrayInputStream(strOutput.getBytes("UTF-8"));
}
catch (Exception e) {
logger.error("Error while getting the XML input stream for translation object", (Throwable)e);
throw new TranslationException("Error while getting XML input stream", (Throwable)e, TranslationException.ErrorCode.REQUEST_FAILED);
}
}
return inputStream;
}
public InputStream getTranslationObjectXLIFFInputStream(String xliffVersion) throws TranslationException {
if (logger.isTraceEnabled()) {
logger.trace("Inside getTranslationObjectXLIFFInputStream()");
logger.trace("Translation Object Path:{}", (Object)this.getPath());
logger.trace("Requested XLIFF Version:{}", (Object)xliffVersion);
}
InputStream inputStream = null;
if (this.getTranslationObjectType() == TranslationObjectType.ASSET) {
inputStream = this.getAssetInputStream();
} else {
try {
String strOutput = this.exportToXLIFFString(xliffVersion);
inputStream = new ByteArrayInputStream(strOutput.getBytes("UTF-8"));
}
catch (Exception e) {
logger.error("Error while getting the XLIFF input stream for translation object", (Throwable)e);
throw new TranslationException("Error while getting XLIFF input stream", (Throwable)e, TranslationException.ErrorCode.REQUEST_FAILED);
}
}
if (inputStream == null) {
logger.trace("Failed to get XLIFF {} inputStream", (Object)xliffVersion);
}
return inputStream;
}
private InputStream getAssetInputStream() throws TranslationException {
try {
if (this.isEmbeddedAsset()) {
Node node = (Node)this.getTranslationSourceResource().adaptTo(Node.class);
return node.getProperty("jcr:data").getBinary().getStream();
}
Asset assetFile = (Asset)this.getTranslationSourceResource().adaptTo(Asset.class);
return assetFile.getOriginal().getStream();
}
catch (RepositoryException e) {
throw new TranslationException("Error while getting embedded asset input stream", TranslationException.ErrorCode.GENERAL_EXCEPTION);
}
}
public InputStream getTranslatedObjectInputStream() {
return null;
}
public TranslationConstants.TranslationStatus getTranslationStatus() {
return this.metadata.getTranslationState().getStatus();
}
public boolean submitForTranslation(TranslationConstants.TranslationMethod translationMethod) throws TranslationException, RepositoryException {
boolean bSaveRequired = false;
if (this.getTranslationStatus() == TranslationConstants.TranslationStatus.DRAFT) {
TranslationService tSvc = this.translationPod.getTranslationService();
if (translationMethod == TranslationConstants.TranslationMethod.HUMAN_TRANSLATION) {
String strID = tSvc.uploadTranslationObject(this.translationPod.getTranslationObjectID(), (TranslationObject)this);
this.setTranslationObjectID(strID);
this.setTranslationStatus(TranslationConstants.TranslationStatus.SUBMITTED, true, true);
bSaveRequired = true;
}
}
return bSaveRequired;
}
public boolean translateNow(TranslationConstants.TranslationMethod translationMethod) throws TranslationException, RepositoryException, IOException {
boolean bSaveRequired = false;
TranslationConstants.TranslationStatus currentStatus = this.getTranslationStatus();
if (currentStatus == TranslationConstants.TranslationStatus.DRAFT || currentStatus == TranslationConstants.TranslationStatus.SUBMITTED || currentStatus == TranslationConstants.TranslationStatus.SCOPE_COMPLETED || currentStatus == TranslationConstants.TranslationStatus.SCOPE_REQUESTED) {
if (this.metadata.isObjectTranslationRequired()) {
TranslationService tSvc = this.translationPod.getTranslationService();
if (translationMethod == TranslationConstants.TranslationMethod.MACHINE_TRANSLATION) {
tSvc.setDefaultCategory(this.translationPod.getContentCategory());
this.setTranslationStatus(TranslationConstants.TranslationStatus.TRANSLATION_IN_PROGRESS, true, true);
bSaveRequired = this.startMachineTranslation(tSvc);
this.createVersionAndSetTranslationDate(translationMethod, tSvc.getTranslationServiceInfo().getTranslationServiceName());
this.setTranslationStatus(TranslationConstants.TranslationStatus.READY_FOR_REVIEW, true, true);
} else {
String strID = tSvc.uploadTranslationObject(this.translationPod.getTranslationObjectID(), (TranslationObject)this);
this.setTranslationObjectID(strID);
this.setTranslationStatus(TranslationConstants.TranslationStatus.COMMITTED_FOR_TRANSLATION, true, true);
}
} else {
this.setTranslationStatus(TranslationConstants.TranslationStatus.READY_FOR_REVIEW, true, true);
bSaveRequired = true;
}
} else {
logger.debug("Translation Resource right now in following status {}" + (Object)currentStatus);
}
return bSaveRequired;
}
private void createVersionAndSetTranslationDate(TranslationConstants.TranslationMethod translationMethod, String strTranslationProvider) throws RepositoryException {
Resource sourcePageContentResource;
Resource sourceResource;
TranslationObjectType objectType = this.getTranslationObjectType();
if ((objectType == TranslationObjectType.PAGE || objectType == TranslationObjectType.CONTENTFRAGMENT) && (sourcePageContentResource = (sourceResource = this.getTranslationSourceResource()).getChild("jcr:content")) != null) {
TranslationUtils.updateLastModifiedTime((Page)sourceResource.adaptTo(Page.class));
Node sourcePageContentNode = (Node)sourcePageContentResource.adaptTo(Node.class);
if (sourcePageContentNode != null) {
if (!sourcePageContentNode.hasProperty("cq:lastTranslationUpdate")) {
Calendar cal = Calendar.getInstance();
sourcePageContentNode.setProperty("cq:lastTranslationUpdate", cal);
}
sourcePageContentNode.setProperty("cq:translationMethod", translationMethod.toString());
sourcePageContentNode.setProperty("cq:translationProviderName", strTranslationProvider);
}
}
}
public void setTranslationStatus(TranslationConstants.TranslationStatus status, boolean bSave, boolean bCallAPI) throws RepositoryException, TranslationException {
if (status != TranslationConstants.TranslationStatus.DRAFT && status != TranslationConstants.TranslationStatus.UNKNOWN_STATE && bCallAPI) {
this.translationPod.setTranslationObjectStatus(this);
}
this.metadata.setTranslationStatus(status, bSave);
if (status.equals((Object)TranslationConstants.TranslationStatus.APPROVED)) {
this.updateAssetsIfRequired();
}
}
private boolean startMachineTranslation(TranslationService tSvc) throws TranslationException, RepositoryException, IOException {
boolean bSaveRequired = false;
TranslationObjectType objectType = this.getTranslationObjectType();
Resource resource = this.getTranslationSourceResource();
switch (objectType) {
case PAGE: {
CQPageMachineTranslator pageTranslator = new CQPageMachineTranslator(this.ruleFile, tSvc);
ArrayList<Resource> resourceList = new ArrayList<Resource>();
resourceList.add(resource);
bSaveRequired = pageTranslator.translateResourceList(this.getResourceResolver(), resourceList, objectType);
break;
}
case FILE: {
break;
}
case ASSET: {
ArrayList<TranslationObjectImpl> assetMetadataList = this.translationPod.getCachedTranslationObjectList(TranslationObjectType.ASSETMETADATA);
TranslationUtils.addOrUpdateRelatedTranslationObjectsProperty(this, assetMetadataList, this.userSession.getValueFactory());
break;
}
case CONTENTFRAGMENT: {
ContentFragmentMachineTranslator contentFragmentTranslator = new ContentFragmentMachineTranslator(this.ruleFile, tSvc, this.userSession.getValueFactory());
ArrayList<Resource> resourceList = new ArrayList<Resource>();
resourceList.add(resource);
bSaveRequired = contentFragmentTranslator.translateResourceList(this.getResourceResolver(), resourceList);
break;
}
case TAG: {
bSaveRequired = TagTranslator.translate(resource, tSvc, this.ruleFile);
break;
}
case I18NDICTIONARY: {
I18nDictionaryMachineTranslation i18nTranslator = new I18nDictionaryMachineTranslation(this.ruleFile, tSvc, this.getResourceResolver());
bSaveRequired = i18nTranslator.translateI18nDictionary(this.getResourceResolver(), resource);
break;
}
case I18NCOMPONENTSTRINGDICT: {
I18nDictionaryMachineTranslation i18nTranslator = new I18nDictionaryMachineTranslation(this.ruleFile, tSvc, this.getResourceResolver());
ArrayList<TranslationObjectImpl> pageList = this.translationPod.getCachedTranslationObjectList(TranslationObjectType.PAGE);
if (pageList == null) break;
ArrayList<Resource> pageResourceList = this.translationPod.getSourceResourceListFromTranslationObjectList(pageList);
bSaveRequired = i18nTranslator.createAndTranslateI18nComponentStringDict(pageResourceList);
break;
}
case ASSETMETADATA: {
ArrayList<TranslationObjectImpl> assetList = this.translationPod.getCachedTranslationObjectList(TranslationObjectType.ASSET);
if (assetList.size() == 0) break;
CQPageMachineTranslator pageTranslator = new CQPageMachineTranslator(this.ruleFile, tSvc);
TranslationUtils.addOrUpdateRelatedTranslationObjectsProperty(this, assetList, this.userSession.getValueFactory());
bSaveRequired = pageTranslator.translateResourceList(this.getResourceResolver(), this.translationPod.getSourceResourceListFromTranslationObjectList(assetList), objectType);
break;
}
case TAGMETADATA: {
ArrayList<TranslationObjectImpl> pageList;
ArrayList<TranslationObjectImpl> totalObjectList = new ArrayList<TranslationObjectImpl>();
if (this.translationPod.isAssetTagMetaDataTranslationRequired()) {
ArrayList<TranslationObjectImpl> contentFragmentList;
ArrayList<TranslationObjectImpl> assetList = this.translationPod.getCachedTranslationObjectList(TranslationObjectType.ASSET);
if (assetList != null) {
totalObjectList.addAll(assetList);
}
if ((contentFragmentList = this.translationPod.getCachedTranslationObjectList(TranslationObjectType.CONTENTFRAGMENT)) != null) {
totalObjectList.addAll(contentFragmentList);
}
}
if (this.translationPod.isPageTagMetaDataTranslationRequired() && (pageList = this.translationPod.getCachedTranslationObjectList(TranslationObjectType.PAGE)) != null) {
totalObjectList.addAll(pageList);
}
if (totalObjectList.size() == 0) break;
CQPageMachineTranslator pageTranslator = new CQPageMachineTranslator(this.ruleFile, tSvc);
bSaveRequired = pageTranslator.translateResourceList(this.getResourceResolver(), this.translationPod.getSourceResourceListFromTranslationObjectList(totalObjectList), objectType);
break;
}
}
return bSaveRequired;
}
private Document exportToXMLDocument() throws PathNotFoundException, IOException, RepositoryException, ParserConfigurationException, SAXException, XPathExpressionException, DOMException, TranslationException, TransformerException {
Document document = null;
TranslationObjectType objectType = this.getTranslationObjectType();
switch (objectType) {
case PAGE: {
Resource cqPageResource = this.getTranslationSourceResource();
CQPageHumanTranslator pageTranslator = new CQPageHumanTranslator(this.ruleFile);
ArrayList<Resource> resourceList = new ArrayList<Resource>();
resourceList.add(this.getTranslationSourceResource());
document = pageTranslator.generateXMLForResourceList(resourceList, TranslationObjectType.PAGE, cqPageResource.getPath());
break;
}
case FILE: {
break;
}
case ASSET: {
break;
}
case CONTENTFRAGMENT: {
Resource contentFragmentResource = this.getTranslationSourceResource();
ContentFragmentHumanTranslator contentFragmentTranslator = new ContentFragmentHumanTranslator(this.ruleFile);
ArrayList<Resource> resourceList = new ArrayList<Resource>();
resourceList.add(this.getTranslationSourceResource());
document = contentFragmentTranslator.generateXMLForResourceList(resourceList, TranslationObjectType.CONTENTFRAGMENT, contentFragmentResource.getPath());
break;
}
case ASSETMETADATA: {
ArrayList<TranslationObjectImpl> assetList = this.translationPod.getCachedTranslationObjectList(TranslationObjectType.ASSET);
if (assetList.size() == 0) {
throw new TranslationException("No Asset present right now", TranslationException.ErrorCode.GENERAL_EXCEPTION);
}
CQPageHumanTranslator pageTranslator = new CQPageHumanTranslator(this.ruleFile);
document = pageTranslator.generateXMLForResourceList(this.translationPod.getSourceResourceListFromTranslationObjectList(assetList), TranslationObjectType.ASSETMETADATA, null);
TranslationUtils.addOrUpdateRelatedTranslationObjectsProperty(this, assetList, this.userSession.getValueFactory());
break;
}
case TAGMETADATA: {
ArrayList<TranslationObjectImpl> pageList;
ArrayList<TranslationObjectImpl> totalObjectList = new ArrayList<TranslationObjectImpl>();
if (this.translationPod.isAssetTagMetaDataTranslationRequired()) {
ArrayList<TranslationObjectImpl> contentFragmentList;
ArrayList<TranslationObjectImpl> assetList = this.translationPod.getCachedTranslationObjectList(TranslationObjectType.ASSET);
if (assetList != null) {
totalObjectList.addAll(assetList);
}
if ((contentFragmentList = this.translationPod.getCachedTranslationObjectList(TranslationObjectType.CONTENTFRAGMENT)) != null) {
totalObjectList.addAll(contentFragmentList);
}
}
if (this.translationPod.isPageTagMetaDataTranslationRequired() && (pageList = this.translationPod.getCachedTranslationObjectList(TranslationObjectType.PAGE)) != null) {
totalObjectList.addAll(pageList);
}
if (totalObjectList.size() == 0) {
throw new TranslationException("No page or asset present right now", TranslationException.ErrorCode.GENERAL_EXCEPTION);
}
CQPageHumanTranslator pageTranslator = new CQPageHumanTranslator(this.ruleFile);
document = pageTranslator.generateXMLForResourceList(this.translationPod.getSourceResourceListFromTranslationObjectList(totalObjectList), TranslationObjectType.TAGMETADATA, null);
break;
}
case TAG: {
document = TagTranslator.generateXML(this.getTranslationSourceResource(), this.ruleFile);
break;
}
case I18NDICTIONARY: {
I18nDictionaryHumanTranslation i18nHumanTranslator = new I18nDictionaryHumanTranslation(this.ruleFile, this.getResourceResolver());
document = i18nHumanTranslator.generateXMLForI18nDictionary(this.getTranslationSourceResource());
break;
}
case I18NCOMPONENTSTRINGDICT: {
I18nDictionaryHumanTranslation i18nHumanTranslator = new I18nDictionaryHumanTranslation(this.ruleFile, this.getResourceResolver());
ArrayList<TranslationObjectImpl> pageList = this.translationPod.getCachedTranslationObjectList(TranslationObjectType.PAGE);
if (pageList == null) break;
ArrayList<Resource> pageResourceList = this.translationPod.getSourceResourceListFromTranslationObjectList(pageList);
document = i18nHumanTranslator.generateXMLForComponentStrings(pageResourceList);
break;
}
}
return document;
}
private String exportToXMLString() throws IOException, RepositoryException, ParserConfigurationException, SAXException, XPathExpressionException, DOMException, TranslationException, TransformerException {
Document document = this.exportToXMLDocument();
return TranslationUtils.convertDOMDocumentToString(document, true);
}
private String exportToXLIFFString(String xliffVersion) throws IOException, RepositoryException, ParserConfigurationException, SAXException, XPathExpressionException, DOMException, TranslationException, TransformerException, TranslationXLIFFServiceException {
Document xmlDocument;
String id;
TranslationXLIFFService xliffService = this.ruleFile.getXLIFFService();
if (xliffService != null) {
xmlDocument = this.exportToXMLDocument();
id = this.getId();
if (id == null) {
id = this.getFileUUID();
}
} else {
logger.error("XLIFF service is not available.Can't export to XLIFF.");
throw new TranslationXLIFFServiceException("XLIFF service is not available", TranslationXLIFFServiceException.ErrorCode.SERVICE_NOT_AVAILABLE);
}
String xliffString = xliffService.convertXMLDocumentToXLIFFString(xmlDocument, id, this.ruleFile.getSourceLanguage(), xliffVersion);
return xliffString;
}
public boolean isSourcePathValid() {
boolean bRetVal = false;
TranslationObjectType objectType = this.getTranslationObjectType();
if (objectType == TranslationObjectType.PAGE || objectType == TranslationObjectType.FILE || objectType == TranslationObjectType.ASSET || objectType == TranslationObjectType.I18NDICTIONARY || objectType == TranslationObjectType.CONTENTFRAGMENT || objectType == TranslationObjectType.TAG) {
try {
Resource sourceResource = this.getTranslationSourceResource();
bRetVal = sourceResource != null;
}
catch (Exception ex) {}
} else {
bRetVal = true;
}
return bRetVal;
}
public String exportFileForTranslation(File tempDir) throws IOException, RepositoryException, XPathExpressionException, DOMException, ParserConfigurationException, SAXException, TransformerException, TranslationException, TranslationXLIFFServiceException {
String strFilePath = null;
if (this.isSourcePathValid()) {
TranslationObjectType objectType = this.getTranslationObjectType();
String strSourcePath = this.getTranslationObjectSourcePath();
if (objectType == TranslationObjectType.ASSET) {
Asset assetFile = (Asset)this.getTranslationSourceResource().adaptTo(Asset.class);
if (assetFile != null) {
strFilePath = TranslationUtils.writeToFile(tempDir, this.getFileUUID(), TranslationUtils.getFileExtension(strSourcePath, this.getMimeType()), assetFile.getOriginal().getStream());
} else if (this.isEmbeddedAsset()) {
Resource resource = this.getResourceResolver().getResource(strSourcePath);
strFilePath = TranslationUtils.writeToFile(tempDir, this.getFileUUID(), TranslationUtils.getFileExtension(strSourcePath, this.getMimeType()), ((Node)resource.adaptTo(Node.class)).getProperty("jcr:data").getBinary().getStream());
}
} else if (objectType == TranslationObjectType.FILE) {
Node fileNode = (Node)this.getTranslationSourceResource().adaptTo(Node.class);
Node fileContentNode = fileNode.getNode("jcr:content");
InputStream fileDataInputStream = fileContentNode.getProperty("jcr:data").getBinary().getStream();
if (fileContentNode != null) {
strFilePath = TranslationUtils.writeToFile(tempDir, this.getFileUUID(), TranslationUtils.getFileExtension(strSourcePath, this.getMimeType()), fileDataInputStream);
}
} else {
String strOutput = null;
String exportFormat = this.ruleFile.getExportFormat();
String strOutputFileExtension = ".xml";
if (exportFormat.equals("xml")) {
strOutput = this.exportToXMLString();
} else {
String xliffVersion = null;
strOutputFileExtension = ".xlf";
if (exportFormat.equalsIgnoreCase("xliff_1.2")) {
xliffVersion = "1.2";
} else if (exportFormat.equalsIgnoreCase("xliff_2.0")) {
xliffVersion = "2.0";
}
strOutput = this.exportToXLIFFString(xliffVersion);
}
if (strOutput != null) {
strFilePath = TranslationUtils.writeToFile(tempDir, this.getFileUUID(), strOutputFileExtension, strOutput);
logger.trace("Export completed to path " + strFilePath);
} else {
throw new TranslationException("Failed to export", TranslationException.ErrorCode.REQUEST_FAILED);
}
}
}
return strFilePath;
}
public void importFile(File file, ResourceResolver resourceResolver) throws ParserConfigurationException, SAXException, IOException, RepositoryException, TranslationException, TransformerException, TranslationXLIFFServiceException {
TranslationConstants.TranslationStatus currentStatus = this.getTranslationStatus();
if (currentStatus != TranslationConstants.TranslationStatus.TRANSLATION_IN_PROGRESS) {
logger.trace("Importing file");
this.importInputStream(new FileInputStream(file), resourceResolver);
}
}
/*
* Enabled aggressive block sorting
* Enabled unnecessary exception pruning
* Enabled aggressive exception aggregation
*/
private void importInputStream(InputStream inputStream, ResourceResolver resourceResolver) throws ParserConfigurationException, SAXException, IOException, PathNotFoundException, RepositoryException, TranslationException {
try {
if (this.metadata.isObjectTranslationRequired()) {
I18nDictionaryHumanTranslation i18nHumanTranslator;
ArrayList sourceFilePathArray;
TranslationXLIFFService xliffService = this.ruleFile.getXLIFFService();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
if (this.isXLIFFStream(bufferedInputStream)) {
logger.trace("Importing XLIFF");
if (xliffService == null) {
logger.error("XLIFF service is not available.Can't import XLIFF file.");
bufferedInputStream.close();
throw new TranslationXLIFFServiceException("XLIFF service is not available", TranslationXLIFFServiceException.ErrorCode.SERVICE_NOT_AVAILABLE);
}
Document document = xliffService.convertXLIFFStreamToXMLDocument((InputStream)bufferedInputStream, this.ruleFile.getSourceLanguage(), this.ruleFile.getDestinationLanguage());
String xmlString = TranslationUtils.convertDOMDocumentToString(document, true);
bufferedInputStream.close();
inputStream = new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
} else {
logger.trace("Importing XML");
inputStream = bufferedInputStream;
}
this.setTranslationStatus(TranslationConstants.TranslationStatus.TRANSLATION_IN_PROGRESS, true, false);
TranslationObjectType objectType = this.getTranslationObjectType();
if (objectType == TranslationObjectType.I18NDICTIONARY) {
i18nHumanTranslator = new I18nDictionaryHumanTranslation(this.ruleFile, this.getResourceResolver());
Resource i18nDictResource = this.getTranslationSourceResource();
i18nHumanTranslator.importI18nDictionaryStream(inputStream, i18nDictResource, resourceResolver);
} else if (objectType == TranslationObjectType.I18NCOMPONENTSTRINGDICT) {
i18nHumanTranslator = new I18nDictionaryHumanTranslation(this.ruleFile, this.getResourceResolver());
i18nHumanTranslator.importI18nComponentStringDict(inputStream, resourceResolver);
} else if (objectType == TranslationObjectType.ASSET) {
this.importAssetInputStream(inputStream);
} else if (objectType == TranslationObjectType.CONTENTFRAGMENT) {
sourceFilePathArray = new ArrayList<String>();
sourceFilePathArray.add((String)this.getTranslationObjectSourcePath());
Resource contentFragmentResource = this.getTranslationSourceResource();
ContentFragmentHumanTranslator contentFragmentTranslator = new ContentFragmentHumanTranslator(this.ruleFile);
contentFragmentTranslator.importInputStream(inputStream, contentFragmentResource, resourceResolver, objectType.toString(), sourceFilePathArray, this.userSession.getValueFactory());
} else if (objectType == TranslationObjectType.FILE) {
Node fileNode = (Node)this.getTranslationSourceResource().adaptTo(Node.class);
Node ntResourceNode = fileNode.getNode("jcr:content");
ntResourceNode.getProperty("jcr:data").setValue(inputStream);
} else if (objectType == TranslationObjectType.TAG) {
TagTranslator.importXMLInputStream(inputStream, this.ruleFile);
} else {
sourceFilePathArray = null;
if (objectType == TranslationObjectType.PAGE) {
sourceFilePathArray = new ArrayList();
sourceFilePathArray.add(this.getTranslationObjectSourcePath());
} else {
sourceFilePathArray = this.getAllSourcePathForObjectType(objectType);
}
Resource cqPageResource = this.getTranslationSourceResource();
CQPageHumanTranslator pageTranslator = new CQPageHumanTranslator(this.ruleFile);
pageTranslator.importInputStream(inputStream, cqPageResource, resourceResolver, objectType.toString(), sourceFilePathArray, objectType == TranslationObjectType.TAGMETADATA);
}
this.setTranslationStatus(TranslationConstants.TranslationStatus.READY_FOR_REVIEW, true, false);
} else {
logger.trace("Translation is not required, skipping it now.");
}
this.save();
return;
}
catch (Exception e) {
throw new TranslationException("Failed to import", (Throwable)e, TranslationException.ErrorCode.GENERAL_EXCEPTION);
}
finally {
if (inputStream != null) {
try {
inputStream.close();
}
catch (IOException e) {
logger.error("Failed to close input stream", (Throwable)e);
}
}
}
}
public Resource getTranslationSourceResource() {
return this.getResourceResolver().getResource(this.getTranslationObjectSourcePath());
}
private void importAssetInputStream(InputStream inputStream) throws FileNotFoundException, RepositoryException {
if (this.getTranslationObjectType() == TranslationObjectType.ASSET) {
if (this.isEmbeddedAsset()) {
Node node = (Node)this.getTranslationSourceResource().adaptTo(Node.class);
node.setProperty("jcr:data", inputStream);
} else {
Asset asset = (Asset)this.getTranslationSourceResource().adaptTo(Asset.class);
TranslationObjectImpl.addExtractMetadataPropertyForAsset(asset, false);
if (asset != null) {
String mimeType = asset.getMimeType();
String renditionName = "original";
asset.addRendition(renditionName, inputStream, mimeType);
}
}
}
}
/*
* Enabled force condition propagation
* Lifted jumps to return sites
*/
private ArrayList<String> getAllSourcePathForObjectType(TranslationObjectType objectType) {
ArrayList<String> sourceFilePathArray = new ArrayList<String>();
if (objectType == TranslationObjectType.ASSETMETADATA) {
ArrayList<TranslationObjectImpl> assetList = this.translationPod.getCachedTranslationObjectList(TranslationObjectType.ASSET);
for (TranslationObjectImpl asset : assetList) {
sourceFilePathArray.add(asset.getTranslationObjectSourcePath());
}
return sourceFilePathArray;
} else {
if (objectType != TranslationObjectType.TAGMETADATA) return sourceFilePathArray;
ArrayList<TranslationObjectImpl> objectList = this.translationPod.getCachedTranslationObjectList(TranslationObjectType.ASSET);
for (TranslationObjectImpl object2 : objectList) {
sourceFilePathArray.add(object2.getTranslationObjectSourcePath());
}
objectList = this.translationPod.getCachedTranslationObjectList(TranslationObjectType.PAGE);
for (TranslationObjectImpl object2 : objectList) {
sourceFilePathArray.add(object2.getTranslationObjectSourcePath());
}
}
return sourceFilePathArray;
}
public boolean isDeleteAllowed() {
return true;
}
public void afterAdd(ResourceResolver resourceResolver, Project project, TranslationObjectImpl parentTranslationObj, MachineTranslationCloudConfig rootResourceMachineCloudConfig) throws RepositoryException, TranslationException {
this.setTranslationStatus(TranslationConstants.TranslationStatus.DRAFT, true, true);
this.addParentReference(parentTranslationObj);
if (rootResourceMachineCloudConfig != null && this.getTranslationObjectType() == TranslationObjectType.ASSET) {
this.metadata.setObjectTranslationRequired(rootResourceMachineCloudConfig.isTranslateAssetsAllowedForAssets());
}
this.setMimeType();
this.createUUID();
}
public String getFileUUID() {
return this.getStringAttribute("fileUniqueID");
}
private void createUUID() {
this.setAttribute("fileUniqueID", UUID.randomUUID().toString());
}
public void beforeDelete(ResourceResolver resourceResolver, Project project) throws RepositoryException, TranslationException, PersistenceException {
this.addRemoveProjectDetailsInSource(resourceResolver, project, false);
if (this.removeAllReferences() || this.removeTemporaryAssetNodeIfRequired(resourceResolver)) {
this.save();
}
}
private boolean removeTemporaryAssetNodeIfRequired(ResourceResolver resourceResolver) throws PersistenceException, RepositoryException {
Node temporaryNode;
String sourcePath = this.getTranslationObjectSourcePath();
Resource temporaryResource = resourceResolver.getResource(sourcePath);
if (null != temporaryResource && TranslationUtils.isTemporaryAssetNode(temporaryNode = (Node)temporaryResource.adaptTo(Node.class))) {
TranslationObjectImpl.deleteTemporaryAssetOrFolderHierarchy(temporaryResource, resourceResolver);
return true;
}
return false;
}
private void addRemoveProjectDetailsInSource(ResourceResolver resourceResolver, Project project, boolean bAddProject) throws TranslationException {
try {
TranslationObjectType objectType = this.getTranslationObjectType();
if (objectType == TranslationObjectType.PAGE) {
Resource sourcePageContentResource;
Resource projectResource = (Resource)project.adaptTo(Resource.class);
Resource sourceResource = TranslationUtils.getSourceResourceFromLaunch(this.getTranslationSourceResource());
if (sourceResource != null && (sourcePageContentResource = sourceResource.getChild("jcr:content")) != null) {
Node sourcePageContentNode = (Node)sourcePageContentResource.adaptTo(Node.class);
Value[] values = new Value[]{};
Property translationProjectProp = null;
if (sourcePageContentNode.hasProperty("cq:translationProject")) {
translationProjectProp = sourcePageContentNode.getProperty("cq:translationProject");
values = translationProjectProp.getValues();
}
ArrayList<String> valueList = new ArrayList<String>();
for (Value value : values) {
valueList.add(value.getString());
}
if (bAddProject) {
if (!valueList.contains(projectResource.getPath())) {
valueList.add(projectResource.getPath());
}
} else if (valueList.contains(projectResource.getPath())) {
valueList.remove(projectResource.getPath());
}
String[] stringList = new String[]{};
stringList = valueList.toArray(stringList);
if (translationProjectProp == null && bAddProject) {
translationProjectProp = sourcePageContentNode.setProperty("cq:translationProject", values);
}
if (translationProjectProp != null) {
if (stringList != null && stringList.length > 0) {
translationProjectProp.setValue(stringList);
} else {
translationProjectProp.remove();
}
}
this.save();
}
}
}
catch (Exception ex) {
logger.error("Error while saving adding translation project into the source path", (Throwable)ex);
}
}
public TranslationObjectType getTranslationObjectType() {
String strFileType = this.getStringAttribute("translationFileType");
return TranslationObjectType.fromString(strFileType);
}
public static TranslationObjectType getTranslationObjectType(ResourceResolver resourceResolver, String strNodePath) throws RepositoryException {
Resource resource = resourceResolver.getResource(strNodePath);
String strResourceType = resource.getResourceType();
Node node = (Node)resource.adaptTo(Node.class);
TranslationObjectType retVal = TranslationObjectType.PAGE;
if (TranslationUtils.isContentFragment((Node)resourceResolver.getResource(strNodePath).adaptTo(Node.class))) {
retVal = TranslationObjectType.CONTENTFRAGMENT;
} else if ("dam:Asset".equals(strResourceType) || TranslationUtils.isBinaryNode(resourceResolver, strNodePath)) {
retVal = TranslationObjectType.ASSET;
} else if (I18nDictionary.isI18nDictionary(resourceResolver.getResource(strNodePath))) {
retVal = TranslationObjectType.I18NDICTIONARY;
} else if ("cq:Tag".equals(node.getPrimaryNodeType().toString())) {
retVal = TranslationObjectType.TAG;
} else if ("nt:file".equals(strResourceType)) {
retVal = TranslationObjectType.FILE;
}
return retVal;
}
public static TranslationObjectType getTranslationObjectType(ResourceResolver resourceResolver, Node cqNode) throws RepositoryException {
return TranslationObjectImpl.getTranslationObjectType(resourceResolver, cqNode.getPath());
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void syncTranslationStateIfRequired(String strTranslationJobID, TranslationService tsvc, ResourceResolver resourceResolver, TranslationConstants.TranslationStatus newStatus) {
TranslationConstants.TranslationStatus currentStatus = this.getTranslationStatus();
if (this.isStatusUpdateRequired() && !this.isSyncTranslationStateInProgress()) {
try {
this.setSyncTranslationStateInProgress(true, logger);
if (newStatus != currentStatus) {
this.setTranslationStatus(newStatus, true, false);
}
if (newStatus == TranslationConstants.TranslationStatus.TRANSLATED) {
InputStream inputStream = tsvc.getTranslatedObject(strTranslationJobID, (TranslationObject)this);
this.importInputStream(inputStream, resourceResolver);
this.createVersionAndSetTranslationDate(TranslationConstants.TranslationMethod.HUMAN_TRANSLATION, tsvc.getTranslationServiceInfo().getTranslationServiceName());
this.setTranslationStatus(TranslationConstants.TranslationStatus.READY_FOR_REVIEW, true, true);
}
}
catch (Exception e) {
logger.error("Error while getting translation status ", (Throwable)e);
}
finally {
this.setSyncTranslationStateInProgress(false, logger);
}
}
}
public void addParentReference(TranslationObjectImpl parentTranslationObj) {
if (parentTranslationObj != null) {
ArrayList<String> parentList = this.getParentReferenceList();
if (!parentList.contains(parentTranslationObj.getPath())) {
parentList.add(parentTranslationObj.getPath());
this.setAttribute("parentReferenceList", parentList);
}
} else {
this.setAttribute("independentObject", true);
}
}
public boolean isIndependentObject() {
return this.getBooleanAttribute("independentObject", false);
}
private void removeParentReference(TranslationObjectImpl parentTranslationObj) {
ArrayList<String> parentList = this.getParentReferenceList();
if (parentList.contains(parentTranslationObj.getPath())) {
parentList.remove(parentTranslationObj.getPath());
this.setAttribute("parentReferenceList", parentList);
}
}
private boolean isParentReferenced(TranslationObjectImpl translationObjectImpl) {
return this.getParentReferenceList().contains(translationObjectImpl.getPath());
}
public ArrayList<String> getParentReferenceList() {
return this.getStringListAttribute("parentReferenceList");
}
private boolean removeAllReferences() throws RepositoryException, TranslationException {
boolean bSave = false;
ArrayList<TranslationObjectImpl> objectList = this.translationPod.getValidTranslationObjects(this.ruleFile, this.userSession);
for (TranslationObjectImpl translationObject : objectList) {
if (translationObject == this || !translationObject.isParentReferenced(this)) continue;
translationObject.removeParentReference(this);
bSave = true;
}
return bSave;
}
public void updateFileReferences(String strReferencedNode) throws RepositoryException {
this.setAttribute("isEmbedded", this.isEmbeddedAsset());
if (this.isEmbeddedAsset()) {
this.setAttribute("parentPath", strReferencedNode);
}
}
public boolean isStatusUpdateRequired() {
TranslationConstants.TranslationStatus currentStatus = this.getTranslationStatus();
return currentStatus != TranslationConstants.TranslationStatus.DRAFT && currentStatus != TranslationConstants.TranslationStatus.UNKNOWN_STATE && currentStatus != TranslationConstants.TranslationStatus.READY_FOR_REVIEW;
}
public void addProjectDetailsInSourceIfRequired(ResourceResolver resourceResolver, Project project) throws RepositoryException, TranslationException {
if (this.getTranslationObjectType() == TranslationObjectType.PAGE && !this.isProjectAlreadyPresentInSourceHierarchy(project)) {
this.addRemoveProjectDetailsInSource(resourceResolver, project, true);
}
}
private boolean isProjectAlreadyPresentInSourceHierarchy(Project project) throws RepositoryException {
boolean bPresent = false;
Resource projectResource = (Resource)project.adaptTo(Resource.class);
String strProjectPath = projectResource.getPath();
block0 : for (Resource resource = TranslationUtils.getSourceResourceFromLaunch((Resource)this.getTranslationSourceResource()); resource != null && !bPresent; resource = resource.getParent()) {
Value[] values;
Node contentNode;
Resource contentResource = resource.getChild("jcr:content");
if (contentResource == null || !(contentNode = (Node)contentResource.adaptTo(Node.class)).hasProperty("cq:translationProject")) continue;
Property translationProjectProp = contentNode.getProperty("cq:translationProject");
for (Value value : values = translationProjectProp.getValues()) {
if (!strProjectPath.equals(value.getString())) continue;
bPresent = true;
continue block0;
}
}
return bPresent;
}
@Override
public int compareTo(TranslationObjectImpl other) {
return this.getTranslationObjectSourcePath().compareTo(other.getTranslationObjectSourcePath());
}
public void deleteLaunchPagesAndTemporaryAssetsIfRequired(ResourceResolver resourceResolver) {
try {
Node currentNode;
TranslationObjectType translationObjectType = this.getTranslationObjectType();
if (translationObjectType == TranslationObjectType.PAGE) {
Resource resource = this.getTranslationSourceResource();
Launch launch = TranslationUtils.getLaunchFromResource(resource);
if (launch != null) {
Resource parent = resource.getParent();
resourceResolver.delete(resource);
resource = parent;
while (resource != null && this.isResourceAllowedForLaunchDelete(resource)) {
parent = resource.getParent();
resourceResolver.delete(resource);
resource = parent;
}
}
} else if ((translationObjectType == TranslationObjectType.ASSET || translationObjectType == TranslationObjectType.CONTENTFRAGMENT) && TranslationUtils.isTemporaryAssetNode(currentNode = (Node)this.getTranslationSourceResource().adaptTo(Node.class))) {
TranslationObjectImpl.deleteTemporaryAssetOrFolderHierarchy(this.getTranslationSourceResource(), resourceResolver);
this.userSession.save();
}
}
catch (Exception ex) {
logger.error("Exception while deleting launch page/temporary asset", (Throwable)ex);
}
}
private boolean isResourceAllowedForLaunchDelete(Resource parent) {
boolean bRetVal = false;
if (parent != null) {
String strResourceType;
Resource contentResource;
boolean bChildPresent = false;
Iterable childList = parent.getChildren();
for (Resource resource : childList) {
if ("jcr:content".equals(resource.getName())) continue;
bChildPresent = true;
break;
}
if (!bChildPresent && (contentResource = parent.getChild("jcr:content")) != null && ("launches/components/outofscope".equals(strResourceType = contentResource.getResourceType()) || "nt:unstructured".equals(strResourceType) || "wcm/launches/components/launch".equals(strResourceType))) {
bRetVal = true;
}
}
return bRetVal;
}
private boolean isXLIFFStream(BufferedInputStream xliffInputStream) throws IOException {
Pattern pattern;
Matcher matcher;
boolean bRetVal = false;
xliffInputStream.mark(MAX_XLIFF_SNIFFER_BUF_LENGTH);
char[] chars = new char[MAX_XLIFF_SNIFFER_BUF_LENGTH];
InputStreamReader reader = new InputStreamReader((InputStream)xliffInputStream, "UTF-8");
reader.read(chars, 0, MAX_XLIFF_SNIFFER_BUF_LENGTH);
String xmlHeadStr = String.valueOf(chars);
String XLIFF_DETECTION_PATTERN_STR = "<\\?xml.*?\\?>.*?<xliff";
xmlHeadStr = xmlHeadStr.trim();
if (xmlHeadStr.length() > 0 && (matcher = (pattern = Pattern.compile("<\\?xml.*?\\?>.*?<xliff", 42)).matcher(xmlHeadStr)).find()) {
bRetVal = true;
}
xliffInputStream.reset();
return bRetVal;
}
void acceptTranslation(boolean bSave, boolean bCallAPI) throws TranslationException, RepositoryException {
TranslationConstants.TranslationStatus status = this.metadata.getTranslationState().getStatus();
if (status != TranslationConstants.TranslationStatus.READY_FOR_REVIEW) {
throw new TranslationException("Translation object is not ready for review.Can't accept translation", TranslationException.ErrorCode.REQUEST_FAILED);
}
this.setTranslationStatus(TranslationConstants.TranslationStatus.APPROVED, bSave, bCallAPI);
}
void rejectTranslation(String commentStr, boolean bSave, boolean bCallAPI) throws TranslationException, RepositoryException {
TranslationConstants.TranslationStatus status = this.metadata.getTranslationState().getStatus();
if (status != TranslationConstants.TranslationStatus.READY_FOR_REVIEW) {
throw new TranslationException("Translation object is not ready for review.Can't reject translation", TranslationException.ErrorCode.REQUEST_FAILED);
}
this.setComment(commentStr, "translationRejected");
this.setTranslationStatus(TranslationConstants.TranslationStatus.REJECTED, bSave, bCallAPI);
}
private void setComment(String commentStr, String annotationStr) {
CommentManager commentManager;
if (StringUtils.isNotBlank((CharSequence)commentStr) && (commentManager = this.ruleFile.getCommentManager()) != null) {
logger.trace("Setting comment: {}; Annotation: {}", (Object)commentStr, (Object)annotationStr);
CommentCollection commentCollection = commentManager.getOrCreateCollection(this.getResource(), CommentCollection.class);
Comment comment = commentCollection.addComment(commentStr, this.userSession.getUserID(), annotationStr);
this.metadata.getTranslationState().setComment(comment);
}
}
private void updateAssetsIfRequired() throws RepositoryException {
Node currentNode = this.getNode();
TranslationObjectType translationObjectType = this.getTranslationObjectType();
if (currentNode.hasProperty(TranslationUtils.ATTRIBUTE_TRANSLATION_RELATED_OBJECTS)) {
if (translationObjectType == TranslationObjectType.ASSETMETADATA) {
Property relatedObjectProperty = currentNode.getProperty(TranslationUtils.ATTRIBUTE_TRANSLATION_RELATED_OBJECTS);
if (relatedObjectProperty.isMultiple()) {
Value[] relatedTranslationObjects;
for (Value path : relatedTranslationObjects = relatedObjectProperty.getValues()) {
this.updateIfRelatedAssetsAreUpdated(path.getString());
}
} else {
Value relatedTranslationObject = relatedObjectProperty.getValue();
this.updateIfRelatedAssetsAreUpdated(relatedTranslationObject.getString());
}
} else {
this.updateIfRelatedAssetsAreUpdated(this.getNode().getPath());
}
} else if (translationObjectType == TranslationObjectType.ASSET || translationObjectType == TranslationObjectType.CONTENTFRAGMENT) {
this.updateAssetWithoutRelatedTranslationObjects(this.getNode().getPath());
}
}
private void updateAssetWithoutRelatedTranslationObjects(String translationObjectPath) {
if (null == this.pageManagerFactory) {
logger.error("Unable to move updated asset. PageManagerFactory is null while translating object: {}", (Object)translationObjectPath);
return;
}
try {
ResourceResolver resourceResolver = this.getResourceResolver();
Node node = (Node)resourceResolver.getResource(translationObjectPath).adaptTo(Node.class);
Resource translationObjectResource = resourceResolver.getResource(node.getPath());
TranslationObjectImpl translationObject = new TranslationObjectImpl(this.translationPod, this.ruleFile, node, translationObjectResource, this.userSession);
TranslationUtils.updateTemporaryAsset(translationObject, resourceResolver, this.translationPod, this.userSession, this.pageManagerFactory);
}
catch (Exception e) {
logger.error("Unable to move updated asset {}", (Object)e.getMessage());
}
}
private void updateIfRelatedAssetsAreUpdated(String translationObjectPath) {
if (null == this.pageManagerFactory) {
logger.error("Unable to move updated asset. PageManagerFactory is null while translating object: {}", (Object)translationObjectPath);
return;
}
try {
ResourceResolver resourceResolver = this.getResourceResolver();
Node node = (Node)resourceResolver.getResource(translationObjectPath).adaptTo(Node.class);
Resource translationObjectResource = resourceResolver.getResource(node.getPath());
TranslationObjectImpl translationObject = new TranslationObjectImpl(this.translationPod, this.ruleFile, node, translationObjectResource, this.userSession);
if (translationObject != null) {
Node currentNode = translationObject.getNode();
if (currentNode.hasProperty(TranslationUtils.ATTRIBUTE_TRANSLATION_RELATED_OBJECTS)) {
if (this.isTranslationComplete(translationObject) && this.relatedObjectsTranslated(currentNode, resourceResolver)) {
TranslationUtils.updateTemporaryAsset(translationObject, resourceResolver, this.translationPod, this.userSession, this.pageManagerFactory);
}
} else {
logger.error("Unable to move updated asset. Missing related translation object property at: {}", (Object)translationObjectPath);
}
} else {
logger.error("Unable to move updated asset. Missing translation object at: {}", (Object)translationObjectPath);
}
}
catch (Exception e) {
logger.error("Unable to move updated asset {}", (Object)e.getMessage());
}
}
private boolean relatedObjectsTranslated(Node currentNode, ResourceResolver resourceResolver) throws RepositoryException {
boolean relatedObjectsTranslated;
relatedObjectsTranslated = true;
Property relatedObjectProperty = currentNode.getProperty(TranslationUtils.ATTRIBUTE_TRANSLATION_RELATED_OBJECTS);
String translationObjectsParentPath = this.getNode().getParent().getPath() + "/";
if (relatedObjectProperty.isMultiple()) {
Value[] relatedTranslationObjects;
for (Value path : relatedTranslationObjects = relatedObjectProperty.getValues()) {
String nodePath = path.getString();
TranslationObjectImpl translationObject = this.getTranslationObject(nodePath, resourceResolver);
if (this.isTranslationComplete(translationObject)) continue;
relatedObjectsTranslated = false;
break;
}
} else {
Value relatedTranslationObject = relatedObjectProperty.getValue();
String nodePath = relatedTranslationObject.getString();
TranslationObjectImpl translationObject = this.getTranslationObject(nodePath, resourceResolver);
if (!this.isTranslationComplete(translationObject)) {
relatedObjectsTranslated = false;
}
}
return relatedObjectsTranslated;
}
private TranslationObjectImpl getTranslationObject(String nodePath, ResourceResolver resourceResolver) throws RepositoryException {
Node node = (Node)resourceResolver.getResource(nodePath).adaptTo(Node.class);
Resource translationObjectResource = resourceResolver.getResource(node.getPath());
TranslationObjectImpl translationObject = new TranslationObjectImpl(this.translationPod, this.ruleFile, node, translationObjectResource, this.userSession);
return translationObject;
}
public FileInputStream getTranslationObjectPreviewFileInputStream() {
FileInputStream retVal = null;
TranslationObjectType objectType = this.getTranslationObjectType();
if (objectType == TranslationObjectType.PAGE) {
CQPagePreviewGenerator pagePreviewGenerator = new CQPagePreviewGenerator(this.ruleFile);
Resource resource = this.getTranslationSourceResource();
try {
retVal = pagePreviewGenerator.generatePreview(this.getResourceResolver(), (Page)resource.adaptTo(Page.class));
}
catch (Exception e) {
logger.error("Error while generating preview {}", (Throwable)e);
}
}
return retVal;
}
public ZipInputStream getTranslationObjectPreview() {
FileInputStream fis = this.getTranslationObjectPreviewFileInputStream();
if (fis != null) {
return new ZipInputStream(fis);
}
return null;
}
private boolean isTranslationComplete(TranslationObjectImpl translationObject) {
boolean retVal = true;
if (translationObject.metadata.isObjectTranslationRequired()) {
TranslationConstants.TranslationStatus translationStatus = translationObject.getTranslationStatus();
retVal = translationStatus.equals((Object)TranslationConstants.TranslationStatus.APPROVED);
}
return retVal;
}
private static void deleteTemporaryAssetOrFolderHierarchy(Resource sourceResource, ResourceResolver resourceResolver) throws PersistenceException {
Resource parentResource = sourceResource.getParent();
resourceResolver.delete(sourceResource);
if (TranslationObjectImpl.isEmptyFolder(parentResource)) {
TranslationObjectImpl.deleteTemporaryAssetOrFolderHierarchy(parentResource, resourceResolver);
}
}
private static boolean isEmptyFolder(Resource folderResource) {
if (!TranslationObjectImpl.slingFolder(folderResource)) {
return false;
}
Iterator children = folderResource.listChildren();
while (children.hasNext()) {
Resource child = (Resource)children.next();
if (child.getName().equals("jcr:content")) continue;
return false;
}
return true;
}
private static boolean slingFolder(Resource resource) {
return resource.isResourceType("sling:Folder") || resource.isResourceType("sling:OrderedFolder");
}
private static void addExtractMetadataPropertyForAsset(Asset asset, boolean bExtractMetadata) throws RepositoryException {
Node assetNode = (Node)asset.adaptTo(Node.class);
if (!assetNode.hasNode("jcr:content")) {
assetNode.addNode("jcr:content", "nt:unstructured");
}
Node assetContentNode = assetNode.getNode("jcr:content");
assetContentNode.setProperty("dam:extractMetadata", bExtractMetadata);
}
public static enum TranslationObjectType {
PAGE,
ASSET,
ASSETMETADATA,
TAGMETADATA,
I18NDICTIONARY,
I18NCOMPONENTSTRINGDICT,
CONTENTFRAGMENT,
FILE,
TAG;
private TranslationObjectType() {
}
public static TranslationObjectType fromString(String text) {
if (text != null) {
for (TranslationObjectType b : TranslationObjectType.values()) {
if (!text.equalsIgnoreCase(b.toString())) continue;
return b;
}
}
return null;
}
}
}