1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kew.rule; |
17 | |
|
18 | |
import org.joda.time.DateTime; |
19 | |
import org.kuali.rice.kew.api.KewApiServiceLocator; |
20 | |
import org.kuali.rice.kew.api.WorkflowRuntimeException; |
21 | |
import org.kuali.rice.kew.engine.RouteContext; |
22 | |
import org.kuali.rice.kew.engine.node.RouteNodeInstance; |
23 | |
import org.kuali.rice.kew.routeheader.DocumentContent; |
24 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
25 | |
import org.kuali.rice.kew.rule.bo.RuleTemplateAttributeBo; |
26 | |
import org.kuali.rice.kew.rule.bo.RuleTemplateBo; |
27 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
28 | |
import org.kuali.rice.kew.util.PerformanceLogger; |
29 | |
|
30 | |
import java.sql.Timestamp; |
31 | |
import java.util.ArrayList; |
32 | |
import java.util.Collections; |
33 | |
import java.util.HashSet; |
34 | |
import java.util.List; |
35 | |
import java.util.Set; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 0 | class TemplateRuleSelector implements RuleSelector { |
42 | |
|
43 | |
|
44 | |
|
45 | |
private int numberOfSelectedRules; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
int getNumberOfSelectedRules() { |
51 | 0 | return numberOfSelectedRules; |
52 | |
} |
53 | |
|
54 | |
public List<Rule> selectRules(RouteContext context, DocumentRouteHeaderValue routeHeader, RouteNodeInstance nodeInstance, String selectionCriterion, Timestamp effectiveDate) { |
55 | |
|
56 | 0 | final String ruleTemplateName = selectionCriterion; |
57 | |
|
58 | 0 | Set<MassRuleAttribute> massRules = new HashSet<MassRuleAttribute>(); |
59 | 0 | RuleTemplateBo template = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(ruleTemplateName); |
60 | |
|
61 | 0 | if (template == null) { |
62 | 0 | throw new WorkflowRuntimeException("Could not locate the rule template with name " + ruleTemplateName + " on document " + routeHeader.getDocumentId()); |
63 | |
} |
64 | 0 | for (RuleTemplateAttributeBo templateAttribute : template.getActiveRuleTemplateAttributes()) { |
65 | 0 | if (!templateAttribute.isWorkflowAttribute()) { |
66 | 0 | continue; |
67 | |
} |
68 | 0 | WorkflowRuleAttribute attribute = templateAttribute.getWorkflowAttribute(); |
69 | 0 | if (attribute instanceof MassRuleAttribute) { |
70 | 0 | massRules.add((MassRuleAttribute) attribute); |
71 | |
} |
72 | |
|
73 | 0 | } |
74 | |
|
75 | 0 | List<org.kuali.rice.kew.api.rule.Rule> rules = Collections.emptyList(); |
76 | 0 | if (effectiveDate == null) { |
77 | 0 | rules = KewApiServiceLocator.getRuleService() |
78 | |
.getRulesByTemplateNameAndDocumentTypeName(ruleTemplateName, |
79 | |
routeHeader.getDocumentType().getName()); |
80 | |
} else { |
81 | 0 | rules = KewApiServiceLocator.getRuleService() |
82 | |
.getRulesByTemplateNameAndDocumentTypeNameAndEffectiveDate(ruleTemplateName, |
83 | |
routeHeader.getDocumentType().getName(), new DateTime(effectiveDate.getTime())); |
84 | |
} |
85 | 0 | numberOfSelectedRules = rules.size(); |
86 | |
|
87 | |
|
88 | |
|
89 | 0 | if (context.getDocument() == null) { |
90 | 0 | context.setDocument(routeHeader); |
91 | |
} |
92 | 0 | if (context.getNodeInstance() == null) { |
93 | 0 | context.setNodeInstance(nodeInstance); |
94 | |
} |
95 | 0 | DocumentContent documentContent = context.getDocumentContent(); |
96 | 0 | PerformanceLogger performanceLogger = new PerformanceLogger(); |
97 | |
|
98 | 0 | for (MassRuleAttribute massRuleAttribute : massRules) { |
99 | 0 | rules = massRuleAttribute.filterNonMatchingRules(context, rules); |
100 | |
} |
101 | 0 | performanceLogger.log("Time to filter massRules for template " + template.getName()); |
102 | |
|
103 | 0 | List<Rule> ruleList = new ArrayList<Rule>(rules.size()); |
104 | 0 | for (org.kuali.rice.kew.api.rule.Rule ruleDefinition: rules) { |
105 | 0 | ruleList.add(new RuleImpl(ruleDefinition)); |
106 | |
} |
107 | 0 | return ruleList; |
108 | |
} |
109 | |
|
110 | |
} |