InvokeDDXProcess.java
10.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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.aemfd.docmanager.Document
* com.adobe.fd.assembler.client.AssemblerOptionSpec
* com.adobe.fd.assembler.client.AssemblerResult
* com.adobe.fd.assembler.service.AssemblerService
* com.adobe.granite.workflow.WorkflowException
* com.adobe.granite.workflow.WorkflowSession
* com.adobe.granite.workflow.exec.WorkItem
* com.adobe.granite.workflow.exec.WorkflowData
* com.adobe.granite.workflow.metadata.MetaDataMap
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Session
* javax.jcr.nodetype.NodeType
* 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.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.fd.workflow.assembler;
import com.adobe.aemfd.docmanager.Document;
import com.adobe.fd.assembler.client.AssemblerOptionSpec;
import com.adobe.fd.assembler.client.AssemblerResult;
import com.adobe.fd.assembler.service.AssemblerService;
import com.adobe.fd.workflow.internal.common.AEMFDWorkflowProcess;
import com.adobe.fd.workflow.utils.DocumentUtils;
import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowData;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeType;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service
@Properties(value={@Property(name="service.description", value={"InvokeDDXProcess"}), @Property(name="service.vendor", value={"Adobe"}), @Property(name="process.label", value={"InvokeDDXProcess"})})
public class InvokeDDXProcess
extends AEMFDWorkflowProcess {
private static Logger LOGGER = LoggerFactory.getLogger(InvokeDDXProcess.class);
private static final String DDX_PARAM = "ddx";
private static final String POPULATE_MAP_FROM_PAYLOAD_PARAM = "populateFromPayLoad";
private static final String INPUT_DOCS_PARAM = "inputDocs";
private static final String INPUT_KEY_PARAM = "inputkey";
private static final String SOURCE_PATH_PARAM = "sourcePath";
private static final String LOG_LEVEL_PARAM = "logLevel";
private static final String VALIDATE_ONLY_PARAM = "validateOnly";
private static final String FAIL_ON_ERROR_PARAM = "failOnError";
private static final String BATES_NUMBER_PARAM = "batesNumber";
private static final String DEFAULT_STYLE_PARAM = "defaultStyle";
private static final String SAVE_TO_PAYLOAD_PARAM = "saveToPayload";
private static final String OUTPUT_DOCS_PARAM = "outputDocs";
private static final String OUTPUT_KEY_PARAM = "outputkey";
private static final String OUTPUT_PATH_PARAM = "outputPath";
private static final String JOB_LOG_PARAM = "jobLog";
@Reference
AssemblerService assemblerService;
@Override
protected void internal_execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
try {
Map<String, Object> inputMap = this.createInputMap(workItem, workflowSession, metaDataMap);
AssemblerOptionSpec options = this.createAssemblerOptions(metaDataMap);
Document ddxDocument = DocumentUtils.getInputDocument(workItem, "ddx");
if (ddxDocument == null) {
throw new WorkflowException("DDX document cannot be null.");
}
AssemblerResult assemblerResult = this.assemblerService.invoke(ddxDocument, inputMap, options);
if (!options.isValidateOnly()) {
this.saveOutputDocuments(assemblerResult, workItem, workflowSession, metaDataMap);
}
if (metaDataMap.get("jobLog", String.class) != null) {
DocumentUtils.saveOutputDocument(workItem, workflowSession, "jobLog", assemblerResult.getJobLog());
}
}
catch (Exception e) {
throw new WorkflowException((Throwable)e);
}
}
private Map<String, Object> createInputMap(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws Exception {
HashMap<String, Object> inputMap = new HashMap<String, Object>();
if (metaDataMap.get("populateFromPayLoad", Boolean.class) != null) {
boolean populateFromPayload = (Boolean)metaDataMap.get("populateFromPayLoad", Boolean.class);
LOGGER.debug("populateFromPayLoad= " + populateFromPayload);
if (populateFromPayload) {
this.populateInputMapFromPayload(inputMap, workItem, workflowSession);
}
}
if (metaDataMap.get("inputDocs", String[].class) != null) {
String[] inputEntries = (String[])metaDataMap.get("inputDocs", String[].class);
LOGGER.debug("inputDocs length = " + inputEntries.length);
for (String inputEntry : inputEntries) {
JSONObject jsonObject = new JSONObject(inputEntry);
Document iDoc = DocumentUtils.getInputDocument(workItem.getWorkflowData().getPayload().toString(), jsonObject.getString("sourcePath"));
if (iDoc == null) continue;
inputMap.put(jsonObject.getString("inputkey"), (Object)iDoc);
}
}
return inputMap;
}
private AssemblerOptionSpec createAssemblerOptions(MetaDataMap metaDataMap) {
AssemblerOptionSpec options = new AssemblerOptionSpec();
if (metaDataMap.get("logLevel", String.class) != null) {
String logLevel = (String)metaDataMap.get("logLevel", String.class);
LOGGER.debug("logLevel= " + logLevel);
options.setLogLevel(logLevel);
}
if (metaDataMap.get("validateOnly", Boolean.class) != null) {
boolean validateOnly = (Boolean)metaDataMap.get("validateOnly", Boolean.class);
LOGGER.debug("validateOnly= " + validateOnly);
options.setValidateOnly(validateOnly);
}
if (metaDataMap.get("failOnError", Boolean.class) != null) {
boolean failOnError = (Boolean)metaDataMap.get("failOnError", Boolean.class);
LOGGER.debug("failOnError= " + failOnError);
options.setFailOnError(failOnError);
}
if (metaDataMap.get("batesNumber", Integer.class) != null) {
int batesNumber = (Integer)metaDataMap.get("batesNumber", Integer.class);
LOGGER.debug("batesNumber= " + batesNumber);
options.setFirstBatesNumber(batesNumber);
}
if (metaDataMap.get("defaultStyle", String.class) != null) {
String defaultStyle = (String)metaDataMap.get("defaultStyle", String.class);
LOGGER.debug("defaultStyle= " + defaultStyle);
options.setDefaultStyle(defaultStyle);
}
return options;
}
private void saveOutputDocuments(AssemblerResult assemblerResult, WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws Exception {
String payloadPath = workItem.getWorkflowData().getPayload().toString();
Map outputDocumentsMap = assemblerResult.getDocuments();
if (metaDataMap.get("saveToPayload", Boolean.class) != null) {
boolean saveToPayload = (Boolean)metaDataMap.get("saveToPayload", Boolean.class);
LOGGER.debug("saveToPayload= " + saveToPayload);
if (saveToPayload) {
for (Map.Entry resultEntry : outputDocumentsMap.entrySet()) {
if (((Session)workflowSession.adaptTo(Session.class)).nodeExists(payloadPath + "/" + (String)resultEntry.getKey())) {
DocumentUtils.updateDocument(workflowSession, (Document)resultEntry.getValue(), payloadPath + "/" + (String)resultEntry.getKey());
continue;
}
DocumentUtils.createOrUpdateDocument(workflowSession, (Document)resultEntry.getValue(), payloadPath, (String)resultEntry.getKey());
}
}
}
if (metaDataMap.get("outputDocs", String[].class) != null) {
String[] outputEntries = (String[])metaDataMap.get("outputDocs", String[].class);
LOGGER.debug("outputDocs length = " + outputEntries.length);
for (String outputEntry : outputEntries) {
JSONObject jsonObject = new JSONObject(outputEntry);
DocumentUtils.createOrUpdateDocument(workflowSession, (Document)outputDocumentsMap.get(jsonObject.getString("outputkey")), workItem.getWorkflowData().getPayload().toString(), jsonObject.getString("outputPath"));
}
}
}
private void populateInputMapFromPayload(Map<String, Object> inputMap, WorkItem workItem, WorkflowSession workflowSession) throws Exception {
String payload = workItem.getWorkflowData().getPayload() + "";
Node payLoadNode = ((Session)workflowSession.adaptTo(Session.class)).getNode(payload);
this.populateMapFromNodeChildren(payLoadNode, inputMap);
}
private void populateMapFromNodeChildren(Node parentNode, Map<String, Object> inputMap) throws Exception {
NodeIterator childItr = parentNode.getNodes();
while (childItr.hasNext()) {
Node childNode = childItr.nextNode();
if (childNode.getPrimaryNodeType().toString().equals("dam:Asset")) {
inputMap.put(childNode.getName(), (Object)new Document(childNode.getPath()));
continue;
}
if (!childNode.getPrimaryNodeType().equals("{http://www.jcp.org/jcr/nt/1.0}folder") && !childNode.getPrimaryNodeType().toString().equals("sling:OrderedFolder") && !childNode.getPrimaryNodeType().toString().equals("sling:Folder")) continue;
this.populateMapFromNodeChildren(childNode, inputMap);
}
}
protected void bindAssemblerService(AssemblerService assemblerService) {
this.assemblerService = assemblerService;
}
protected void unbindAssemblerService(AssemblerService assemblerService) {
if (this.assemblerService == assemblerService) {
this.assemblerService = null;
}
}
}