ModelGenerateServlet.java
20.7 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.workflow.WorkflowException
* com.adobe.granite.workflow.WorkflowSession
* com.adobe.granite.workflow.metadata.MetaDataMap
* com.adobe.granite.workflow.model.WorkflowModel
* com.adobe.granite.workflow.model.WorkflowNode
* com.adobe.granite.workflow.model.WorkflowTransition
* javax.jcr.AccessDeniedException
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.PropertyIterator
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Value
* javax.jcr.ValueFormatException
* javax.jcr.util.TraversingItemVisitor
* javax.jcr.util.TraversingItemVisitor$Default
* javax.servlet.Servlet
* javax.servlet.ServletException
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.jackrabbit.api.JackrabbitSession
* org.apache.jackrabbit.api.security.user.Authorizable
* org.apache.jackrabbit.api.security.user.UserManager
* org.apache.jackrabbit.util.Text
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.workflow.console.servlet;
import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.console.servlet.ServletUtil;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.adobe.granite.workflow.model.WorkflowModel;
import com.adobe.granite.workflow.model.WorkflowNode;
import com.adobe.granite.workflow.model.WorkflowTransition;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import javax.jcr.AccessDeniedException;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFormatException;
import javax.jcr.util.TraversingItemVisitor;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service(value={Servlet.class})
@Properties(value={@org.apache.felix.scr.annotations.Property(name="sling.servlet.resourceTypes", value={"cq/workflow/components/pages/model"}), @org.apache.felix.scr.annotations.Property(name="sling.servlet.selectors", value={"generate"}), @org.apache.felix.scr.annotations.Property(name="sling.servlet.extensions", value={"json"}), @org.apache.felix.scr.annotations.Property(name="sling.servlet.methods", value={"POST"}), @org.apache.felix.scr.annotations.Property(name="service.description", value={"Workflow Model Generator Servlet"})})
public class ModelGenerateServlet
extends SlingAllMethodsServlet {
private static final long serialVersionUID = -7850496483294071571L;
private static final Logger log = LoggerFactory.getLogger(ModelGenerateServlet.class);
private static final String STEP_RES_TYPE = "cq/workflow/components/model/step";
private static final String SPLIT_RES_TYPE = "cq/flow/components/control/split";
private static final String PARTICIPANT_RES_TYPE = "cq/workflow/components/model/participant";
private static final String DYNAMIC_PARTICIPANT_RES_TYPE = "cq/workflow/components/model/dynamic_participant";
private static final String PROCESS_RES_TYPE = "cq/workflow/components/model/process";
private static final String CONTAINER_RES_TYPE = "cq/workflow/components/model/container";
private static final String EXTERNAL_PROCESS_RES_TYPE = "cq/workflow/components/model/external_process";
private static final String WORKFLOW_TAG_NAMESPACE = "workflow:";
private static final String TARGET_NODE_PATH = "TARGET_NODE_PATH";
private static final String TARGET_NODE_ID = "TARGET_NODE_ID";
private Map<String, String> STEP_MAPPINGS;
private Map<String, WorkflowNode> mapDesigntimePathsToNodes;
public ModelGenerateServlet() {
this.STEP_MAPPINGS = new HashMap<String, String>(){
private static final long serialVersionUID = 1;
};
this.mapDesigntimePathsToNodes = null;
}
protected void doPost(SlingHttpServletRequest req, SlingHttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("application/json");
resp.setCharacterEncoding("utf-8");
ResourceResolver resolver = req.getResourceResolver();
Session sess = (Session)resolver.adaptTo(Session.class);
WorkflowSession wfSess = (WorkflowSession)resolver.adaptTo(WorkflowSession.class);
Resource res = req.getResource();
ValueMap props = (ValueMap)res.adaptTo(ValueMap.class);
String title = (String)props.get("jcr:title", (Object)"No Title");
String desc = (String)props.get("jcr:description", (Object)"No Description");
String msg = "Model successfully generated.";
String modelPath = null;
try {
Node content = (Node)res.adaptTo(Node.class);
if (content.hasNode("flow")) {
String modelId = content.getPath() + "/model";
WorkflowModel model = wfSess.getModel(modelId);
if (model == null) {
model = wfSess.createNewModel(title, modelId);
}
model.setTitle(title);
model.setDescription(desc);
model.getNodes().clear();
model.getTransitions().clear();
ContentVisitor v = new ContentVisitor(resolver, model);
v.visit(content.getNode("flow"));
model = v.getModel();
modelPath = model.getId();
long now = Calendar.getInstance().getTimeInMillis();
model.getMetaDataMap().put((Object)"cq:lastModified", (Object)now);
model.getMetaDataMap().put((Object)"cq:lastModifiedBy", (Object)sess.getUserID());
this.copyModelMetadata(model, props);
this.setTags(model, content);
this.resolvePaths(model);
wfSess.deployModel(model);
}
}
catch (Exception e) {
this.log("Could not create workflow model", (Throwable)e);
msg = "Could not create workflow model. " + e.getMessage();
resp.setStatus(500);
}
JSONWriter w = new JSONWriter((Writer)resp.getWriter());
try {
w.object();
w.key("msg").value((Object)msg);
if (modelPath != null) {
w.key("modelPath").value((Object)modelPath);
}
w.endObject();
}
catch (JSONException e) {
this.log("Error", (Throwable)e);
}
}
private void copyModelMetadata(WorkflowModel model, ValueMap modelPageValueMap) {
MetaDataMap modelMetadataMap = model.getMetaDataMap();
Iterator mmIterator = modelMetadataMap.entrySet().iterator();
while (mmIterator.hasNext()) {
Map.Entry entry = (Map.Entry)mmIterator.next();
if (!this.includeKey((String)entry.getKey())) continue;
mmIterator.remove();
}
for (String key : modelPageValueMap.keySet()) {
Object value;
if (!this.includeKey(key) || (value = modelPageValueMap.get((Object)key)) == null) continue;
modelMetadataMap.put((Object)key, value);
}
}
private boolean includeKey(String key) {
return key != null && !key.startsWith("jcr:") && !key.startsWith("cq:") && !key.startsWith("sling:");
}
private void setTags(WorkflowModel model, Node content) throws RepositoryException {
if (content.hasProperty("cq:tags")) {
ArrayList<String> tags = new ArrayList<String>();
for (Value v : content.getProperty("cq:tags").getValues()) {
if (!v.getString().startsWith("workflow:")) continue;
tags.add(v.getString().substring("workflow:".length()).replace('/', '.'));
}
model.getMetaDataMap().put((Object)"tags", (Object)Text.implode((String[])tags.toArray(new String[tags.size()]), (String)","));
}
}
private void addPathToNodeMapping(Resource res, WorkflowNode node) {
if (res != null && res.getPath() != null && node != null) {
if (this.mapDesigntimePathsToNodes == null) {
this.mapDesigntimePathsToNodes = new HashMap<String, WorkflowNode>();
}
this.mapDesigntimePathsToNodes.put(res.getPath(), node);
}
}
private void resolvePaths(WorkflowModel model) throws WorkflowException {
if (model == null || this.mapDesigntimePathsToNodes == null) {
return;
}
List allNodes = model.getNodes();
for (WorkflowNode node : allNodes) {
String targetNodePath;
if (!node.getMetaDataMap().containsKey((Object)"TARGET_NODE_PATH") || (targetNodePath = (String)node.getMetaDataMap().get("TARGET_NODE_PATH", String.class)) == null || targetNodePath.trim().length() <= 0) continue;
WorkflowNode targetNode = this.mapDesigntimePathsToNodes.get(targetNodePath);
if (targetNode == null) {
throw new WorkflowException("target node '" + targetNodePath + "' on node '" + node.getId() + "' not found");
}
node.getMetaDataMap().put((Object)"TARGET_NODE_ID", (Object)targetNode.getId());
}
}
private class ContentVisitor
extends TraversingItemVisitor.Default {
private ResourceResolver resolver;
private WorkflowModel model;
private Resource flow;
private Map<WorkflowNode, ValueMap> rules;
private Stack<List<WorkflowNode>> nested;
public ContentVisitor(ResourceResolver res, WorkflowModel m) {
this.rules = new HashMap<WorkflowNode, ValueMap>();
this.nested = new Stack();
this.resolver = res;
this.model = m;
}
public WorkflowModel getModel() {
return this.model;
}
protected void entering(Node node, int level) throws RepositoryException {
Resource res = this.resolver.getResource(node.getPath());
ValueMap props = (ValueMap)res.adaptTo(ValueMap.class);
log.info("Entering " + (Object)res);
if (res != null) {
Resource parent = ResourceUtil.getParent((Resource)res);
if (this.flow == null) {
this.flow = res;
ArrayList<WorkflowNode> current = new ArrayList<WorkflowNode>();
this.nested.push(current);
WorkflowNode start = this.model.createNode("Start", "START", null);
this.model.setRootNode(start);
current.add(start);
} else if (ResourceUtil.isA((Resource)res, (String)"cq/flow/components/control/split")) {
boolean isOr = "true".equals(props.get("orSplit", (Object)""));
String st = isOr ? "OR_SPLIT" : "AND_SPLIT";
String jt = isOr ? "OR_JOIN" : "AND_JOIN";
WorkflowNode split = this.model.createNode(st, st, null);
WorkflowNode join = this.model.createNode(jt, jt, null);
List<WorkflowNode> current = this.nested.peek();
WorkflowNode last = current.get(current.size() - 1);
if (this.nested.size() > 1) {
last = current.get(current.size() - 2);
current.add(current.size() - 1, split);
current.add(current.size() - 1, join);
} else {
current.add(split);
current.add(join);
}
String rule = null;
if (last.getType().equals("OR_SPLIT") && this.rules.get((Object)last) != null) {
int branch = last.getTransitions().size() + 1;
if (this.rules.get((Object)last).containsKey((Object)("scriptPath" + branch))) {
rule = (String)this.rules.get((Object)last).get((Object)("scriptPath" + branch));
} else if (this.rules.get((Object)last).containsKey((Object)("script" + branch))) {
rule = (String)this.rules.get((Object)last).get((Object)("script" + branch));
}
}
this.model.createTransition(last, split, rule);
if (isOr) {
this.rules.put(split, props);
}
} else if (ResourceUtil.isA((Resource)parent, (String)"cq/flow/components/control/split")) {
WorkflowNode join;
WorkflowNode split;
List<WorkflowNode> current = this.nested.peek();
if (this.nested.size() > 1) {
split = current.get(current.size() - 3);
join = current.get(current.size() - 2);
} else {
split = current.get(current.size() - 2);
join = current.get(current.size() - 1);
}
ArrayList<WorkflowNode> next = new ArrayList<WorkflowNode>();
this.nested.push(next);
next.add(split);
next.add(join);
} else if (ResourceUtil.isA((Resource)res, (String)"cq/workflow/components/model/step")) {
this.handleStep(res, props);
}
}
}
protected void leaving(Node node, int level) throws RepositoryException {
Resource res = this.resolver.getResource(node.getPath());
log.info("Leaving " + (Object)res);
if (res != null) {
Resource parent = ResourceUtil.getParent((Resource)res);
if (this.flow.getPath().equals(res.getPath())) {
WorkflowNode end = this.model.createNode("End", "END", null);
this.model.setEndNode(end);
List<WorkflowNode> current = this.nested.pop();
WorkflowNode last = current.get(current.size() - 1);
this.model.createTransition(last, end, null);
this.flow = null;
} else if (ResourceUtil.isA((Resource)parent, (String)"cq/flow/components/control/split")) {
List<WorkflowNode> prev = this.nested.pop();
WorkflowNode last = prev.get(prev.size() - 2);
WorkflowNode join = prev.get(prev.size() - 1);
this.model.createTransition(last, join, null);
}
}
}
private WorkflowNode handleStep(Resource res, ValueMap props) {
String title = (String)props.get("jcr:title", String.class);
String desc = (String)props.get("jcr:description", String.class);
String type = this.getStepType(res);
if (type == null) {
return null;
}
WorkflowNode node = this.model.createNode(title, type, desc);
this.copyMetaData(res, node);
ModelGenerateServlet.this.addPathToNodeMapping(res, node);
List<WorkflowNode> current = this.nested.peek();
WorkflowNode last = current.get(current.size() - 1);
if (this.nested.size() > 1) {
last = current.get(current.size() - 2);
current.add(current.size() - 1, node);
} else {
current.add(node);
}
if (last.getType().equals("OR_SPLIT") && this.rules.get((Object)last) != null) {
int branch = last.getTransitions().size() + 1;
String rule = "";
if (this.rules.get((Object)last).containsKey((Object)("scriptPath" + branch))) {
rule = (String)this.rules.get((Object)last).get((Object)("scriptPath" + branch));
} else if (this.rules.get((Object)last).containsKey((Object)("script" + branch))) {
rule = (String)this.rules.get((Object)last).get((Object)("script" + branch));
}
WorkflowTransition transition = this.model.createTransition(last, node, rule);
String defaultRoute = (String)this.rules.get((Object)last).get((Object)("default" + branch));
transition.getMetaDataMap().put((Object)"isDefault", (Object)(defaultRoute != null ? defaultRoute : "false"));
} else {
this.model.createTransition(last, node, "");
}
return node;
}
private String getStepType(Resource res) {
for (String resType : ModelGenerateServlet.this.STEP_MAPPINGS.keySet()) {
if (!ResourceUtil.isA((Resource)res, (String)resType)) continue;
return (String)ModelGenerateServlet.this.STEP_MAPPINGS.get(resType);
}
log.error("Not a valid workflow step resource: {}", (Object)res);
return null;
}
private void copyMetaData(Resource res, WorkflowNode node) {
ResourceResolver resolver = res.getResourceResolver();
Resource meta = resolver.getResource(res.getPath() + "/metaData");
if (meta == null) {
log.error("no metaData node available for resource " + res.getPath());
return;
}
Node metaDataNode = (Node)meta.adaptTo(Node.class);
try {
PropertyIterator pIt = metaDataNode.getProperties();
while (pIt.hasNext()) {
Property prop = pIt.nextProperty();
if (prop.getName().startsWith("jcr:")) continue;
if (prop.getName().equals("PARTICIPANT")) {
try {
String value = prop.getString();
Session session = (Session)resolver.adaptTo(Session.class);
if (session instanceof JackrabbitSession) {
UserManager um = ((JackrabbitSession)session).getUserManager();
Authorizable auth = value.startsWith("/") ? ServletUtil.findByHome(session, um, value) : ServletUtil.getAuthorizable(um, value);
value = auth.getID();
node.getMetaDataMap().put((Object)prop.getName(), (Object)value);
continue;
}
log.error("Unexpected session type");
}
catch (AccessDeniedException e) {
log.error("Could not get user manager {}", (Throwable)e);
}
continue;
}
if (prop.isMultiple()) {
node.getMetaDataMap().put((Object)prop.getName(), (Object)prop.getValues());
continue;
}
node.getMetaDataMap().put((Object)prop.getName(), (Object)prop.getValue());
}
}
catch (ValueFormatException vfe) {
log.error(vfe.getMessage(), (Throwable)vfe);
}
catch (RepositoryException e) {
log.error(e.getMessage(), (Throwable)e);
}
}
}
}