InjectMasterLanguageIntoLaunchProcess.java
17.3 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.aemds.guide.service.GuideLocalizationService
* com.adobe.cq.launches.api.Launch
* com.adobe.cq.launches.api.LaunchManager
* com.adobe.cq.launches.api.LaunchManagerFactory
* com.adobe.granite.taskmanagement.Task
* com.adobe.granite.taskmanagement.TaskAction
* com.adobe.granite.taskmanagement.TaskManager
* com.adobe.granite.taskmanagement.TaskManagerFactory
* com.adobe.granite.translation.api.TranslationConfig
* com.adobe.granite.translation.api.TranslationManager
* com.adobe.granite.translation.core.TranslationCloudConfigUtil
* com.adobe.granite.ui.clientlibs.HtmlLibraryManager
* com.day.cq.commons.LanguageUtil
* com.day.cq.wcm.api.LanguageManager
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.api.PageManagerFactory
* com.day.cq.workflow.WorkflowException
* com.day.cq.workflow.WorkflowSession
* com.day.cq.workflow.exec.WorkItem
* com.day.cq.workflow.exec.Workflow
* com.day.cq.workflow.exec.WorkflowData
* com.day.cq.workflow.exec.WorkflowProcess
* com.day.cq.workflow.metadata.MetaDataMap
* com.day.text.Text
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.Session
* org.apache.commons.lang3.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.ModifiableValueMap
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.apache.sling.jcr.api.SlingRepository
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.wcm.translation.impl.process;
import com.adobe.aemds.guide.service.GuideLocalizationService;
import com.adobe.cq.launches.api.Launch;
import com.adobe.cq.launches.api.LaunchManager;
import com.adobe.cq.launches.api.LaunchManagerFactory;
import com.adobe.cq.wcm.translation.impl.TranslationLanguageCopy;
import com.adobe.cq.wcm.translation.impl.TranslationRuleConfigurationFile;
import com.adobe.cq.wcm.translation.impl.process.AbstractWCMTranslationProcess;
import com.adobe.granite.taskmanagement.Task;
import com.adobe.granite.taskmanagement.TaskAction;
import com.adobe.granite.taskmanagement.TaskManager;
import com.adobe.granite.taskmanagement.TaskManagerFactory;
import com.adobe.granite.translation.api.TranslationConfig;
import com.adobe.granite.translation.api.TranslationManager;
import com.adobe.granite.translation.core.TranslationCloudConfigUtil;
import com.adobe.granite.ui.clientlibs.HtmlLibraryManager;
import com.day.cq.commons.LanguageUtil;
import com.day.cq.wcm.api.LanguageManager;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.PageManagerFactory;
import com.day.cq.workflow.WorkflowException;
import com.day.cq.workflow.WorkflowSession;
import com.day.cq.workflow.exec.WorkItem;
import com.day.cq.workflow.exec.Workflow;
import com.day.cq.workflow.exec.WorkflowData;
import com.day.cq.workflow.exec.WorkflowProcess;
import com.day.cq.workflow.metadata.MetaDataMap;
import com.day.text.Text;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.Session;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service(value={WorkflowProcess.class})
@Properties(value={@org.apache.felix.scr.annotations.Property(name="service.description", value={"Inject the content of master language into the language copy launch"}), @org.apache.felix.scr.annotations.Property(name="service.vendor", value={"Adobe"}), @org.apache.felix.scr.annotations.Property(name="process.label", value={"WCM: Inject Master Language into Language Copy Launch Process"})})
public class InjectMasterLanguageIntoLaunchProcess
extends AbstractWCMTranslationProcess {
private static final Logger log = LoggerFactory.getLogger(InjectMasterLanguageIntoLaunchProcess.class);
public static final String TASK_TYPE = "Notification";
@Reference
private ResourceResolverFactory resourceResolverFactory = null;
@Reference
LaunchManagerFactory launchesManagerFactory = null;
@Reference
private TranslationManager translationManager = null;
@Reference
private TranslationCloudConfigUtil translationCloudConfigUtil = null;
@Reference
private LanguageManager languageManager;
@Reference
private SlingRepository repository;
@Reference
protected TranslationConfig transConfig;
@Reference
PageManagerFactory pageManagerFactory;
@Reference
private HtmlLibraryManager htmlLibraryManager = null;
@Reference
private GuideLocalizationService guideLocalizationService = null;
public void execute(WorkItem item, WorkflowSession workflowSession, MetaDataMap args) throws WorkflowException {
block11 : {
Session session = workflowSession.getSession();
ResourceResolver resourceResolver = null;
String launchDest = null;
try {
resourceResolver = this.resourceResolverFactory.getServiceResourceResolver(Collections.singletonMap("sling.service.subservice", "translation-job"));
session = (Session)resourceResolver.adaptTo(Session.class);
PageManager pageManager = (PageManager)resourceResolver.adaptTo(PageManager.class);
LaunchManager launchManager = this.launchesManagerFactory.getLaunchManager(resourceResolver);
WorkflowData data = item.getWorkflowData();
MetaDataMap metaData = data.getMetaDataMap();
String launchPath = (String)metaData.get("launchRootResourcePath", (Object)"");
String translationWorkflowModel = (String)metaData.get("translationWorkflowModel", String.class);
String strProjectFolderPath = (String)metaData.get("projectFolderPath", String.class);
String strLanguageRefCount = (String)metaData.get("projectFolderLanguageRefCount", String.class);
this.setProjectFolderCount(resourceResolver, session, strProjectFolderPath, strLanguageRefCount);
Launch launch = launchManager.getLaunch(launchPath);
if (launch == null) {
throw new WorkflowException("Failed to load launch resource from payload.");
}
boolean deep = "true".equals(metaData.get("deep", String.class));
String masterLanguage = (String)metaData.get("language", String.class);
if (StringUtils.isEmpty((CharSequence)masterLanguage)) {
throw new WorkflowException("Failed to load master language from payload.");
}
String destLanguage = (String)metaData.get("destinationLanguage", String.class);
Resource launchSourceRootRes = launch.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;
launchDest = launchPath + launchSrcRootPath;
Resource masterPageResource = resourceResolver.getResource(masterPagePath);
if (masterPageResource == null) {
throw new WorkflowException("Master page resource does not exist.");
}
TranslationLanguageCopy languageCopy = new TranslationLanguageCopy();
ArrayList<Resource> resourceList = languageCopy.injectMasterLanguageIntoLaunch(new TranslationRuleConfigurationFile(resourceResolver, "/etc/workflow/models/translation/translation_rules.xml", masterLanguage, destLanguage, this.translationManager, this.translationCloudConfigUtil, this.pageManagerFactory, this.htmlLibraryManager, this.guideLocalizationService, true, true), pageManager, resourceResolver, launch, masterLanguage, session, deep, true);
Resource launchDestRes = resourceResolver.getResource(resourceList.get(0).getPath());
if (launchDestRes != null) {
if (launchLangRootPath.equals(launchSrcRootPath)) {
String languageRootPageTitle = ((Node)launchSourceRootRes.getChild("jcr:content").adaptTo(Node.class)).getProperty("jcr:title").getString();
((Node)launchDestRes.getChild("jcr:content").adaptTo(Node.class)).setProperty("jcr:title", languageRootPageTitle);
log.info("Set Page Title {} to new Language Copy root language at {}", (Object)languageRootPageTitle, (Object)launchDestRes.getPath());
session.save();
}
this.startTranslationWorkflow(translationWorkflowModel, strProjectFolderPath, item, workflowSession, launchDestRes.getPath(), launchSrcRootPath, metaData);
break block11;
}
this.cleanUpProjectFolderIfRequired(resourceResolver, session, strProjectFolderPath);
this.addInboxNotificationNow(resourceResolver, item.getWorkflow().getInitiator(), masterPagePath, launchSrcRootPath);
log.error("There is no change in source path {}", (Object)launchLangRootPath);
}
catch (Exception e) {
log.error("Error while updating the content of {}.", new Object[]{launchDest});
throw new WorkflowException("Error while updating the content of " + launchDest + ".", (Throwable)e);
}
finally {
if (resourceResolver != null && resourceResolver.isLive()) {
resourceResolver.close();
}
}
}
}
private void cleanUpProjectFolderIfRequired(ResourceResolver resourceResolver, Session session, String strProjectFolderPath) {
if (!StringUtils.isEmpty((CharSequence)strProjectFolderPath)) {
try {
ModifiableValueMap properties;
Resource resource = resourceResolver.getResource(strProjectFolderPath);
if (resource != null && (properties = (ModifiableValueMap)resource.adaptTo(ModifiableValueMap.class)).containsKey((Object)"projectFolderLanguageRefCount")) {
long iValue = ((Integer)properties.get("projectFolderLanguageRefCount", (Object)0)).intValue();
properties.put((Object)"projectFolderLanguageRefCount", (Object)(--iValue));
if (iValue <= 0) {
Node project = (Node)resource.adaptTo(Node.class);
if (!resource.hasChildren()) {
project.remove();
}
}
session.save();
}
}
catch (Exception ex) {
log.error("Error while reducing the Project Folder Language Ref Count");
}
}
}
private void setProjectFolderCount(ResourceResolver resourceResolver, Session session, String strProjectFolderPath, String strLanguageRefCount) {
if (!StringUtils.isEmpty((CharSequence)strProjectFolderPath) && !StringUtils.isEmpty((CharSequence)strLanguageRefCount)) {
long iValue = Long.parseLong(strLanguageRefCount);
try {
ModifiableValueMap properties;
Resource resource = resourceResolver.getResource(strProjectFolderPath);
if (resource != null && !(properties = (ModifiableValueMap)resource.adaptTo(ModifiableValueMap.class)).containsKey((Object)"projectFolderLanguageRefCount")) {
properties.put((Object)"projectFolderLanguageRefCount", (Object)iValue);
session.save();
}
}
catch (Exception ex) {
log.error("Error while setting up Project Folder Language Ref Count");
}
}
}
private void addInboxNotificationNow(ResourceResolver resourceResolver, String strInitiator, String masterPagePath, String launchSrcRootPath) {
try {
TaskManager tm = (TaskManager)resourceResolver.adaptTo(TaskManager.class);
TaskManagerFactory tmf = tm.getTaskManagerFactory();
StringBuffer description = new StringBuffer(String.format("No translation project created, There is no change detected between %s and %s", masterPagePath, launchSrcRootPath));
Task newTask = tmf.newTask("Notification");
newTask.setName(String.format("No translation project created.", new Object[0]));
TaskAction newTaskAction = tmf.newTaskAction("Remove");
newTask.setActions(Collections.singletonList(newTaskAction));
newTask.setContentPath(launchSrcRootPath);
newTask.setCurrentAssignee(strInitiator);
newTask.setDescription(description.toString());
tm.createTask(newTask);
}
catch (Exception e) {
log.error("Unable to send Task notificaiton {}", (Throwable)e);
}
}
protected void bindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
this.resourceResolverFactory = resourceResolverFactory;
}
protected void unbindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
if (this.resourceResolverFactory == resourceResolverFactory) {
this.resourceResolverFactory = null;
}
}
protected void bindLaunchesManagerFactory(LaunchManagerFactory launchManagerFactory) {
this.launchesManagerFactory = launchManagerFactory;
}
protected void unbindLaunchesManagerFactory(LaunchManagerFactory launchManagerFactory) {
if (this.launchesManagerFactory == launchManagerFactory) {
this.launchesManagerFactory = null;
}
}
protected void bindTranslationManager(TranslationManager translationManager) {
this.translationManager = translationManager;
}
protected void unbindTranslationManager(TranslationManager translationManager) {
if (this.translationManager == translationManager) {
this.translationManager = null;
}
}
protected void bindTranslationCloudConfigUtil(TranslationCloudConfigUtil translationCloudConfigUtil) {
this.translationCloudConfigUtil = translationCloudConfigUtil;
}
protected void unbindTranslationCloudConfigUtil(TranslationCloudConfigUtil translationCloudConfigUtil) {
if (this.translationCloudConfigUtil == translationCloudConfigUtil) {
this.translationCloudConfigUtil = null;
}
}
protected void bindLanguageManager(LanguageManager languageManager) {
this.languageManager = languageManager;
}
protected void unbindLanguageManager(LanguageManager languageManager) {
if (this.languageManager == languageManager) {
this.languageManager = null;
}
}
protected void bindRepository(SlingRepository slingRepository) {
this.repository = slingRepository;
}
protected void unbindRepository(SlingRepository slingRepository) {
if (this.repository == slingRepository) {
this.repository = null;
}
}
protected void bindTransConfig(TranslationConfig translationConfig) {
this.transConfig = translationConfig;
}
protected void unbindTransConfig(TranslationConfig translationConfig) {
if (this.transConfig == translationConfig) {
this.transConfig = null;
}
}
protected void bindPageManagerFactory(PageManagerFactory pageManagerFactory) {
this.pageManagerFactory = pageManagerFactory;
}
protected void unbindPageManagerFactory(PageManagerFactory pageManagerFactory) {
if (this.pageManagerFactory == pageManagerFactory) {
this.pageManagerFactory = null;
}
}
protected void bindHtmlLibraryManager(HtmlLibraryManager htmlLibraryManager) {
this.htmlLibraryManager = htmlLibraryManager;
}
protected void unbindHtmlLibraryManager(HtmlLibraryManager htmlLibraryManager) {
if (this.htmlLibraryManager == htmlLibraryManager) {
this.htmlLibraryManager = null;
}
}
protected void bindGuideLocalizationService(GuideLocalizationService guideLocalizationService) {
this.guideLocalizationService = guideLocalizationService;
}
protected void unbindGuideLocalizationService(GuideLocalizationService guideLocalizationService) {
if (this.guideLocalizationService == guideLocalizationService) {
this.guideLocalizationService = null;
}
}
}