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 java.sql.Timestamp; |
19 | |
import java.util.ArrayList; |
20 | |
import java.util.HashSet; |
21 | |
import java.util.Iterator; |
22 | |
import java.util.List; |
23 | |
import java.util.Set; |
24 | |
|
25 | |
import org.kuali.rice.kew.api.WorkflowRuntimeException; |
26 | |
import org.kuali.rice.kew.engine.RouteContext; |
27 | |
import org.kuali.rice.kew.engine.node.RouteNodeInstance; |
28 | |
import org.kuali.rice.kew.exception.WorkflowException; |
29 | |
import org.kuali.rice.kew.routeheader.DocumentContent; |
30 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
31 | |
import org.kuali.rice.kew.rule.bo.RuleTemplate; |
32 | |
import org.kuali.rice.kew.rule.bo.RuleTemplateAttribute; |
33 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
34 | |
import org.kuali.rice.kew.util.PerformanceLogger; |
35 | |
|
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) throws WorkflowException { |
55 | |
|
56 | 0 | final String ruleTemplateName = selectionCriterion; |
57 | |
|
58 | 0 | Set<MassRuleAttribute> massRules = new HashSet<MassRuleAttribute>(); |
59 | 0 | RuleTemplate template = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(ruleTemplateName); |
60 | 0 | if (template == null) { |
61 | 0 | throw new WorkflowRuntimeException("Could not locate the rule template with name " + ruleTemplateName + " on document " + routeHeader.getDocumentId()); |
62 | |
} |
63 | 0 | for (Iterator iter = template.getActiveRuleTemplateAttributes().iterator(); iter.hasNext();) { |
64 | |
|
65 | 0 | RuleTemplateAttribute templateAttribute = (RuleTemplateAttribute) iter.next(); |
66 | 0 | if (!templateAttribute.isWorkflowAttribute()) { |
67 | 0 | continue; |
68 | |
} |
69 | 0 | WorkflowAttribute attribute = templateAttribute.getWorkflowAttribute(); |
70 | 0 | if (attribute instanceof MassRuleAttribute) { |
71 | 0 | massRules.add((MassRuleAttribute) attribute); |
72 | |
} |
73 | |
|
74 | 0 | } |
75 | |
|
76 | 0 | List rules = null; |
77 | 0 | if (effectiveDate != null) { |
78 | 0 | rules = KEWServiceLocator.getRuleService().fetchAllCurrentRulesForTemplateDocCombination(ruleTemplateName, routeHeader.getDocumentType().getName(), effectiveDate); |
79 | |
} else { |
80 | 0 | rules = KEWServiceLocator.getRuleService().fetchAllCurrentRulesForTemplateDocCombination(ruleTemplateName, routeHeader.getDocumentType().getName()); |
81 | |
} |
82 | 0 | numberOfSelectedRules = rules.size(); |
83 | |
|
84 | |
|
85 | |
|
86 | 0 | if (context.getDocument() == null) { |
87 | 0 | context.setDocument(routeHeader); |
88 | |
} |
89 | 0 | if (context.getNodeInstance() == null) { |
90 | 0 | context.setNodeInstance(nodeInstance); |
91 | |
} |
92 | 0 | DocumentContent documentContent = context.getDocumentContent(); |
93 | 0 | PerformanceLogger performanceLogger = new PerformanceLogger(); |
94 | |
|
95 | 0 | for (Iterator iter = massRules.iterator(); iter.hasNext();) { |
96 | 0 | MassRuleAttribute massRuleAttribute = (MassRuleAttribute) iter.next(); |
97 | 0 | rules = massRuleAttribute.filterNonMatchingRules(context, rules); |
98 | 0 | } |
99 | 0 | performanceLogger.log("Time to filter massRules for template " + template.getName()); |
100 | |
|
101 | 0 | List<Rule> ruleList = new ArrayList<Rule>(rules.size()); |
102 | 0 | for (RuleBaseValues ruleDefinition: (List<RuleBaseValues>) rules) { |
103 | 0 | ruleList.add(new RuleImpl(ruleDefinition)); |
104 | |
} |
105 | 0 | return ruleList; |
106 | |
} |
107 | |
|
108 | |
} |