AFtoDORStep.java
8.52 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.aemds.guide.addon.dor.DoROptions
* com.adobe.aemds.guide.addon.dor.DoRResult
* com.adobe.aemds.guide.addon.dor.DoRService
* com.adobe.aemfd.docmanager.Document
* 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.exec.WorkflowProcess
* com.adobe.granite.workflow.metadata.MetaDataMap
* com.adobe.granite.workflow.model.WorkflowNode
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.lang.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.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.fd.workflow.dorGeneration;
import com.adobe.aemds.guide.addon.dor.DoROptions;
import com.adobe.aemds.guide.addon.dor.DoRResult;
import com.adobe.aemds.guide.addon.dor.DoRService;
import com.adobe.aemfd.docmanager.Document;
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.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.adobe.granite.workflow.model.WorkflowNode;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
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.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;
@Component
@Service
@Properties(value={@Property(name="service.description", value={"AF To DOR Conversion Step"}), @Property(name="process.label", value={"AF To DOR Conversion Step"})})
public class AFtoDORStep
implements WorkflowProcess {
protected final Logger log;
@Reference
private DoRService dorService;
@Reference
private ResourceResolverFactory resourceResolverFactory;
public AFtoDORStep() {
this.log = LoggerFactory.getLogger(this.getClass());
}
public void execute(WorkItem workItem, WorkflowSession session, MetaDataMap args) throws WorkflowException {
Document dor = null;
try {
Node afNode;
String afPath = this.getAFPath(workItem);
String relativeDataXmlPath = this.getInputDataXMLPath(workItem);
String dorOutputPath = this.getDORPAth(workItem);
Locale inputLocale = new Locale(this.getLocale(workItem));
String payLoadPath = this.getPayloadPath(workItem);
String dataXmlPath = null;
Session userSession = (Session)session.adaptTo(Session.class);
String afContentPath = afPath + "/" + "jcr:content";
if (!(!userSession.nodeExists(afContentPath) || (afNode = userSession.getNode(afContentPath)).hasProperty("guide") && afNode.getProperty("guide").getString().equals("1"))) {
throw new Exception("Not a valid Adaptive Form");
}
if (StringUtils.isNotBlank((String)relativeDataXmlPath)) {
if ((relativeDataXmlPath.startsWith("FILE_PAYLOAD") || this.isPayloadFile(userSession, payLoadPath)) && dorOutputPath.startsWith("RELATIVE_PLOAD")) {
throw new Exception("Can't save node under node of type file");
}
dataXmlPath = DocumentUtils.getAbsolutePath(payLoadPath, relativeDataXmlPath);
}
DoROptions dorOptions = new DoROptions();
ResourceResolver resourceResolver = this.getResourceResolver((Session)session.adaptTo(Session.class));
dorOptions.setFormResource(resourceResolver.getResource(afPath));
dorOptions.setData(new String(DocumentUtils.readFileContentForFileNode(userSession.getNode(dataXmlPath))));
dorOptions.setLocale(inputLocale);
DoRResult dorResult = this.dorService.render(dorOptions);
dor = new Document(dorResult.getContent());
String dorPath = null;
if (dorOutputPath.startsWith("OVERWRITE:")) {
dorOutputPath = "";
dorPath = payLoadPath;
} else {
dorPath = (dorOutputPath = dorOutputPath.split(":")[1]).startsWith("/") ? payLoadPath + dorOutputPath : payLoadPath + "/" + dorOutputPath;
dorOutputPath = dorOutputPath.contains("/") ? dorOutputPath.substring(0, dorOutputPath.lastIndexOf("/")) : "";
}
if (userSession.nodeExists(payLoadPath)) {
Node parentNode = userSession.getNode(payLoadPath);
if (StringUtils.isNotBlank((String)dorOutputPath)) {
DocumentUtils.createFolderPathForFile(userSession, parentNode, dorOutputPath);
}
DocumentUtils.createOrUpdateDocument(userSession, dor, dorPath);
}
}
catch (Exception e) {
throw new WorkflowException((Throwable)e);
}
finally {
try {
if (dor != null) {
dor.close();
}
}
catch (IOException e) {
this.log.error("exception while closing the dor", (Throwable)e);
}
}
}
protected ResourceResolver getResourceResolver(Session session) throws WorkflowException {
try {
HashMap<String, Session> authenticationMap = new HashMap<String, Session>();
authenticationMap.put("user.jcr.session", session);
return this.resourceResolverFactory.getResourceResolver(authenticationMap);
}
catch (Exception e) {
throw new WorkflowException("Exception in getting Resource Resolver", (Throwable)e);
}
}
private boolean isPayloadFile(Session session, String payloadPath) throws RepositoryException {
Node payloadNode;
boolean isFile = false;
if (session.nodeExists(payloadPath) && (payloadNode = session.getNode(payloadPath)).hasProperty("jcr:primaryType") && payloadNode.getProperty("jcr:primaryType").getString().equals("nt:file")) {
isFile = true;
}
return isFile;
}
private String getPayloadPath(WorkItem workItem) {
return (String)workItem.getWorkflowData().getPayload();
}
private String getAFPath(WorkItem workItem) {
MetaDataMap args = workItem.getNode().getMetaDataMap();
return (String)args.get("AF_PATH", String.class);
}
private String getInputDataXMLPath(WorkItem workItem) {
MetaDataMap args = workItem.getNode().getMetaDataMap();
return (String)args.get("INPUT_DATAXML", String.class);
}
private String getDORPAth(WorkItem workItem) {
MetaDataMap args = workItem.getNode().getMetaDataMap();
return (String)args.get("DOR_PATH", String.class);
}
private String getLocale(WorkItem workItem) {
MetaDataMap args = workItem.getNode().getMetaDataMap();
return (String)args.get("LOCALE", String.class);
}
protected void bindDorService(DoRService doRService) {
this.dorService = doRService;
}
protected void unbindDorService(DoRService doRService) {
if (this.dorService == doRService) {
this.dorService = null;
}
}
protected void bindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
this.resourceResolverFactory = resourceResolverFactory;
}
protected void unbindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
if (this.resourceResolverFactory == resourceResolverFactory) {
this.resourceResolverFactory = null;
}
}
}