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