/
ChecklistItemFactory.java
231 lines (217 loc) · 7.2 KB
/
ChecklistItemFactory.java
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
/*
* Copyright 2015 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jbpm.examples.checklist.impl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jbpm.process.core.Work;
import org.jbpm.examples.checklist.ChecklistItem;
import org.jbpm.examples.checklist.ChecklistItem.Status;
import org.jbpm.workflow.core.impl.NodeImpl;
import org.jbpm.workflow.core.node.HumanTaskNode;
import org.jbpm.workflow.core.node.StartNode;
import org.kie.api.definition.process.Node;
import org.kie.api.definition.process.NodeContainer;
import org.kie.api.definition.process.WorkflowProcess;
import org.kie.api.runtime.manager.audit.NodeInstanceLog;
import org.kie.api.task.model.I18NText;
import org.kie.api.task.model.OrganizationalEntity;
import org.kie.api.task.model.Task;
import org.kie.api.task.model.User;
public final class ChecklistItemFactory {
public static ChecklistItem createChecklistItem(Task task) {
return new DefaultChecklistItem(
getText(task.getNames()),
getStatus(task.getTaskData().getStatus()),
task.getId(),
"HumanTaskNode",
getActors(task),
task.getPriority(),
task.getTaskData().getProcessId(),
task.getTaskData().getProcessInstanceId(),
getText(task.getDescriptions()));
}
private static String getText(List<I18NText> texts) {
if (texts == null) {
return null;
}
for (I18NText text: texts) {
if ("en-UK".equals(text.getLanguage())) {
return text.getText();
}
}
return null;
}
private static Status getStatus(org.kie.api.task.model.Status status) {
switch (status) {
case Completed: return Status.Completed;
case Ready:
case Created: return Status.Created;
case Error:
case Exited:
case Failed:
case Obsolete: return Status.Aborted;
case Suspended:
case InProgress: return Status.InProgress;
case Reserved: return Status.Reserved;
default: return null;
}
}
private static String getActors(Task task) {
User actualOwner = task.getTaskData().getActualOwner();
if (actualOwner != null) {
return actualOwner.getId();
} else {
String result = "";
for (OrganizationalEntity o: task.getPeopleAssignments().getPotentialOwners()) {
if (result.length() != 0) {
result += ",";
}
result += o.getId();
}
return result;
}
}
public static Collection<ChecklistItem> getPendingChecklistItems(WorkflowProcess process) {
List<ChecklistItem> result = new ArrayList<ChecklistItem>();
getPendingChecklistItems(process, result, process.getId());
return result;
}
private static void getPendingChecklistItems(NodeContainer container, List<ChecklistItem> result, String processId) {
for (Node node: container.getNodes()) {
if (node instanceof HumanTaskNode) {
Work workItem = ((HumanTaskNode) node).getWork();
int priority = 0;
String priorityString = (String) workItem.getParameter("Priority");
if (priorityString != null) {
try {
priority = new Integer(priorityString);
} catch (NumberFormatException e) {
// Do nothing
}
}
String actorId = (String) workItem.getParameter("ActorId");
if (actorId != null && actorId.trim().length() == 0) {
actorId = null;
}
String groupId = (String) workItem.getParameter("GroupId");
if (groupId != null && groupId.trim().length() == 0) {
groupId = null;
}
String actors = null;
if (actorId == null) {
if (groupId == null) {
actors = "";
} else {
actors = groupId;
}
} else {
if (groupId == null) {
actors = actorId;
} else {
actors = actorId + "," + groupId;
}
}
Status status = Status.Pending;
if (((HumanTaskNode) node).getDefaultIncomingConnections().size() == 0) {
status = Status.Optional;
}
result.add(
createChecklistItem(
(String) workItem.getParameter("TaskName"),
"HumanTaskNode",
actors,
(String) workItem.getParameter("Comment"),
priority, processId, status));
} else if (node instanceof NodeContainer) {
getPendingChecklistItems((NodeContainer) node, result, processId);
} else {
String docs = (String) node.getMetaData().get("Documentation");
if (docs != null) {
int position = docs.indexOf("OrderingNb=");
if (position >= 0) {
int end = docs.indexOf(";", position + 1);
String orderingNumber = docs.substring(position + 11, end);
Status status = Status.Pending;
if (((NodeImpl) node).getDefaultIncomingConnections().size() == 0 && !(node instanceof StartNode)) {
status = Status.Optional;
}
result.add(
createChecklistItem(
node.getName(),
node.getClass().getSimpleName(),
"",
orderingNumber,
0,
processId,
status));
}
}
}
}
}
public static Collection<ChecklistItem> getLoggedChecklistItems(WorkflowProcess process, List<NodeInstanceLog> nodeInstances) {
Map<String, ChecklistItem> result = new HashMap<String, ChecklistItem>();
Map<String, String> relevantNodes = new HashMap<String, String>();
getRelevantNodes(process, relevantNodes);
for (NodeInstanceLog log: nodeInstances) {
String orderingNb = relevantNodes.get(log.getNodeId());
if (orderingNb != null) {
if (log.getType() == NodeInstanceLog.TYPE_EXIT) {
result.put(orderingNb, createChecklistItem(log.getNodeName(), log.getNodeType(), "", orderingNb, 0, log.getProcessId(), Status.Completed));
} else {
if (result.get(orderingNb) == null) {
result.put(orderingNb, createChecklistItem(log.getNodeName(), log.getNodeType(), "", orderingNb, 0, log.getProcessId(), Status.InProgress));
}
}
}
}
return result.values();
}
private static void getRelevantNodes(NodeContainer container, Map<String, String> result) {
for (Node node: container.getNodes()) {
if (node instanceof NodeContainer) {
getRelevantNodes((NodeContainer) node, result);
}
String docs = (String) node.getMetaData().get("Documentation");
if (docs != null) {
int position = docs.indexOf("OrderingNb=");
if (position >= 0) {
int end = docs.indexOf(";", position + 1);
String orderingNumber = docs.substring(position + 11, end);
String nodeId = (String)node.getMetaData().get("UniqueId");
if (nodeId == null) {
nodeId = ((NodeImpl) node).getUniqueId();
}
result.put(nodeId, orderingNumber);
}
}
}
}
private static ChecklistItem createChecklistItem(String name, String type, String actors, String orderingNb, long priority, String processId, Status status) {
return new DefaultChecklistItem(
name == null ? "" : name,
status,
null,
type,
actors == null ? "" : actors,
priority,
processId,
null,
orderingNb);
}
}