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 java.util.ArrayList; |
19 | |
import java.util.Collections; |
20 | |
import java.util.Iterator; |
21 | |
import java.util.List; |
22 | |
|
23 | |
import org.apache.commons.collections.CollectionUtils; |
24 | |
import org.apache.commons.lang.StringUtils; |
25 | |
import org.apache.log4j.MDC; |
26 | |
import org.kuali.rice.kew.actionitem.ActionItem; |
27 | |
import org.kuali.rice.kew.actionrequest.ActionRequestValue; |
28 | |
import org.kuali.rice.kew.api.action.ActionRequestStatus; |
29 | |
import org.kuali.rice.kew.api.exception.WorkflowException; |
30 | |
import org.kuali.rice.kew.engine.RouteContext; |
31 | |
import org.kuali.rice.kew.engine.RouteHelper; |
32 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
33 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
34 | |
import org.kuali.rice.kew.api.KewApiConstants; |
35 | |
import org.kuali.rice.kew.util.PerformanceLogger; |
36 | |
import org.kuali.rice.kew.util.Utilities; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 0 | public class RequestActivationNode extends RequestActivationNodeBase { |
46 | |
|
47 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger( RequestActivationNode.class ); |
48 | 0 | private static long generatedRequestPriority = 0; |
49 | |
|
50 | |
public SimpleResult process(RouteContext routeContext, RouteHelper routeHelper) throws Exception { |
51 | 0 | DocumentRouteHeaderValue document = routeContext.getDocument(); |
52 | 0 | RouteNodeInstance nodeInstance = routeContext.getNodeInstance(); |
53 | 0 | if (routeContext.isSimulation()) { |
54 | 0 | if (routeContext.getActivationContext().isActivateRequests()) { |
55 | 0 | activateRequests(routeContext, document, nodeInstance); |
56 | |
} |
57 | 0 | return new SimpleResult(true); |
58 | 0 | } else if (!activateRequests(routeContext, document, nodeInstance) && shouldTransition(document, nodeInstance)) { |
59 | 0 | return new SimpleResult(true); |
60 | |
} else { |
61 | 0 | return new SimpleResult(false); |
62 | |
} |
63 | |
} |
64 | |
|
65 | |
public boolean shouldTransition(DocumentRouteHeaderValue document, RouteNodeInstance nodeInstance) { |
66 | 0 | List requests = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocIdAtRouteNode(document.getDocumentId(), nodeInstance.getRouteNodeInstanceId()); |
67 | 0 | boolean shouldTransition = true; |
68 | 0 | for (Iterator iterator = requests.iterator(); iterator.hasNext();) { |
69 | 0 | ActionRequestValue request = (ActionRequestValue) iterator.next(); |
70 | 0 | if (request.isApproveOrCompleteRequest()) { |
71 | 0 | shouldTransition = false; |
72 | 0 | break; |
73 | |
} |
74 | 0 | } |
75 | 0 | return shouldTransition; |
76 | |
} |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
public boolean activateRequests(RouteContext context, DocumentRouteHeaderValue document, RouteNodeInstance nodeInstance) throws WorkflowException { |
93 | 0 | MDC.put("docId", document.getDocumentId()); |
94 | 0 | PerformanceLogger performanceLogger = new PerformanceLogger(document.getDocumentId()); |
95 | 0 | List<ActionItem> generatedActionItems = new ArrayList<ActionItem>(); |
96 | 0 | List<ActionRequestValue> requests = new ArrayList<ActionRequestValue>(); |
97 | 0 | if (context.isSimulation()) { |
98 | 0 | for (ActionRequestValue ar : context.getDocument().getActionRequests()) { |
99 | |
|
100 | 0 | if (ar.getCurrentIndicator() |
101 | |
&& (ActionRequestStatus.INITIALIZED.getCode().equals(ar.getStatus()) || ActionRequestStatus.ACTIVATED.getCode().equals(ar.getStatus())) |
102 | |
&& ar.getNodeInstance().getRouteNodeInstanceId().equals(nodeInstance.getRouteNodeInstanceId()) |
103 | |
&& ar.getParentActionRequest() == null) { |
104 | 0 | requests.add(ar); |
105 | |
} |
106 | |
} |
107 | 0 | requests.addAll(context.getEngineState().getGeneratedRequests()); |
108 | |
} else { |
109 | 0 | requests = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocIdAtRouteNode(document.getDocumentId(), nodeInstance.getRouteNodeInstanceId()); |
110 | |
} |
111 | 0 | if ( LOG.isDebugEnabled() ) { |
112 | 0 | LOG.debug("Pending Root Requests " + requests.size()); |
113 | |
} |
114 | 0 | boolean activatedApproveRequest = activateRequestsCustom( context, requests, generatedActionItems, document, nodeInstance ); |
115 | |
|
116 | |
|
117 | |
|
118 | 0 | notify(context, generatedActionItems, nodeInstance); |
119 | |
|
120 | 0 | performanceLogger.log("Time to activate requests."); |
121 | 0 | return activatedApproveRequest; |
122 | |
} |
123 | |
|
124 | |
protected boolean activateRequestsCustom( RouteContext context, List<ActionRequestValue> requests, List<ActionItem> generatedActionItems, |
125 | |
DocumentRouteHeaderValue document, RouteNodeInstance nodeInstance) throws WorkflowException { |
126 | 0 | Collections.sort(requests, new Utilities.PrioritySorter()); |
127 | 0 | String activationType = nodeInstance.getRouteNode().getActivationType(); |
128 | 0 | if (StringUtils.isBlank(activationType)) { |
129 | |
|
130 | 0 | activationType = KewApiConstants.ROUTE_LEVEL_SEQUENCE; |
131 | |
} |
132 | 0 | boolean isParallel = KewApiConstants.ROUTE_LEVEL_PARALLEL.equals(activationType); |
133 | 0 | boolean isPriorityParallel = KewApiConstants.ROUTE_LEVEL_PRIORITY_PARALLEL.equals(activationType); |
134 | 0 | boolean isSequential = KewApiConstants.ROUTE_LEVEL_SEQUENCE.equals(activationType); |
135 | |
|
136 | 0 | boolean activatedApproveRequest = false; |
137 | 0 | if (CollectionUtils.isNotEmpty(requests)) { |
138 | |
|
139 | 0 | int currentPriority = requests.get(0).getPriority(); |
140 | 0 | for (ActionRequestValue request : requests ) { |
141 | 0 | if (request.getParentActionRequest() != null || request.getNodeInstance() == null) { |
142 | |
|
143 | |
|
144 | 0 | continue; |
145 | |
} |
146 | 0 | if (activatedApproveRequest && (!context.isSimulation() || !context.getActivationContext().isActivateRequests())) { |
147 | 0 | if (isSequential || (isPriorityParallel && request.getPriority() != currentPriority)) { |
148 | 0 | break; |
149 | |
} |
150 | |
} |
151 | 0 | currentPriority = request.getPriority(); |
152 | 0 | if (request.isActive()) { |
153 | 0 | activatedApproveRequest = activatedApproveRequest || request.isApproveOrCompleteRequest(); |
154 | 0 | continue; |
155 | |
} |
156 | 0 | logProcessingMessage(request); |
157 | 0 | if ( LOG.isDebugEnabled() ) { |
158 | 0 | LOG.debug("Activating request: " + request); |
159 | |
} |
160 | 0 | activatedApproveRequest = activateRequest(context, request, nodeInstance, generatedActionItems) || activatedApproveRequest; |
161 | |
} |
162 | |
} |
163 | 0 | return activatedApproveRequest; |
164 | |
} |
165 | |
|
166 | |
protected boolean activateRequest(RouteContext context, ActionRequestValue actionRequest, RouteNodeInstance nodeInstance, List generatedActionItems) { |
167 | 0 | if (actionRequest.isRoleRequest()) { |
168 | 0 | List actionRequests = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocIdAtRouteNode(actionRequest.getDocumentId(), nodeInstance.getRouteNodeInstanceId()); |
169 | 0 | for (Iterator iterator = actionRequests.iterator(); iterator.hasNext();) { |
170 | 0 | ActionRequestValue siblingRequest = (ActionRequestValue) iterator.next(); |
171 | 0 | if (actionRequest.getRoleName().equals(siblingRequest.getRoleName())) { |
172 | 0 | generatedActionItems.addAll(KEWServiceLocator.getActionRequestService().activateRequestNoNotification(siblingRequest, context.getActivationContext())); |
173 | |
} |
174 | 0 | } |
175 | |
} |
176 | 0 | generatedActionItems.addAll(KEWServiceLocator.getActionRequestService().activateRequestNoNotification(actionRequest, context.getActivationContext())); |
177 | 0 | return actionRequest.isApproveOrCompleteRequest() && ! actionRequest.isDone(); |
178 | |
} |
179 | |
|
180 | |
protected void saveActionRequest(RouteContext context, ActionRequestValue actionRequest) { |
181 | 0 | if (!context.isSimulation()) { |
182 | 0 | KEWServiceLocator.getActionRequestService().saveActionRequest(actionRequest); |
183 | |
} else { |
184 | 0 | actionRequest.setActionRequestId(String.valueOf(generatedRequestPriority++)); |
185 | 0 | context.getEngineState().getGeneratedRequests().add(actionRequest); |
186 | |
} |
187 | |
|
188 | 0 | } |
189 | |
|
190 | |
protected void saveDocument(RouteContext context, DocumentRouteHeaderValue document) { |
191 | 0 | if (!context.isSimulation()) { |
192 | 0 | KEWServiceLocator.getRouteHeaderService().saveRouteHeader(document); |
193 | |
} |
194 | 0 | } |
195 | |
|
196 | |
protected void logProcessingMessage(ActionRequestValue request) { |
197 | 0 | if (LOG.isDebugEnabled()) { |
198 | 0 | RouteNodeInstance nodeInstance = request.getNodeInstance(); |
199 | 0 | StringBuffer buffer = new StringBuffer(); |
200 | 0 | buffer.append("Processing AR: ").append(request.getActionRequestId()).append("\n"); |
201 | 0 | buffer.append("AR Node Name: ").append(nodeInstance != null ? nodeInstance.getName() : "null").append("\n"); |
202 | 0 | buffer.append("AR RouteLevel: ").append(request.getRouteLevel()).append("\n"); |
203 | 0 | buffer.append("AR Request Code: ").append(request.getActionRequested()).append("\n"); |
204 | 0 | buffer.append("AR Request priority: ").append(request.getPriority()).append("\n"); |
205 | 0 | LOG.debug(buffer); |
206 | |
} |
207 | 0 | } |
208 | |
|
209 | |
} |