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