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