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.kuali.rice.core.api.exception.RiceIllegalArgumentException;
19 import org.kuali.rice.kew.api.KewApiConstants;
20 import org.kuali.rice.kew.api.KewApiServiceLocator;
21 import org.kuali.rice.kew.api.extension.ExtensionDefinition;
22 import org.kuali.rice.kew.api.extension.ExtensionUtils;
23 import org.kuali.rice.kew.api.rule.RuleExtension;
24 import org.kuali.rice.kew.api.rule.RuleTemplateAttribute;
25 import org.kuali.rice.kew.engine.RouteContext;
26 import org.kuali.rice.kew.routeheader.DocumentContent;
27 import org.kuali.rice.kew.rule.bo.RuleAttribute;
28 import org.kuali.rice.kew.rule.xmlrouting.GenericXMLRuleAttribute;
29
30 import java.util.ArrayList;
31 import java.util.List;
32
33
34
35
36
37
38 class WorkflowAttributeRuleExpression implements RuleExpression {
39
40 public RuleExpressionResult evaluate(Rule rule, RouteContext context) {
41
42 org.kuali.rice.kew.api.rule.Rule ruleDefinition =
43 org.kuali.rice.kew.api.rule.Rule.Builder.create(rule.getDefinition()).build();
44 boolean match = isMatch(ruleDefinition, context.getDocumentContent());
45 if (match) {
46 return new RuleExpressionResult(rule, match, ruleDefinition.getRuleResponsibilities());
47 }
48 return new RuleExpressionResult(rule, match);
49 }
50
51 public boolean isMatch(org.kuali.rice.kew.api.rule.Rule ruleDefinition, DocumentContent docContent) {
52 if (ruleDefinition.getRuleTemplate() == null) {
53
54
55
56
57
58
59
60
61
62 return true;
63 }
64
65 for (RuleTemplateAttribute ruleTemplateAttribute : ruleDefinition.getRuleTemplate().getActiveRuleTemplateAttributes()) {
66 String attributeName = ruleTemplateAttribute.getRuleAttribute().getName();
67 if (!RuleAttribute.isWorkflowAttribute(ruleTemplateAttribute.getRuleAttribute().getType())) {
68 continue;
69 }
70
71 ExtensionDefinition extensionDefinition = KewApiServiceLocator.getExtensionRepositoryService().getExtensionByName(attributeName);
72 Object attribute = ExtensionUtils.loadExtension(extensionDefinition);
73 if (attribute == null) {
74 throw new RiceIllegalArgumentException("Failed to load WorkflowRuleAttribute for: " + extensionDefinition);
75 }
76 if (!WorkflowRuleAttribute.class.isAssignableFrom(attribute.getClass())) {
77 throw new RiceIllegalArgumentException("Failed to locate a WorkflowRuleAttribute with the given name: " + attributeName);
78 }
79 if (attribute instanceof XmlConfiguredAttribute) {
80 ((XmlConfiguredAttribute)attribute).setExtensionDefinition(extensionDefinition);
81 }
82
83 WorkflowRuleAttribute routingAttribute = (WorkflowRuleAttribute)attribute;
84
85 ExtensionDefinition ruleAttribute = ruleTemplateAttribute.getRuleAttribute();
86 if (ruleAttribute.getType().equals(KewApiConstants.RULE_XML_ATTRIBUTE_TYPE)) {
87 ((GenericXMLRuleAttribute) routingAttribute).setExtensionDefinition(ruleAttribute);
88 }
89 String className = ruleAttribute.getResourceDescriptor();
90 List<RuleExtension> editedRuleExtensions = new ArrayList();
91 for (RuleExtension extension : ruleDefinition.getRuleExtensions()) {
92 if (extension.getRuleTemplateAttribute().getRuleAttribute().getResourceDescriptor().equals(className)) {
93 editedRuleExtensions.add(extension);
94 }
95 }
96 if (!routingAttribute.isMatch(docContent, editedRuleExtensions)) {
97 return false;
98 }
99 }
100 return true;
101 }
102
103 }