View Javadoc

1   /**
2    * Copyright 2005-2011 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.kew.engine.RouteContext;
19  import org.kuali.rice.kew.routeheader.DocumentContent;
20  import org.kuali.rice.kew.rule.bo.RuleAttribute;
21  import org.kuali.rice.kew.rule.bo.RuleTemplateAttributeBo;
22  import org.kuali.rice.kew.rule.xmlrouting.GenericXMLRuleAttribute;
23  import org.kuali.rice.kew.service.KEWServiceLocator;
24  import org.kuali.rice.kew.api.KewApiConstants;
25  
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  
30  /**
31   * Standard rule expression implementation that evaluates the attributes associated with the rule definition 
32   * @author Kuali Rice Team (rice.collab@kuali.org)
33   */
34  class WorkflowAttributeRuleExpression implements RuleExpression {
35      
36  	public RuleExpressionResult evaluate(Rule rule, RouteContext context) {
37          
38  		org.kuali.rice.kew.api.rule.Rule ruleDefinition =
39                  org.kuali.rice.kew.api.rule.Rule.Builder.create(rule.getDefinition()).build();
40          boolean match = isMatch(ruleDefinition, context.getDocumentContent());
41          if (match) {
42              return new RuleExpressionResult(rule, match, ruleDefinition.getRuleResponsibilities());
43          }
44  		return new RuleExpressionResult(rule, match);
45      }
46  
47      public boolean isMatch(org.kuali.rice.kew.api.rule.Rule ruleDefinition, DocumentContent docContent) {
48          if (ruleDefinition.getRuleTemplate() == null) {
49              // this can happen if neither rule template nor expression was specified in the rule definition
50              // because WorkflowAttributeRuleExpression is the default expression implementation, we should
51              // handle this here in order to support rules that fire unconditionally (and just return a statically
52              // configured list of responsibilities)
53              // the alternative is to either detect this situation in the rule xml parser or RuleImpl, and either substitute
54              // a different RuleExpression implementation (a "default default" one) in the configuration, or at runtime,
55              // that simply implements the one-liner of returning responsibilities.
56              // doing this in the existing WorkflowAttributeRuleExpression implementation introduces the least change
57              // or compatibilities issues, and avoids pushing compensating logic into RuleImpl
58              return true;
59          }
60          RuleBaseValues rule = KEWServiceLocator.getRuleService().getRuleByName(ruleDefinition.getName());
61          for (RuleTemplateAttributeBo ruleTemplateAttribute : rule.getRuleTemplate().getActiveRuleTemplateAttributes()) {
62              if (!ruleTemplateAttribute.isWorkflowAttribute()) {
63                  continue;
64              }
65              WorkflowRuleAttribute routingAttribute = (WorkflowRuleAttribute) ruleTemplateAttribute.getWorkflowAttribute();
66  
67              RuleAttribute ruleAttribute = ruleTemplateAttribute.getRuleAttribute();
68              if (ruleAttribute.getType().equals(KewApiConstants.RULE_XML_ATTRIBUTE_TYPE)) {
69                  ((GenericXMLRuleAttribute) routingAttribute).setExtensionDefinition(RuleAttribute.to(ruleAttribute));
70              }
71              String className = ruleAttribute.getResourceDescriptor();
72              List<RuleExtensionBo> editedRuleExtensions = new ArrayList();
73              for (RuleExtensionBo extension : rule.getRuleExtensions()) {
74                  if (extension.getRuleTemplateAttribute().getRuleAttribute().getResourceDescriptor().equals(className)) {
75                      editedRuleExtensions.add(extension);
76                  }
77              }
78              if (!routingAttribute.isMatch(docContent, editedRuleExtensions)) {
79                  return false;
80              }
81          }
82          return true;
83      }
84  
85  }