001    /*
002     * Copyright 2007 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kew.rule;
017    
018    import java.util.ArrayList;
019    import java.util.Iterator;
020    import java.util.List;
021    
022    import org.kuali.rice.kew.engine.RouteContext;
023    import org.kuali.rice.kew.routeheader.DocumentContent;
024    import org.kuali.rice.kew.rule.bo.RuleAttribute;
025    import org.kuali.rice.kew.rule.bo.RuleTemplateAttribute;
026    import org.kuali.rice.kew.rule.xmlrouting.GenericXMLRuleAttribute;
027    import org.kuali.rice.kew.util.KEWConstants;
028    
029    
030    /**
031     * Standard rule expression implementation that evaluates the attributes associated with the rule definition 
032     * @author Kuali Rice Team (rice.collab@kuali.org)
033     */
034    class WorkflowAttributeRuleExpression implements RuleExpression {
035        
036            public RuleExpressionResult evaluate(Rule rule, RouteContext context) {
037            
038                    RuleBaseValues ruleDefinition = rule.getDefinition();
039            boolean match = isMatch(ruleDefinition, context.getDocumentContent());
040            if (match) {
041                return new RuleExpressionResult(rule, match, ruleDefinition.getResponsibilities());
042            }
043                    return new RuleExpressionResult(rule, match);
044        }
045    
046        public boolean isMatch(RuleBaseValues ruleDefinition, DocumentContent docContent) {
047            if (ruleDefinition.getRuleTemplate() == null) {
048                // this can happen if neither rule template nor expression was specified in the rule definition
049                // because WorkflowAttributeRuleExpression is the default expression implementation, we should
050                // handle this here in order to support rules that fire unconditionally (and just return a statically
051                // configured list of responsibilities)
052                // the alternative is to either detect this situation in the rule xml parser or RuleImpl, and either substitute
053                // a different RuleExpression implementation (a "default default" one) in the configuration, or at runtime,
054                // that simply implements the one-liner of returning responsibilities.
055                // doing this in the existing WorkflowAttributeRuleExpression implementation introduces the least change
056                // or compatibilities issues, and avoids pushing compensating logic into RuleImpl
057                return true;
058            }
059            for (Iterator iter = ruleDefinition.getRuleTemplate().getActiveRuleTemplateAttributes().iterator(); iter.hasNext();) {
060                RuleTemplateAttribute ruleTemplateAttribute = (RuleTemplateAttribute) iter.next();
061                if (!ruleTemplateAttribute.isWorkflowAttribute()) {
062                    continue;
063                }
064                WorkflowAttribute routingAttribute = (WorkflowAttribute) ruleTemplateAttribute.getWorkflowAttribute();
065    
066                RuleAttribute ruleAttribute = ruleTemplateAttribute.getRuleAttribute();
067                if (ruleAttribute.getType().equals(KEWConstants.RULE_XML_ATTRIBUTE_TYPE)) {
068                    ((GenericXMLRuleAttribute) routingAttribute).setRuleAttribute(ruleAttribute);
069                }
070                String className = ruleAttribute.getClassName();
071                List editedRuleExtensions = new ArrayList();
072                for (Iterator iter2 = ruleDefinition.getRuleExtensions().iterator(); iter2.hasNext();) {
073                    RuleExtension extension = (RuleExtension) iter2.next();
074                    if (extension.getRuleTemplateAttribute().getRuleAttribute().getClassName().equals(className)) {
075                        editedRuleExtensions.add(extension);
076                    }
077                }
078                if (!routingAttribute.isMatch(docContent, editedRuleExtensions)) {
079                    return false;
080                }
081            }
082            return true;
083        }
084    
085    }