Coverage Report - org.kuali.rice.kew.rule.RuleImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
RuleImpl
0%
0/31
0%
0/10
3.75
 
 1  
 /*
 2  
  * Copyright 2007 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.apache.commons.lang.StringUtils;
 19  
 import org.kuali.rice.core.util.ClassLoaderUtils;
 20  
 import org.kuali.rice.kew.engine.RouteContext;
 21  
 import org.kuali.rice.kew.exception.WorkflowException;
 22  
 
 23  
 
 24  
 /**
 25  
  * {@link Rule} implementation 
 26  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 27  
  */
 28  
 class RuleImpl implements Rule {
 29  
     /**
 30  
      * The default type of rule expression implementation to use if none is explicitly
 31  
      * specified for the node.
 32  
      */
 33  
     public static final String DEFAULT_RULE_EXPRESSION = "WorkflowAttribute";
 34  
     /**
 35  
      * Package in which rule expression implementations live
 36  
      */
 37  
     private static final String RULE_EXPRESSION_PACKAGE = "org.kuali.rice.kew.rule";
 38  
     /**
 39  
      * The class name suffix all rule expressions should have; e.g. FooRuleExpression
 40  
      */
 41  
     private static final String RULE_EXPRESSION_SUFFIX= "RuleExpression";
 42  
 
 43  
     /**
 44  
      * The BO of the rule definition in the system
 45  
      */
 46  
     private final RuleBaseValues ruleDefinition;
 47  
 
 48  0
     RuleImpl(RuleBaseValues ruleDefinition) {
 49  0
         this.ruleDefinition = ruleDefinition;
 50  0
     }
 51  
 
 52  
     public RuleBaseValues getDefinition() {
 53  0
         return ruleDefinition;
 54  
     }
 55  
 
 56  
     // loads a RuleExpression implementation
 57  
     protected RuleExpression loadRuleExpression(String type) throws WorkflowException {
 58  0
         if (type == null) {
 59  0
             type = DEFAULT_RULE_EXPRESSION;
 60  
         }
 61  
         // type is of the format 'category:qualifier'
 62  
         // we just want the category
 63  0
         int colon = type.indexOf(':');
 64  0
         if (colon == -1) colon = type.length();
 65  0
         type = type.substring(0, colon);
 66  0
         type = StringUtils.capitalize(type);
 67  
 
 68  
         // load up the rule expression implementation
 69  0
         String className = RULE_EXPRESSION_PACKAGE + "." + type + RULE_EXPRESSION_SUFFIX;
 70  
         Class<?> ruleExpressionClass;
 71  
         try {
 72  0
             ruleExpressionClass = ClassLoaderUtils.getDefaultClassLoader().loadClass(className);
 73  0
         } catch (ClassNotFoundException cnfe) {
 74  0
             throw new WorkflowException("Rule expression implementation '" + className + "' not found", cnfe);
 75  0
         }
 76  0
         if (!RuleExpression.class.isAssignableFrom(ruleExpressionClass)) {
 77  0
             throw new WorkflowException("Specified class '" + ruleExpressionClass + "' does not implement RuleExpression interface");
 78  
         }
 79  
         RuleExpression ruleExpression;
 80  
         try {
 81  0
             ruleExpression = ((Class<RuleExpression>) ruleExpressionClass).newInstance();
 82  0
         } catch (Exception e) {
 83  0
                 if (e instanceof RuntimeException) {
 84  0
                         throw (RuntimeException)e;
 85  
                 }
 86  0
             throw new WorkflowException("Error instantiating rule expression implementation '" + ruleExpressionClass + "'", e);
 87  0
         }
 88  
 
 89  0
         return ruleExpression;
 90  
     }
 91  
 
 92  
     public RuleExpressionResult evaluate(Rule rule, RouteContext context) throws WorkflowException {
 93  0
         RuleBaseValues ruleDefinition = rule.getDefinition();
 94  0
         RuleExpressionDef ruleExprDef = ruleDefinition.getRuleExpressionDef();
 95  0
         String type = DEFAULT_RULE_EXPRESSION;
 96  0
         if (ruleExprDef != null) {
 97  0
             type = ruleExprDef.getType();
 98  
         }
 99  0
         RuleExpression ruleExpression = loadRuleExpression(type);        
 100  0
         return ruleExpression.evaluate(rule, context);
 101  
     }
 102  
 }