1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kew.engine; |
18 | |
|
19 | |
import java.util.ArrayList; |
20 | |
import java.util.HashSet; |
21 | |
import java.util.Iterator; |
22 | |
import java.util.LinkedList; |
23 | |
import java.util.List; |
24 | |
import java.util.Set; |
25 | |
|
26 | |
import org.apache.log4j.MDC; |
27 | |
import org.kuali.rice.core.framework.parameter.ParameterService; |
28 | |
import org.kuali.rice.kew.actionrequest.ActionRequestFactory; |
29 | |
import org.kuali.rice.kew.actionrequest.ActionRequestValue; |
30 | |
import org.kuali.rice.kew.actionrequest.KimPrincipalRecipient; |
31 | |
import org.kuali.rice.kew.actionrequest.service.ActionRequestService; |
32 | |
import org.kuali.rice.kew.actions.NotificationContext; |
33 | |
import org.kuali.rice.kew.actiontaken.ActionTakenValue; |
34 | |
import org.kuali.rice.kew.api.WorkflowRuntimeException; |
35 | |
import org.kuali.rice.kew.engine.node.Process; |
36 | |
import org.kuali.rice.kew.engine.node.RouteNode; |
37 | |
import org.kuali.rice.kew.engine.node.RouteNodeInstance; |
38 | |
import org.kuali.rice.kew.engine.node.service.RouteNodeService; |
39 | |
import org.kuali.rice.kew.exception.InvalidActionTakenException; |
40 | |
import org.kuali.rice.kew.exception.WorkflowException; |
41 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
42 | |
import org.kuali.rice.kew.routeheader.service.RouteHeaderService; |
43 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
44 | |
import org.kuali.rice.kew.util.KEWConstants; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
public class BlanketApproveEngine extends StandardWorkflowEngine { |
53 | |
|
54 | 1 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BlanketApproveEngine.class); |
55 | |
|
56 | |
|
57 | |
BlanketApproveEngine(RouteNodeService routeNodeService, RouteHeaderService routeHeaderService, |
58 | |
ParameterService parameterService, OrchestrationConfig config) { |
59 | 1 | super(routeNodeService, routeHeaderService, parameterService, config); |
60 | 1 | } |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public void process(String documentId, String nodeInstanceId) throws Exception { |
66 | 0 | if (documentId == null) { |
67 | 0 | throw new IllegalArgumentException("Cannot process a null document id."); |
68 | |
} |
69 | 0 | MDC.put("docId", documentId); |
70 | 0 | RouteContext context = RouteContext.getCurrentRouteContext(); |
71 | |
try { |
72 | 0 | KEWServiceLocator.getRouteHeaderService().lockRouteHeader(documentId, true); |
73 | 0 | if ( LOG.isInfoEnabled() ) { |
74 | 0 | LOG.info("Processing document for Blanket Approval: " + documentId + " : " + nodeInstanceId); |
75 | |
} |
76 | 0 | DocumentRouteHeaderValue document = getRouteHeaderService().getRouteHeader(documentId); |
77 | 0 | if (!document.isRoutable()) { |
78 | 0 | LOG.debug("Document not routable so returning with doing no action"); |
79 | |
return; |
80 | |
} |
81 | 0 | List<RouteNodeInstance> activeNodeInstances = new ArrayList<RouteNodeInstance>(); |
82 | 0 | if (nodeInstanceId == null) { |
83 | 0 | activeNodeInstances.addAll(getRouteNodeService().getActiveNodeInstances(documentId)); |
84 | |
} else { |
85 | 0 | RouteNodeInstance instanceNode = getRouteNodeService().findRouteNodeInstanceById(nodeInstanceId); |
86 | 0 | if (instanceNode == null) { |
87 | 0 | throw new IllegalArgumentException("Invalid node instance id: " + nodeInstanceId); |
88 | |
} |
89 | 0 | activeNodeInstances.add(instanceNode); |
90 | |
} |
91 | 0 | List<RouteNodeInstance> nodeInstancesToProcess = determineNodeInstancesToProcess(activeNodeInstances, config.getDestinationNodeNames()); |
92 | |
|
93 | |
|
94 | 0 | context.setDoNotSendApproveNotificationEmails(true); |
95 | 0 | context.setDocument(document); |
96 | 0 | context.setEngineState(new EngineState()); |
97 | 0 | NotificationContext notifyContext = null; |
98 | 0 | if (config.isSendNotifications()) { |
99 | 0 | notifyContext = new NotificationContext(KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, config.getCause().getPrincipal(), config.getCause().getActionTaken()); |
100 | |
} |
101 | 0 | lockAdditionalDocuments(document); |
102 | |
try { |
103 | 0 | List<ProcessEntry> processingQueue = new LinkedList<ProcessEntry>(); |
104 | 0 | for (RouteNodeInstance nodeInstancesToProcesses : nodeInstancesToProcess) |
105 | |
{ |
106 | 0 | processingQueue.add(new ProcessEntry((RouteNodeInstance) nodeInstancesToProcesses)); |
107 | |
} |
108 | 0 | Set<String> nodesCompleted = new HashSet<String>(); |
109 | |
|
110 | |
|
111 | 0 | while (!processingQueue.isEmpty() && !isReachedDestinationNodes(config.getDestinationNodeNames(), nodesCompleted)) { |
112 | 0 | ProcessEntry entry = processingQueue.remove(0); |
113 | |
|
114 | |
|
115 | |
|
116 | 0 | if (entry.getTimesProcessed() > 20) { |
117 | 0 | throw new WorkflowException("Could not process document through to blanket approval." + " Document failed to progress past node " + entry.getNodeInstance().getRouteNode().getRouteNodeName()); |
118 | |
} |
119 | 0 | RouteNodeInstance nodeInstance = entry.getNodeInstance(); |
120 | 0 | context.setNodeInstance(nodeInstance); |
121 | 0 | if (config.getDestinationNodeNames().contains(nodeInstance.getName())) { |
122 | 0 | nodesCompleted.add(nodeInstance.getName()); |
123 | 0 | continue; |
124 | |
} |
125 | 0 | ProcessContext resultProcessContext = processNodeInstance(context, helper); |
126 | 0 | invokeBlanketApproval(config.getCause(), nodeInstance, notifyContext); |
127 | 0 | if (!resultProcessContext.getNextNodeInstances().isEmpty() || resultProcessContext.isComplete()) { |
128 | 0 | for (Iterator nodeIt = resultProcessContext.getNextNodeInstances().iterator(); nodeIt.hasNext();) { |
129 | 0 | addToProcessingQueue(processingQueue, (RouteNodeInstance) nodeIt.next()); |
130 | |
} |
131 | |
} else { |
132 | 0 | entry.increment(); |
133 | 0 | processingQueue.add(processingQueue.size(), entry); |
134 | |
} |
135 | 0 | } |
136 | |
|
137 | 0 | RouteContext.clearCurrentRouteContext(); |
138 | |
|
139 | |
|
140 | 0 | super.process(documentId, null); |
141 | 0 | } catch (Exception e) { |
142 | 0 | if (e instanceof RuntimeException) { |
143 | 0 | throw (RuntimeException)e; |
144 | |
} else { |
145 | 0 | throw new WorkflowRuntimeException(e.toString(), e); |
146 | |
} |
147 | 0 | } |
148 | |
} finally { |
149 | 0 | RouteContext.clearCurrentRouteContext(); |
150 | 0 | MDC.remove("docId"); |
151 | 0 | } |
152 | 0 | } |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
private boolean isReachedDestinationNodes(Set destinationNodesNames, Set<String> nodeNamesCompleted) { |
158 | 0 | return !destinationNodesNames.isEmpty() && nodeNamesCompleted.equals(destinationNodesNames); |
159 | |
} |
160 | |
|
161 | |
private void addToProcessingQueue(List<ProcessEntry> processingQueue, RouteNodeInstance nodeInstance) { |
162 | |
|
163 | 0 | for (ProcessEntry entry : processingQueue) |
164 | |
{ |
165 | 0 | if (entry.getNodeInstance().getRouteNodeInstanceId().equals(nodeInstance.getRouteNodeInstanceId())) |
166 | |
{ |
167 | 0 | entry.setNodeInstance(nodeInstance); |
168 | 0 | return; |
169 | |
} |
170 | |
} |
171 | 0 | processingQueue.add(processingQueue.size(), new ProcessEntry(nodeInstance)); |
172 | 0 | } |
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
private List<RouteNodeInstance> determineNodeInstancesToProcess(List<RouteNodeInstance> activeNodeInstances, Set nodeNames) throws Exception { |
178 | 0 | if (nodeNames.isEmpty()) { |
179 | 0 | return activeNodeInstances; |
180 | |
} |
181 | 0 | List<RouteNodeInstance> nodeInstancesToProcess = new ArrayList<RouteNodeInstance>(); |
182 | 0 | for (Iterator<RouteNodeInstance> iterator = activeNodeInstances.iterator(); iterator.hasNext();) { |
183 | 0 | RouteNodeInstance nodeInstance = (RouteNodeInstance) iterator.next(); |
184 | 0 | if (isNodeNameInPath(nodeNames, nodeInstance)) { |
185 | 0 | nodeInstancesToProcess.add(nodeInstance); |
186 | |
} |
187 | 0 | } |
188 | 0 | if (nodeInstancesToProcess.size() == 0) { |
189 | 0 | throw new InvalidActionTakenException("Could not locate nodes with the given names in the blanket approval path '" + printNodeNames(nodeNames) + "'. " + "The document is probably already passed the specified nodes or does not contain the nodes."); |
190 | |
} |
191 | 0 | return nodeInstancesToProcess; |
192 | |
} |
193 | |
|
194 | |
private boolean isNodeNameInPath(Set nodeNames, RouteNodeInstance nodeInstance) throws Exception { |
195 | 0 | boolean isInPath = false; |
196 | 0 | for (Object nodeName1 : nodeNames) |
197 | |
{ |
198 | 0 | String nodeName = (String) nodeName1; |
199 | 0 | for (RouteNode nextNode : nodeInstance.getRouteNode().getNextNodes()) |
200 | |
{ |
201 | 0 | isInPath = isInPath || isNodeNameInPath(nodeName, nextNode, new HashSet<String>()); |
202 | |
} |
203 | 0 | } |
204 | 0 | return isInPath; |
205 | |
} |
206 | |
|
207 | |
private boolean isNodeNameInPath(String nodeName, RouteNode node, Set<String> inspected) throws Exception { |
208 | 0 | boolean isInPath = !inspected.contains(node.getRouteNodeId()) && node.getRouteNodeName().equals(nodeName); |
209 | 0 | inspected.add(node.getRouteNodeId()); |
210 | 0 | if (helper.isSubProcessNode(node)) { |
211 | 0 | Process subProcess = node.getDocumentType().getNamedProcess(node.getRouteNodeName()); |
212 | 0 | RouteNode subNode = subProcess.getInitialRouteNode(); |
213 | 0 | isInPath = isInPath || isNodeNameInPath(nodeName, subNode, inspected); |
214 | |
} |
215 | 0 | for (RouteNode nextNode : node.getNextNodes()) |
216 | |
{ |
217 | 0 | isInPath = isInPath || isNodeNameInPath(nodeName, nextNode, inspected); |
218 | |
} |
219 | 0 | return isInPath; |
220 | |
} |
221 | |
|
222 | |
private String printNodeNames(Set nodesNames) { |
223 | 0 | StringBuffer buffer = new StringBuffer(); |
224 | 0 | for (Iterator iterator = nodesNames.iterator(); iterator.hasNext();) { |
225 | 0 | String nodeName = (String) iterator.next(); |
226 | 0 | buffer.append(nodeName); |
227 | 0 | buffer.append((iterator.hasNext() ? ", " : "")); |
228 | 0 | } |
229 | 0 | return buffer.toString(); |
230 | |
} |
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
private void invokeBlanketApproval(ActionTakenValue actionTaken, RouteNodeInstance nodeInstance, NotificationContext notifyContext) { |
236 | 0 | List actionRequests = getActionRequestService().findPendingRootRequestsByDocIdAtRouteNode(nodeInstance.getDocumentId(), nodeInstance.getRouteNodeInstanceId()); |
237 | 0 | actionRequests = getActionRequestService().getRootRequests(actionRequests); |
238 | 0 | List<ActionRequestValue> requestsToNotify = new ArrayList<ActionRequestValue>(); |
239 | 0 | for (Iterator iterator = actionRequests.iterator(); iterator.hasNext();) { |
240 | 0 | ActionRequestValue request = (ActionRequestValue) iterator.next(); |
241 | 0 | if (request.isApproveOrCompleteRequest()) { |
242 | 0 | getActionRequestService().deactivateRequest(actionTaken, request); |
243 | 0 | requestsToNotify.add(request); |
244 | |
} |
245 | 0 | } |
246 | 0 | if (notifyContext != null) { |
247 | 0 | ActionRequestFactory arFactory = new ActionRequestFactory(RouteContext.getCurrentRouteContext().getDocument(), nodeInstance); |
248 | 0 | KimPrincipalRecipient delegatorRecipient = null; |
249 | 0 | if (actionTaken.getDelegatorPrincipal() != null) { |
250 | 0 | delegatorRecipient = new KimPrincipalRecipient(actionTaken.getDelegatorPrincipal()); |
251 | |
} |
252 | 0 | List<ActionRequestValue> notificationRequests = arFactory.generateNotifications(requestsToNotify, notifyContext.getPrincipalTakingAction(), delegatorRecipient, notifyContext.getNotificationRequestCode(), notifyContext.getActionTakenCode()); |
253 | 0 | getActionRequestService().activateRequests(notificationRequests); |
254 | |
} |
255 | 0 | } |
256 | |
|
257 | |
private ActionRequestService getActionRequestService() { |
258 | 0 | return KEWServiceLocator.getActionRequestService(); |
259 | |
} |
260 | |
|
261 | |
private class ProcessEntry { |
262 | |
|
263 | |
private RouteNodeInstance nodeInstance; |
264 | 0 | private int timesProcessed = 0; |
265 | |
|
266 | 0 | public ProcessEntry(RouteNodeInstance nodeInstance) { |
267 | 0 | this.nodeInstance = nodeInstance; |
268 | 0 | } |
269 | |
|
270 | |
public RouteNodeInstance getNodeInstance() { |
271 | 0 | return nodeInstance; |
272 | |
} |
273 | |
|
274 | |
public void setNodeInstance(RouteNodeInstance nodeInstance) { |
275 | 0 | this.nodeInstance = nodeInstance; |
276 | 0 | } |
277 | |
|
278 | |
public void increment() { |
279 | 0 | timesProcessed++; |
280 | 0 | } |
281 | |
|
282 | |
public int getTimesProcessed() { |
283 | 0 | return timesProcessed; |
284 | |
} |
285 | |
|
286 | |
} |
287 | |
|
288 | |
} |