1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kew.engine.node; |
18 | |
|
19 | |
import org.apache.log4j.MDC; |
20 | |
import org.kuali.rice.kew.actionrequest.ActionRequestValue; |
21 | |
import org.kuali.rice.kew.engine.RouteContext; |
22 | |
import org.kuali.rice.kew.engine.RouteHelper; |
23 | |
import org.kuali.rice.kew.exception.ResourceUnavailableException; |
24 | |
import org.kuali.rice.kew.exception.WorkflowException; |
25 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
26 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
27 | |
import org.kuali.rice.kew.util.KEWConstants; |
28 | |
import org.kuali.rice.kew.util.PerformanceLogger; |
29 | |
import org.kuali.rice.kew.util.Utilities; |
30 | |
|
31 | |
import java.util.ArrayList; |
32 | |
import java.util.Collections; |
33 | |
import java.util.Iterator; |
34 | |
import java.util.List; |
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 | 0 | public class IteratedRequestActivationNode implements SimpleNode { |
66 | |
|
67 | 0 | protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(getClass()); |
68 | 0 | private static long generatedRequestPriority = 0; |
69 | |
|
70 | |
protected static interface RequestFulfillmentCriteria { |
71 | |
public boolean pendingRequestsAreFulfilled(boolean activatedBlockingRequests, RouteContext routeContext); |
72 | |
} |
73 | |
|
74 | 0 | private static class AllBlockingRequestsCompleteCriteria implements RequestFulfillmentCriteria { |
75 | |
public boolean pendingRequestsAreFulfilled(boolean activatedBlockingRequests, RouteContext routeContext) { |
76 | |
|
77 | 0 | if (activatedBlockingRequests) { |
78 | |
|
79 | 0 | assert(activatedBlockingRequests == blockingRequestsArePending(routeContext.getDocument(), routeContext.getNodeInstance())) : "Blocking requests were activated but none were subsequently found"; |
80 | 0 | return false; |
81 | |
} else { |
82 | |
|
83 | 0 | return !blockingRequestsArePending(routeContext.getDocument(), routeContext.getNodeInstance()); |
84 | |
} |
85 | |
} |
86 | |
} |
87 | |
|
88 | 0 | private static class SimulatingCriteria implements RequestFulfillmentCriteria { |
89 | |
public boolean pendingRequestsAreFulfilled(boolean activatedBlockingRequests, RouteContext routeContext) { |
90 | |
|
91 | 0 | return true; |
92 | |
} |
93 | |
} |
94 | |
|
95 | |
public SimpleResult process(RouteContext routeContext, RouteHelper routeHelper) throws Exception { |
96 | |
|
97 | 0 | RequestFulfillmentCriteria criteria = getRequestFulfillmentCriteria(routeContext); |
98 | |
|
99 | |
|
100 | 0 | boolean activatedBlockingRequests = activateRequests(routeContext); |
101 | 0 | boolean initialRequestGeneration = routeContext.getNodeInstance().isInitial(); |
102 | 0 | while (criteria.pendingRequestsAreFulfilled(activatedBlockingRequests, routeContext)) { |
103 | 0 | boolean newRequestsGenerated = generateNewRequests(initialRequestGeneration, routeContext, routeHelper); |
104 | 0 | initialRequestGeneration = false; |
105 | |
|
106 | 0 | if (!newRequestsGenerated) { |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | 0 | return new SimpleResult(true); |
112 | |
} |
113 | |
|
114 | |
|
115 | 0 | activatedBlockingRequests = activateRequests(routeContext); |
116 | |
|
117 | |
|
118 | |
|
119 | 0 | if (routeContext.isSimulation()) { |
120 | 0 | criteria = new SimulatingCriteria(); |
121 | |
} else { |
122 | 0 | criteria = getRequestFulfillmentCriteria(routeContext); |
123 | |
} |
124 | 0 | } |
125 | |
|
126 | |
|
127 | 0 | return new SimpleResult(false); |
128 | |
} |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
protected RequestFulfillmentCriteria getRequestFulfillmentCriteria(RouteContext routeContext) { |
136 | 0 | return new AllBlockingRequestsCompleteCriteria(); |
137 | |
} |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
protected boolean generateNewRequests(boolean initial, RouteContext context, RouteHelper routeHelper) throws WorkflowException, Exception { |
146 | 0 | return false; |
147 | |
} |
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
protected boolean activateRequests(RouteContext routeContext) throws WorkflowException { |
156 | 0 | DocumentRouteHeaderValue document = routeContext.getDocument(); |
157 | 0 | RouteNodeInstance nodeInstance = routeContext.getNodeInstance(); |
158 | 0 | if (routeContext.isSimulation()) { |
159 | |
|
160 | 0 | if (routeContext.getActivationContext().isActivateRequests()) { |
161 | 0 | activateRequests(routeContext, document, nodeInstance); |
162 | |
} |
163 | |
|
164 | 0 | return false; |
165 | |
} else { |
166 | |
|
167 | 0 | return activateRequests(routeContext, document, nodeInstance); |
168 | |
} |
169 | |
} |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
private static boolean blockingRequestsArePending(DocumentRouteHeaderValue document, RouteNodeInstance nodeInstance) { |
175 | |
|
176 | 0 | List<ActionRequestValue> requests = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocIdAtRouteNode(document.getDocumentId(), nodeInstance.getRouteNodeInstanceId()); |
177 | 0 | boolean blockingRequestsArePending = false; |
178 | 0 | for (ActionRequestValue request: requests) { |
179 | 0 | if (request.isApproveOrCompleteRequest()) { |
180 | 0 | blockingRequestsArePending = true; |
181 | 0 | break; |
182 | |
} |
183 | |
} |
184 | 0 | return blockingRequestsArePending; |
185 | |
} |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
private boolean activateRequests(RouteContext context, DocumentRouteHeaderValue document, RouteNodeInstance nodeInstance) throws WorkflowException { |
206 | 0 | MDC.put("docId", document.getDocumentId()); |
207 | 0 | PerformanceLogger performanceLogger = new PerformanceLogger(document.getDocumentId()); |
208 | 0 | List generatedActionItems = new ArrayList(); |
209 | 0 | List<ActionRequestValue> requests = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocIdAtRouteNode(document.getDocumentId(), nodeInstance.getRouteNodeInstanceId()); |
210 | 0 | if (context.isSimulation()) { |
211 | 0 | requests.addAll(context.getEngineState().getGeneratedRequests()); |
212 | |
} |
213 | |
|
214 | |
|
215 | |
|
216 | 0 | Collections.sort(requests, new Utilities.PrioritySorter()); |
217 | 0 | LOG.info("Pending Root Requests " + requests.size()); |
218 | 0 | String activationType = nodeInstance.getRouteNode().getActivationType(); |
219 | 0 | boolean isParallel = KEWConstants.ROUTE_LEVEL_PARALLEL.equals(activationType); |
220 | 0 | boolean activatedApproveRequest = false; |
221 | 0 | for (Iterator iter = requests.iterator(); iter.hasNext();) { |
222 | 0 | if (activatedApproveRequest && !isParallel) { |
223 | 0 | LOG.info("Already activated an apprve request and serial, so not activating any more"); |
224 | 0 | break; |
225 | |
} |
226 | 0 | ActionRequestValue request = (ActionRequestValue) iter.next(); |
227 | 0 | LOG.info("ActionRequestValue: " + request); |
228 | 0 | if (request.getParentActionRequest() != null || request.getNodeInstance() == null) { |
229 | |
|
230 | |
|
231 | 0 | continue; |
232 | |
} |
233 | 0 | if (request.isActive()) { |
234 | 0 | activatedApproveRequest = activatedApproveRequest || request.isApproveOrCompleteRequest(); |
235 | 0 | continue; |
236 | |
} |
237 | 0 | logProcessingMessage(request); |
238 | 0 | LOG.info("Activating request. " + request); |
239 | 0 | activatedApproveRequest = activateRequest(context, request, nodeInstance, generatedActionItems) || activatedApproveRequest; |
240 | 0 | } |
241 | |
|
242 | |
|
243 | 0 | if (!context.isSimulation()) { |
244 | 0 | KEWServiceLocator.getNotificationService().notify(generatedActionItems); |
245 | |
} |
246 | 0 | performanceLogger.log("Time to activate requests."); |
247 | 0 | return activatedApproveRequest; |
248 | |
} |
249 | |
|
250 | |
private boolean activateRequest(RouteContext context, ActionRequestValue actionRequest, RouteNodeInstance nodeInstance, List generatedActionItems) { |
251 | 0 | if (actionRequest.isRoleRequest()) { |
252 | 0 | List actionRequests = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocIdAtRouteNode(actionRequest.getDocumentId(), nodeInstance.getRouteNodeInstanceId()); |
253 | 0 | for (Iterator iterator = actionRequests.iterator(); iterator.hasNext();) { |
254 | 0 | ActionRequestValue siblingRequest = (ActionRequestValue) iterator.next(); |
255 | 0 | if (actionRequest.getRoleName().equals(siblingRequest.getRoleName())) { |
256 | 0 | generatedActionItems.addAll(KEWServiceLocator.getActionRequestService().activateRequestNoNotification(siblingRequest, context.getActivationContext())); |
257 | |
} |
258 | 0 | } |
259 | |
} |
260 | 0 | generatedActionItems.addAll(KEWServiceLocator.getActionRequestService().activateRequestNoNotification(actionRequest, context.getActivationContext())); |
261 | 0 | return actionRequest.isApproveOrCompleteRequest() && ! actionRequest.isDone(); |
262 | |
} |
263 | |
|
264 | |
protected void saveActionRequest(RouteContext context, ActionRequestValue actionRequest) { |
265 | 0 | if (!context.isSimulation()) { |
266 | 0 | KEWServiceLocator.getActionRequestService().saveActionRequest(actionRequest); |
267 | |
} else { |
268 | 0 | actionRequest.setActionRequestId(String.valueOf(generatedRequestPriority++)); |
269 | 0 | context.getEngineState().getGeneratedRequests().add(actionRequest); |
270 | |
} |
271 | |
|
272 | 0 | } |
273 | |
|
274 | |
private void logProcessingMessage(ActionRequestValue request) { |
275 | |
|
276 | 0 | RouteNodeInstance nodeInstance = request.getNodeInstance(); |
277 | 0 | StringBuffer buffer = new StringBuffer(); |
278 | 0 | buffer.append("Processing AR: ").append(request.getActionRequestId()).append("\n"); |
279 | 0 | buffer.append("AR Node Name: ").append(nodeInstance != null ? nodeInstance.getName() : "null").append("\n"); |
280 | 0 | buffer.append("AR RouteLevel: ").append(request.getRouteLevel()).append("\n"); |
281 | 0 | buffer.append("AR Request Code: ").append(request.getActionRequested()).append("\n"); |
282 | 0 | buffer.append("AR Request priority: ").append(request.getPriority()).append("\n"); |
283 | 0 | LOG.info(buffer); |
284 | |
|
285 | 0 | } |
286 | |
|
287 | |
} |