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.commons.lang.StringUtils; |
19 | |
import org.apache.log4j.MDC; |
20 | |
import org.kuali.rice.core.api.criteria.Predicate; |
21 | |
import org.kuali.rice.core.api.criteria.QueryByCriteria; |
22 | |
import org.kuali.rice.kew.actionitem.ActionItem; |
23 | |
import org.kuali.rice.kew.actionrequest.ActionRequestValue; |
24 | |
import org.kuali.rice.kew.api.action.ActionRequestStatus; |
25 | |
import org.kuali.rice.kew.doctype.bo.DocumentType; |
26 | |
import org.kuali.rice.kew.engine.RouteContext; |
27 | |
import org.kuali.rice.kew.engine.RouteHelper; |
28 | |
import org.kuali.rice.kew.exception.RouteManagerException; |
29 | |
import org.kuali.rice.kew.api.exception.WorkflowException; |
30 | |
import org.kuali.rice.kew.role.RoleRouteModule; |
31 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
32 | |
import org.kuali.rice.kew.routemodule.RouteModule; |
33 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
34 | |
import org.kuali.rice.kew.util.ClassDumper; |
35 | |
import org.kuali.rice.kew.api.KewApiConstants; |
36 | |
import org.kuali.rice.kew.util.PerformanceLogger; |
37 | |
import org.kuali.rice.kim.api.KimConstants; |
38 | |
import org.kuali.rice.kim.api.responsibility.Responsibility; |
39 | |
import org.kuali.rice.kim.api.services.KimApiServiceLocator; |
40 | |
import org.kuali.rice.krad.util.KRADConstants; |
41 | |
|
42 | |
import java.util.ArrayList; |
43 | |
import java.util.Collections; |
44 | |
import java.util.Comparator; |
45 | |
import java.util.List; |
46 | |
|
47 | |
import static org.kuali.rice.core.api.criteria.PredicateFactory.and; |
48 | |
import static org.kuali.rice.core.api.criteria.PredicateFactory.equal; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | 0 | public class RoleNode extends RequestsNode { |
59 | |
|
60 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger |
61 | |
.getLogger( RoleNode.class ); |
62 | |
|
63 | |
@Override |
64 | |
protected RouteModule getRouteModule(RouteContext context) throws Exception { |
65 | 0 | return new RoleRouteModule(); |
66 | |
} |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
@Override |
72 | |
protected boolean processCustom(RouteContext routeContext, RouteHelper routeHelper) throws Exception { |
73 | 0 | DocumentRouteHeaderValue document = routeContext.getDocument(); |
74 | 0 | RouteNodeInstance nodeInstance = routeContext.getNodeInstance(); |
75 | 0 | RouteNode node = nodeInstance.getRouteNode(); |
76 | |
|
77 | |
|
78 | 0 | if ( nodeInstance.isInitial() ) { |
79 | 0 | if ( LOG.isDebugEnabled() ) { |
80 | 0 | LOG.debug( "RouteHeader info inside routing loop\n" |
81 | |
+ ClassDumper.dumpFields( routeContext.getDocument() ) ); |
82 | 0 | LOG.debug( "Looking for new actionRequests - routeLevel: " |
83 | |
+ node.getRouteNodeName() ); |
84 | |
} |
85 | 0 | boolean suppressPolicyErrors = isSupressingPolicyErrors( routeContext ); |
86 | 0 | List<ActionRequestValue> requests = getNewActionRequests( routeContext ); |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | 0 | if ( requests.isEmpty() && !suppressPolicyErrors) { |
94 | 0 | Responsibility resp = getFirstResponsibilityWithMandatoryRouteFlag( document, node ); |
95 | 0 | if ( resp != null ) { |
96 | 0 | throw new RouteManagerException( "No requests generated for KIM Responsibility-based mandatory route.\n" + |
97 | |
"Document Id: " + document.getDocumentId() + "\n" + |
98 | |
"DocumentType: " + document.getDocumentType().getName() + "\n" + |
99 | |
"Route Node: " + node.getRouteNodeName() + "\n" + |
100 | |
"Responsibility: " + resp, |
101 | |
routeContext ); |
102 | |
} |
103 | |
} |
104 | |
|
105 | |
|
106 | 0 | if ( !suppressPolicyErrors ) { |
107 | 0 | verifyFinalApprovalRequest( document, requests, nodeInstance, routeContext ); |
108 | |
} |
109 | |
} |
110 | 0 | return true; |
111 | |
} |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
protected Responsibility getFirstResponsibilityWithMandatoryRouteFlag( DocumentRouteHeaderValue document, RouteNode node ) { |
119 | |
|
120 | |
|
121 | 0 | DocumentType docType = document.getDocumentType(); |
122 | 0 | while ( docType != null ) { |
123 | 0 | QueryByCriteria.Builder builder = QueryByCriteria.Builder.create(); |
124 | 0 | Predicate p = and( |
125 | |
equal("template.namespaceCode", KRADConstants.KUALI_RICE_WORKFLOW_NAMESPACE), |
126 | |
equal("template.name", KewApiConstants.DEFAULT_RESPONSIBILITY_TEMPLATE_NAME), |
127 | |
equal("active", "Y"), |
128 | |
equal("attributes[documentTypeName]", docType.getName()), |
129 | |
equal("attributes[routeNodeName]", node.getRouteNodeName()) |
130 | |
); |
131 | 0 | builder.setPredicates(p); |
132 | |
|
133 | 0 | List<Responsibility> responsibilities = KimApiServiceLocator.getResponsibilityService().findResponsibilities(builder.build()).getResults(); |
134 | |
|
135 | |
|
136 | 0 | if ( !responsibilities.isEmpty() ) { |
137 | |
|
138 | 0 | for ( Responsibility resp : responsibilities ) { |
139 | 0 | if ( Boolean.parseBoolean( resp.getAttributes().get( KimConstants.AttributeConstants.REQUIRED ) ) ) { |
140 | 0 | return resp; |
141 | |
} |
142 | |
} |
143 | 0 | return null; |
144 | |
} |
145 | 0 | docType = docType.getParentDocType(); |
146 | 0 | } |
147 | |
|
148 | 0 | return null; |
149 | |
} |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
@SuppressWarnings("unchecked") |
170 | |
public boolean activateRequests(RouteContext context, DocumentRouteHeaderValue document, |
171 | |
RouteNodeInstance nodeInstance) throws WorkflowException { |
172 | 0 | MDC.put( "docId", document.getDocumentId() ); |
173 | 0 | PerformanceLogger performanceLogger = new PerformanceLogger( document.getDocumentId() ); |
174 | 0 | List<ActionItem> generatedActionItems = new ArrayList<ActionItem>(); |
175 | 0 | List<ActionRequestValue> requests = new ArrayList<ActionRequestValue>(); |
176 | 0 | if ( context.isSimulation() ) { |
177 | 0 | for ( ActionRequestValue ar : context.getDocument().getActionRequests() ) { |
178 | |
|
179 | |
|
180 | |
|
181 | 0 | if ( ar.getCurrentIndicator() |
182 | |
&& (ActionRequestStatus.INITIALIZED.getCode().equals( ar.getStatus() ) || ActionRequestStatus.ACTIVATED.getCode() |
183 | |
.equals( ar.getStatus() )) |
184 | |
&& ar.getNodeInstance().getRouteNodeInstanceId().equals( |
185 | |
nodeInstance.getRouteNodeInstanceId() ) |
186 | |
&& ar.getParentActionRequest() == null ) { |
187 | 0 | requests.add( ar ); |
188 | |
} |
189 | |
} |
190 | 0 | requests.addAll( context.getEngineState().getGeneratedRequests() ); |
191 | |
} else { |
192 | 0 | requests = KEWServiceLocator.getActionRequestService() |
193 | |
.findPendingRootRequestsByDocIdAtRouteNode( document.getDocumentId(), |
194 | |
nodeInstance.getRouteNodeInstanceId() ); |
195 | |
} |
196 | 0 | if ( LOG.isDebugEnabled() ) { |
197 | 0 | LOG.debug( "Pending Root Requests " + requests.size() ); |
198 | |
} |
199 | 0 | boolean requestActivated = activateRequestsCustom( context, requests, generatedActionItems, |
200 | |
document, nodeInstance ); |
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | 0 | notify(context, generatedActionItems, nodeInstance); |
206 | |
|
207 | 0 | performanceLogger.log( "Time to activate requests." ); |
208 | 0 | return requestActivated; |
209 | |
} |
210 | |
|
211 | 0 | protected static class RoleRequestSorter implements Comparator<ActionRequestValue> { |
212 | |
public int compare(ActionRequestValue ar1, ActionRequestValue ar2) { |
213 | 0 | int result = 0; |
214 | |
|
215 | 0 | if ( ar1.getResponsibilityDesc() != null && ar2.getResponsibilityDesc() != null ) { |
216 | 0 | result = ar1.getResponsibilityDesc().compareTo( ar2.getResponsibilityDesc() ); |
217 | |
} |
218 | 0 | if ( result != 0 ) return result; |
219 | |
|
220 | 0 | result = ar1.getPriority().compareTo(ar2.getPriority()); |
221 | 0 | if ( result != 0 ) return result; |
222 | |
|
223 | 0 | result = ActionRequestValue.compareActionCode(ar1.getActionRequested(), ar2.getActionRequested(), true); |
224 | 0 | if ( result != 0 ) return result; |
225 | |
|
226 | 0 | if ( (ar1.getActionRequestId() != null) && (ar2.getActionRequestId() != null) ) { |
227 | 0 | result = ar1.getActionRequestId().compareTo(ar2.getActionRequestId()); |
228 | |
} else { |
229 | |
|
230 | 0 | result = 0; |
231 | |
} |
232 | 0 | return result; |
233 | |
} |
234 | |
} |
235 | 0 | protected static final Comparator<ActionRequestValue> ROLE_REQUEST_SORTER = new RoleRequestSorter(); |
236 | |
|
237 | |
|
238 | |
protected boolean activateRequestsCustom(RouteContext context, |
239 | |
List<ActionRequestValue> requests, List<ActionItem> generatedActionItems, |
240 | |
DocumentRouteHeaderValue document, RouteNodeInstance nodeInstance) |
241 | |
throws WorkflowException { |
242 | 0 | Collections.sort( requests, ROLE_REQUEST_SORTER ); |
243 | 0 | String activationType = nodeInstance.getRouteNode().getActivationType(); |
244 | 0 | boolean isParallel = KewApiConstants.ROUTE_LEVEL_PARALLEL.equals( activationType ); |
245 | 0 | boolean requestActivated = false; |
246 | 0 | String groupToActivate = null; |
247 | 0 | Integer priorityToActivate = null; |
248 | 0 | for ( ActionRequestValue request : requests ) { |
249 | |
|
250 | |
|
251 | 0 | if ( requestActivated |
252 | |
&& !isParallel |
253 | |
&& (!context.isSimulation() || !context.getActivationContext() |
254 | |
.isActivateRequests()) ) { |
255 | 0 | break; |
256 | |
} |
257 | 0 | if ( request.getParentActionRequest() != null || request.getNodeInstance() == null ) { |
258 | |
|
259 | |
|
260 | |
|
261 | 0 | continue; |
262 | |
} |
263 | 0 | if ( request.isApproveOrCompleteRequest() ) { |
264 | 0 | boolean thisRequestActivated = false; |
265 | |
|
266 | |
|
267 | 0 | if ( priorityToActivate == null ) { |
268 | 0 | priorityToActivate = request.getPriority(); |
269 | |
} |
270 | 0 | if ( groupToActivate == null ) { |
271 | 0 | groupToActivate = request.getResponsibilityDesc(); |
272 | |
} |
273 | |
|
274 | |
|
275 | 0 | if ( StringUtils.equals( groupToActivate, request.getResponsibilityDesc() ) |
276 | |
&& ( |
277 | |
(priorityToActivate != null && request.getPriority() != null && priorityToActivate.equals(request.getPriority())) |
278 | |
|| (priorityToActivate == null && request.getPriority() == null) |
279 | |
) |
280 | |
) { |
281 | |
|
282 | |
|
283 | 0 | if ( request.isActive() ) { |
284 | 0 | requestActivated = true; |
285 | 0 | continue; |
286 | |
} |
287 | 0 | logProcessingMessage( request ); |
288 | 0 | if ( LOG.isDebugEnabled() ) { |
289 | 0 | LOG.debug( "Activating request: " + request ); |
290 | |
} |
291 | |
|
292 | 0 | thisRequestActivated = activateRequest( context, request, nodeInstance, |
293 | |
generatedActionItems ); |
294 | 0 | requestActivated |= thisRequestActivated; |
295 | |
} |
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | 0 | if ( !thisRequestActivated && !requestActivated ) { |
302 | 0 | priorityToActivate = null; |
303 | 0 | groupToActivate = null; |
304 | |
} |
305 | 0 | } else { |
306 | 0 | logProcessingMessage( request ); |
307 | 0 | if ( LOG.isDebugEnabled() ) { |
308 | 0 | LOG.debug( "Activating request: " + request ); |
309 | |
} |
310 | 0 | requestActivated = activateRequest( context, request, nodeInstance, |
311 | |
generatedActionItems ) |
312 | |
|| requestActivated; |
313 | |
} |
314 | |
} |
315 | 0 | return requestActivated; |
316 | |
} |
317 | |
} |