TaskWorkflowProcess.java
12 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.taskmanagement.Task
* com.adobe.granite.taskmanagement.TaskAction
* com.adobe.granite.taskmanagement.TaskManager
* com.adobe.granite.taskmanagement.TaskManagerException
* com.adobe.granite.taskmanagement.TaskManagerFactory
* com.adobe.granite.workflow.WorkflowException
* com.adobe.granite.workflow.WorkflowSession
* com.adobe.granite.workflow.exec.WorkItem
* com.adobe.granite.workflow.exec.Workflow
* com.adobe.granite.workflow.exec.WorkflowData
* com.adobe.granite.workflow.exec.WorkflowProcess
* com.adobe.granite.workflow.metadata.MetaDataMap
* javax.jcr.Session
* 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.adapter.AdapterManager
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.scripting.SlingBindings
* org.apache.sling.api.scripting.SlingScript
* org.apache.sling.jcr.api.SlingRepository
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.taskmanagement.impl.workflow;
import com.adobe.granite.taskmanagement.Task;
import com.adobe.granite.taskmanagement.TaskAction;
import com.adobe.granite.taskmanagement.TaskManager;
import com.adobe.granite.taskmanagement.TaskManagerException;
import com.adobe.granite.taskmanagement.TaskManagerFactory;
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.Workflow;
import com.adobe.granite.workflow.exec.WorkflowData;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.jcr.Session;
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.adapter.AdapterManager;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.scripting.SlingBindings;
import org.apache.sling.api.scripting.SlingScript;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service
@Properties(value={@Property(name="service.description", value={"Task Manager Step implementation."}), @Property(name="service.vendor", value={"Adobe"}), @Property(name="process.label", value={"Task Manager Step"})})
public class TaskWorkflowProcess
implements WorkflowProcess {
private static final Logger log = LoggerFactory.getLogger(TaskWorkflowProcess.class);
static final String WORK_ITEM_ID = "workItemId";
static final String WORKFLOW_INSTANCE_ID = "wfInstanceId";
public static final String PARM_PUSH_PAYLOAD_CHANGES = "pushPayloadChanges";
static final String ARG_TASK_TYPE = "argTaskType";
static final String ARG_TASK_NAME = "argTaskName";
static final String ARG_CONTENT_SOURCE = "argContentSource";
static final String ARG_TASK_CONTENT = "argTaskContent";
static final String ARG_TASK_DUE_DELTA_DAYS = "argTaskDueDeltaDays";
static final String ARG_TASK_DUE_DELTA_HOURS = "argTaskDueDeltaHours";
static final String ARG_ACTIONS = "argActions";
static final String ARG_TASK_PRIORITY = "argTaskPriority";
static final String ARG_TASK_DESCRIPTION = "argTaskDescription";
static final String TASKPROPERTY_DUE_DATE = "taskDueDate";
static final String TASKPROPERTY_PRIORITY = "taskPriority";
static final String ARG_TASK_OWNER = "argTaskOwner";
static final String WORKITEMPROPERTY_TASK_ID = "taskId";
static final String ARG_PARENT_PROJECT = "argParentProject";
static final String ARG_ON_ASSIGN_SCRIPT = "onCreateScript";
static final String CONTENTSOURCE_WORKFLOW_PAYLOAD = "WorkflowPayload";
static final String CONTENTSOURCE_PAYLOAD_SPECIFIED = "SpecifiedPayload";
static final String TASKPROPERTY_COMMENT = "comment";
static final String WORKITEM_COMMENT = "comment";
@Reference
private AdapterManager adapterManager;
@Reference
protected SlingRepository repository;
public void execute(WorkItem item, WorkflowSession session, MetaDataMap args) throws WorkflowException {
log.debug("creating task for workitem {}", (Object)(item != null ? item.getId() : "<null>"));
try {
TaskManager taskManager = (TaskManager)this.adapterManager.getAdapter(session.adaptTo(Session.class), TaskManager.class);
String parentTaskId = (String)args.get("argParentProject", String.class);
Task task = this.constructTaskInstanceFromParameters(taskManager, item, args);
this.runOnCreateScript(session, item.getWorkflowData(), task, args);
boolean projectSpecifiedInScript = false;
if (task.getParentId() != null && task.getParentId().trim().length() > 0) {
parentTaskId = task.getParentId();
projectSpecifiedInScript = true;
}
try {
taskManager.getTask(parentTaskId);
}
catch (TaskManagerException e) {
if (projectSpecifiedInScript) {
throw new WorkflowException("Failed to create task: Project not found. The Project Id specified by the task script does not exist.");
}
throw new WorkflowException("Failed to create task: Project not found. Please pick a new Project for this step in the workflow model.");
}
Task result = taskManager.createTask(parentTaskId, task);
if (result != null) {
item.getMetaDataMap().put((Object)"taskId", (Object)result.getId());
log.debug("task {} created", (Object)result.getId());
}
}
catch (TaskManagerException e) {
throw new WorkflowException("Failed to create task", (Throwable)e);
}
}
private Task constructTaskInstanceFromParameters(TaskManager taskManager, WorkItem workItem, MetaDataMap args) throws TaskManagerException, WorkflowException {
String[] actions;
String taskOwner = (String)args.get("argTaskOwner", String.class);
if (taskOwner == null || taskOwner.trim().length() == 0) {
throw new WorkflowException("Task creation failed - no owner specified");
}
String taskType = (String)args.get("argTaskType", String.class);
if (taskType == null) {
taskType = "default";
}
Task result = taskManager.getTaskManagerFactory().newTask(taskType);
result.setCurrentAssignee(taskOwner);
result.setProperty("workItemId", (Object)workItem.getId());
result.setProperty("wfInstanceId", (Object)workItem.getWorkflow().getId());
String taskName = (String)args.get("argTaskName", String.class);
if (taskName != null) {
result.setName(taskName);
}
String pushPayloadChange = "false";
String taskContentSelector = (String)args.get("argContentSource", String.class);
if ("WorkflowPayload".equals(taskContentSelector)) {
Object wfPayload = workItem.getWorkflowData().getPayload();
if (wfPayload instanceof String) {
result.setContentPath((String)wfPayload);
pushPayloadChange = "true";
}
} else {
String taskContentPath = (String)args.get("argTaskContent", String.class);
if (taskContentPath != null) {
result.setContentPath(taskContentPath);
}
}
result.setProperty("pushPayloadChanges", (Object)pushPayloadChange);
String taskDescription = (String)args.get("argTaskDescription", String.class);
if (taskDescription != null) {
result.setDescription(taskDescription);
}
int deltaDays = this.getInt(args, "argTaskDueDeltaDays");
int deltaHours = this.getInt(args, "argTaskDueDeltaHours");
if (deltaDays != 0 || deltaHours != 0) {
Calendar calendar = Calendar.getInstance();
calendar.add(10, deltaHours);
calendar.add(5, deltaDays);
result.setProperty("taskDueDate", (Object)calendar.getTime());
}
if ((actions = (String[])args.get("argActions", String[].class)) != null && actions.length > 0) {
ArrayList<TaskAction> actionList = new ArrayList<TaskAction>();
for (String action : actions) {
if (action == null || action.trim().length() <= 0) continue;
actionList.add(taskManager.getTaskManagerFactory().newTaskAction(action));
}
if (actionList.size() > 0) {
result.setActions(actionList);
}
}
String taskPriority = (String)args.get("argTaskPriority", String.class);
result.setProperty("taskPriority", (Object)taskPriority);
String workitemComment = (String)workItem.getMetaDataMap().get("comment", String.class);
if (workitemComment != null) {
result.setProperty("comment", (Object)workitemComment);
}
return result;
}
private int getInt(MetaDataMap args, String propertyName) {
if (args != null && propertyName != null) {
Integer integer = (Integer)args.get(propertyName, Integer.class);
if (integer != null) {
return integer;
}
String numberAsString = (String)args.get(propertyName, String.class);
if (numberAsString != null) {
try {
return Integer.parseInt(numberAsString);
}
catch (NumberFormatException nfe) {
log.debug("value of {} is not an integer number, instead got {}", (Object)propertyName, (Object)numberAsString);
}
}
}
return 0;
}
private void runOnCreateScript(WorkflowSession session, WorkflowData workflowData, Task task, MetaDataMap args) {
if (args.containsKey((Object)"onCreateScript")) {
ResourceResolver resourceResolver = (ResourceResolver)session.adaptTo(ResourceResolver.class);
String scriptPath = (String)args.get("onCreateScript", String.class);
Resource script = resourceResolver.getResource(scriptPath);
this.runOnCreateScript(script, workflowData, task, session, resourceResolver);
}
}
private void runOnCreateScript(Resource scriptResource, WorkflowData data, Task task, WorkflowSession workflowSession, ResourceResolver resolver) {
SlingScript script = (SlingScript)scriptResource.adaptTo(SlingScript.class);
SlingBindings props = new SlingBindings();
props.put((Object)"workflowData", (Object)data);
props.put((Object)"workflowSession", (Object)workflowSession);
props.put((Object)"jcrSession", resolver.adaptTo(Session.class));
props.put((Object)"task", (Object)task);
log.debug("Calling script {}.", (Object)script);
Object result = script.eval(props);
log.debug("Result from script {} is {}", (Object)script, result);
}
protected void bindAdapterManager(AdapterManager adapterManager) {
this.adapterManager = adapterManager;
}
protected void unbindAdapterManager(AdapterManager adapterManager) {
if (this.adapterManager == adapterManager) {
this.adapterManager = null;
}
}
protected void bindRepository(SlingRepository slingRepository) {
this.repository = slingRepository;
}
protected void unbindRepository(SlingRepository slingRepository) {
if (this.repository == slingRepository) {
this.repository = null;
}
}
}