1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kew.rule; |
18 | |
|
19 | |
import org.apache.commons.lang.ObjectUtils; |
20 | |
import org.apache.commons.lang.StringUtils; |
21 | |
import org.apache.log4j.Logger; |
22 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
23 | |
import org.kuali.rice.core.util.ClassLoaderUtils; |
24 | |
import org.kuali.rice.kew.actionrequest.*; |
25 | |
import org.kuali.rice.kew.actionrequest.service.ActionRequestService; |
26 | |
import org.kuali.rice.kew.engine.RouteContext; |
27 | |
import org.kuali.rice.kew.engine.node.NodeState; |
28 | |
import org.kuali.rice.kew.engine.node.RouteNode; |
29 | |
import org.kuali.rice.kew.engine.node.RouteNodeInstance; |
30 | |
import org.kuali.rice.kew.exception.WorkflowException; |
31 | |
import org.kuali.rice.kew.exception.WorkflowRuntimeException; |
32 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
33 | |
import org.kuali.rice.kew.rule.bo.RuleAttribute; |
34 | |
import org.kuali.rice.kew.rule.bo.RuleTemplate; |
35 | |
import org.kuali.rice.kew.rule.bo.RuleTemplateAttribute; |
36 | |
import org.kuali.rice.kew.rule.service.RuleDelegationService; |
37 | |
import org.kuali.rice.kew.rule.service.RuleService; |
38 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
39 | |
import org.kuali.rice.kew.user.RoleRecipient; |
40 | |
import org.kuali.rice.kew.util.KEWConstants; |
41 | |
import org.kuali.rice.kew.util.PerformanceLogger; |
42 | |
import org.kuali.rice.kew.util.ResponsibleParty; |
43 | |
import org.kuali.rice.kew.util.Utilities; |
44 | |
|
45 | |
import java.lang.reflect.Method; |
46 | |
import java.sql.Timestamp; |
47 | |
import java.util.ArrayList; |
48 | |
import java.util.Date; |
49 | |
import java.util.List; |
50 | |
import java.util.Map; |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public class FlexRM { |
64 | |
|
65 | 0 | private static final Logger LOG = Logger.getLogger(FlexRM.class); |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
public static final String DEFAULT_RULE_SELECTOR = "Template"; |
72 | |
|
73 | |
|
74 | |
|
75 | |
private static final String RULE_SELECTOR_PACKAGE = "org.kuali.rice.kew.rule"; |
76 | |
|
77 | |
|
78 | |
|
79 | |
private static final String RULE_SELECTOR_SUFFIX= "RuleSelector"; |
80 | |
|
81 | |
private final Timestamp effectiveDate; |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
private int selectedRules; |
87 | |
|
88 | 0 | public FlexRM() { |
89 | 0 | this.effectiveDate = null; |
90 | 0 | } |
91 | |
|
92 | 0 | public FlexRM(Timestamp effectiveDate) { |
93 | 0 | this.effectiveDate = effectiveDate; |
94 | 0 | } |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
protected RuleSelector loadRuleSelector(RouteNode routeNodeDef, RouteNodeInstance nodeInstance) throws WorkflowException { |
102 | |
|
103 | 0 | NodeState ns = null; |
104 | 0 | if (nodeInstance != null) { |
105 | 0 | ns = nodeInstance.getNodeState(KEWConstants.RULE_SELECTOR_NODE_STATE_KEY); |
106 | |
} |
107 | 0 | String ruleSelectorName = null; |
108 | 0 | if (ns != null) { |
109 | 0 | ruleSelectorName = ns.getValue(); |
110 | |
} else { |
111 | |
|
112 | 0 | Map<String, String> nodeCfgParams = Utilities.getKeyValueCollectionAsMap( |
113 | |
routeNodeDef. |
114 | |
getConfigParams()); |
115 | 0 | ruleSelectorName = nodeCfgParams.get(RouteNode.RULE_SELECTOR_CFG_KEY); |
116 | |
} |
117 | |
|
118 | 0 | if (ruleSelectorName == null) { |
119 | 0 | ruleSelectorName = DEFAULT_RULE_SELECTOR; |
120 | |
} |
121 | 0 | ruleSelectorName = StringUtils.capitalize(ruleSelectorName); |
122 | |
|
123 | |
|
124 | 0 | String className = RULE_SELECTOR_PACKAGE + "." + ruleSelectorName + RULE_SELECTOR_SUFFIX; |
125 | |
Class<?> ruleSelectorClass; |
126 | |
try { |
127 | 0 | ruleSelectorClass = ClassLoaderUtils.getDefaultClassLoader().loadClass(className); |
128 | 0 | } catch (ClassNotFoundException cnfe) { |
129 | 0 | throw new WorkflowException("Rule selector implementation '" + className + "' not found", cnfe); |
130 | 0 | } |
131 | 0 | if (!RuleSelector.class.isAssignableFrom(ruleSelectorClass)) { |
132 | 0 | throw new WorkflowException("Specified class '" + ruleSelectorClass + "' does not implement RuleSelector interface"); |
133 | |
} |
134 | |
RuleSelector ruleSelector; |
135 | |
try { |
136 | 0 | ruleSelector = ((Class<RuleSelector>) ruleSelectorClass).newInstance(); |
137 | 0 | } catch (Exception e) { |
138 | 0 | if (e instanceof RuntimeException) { |
139 | 0 | throw (RuntimeException)e; |
140 | |
} |
141 | 0 | throw new WorkflowException("Error instantiating rule selector implementation '" + ruleSelectorClass + "'", e); |
142 | 0 | } |
143 | |
|
144 | 0 | return ruleSelector; |
145 | |
} |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
public List<ActionRequestValue> getActionRequests(DocumentRouteHeaderValue routeHeader, RouteNodeInstance nodeInstance, String ruleTemplateName) |
156 | |
throws WorkflowException { |
157 | 0 | return getActionRequests(routeHeader, nodeInstance.getRouteNode(), nodeInstance, ruleTemplateName); |
158 | |
} |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
public List<ActionRequestValue> getActionRequests(DocumentRouteHeaderValue routeHeader, RouteNode routeNodeDef, RouteNodeInstance nodeInstance, String ruleTemplateName) |
170 | |
throws WorkflowException { |
171 | 0 | RouteContext context = RouteContext.getCurrentRouteContext(); |
172 | |
|
173 | |
|
174 | 0 | if (context.getDocument() == null) { |
175 | 0 | context.setDocument(routeHeader); |
176 | |
} |
177 | 0 | if (context.getNodeInstance() == null) { |
178 | 0 | context.setNodeInstance(nodeInstance); |
179 | |
} |
180 | |
|
181 | 0 | LOG.debug("Making action requests for document " + routeHeader.getDocumentId()); |
182 | |
|
183 | 0 | RuleSelector ruleSelector = loadRuleSelector(routeNodeDef, nodeInstance); |
184 | |
|
185 | 0 | List<Rule> rules = ruleSelector.selectRules(context, routeHeader, nodeInstance, ruleTemplateName, effectiveDate); |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | 0 | if (ruleSelector instanceof TemplateRuleSelector) { |
196 | 0 | selectedRules += ((TemplateRuleSelector) ruleSelector).getNumberOfSelectedRules(); |
197 | |
} |
198 | |
|
199 | 0 | PerformanceLogger performanceLogger = new PerformanceLogger(); |
200 | |
|
201 | 0 | ActionRequestFactory arFactory = new ActionRequestFactory(routeHeader, context.getNodeInstance()); |
202 | |
|
203 | 0 | List<ActionRequestValue> actionRequests = new ArrayList<ActionRequestValue>(); |
204 | 0 | if (rules != null) { |
205 | 0 | LOG.info("Total number of rules selected by RuleSelector for documentType=" + routeHeader.getDocumentType().getName() + " and ruleTemplate=" + ruleTemplateName + ": " + rules.size()); |
206 | 0 | for (Rule rule: rules) { |
207 | 0 | RuleExpressionResult result = rule.evaluate(rule, context); |
208 | 0 | if (result.isSuccess() && result.getResponsibilities() != null) { |
209 | |
|
210 | 0 | makeActionRequests(arFactory, result.getResponsibilities(), context, rule.getDefinition(), routeHeader, null, null); |
211 | |
} |
212 | 0 | } |
213 | |
} |
214 | 0 | actionRequests = new ArrayList<ActionRequestValue>(arFactory.getRequestGraphs()); |
215 | 0 | performanceLogger.log("Time to make action request for template " + ruleTemplateName); |
216 | |
|
217 | 0 | return actionRequests; |
218 | |
} |
219 | |
|
220 | |
public ResponsibleParty resolveResponsibilityId(Long responsibilityId) { |
221 | 0 | if (responsibilityId == null) { |
222 | 0 | throw new IllegalArgumentException("A null responsibilityId was passed to resolve responsibility!"); |
223 | |
} |
224 | 0 | RuleResponsibility resp = getRuleService().findRuleResponsibility(responsibilityId); |
225 | 0 | ResponsibleParty responsibleParty = new ResponsibleParty(); |
226 | 0 | if (resp!=null && resp.isUsingRole()) { |
227 | 0 | responsibleParty.setRoleName(resp.getResolvedRoleName()); |
228 | 0 | } else if (resp!=null && resp.isUsingWorkflowUser()) { |
229 | 0 | responsibleParty.setPrincipalId(resp.getRuleResponsibilityName()); |
230 | 0 | } else if (resp!=null && resp.isUsingGroup()) { |
231 | 0 | responsibleParty.setGroupId(resp.getRuleResponsibilityName()); |
232 | |
} else { |
233 | 0 | throw new RiceRuntimeException("Failed to resolve responsibility from responsibility ID " + responsibilityId + ". Responsibility was an invalid type: " + resp); |
234 | |
} |
235 | 0 | return responsibleParty; |
236 | |
} |
237 | |
|
238 | |
private void makeActionRequests(ActionRequestFactory arFactory, RouteContext context, RuleBaseValues rule, DocumentRouteHeaderValue routeHeader, ActionRequestValue parentRequest, RuleDelegation ruleDelegation) |
239 | |
throws WorkflowException { |
240 | |
|
241 | 0 | List<RuleResponsibility> responsibilities = rule.getResponsibilities(); |
242 | 0 | makeActionRequests(arFactory, responsibilities, context, rule, routeHeader, parentRequest, ruleDelegation); |
243 | 0 | } |
244 | |
|
245 | |
public void makeActionRequests(ActionRequestFactory arFactory, List<RuleResponsibility> responsibilities, RouteContext context, RuleBaseValues rule, DocumentRouteHeaderValue routeHeader, ActionRequestValue parentRequest, RuleDelegation ruleDelegation) |
246 | |
throws WorkflowException { |
247 | |
|
248 | |
|
249 | 0 | for (RuleResponsibility responsibility : responsibilities) |
250 | |
{ |
251 | |
|
252 | |
|
253 | 0 | if (responsibility.isUsingRole()) |
254 | |
{ |
255 | 0 | makeRoleActionRequests(arFactory, context, rule, responsibility, routeHeader, parentRequest, ruleDelegation); |
256 | |
} else |
257 | |
{ |
258 | 0 | makeActionRequest(arFactory, context, rule, routeHeader, responsibility, parentRequest, ruleDelegation); |
259 | |
} |
260 | |
|
261 | |
|
262 | |
|
263 | |
} |
264 | 0 | } |
265 | |
|
266 | |
private void buildDelegationGraph(ActionRequestFactory arFactory, RouteContext context, |
267 | |
RuleBaseValues delegationRule, DocumentRouteHeaderValue routeHeaderValue, ActionRequestValue parentRequest, RuleDelegation ruleDelegation) throws WorkflowException { |
268 | 0 | context.setActionRequest(parentRequest); |
269 | 0 | if (delegationRule.isActive(new Date())) { |
270 | 0 | for (RuleResponsibility delegationResp : delegationRule.getResponsibilities()) |
271 | |
{ |
272 | 0 | if (delegationResp.isUsingRole()) |
273 | |
{ |
274 | 0 | makeRoleActionRequests(arFactory, context, delegationRule, delegationResp, routeHeaderValue, parentRequest, ruleDelegation); |
275 | 0 | } else if (delegationRule.isMatch(context.getDocumentContent())) |
276 | |
{ |
277 | 0 | makeActionRequest(arFactory, context, delegationRule, routeHeaderValue, delegationResp, parentRequest, ruleDelegation); |
278 | |
} |
279 | |
} |
280 | |
} |
281 | 0 | } |
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
private void makeRoleActionRequests(ActionRequestFactory arFactory, RouteContext context, |
287 | |
RuleBaseValues rule, RuleResponsibility resp, DocumentRouteHeaderValue routeHeader, ActionRequestValue parentRequest, |
288 | |
RuleDelegation ruleDelegation) throws WorkflowException |
289 | |
{ |
290 | 0 | String roleName = resp.getResolvedRoleName(); |
291 | 0 | RoleAttribute roleAttribute = resp.resolveRoleAttribute(); |
292 | 0 | setRuleAttribute(roleAttribute, rule, resp.getRoleAttributeName()); |
293 | 0 | List<String> qualifiedRoleNames = new ArrayList<String>(); |
294 | 0 | if (parentRequest != null && parentRequest.getQualifiedRoleName() != null) { |
295 | 0 | qualifiedRoleNames.add(parentRequest.getQualifiedRoleName()); |
296 | |
} else { |
297 | 0 | qualifiedRoleNames.addAll(roleAttribute.getQualifiedRoleNames(roleName, context.getDocumentContent())); |
298 | |
} |
299 | 0 | for (String qualifiedRoleName : qualifiedRoleNames) |
300 | |
{ |
301 | 0 | if (parentRequest == null && isDuplicateActionRequestDetected(routeHeader, context.getNodeInstance(), resp, qualifiedRoleName)) |
302 | |
{ |
303 | 0 | continue; |
304 | |
} |
305 | |
|
306 | 0 | ResolvedQualifiedRole resolvedRole = roleAttribute.resolveQualifiedRole(context, roleName, qualifiedRoleName); |
307 | 0 | RoleRecipient recipient = new RoleRecipient(roleName, qualifiedRoleName, resolvedRole); |
308 | 0 | if (parentRequest == null) |
309 | |
{ |
310 | 0 | ActionRequestValue roleRequest = arFactory.addRoleRequest(recipient, resp.getActionRequestedCd(), resp.getApprovePolicy(), resp.getPriority(), resp.getResponsibilityId(), rule |
311 | |
.getForceAction(), rule.getDescription(), rule.getRuleBaseValuesId()); |
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | 0 | List<RuleDelegation> ruleDelegations = getRuleDelegationService().findByResponsibilityId(resp.getResponsibilityId()); |
328 | 0 | if (ruleDelegations != null && !ruleDelegations.isEmpty()) |
329 | |
{ |
330 | |
|
331 | 0 | for (ActionRequestValue request : roleRequest.getChildrenRequests()) |
332 | |
{ |
333 | 0 | for (RuleDelegation childRuleDelegation : ruleDelegations) |
334 | |
{ |
335 | 0 | buildDelegationGraph(arFactory, context, childRuleDelegation.getDelegationRuleBaseValues(), routeHeader, request, childRuleDelegation); |
336 | |
} |
337 | |
} |
338 | |
} |
339 | |
|
340 | 0 | } else |
341 | |
{ |
342 | 0 | arFactory.addDelegationRoleRequest(parentRequest, resp.getApprovePolicy(), recipient, resp.getResponsibilityId(), rule.getForceAction(), ruleDelegation.getDelegationType(), rule.getDescription(), rule.getRuleBaseValuesId()); |
343 | |
} |
344 | 0 | } |
345 | 0 | } |
346 | |
|
347 | |
|
348 | |
|
349 | |
|
350 | |
private void setRuleAttribute(RoleAttribute roleAttribute, RuleBaseValues rule, String roleAttributeName) { |
351 | |
|
352 | 0 | Method setRuleAttributeMethod = null; |
353 | |
try { |
354 | 0 | setRuleAttributeMethod = roleAttribute.getClass().getMethod("setRuleAttribute", RuleAttribute.class); |
355 | 0 | } catch (NoSuchMethodException e) {} |
356 | 0 | if (setRuleAttributeMethod == null) { |
357 | 0 | return; |
358 | |
} |
359 | |
|
360 | 0 | RuleTemplate ruleTemplate = rule.getRuleTemplate(); |
361 | 0 | if (ruleTemplate != null) { |
362 | 0 | for (RuleTemplateAttribute ruleTemplateAttribute : ruleTemplate.getActiveRuleTemplateAttributes()) |
363 | |
{ |
364 | 0 | RuleAttribute ruleAttribute = ruleTemplateAttribute.getRuleAttribute(); |
365 | 0 | if (ruleAttribute.getClassName().equals(roleAttributeName)) |
366 | |
{ |
367 | |
|
368 | |
try |
369 | |
{ |
370 | 0 | setRuleAttributeMethod.invoke(roleAttribute, ruleAttribute); |
371 | 0 | break; |
372 | 0 | } catch (Exception e) |
373 | |
{ |
374 | 0 | throw new WorkflowRuntimeException("Failed to set RuleAttribute on our RoleAttribute!", e); |
375 | |
} |
376 | |
} |
377 | 0 | } |
378 | |
} |
379 | 0 | } |
380 | |
|
381 | |
|
382 | |
|
383 | |
|
384 | |
|
385 | |
private void makeActionRequest(ActionRequestFactory arFactory, RouteContext context, RuleBaseValues rule, DocumentRouteHeaderValue routeHeader, RuleResponsibility resp, ActionRequestValue parentRequest, |
386 | |
RuleDelegation ruleDelegation) throws WorkflowException { |
387 | 0 | if (parentRequest == null && isDuplicateActionRequestDetected(routeHeader, context.getNodeInstance(), resp, null)) { |
388 | 0 | return; |
389 | |
} |
390 | |
Recipient recipient; |
391 | 0 | if (resp.isUsingWorkflowUser()) { |
392 | 0 | recipient = new KimPrincipalRecipient(resp.getPrincipal()); |
393 | 0 | } else if (resp.isUsingGroup()) { |
394 | 0 | recipient = new KimGroupRecipient(resp.getGroup()); |
395 | |
} else { |
396 | 0 | throw new RiceRuntimeException("Illegal rule responsibility type encountered: " + resp.getRuleResponsibilityType()); |
397 | |
} |
398 | |
ActionRequestValue actionRequest; |
399 | 0 | if (parentRequest == null) { |
400 | 0 | actionRequest = arFactory.addRootActionRequest(resp.getActionRequestedCd(), |
401 | |
resp.getPriority(), |
402 | |
recipient, |
403 | |
rule.getDescription(), |
404 | |
resp.getResponsibilityId(), |
405 | |
rule.getForceAction(), |
406 | |
resp.getApprovePolicy(), |
407 | |
rule.getRuleBaseValuesId()); |
408 | |
|
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | |
|
415 | |
|
416 | |
|
417 | |
|
418 | |
|
419 | |
|
420 | |
|
421 | 0 | List<RuleDelegation> ruleDelegations = getRuleDelegationService().findByResponsibilityId(resp.getResponsibilityId()); |
422 | 0 | if (ruleDelegations != null && !ruleDelegations.isEmpty()) { |
423 | 0 | for (RuleDelegation childRuleDelegation : ruleDelegations) { |
424 | 0 | buildDelegationGraph(arFactory, context, childRuleDelegation.getDelegationRuleBaseValues(), routeHeader, actionRequest, childRuleDelegation); |
425 | |
} |
426 | |
} |
427 | |
|
428 | 0 | } else { |
429 | 0 | arFactory.addDelegationRequest(parentRequest, recipient, resp.getResponsibilityId(), rule.getForceAction(), ruleDelegation.getDelegationType(), rule.getDescription(), rule.getRuleBaseValuesId()); |
430 | |
} |
431 | 0 | } |
432 | |
|
433 | |
private boolean isDuplicateActionRequestDetected(DocumentRouteHeaderValue routeHeader, RouteNodeInstance nodeInstance, RuleResponsibility resp, String qualifiedRoleName) { |
434 | 0 | List<ActionRequestValue> requests = getActionRequestService().findByStatusAndDocId(KEWConstants.ACTION_REQUEST_DONE_STATE, routeHeader.getDocumentId()); |
435 | 0 | for (ActionRequestValue request : requests) |
436 | |
{ |
437 | 0 | if (((nodeInstance != null && request.getNodeInstance() != null && request.getNodeInstance().getRouteNodeInstanceId().equals(nodeInstance.getRouteNodeInstanceId())) || request |
438 | |
.getRouteLevel().equals(routeHeader.getDocRouteLevel())) |
439 | |
&& request.getResponsibilityId().equals(resp.getResponsibilityId()) && ObjectUtils.equals(request.getQualifiedRoleName(), qualifiedRoleName)) |
440 | |
{ |
441 | 0 | return true; |
442 | |
} |
443 | |
} |
444 | 0 | return false; |
445 | |
} |
446 | |
|
447 | |
public RuleService getRuleService() { |
448 | 0 | return KEWServiceLocator.getRuleService(); |
449 | |
} |
450 | |
|
451 | |
public RuleDelegationService getRuleDelegationService() { |
452 | 0 | return KEWServiceLocator.getRuleDelegationService(); |
453 | |
} |
454 | |
|
455 | |
|
456 | |
private ActionRequestService getActionRequestService() { |
457 | 0 | return KEWServiceLocator.getActionRequestService(); |
458 | |
} |
459 | |
|
460 | |
public int getNumberOfMatchingRules() { |
461 | 0 | return selectedRules; |
462 | |
} |
463 | |
|
464 | |
|
465 | |
|
466 | |
|
467 | |
|
468 | |
|
469 | |
|
470 | |
|
471 | |
} |