View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Standard rule expression implementation that evaluates the attributes associated with the rule definition 
36   * @author Kuali Rice Team (rice.collab@kuali.org)
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              // this can happen if neither rule template nor expression was specified in the rule definition
54              // because WorkflowAttributeRuleExpression is the default expression implementation, we should
55              // handle this here in order to support rules that fire unconditionally (and just return a statically
56              // configured list of responsibilities)
57              // the alternative is to either detect this situation in the rule xml parser or RuleImpl, and either substitute
58              // a different RuleExpression implementation (a "default default" one) in the configuration, or at runtime,
59              // that simply implements the one-liner of returning responsibilities.
60              // doing this in the existing WorkflowAttributeRuleExpression implementation introduces the least change
61              // or compatibilities issues, and avoids pushing compensating logic into RuleImpl
62              return true;
63          }
64          //RuleBaseValues rule = KEWServiceLocator.getRuleService().getRuleByName(ruleDefinition.getName());
65          for (RuleTemplateAttribute ruleTemplateAttribute : ruleDefinition.getRuleTemplate().getActiveRuleTemplateAttributes()) {
66              String attributeName = ruleTemplateAttribute.getRuleAttribute().getName();
67              if (!RuleAttribute.isWorkflowAttribute(ruleTemplateAttribute.getRuleAttribute().getType())) {
68                  continue;
69              }
70              //WorkflowRuleAttribute routingAttribute = (WorkflowRuleAttribute) ruleTemplateAttribute.getWorkflowAttribute();
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 }