DefaultPayloadMapper.java
6.05 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.aemfd.docmanager.Document
* com.adobe.granite.workflow.exec.Workflow
* javax.jcr.Binary
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Property
* javax.jcr.PropertyIterator
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.ResourceResolver
*/
package com.adobe.aemfd.watchfolder.workflow;
import com.adobe.aemfd.docmanager.Document;
import com.adobe.aemfd.watchfolder.util.JcrUtil;
import com.adobe.aemfd.watchfolder.workflow.api.payload.PayloadMapper;
import com.adobe.aemfd.watchfolder.workflow.api.payload.WorkflowExecutionContext;
import com.adobe.aemfd.watchfolder.workflow.api.payload.WorkflowInitializationContext;
import com.adobe.aemfd.watchfolder.workflow.api.payload.WorkflowVariable;
import com.adobe.granite.workflow.exec.Workflow;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.ResourceResolver;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
@Component(immediate=1)
@Service(value={PayloadMapper.class})
public class DefaultPayloadMapper
implements PayloadMapper {
private static final String VAR_PROP_PREFIX = "var.";
@Override
public Node createPayload(WorkflowInitializationContext wfInitCtxt, Node stagingFolder, String uniquePayloadName, Map<String, Binary> inputs, Collection<WorkflowVariable> variableDefs) throws Exception {
Node payloadNode = stagingFolder.addNode(uniquePayloadName, "sling:Folder");
payloadNode.addNode("input", "nt:folder");
for (Map.Entry<String, Binary> input : inputs.entrySet()) {
this.initInput(payloadNode, input.getKey(), input.getValue());
}
payloadNode.addNode("output", "nt:folder");
payloadNode.addNode("var", "nt:unstructured");
for (WorkflowVariable var : variableDefs) {
this.setVariable(payloadNode, var);
}
return payloadNode;
}
private void initInput(Node payload, String fileName, Binary contents) throws Exception {
Node inputNode = payload.getNode("input");
Node fileNode = inputNode.addNode(fileName, "nt:file");
Node resNode = fileNode.addNode("jcr:content", "nt:resource");
resNode.setProperty("jcr:data", contents);
}
@Override
public Map<String, Document> getInputs(WorkflowInitializationContext wfInitCtxt, WorkflowExecutionContext wfExecCtxt, Node payload, ResourceResolver resourceResolver) throws Exception {
return this.getDocuments(payload, "input", resourceResolver);
}
private Map<String, Document> getDocuments(Node payload, String folder, ResourceResolver resourceResolver) throws Exception {
Node inFolder = payload.getNode(folder);
NodeIterator ni = inFolder.getNodes();
HashMap<String, Document> result = new HashMap<String, Document>();
while (ni.hasNext()) {
Node inFile = ni.nextNode();
result.put(inFile.getName(), new Document(inFile.getPath(), resourceResolver));
}
return result;
}
@Override
public Map<String, Document> getIntermediateOutputs(WorkflowInitializationContext wfInitCtxt, WorkflowExecutionContext wfExecCtxt, Node payload, ResourceResolver resourceResolver) throws Exception {
return this.getOutputs(payload, resourceResolver);
}
@Override
public Map<String, Document> getFinalOutputs(WorkflowInitializationContext wfInitCtxt, Workflow workflow, Node payload, ResourceResolver resourceResolver) throws Exception {
return this.getOutputs(payload, resourceResolver);
}
private Map<String, Document> getOutputs(Node payload, ResourceResolver resourceResolver) throws Exception {
return this.getDocuments(payload, "output", resourceResolver);
}
@Override
public void setOutput(WorkflowInitializationContext wfInitCtxt, WorkflowExecutionContext wfExecCtxt, Node payload, String fileName, Binary contents, int outputMode) throws Exception {
Node outFolder = payload.getNode("output");
if (outputMode == 3) {
outFolder.getNode(fileName).remove();
} else {
Node resNode;
if (outputMode == 2) {
resNode = outFolder.getNode(fileName).getNode("jcr:content");
} else {
Node fileNode = outFolder.addNode(fileName, "nt:file");
resNode = fileNode.addNode("jcr:content", "nt:resource");
}
resNode.setProperty("jcr:data", contents);
}
}
@Override
public Map<String, Object> getVariables(WorkflowInitializationContext wfInitCtxt, WorkflowExecutionContext wfExecCtxt, Node payload) throws Exception {
Node inFolder = payload.getNode("var");
PropertyIterator pi = inFolder.getProperties();
HashMap<String, Object> result = new HashMap<String, Object>();
while (pi.hasNext()) {
Property p = pi.nextProperty();
String pName = p.getName();
if (!pName.startsWith("var.")) continue;
String varName = pName.substring("var.".length());
result.put(varName, JcrUtil.getPropertyValue(p, null, false));
}
return result;
}
@Override
public void setVariable(WorkflowInitializationContext wfInitCtxt, WorkflowExecutionContext wfExecCtxt, Node payload, WorkflowVariable variable) throws Exception {
this.setVariable(payload, variable);
}
private void setVariable(Node payload, WorkflowVariable var) throws Exception {
String propName = "var." + var.getName();
Node varNode = payload.getNode("var");
JcrUtil.setPropertyValue(varNode, propName, var.getType(), var.isMultiValued(), var.getValue());
}
}