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