Coverage Report - org.kuali.rice.kew.rule.AccumulatingBSFRuleExpression
 
Classes in this File Line Coverage Branch Coverage Complexity
AccumulatingBSFRuleExpression
0%
0/8
N/A
1.714
AccumulatingBSFRuleExpression$1
N/A
N/A
1.714
AccumulatingBSFRuleExpression$RuleHelper
0%
0/23
0%
0/8
1.714
 
 1  
 /*
 2  
  * Copyright 2007-2008 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 java.util.ArrayList;
 19  
 import java.util.List;
 20  
 
 21  
 import org.apache.bsf.BSFException;
 22  
 import org.apache.bsf.BSFManager;
 23  
 import org.apache.log4j.Logger;
 24  
 import org.kuali.rice.kew.engine.RouteContext;
 25  
 import org.kuali.rice.kew.exception.WorkflowException;
 26  
 
 27  
 
 28  
 /**
 29  
  * An extension of BSFRuleExpression that makes it easier to accumulate a list of responsibilities
 30  
  * to emit. 
 31  
  * 
 32  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 33  
  */
 34  0
 public class AccumulatingBSFRuleExpression extends BSFRuleExpression {
 35  0
     private static final Logger LOG = Logger.getLogger(AccumulatingBSFRuleExpression.class);
 36  
 
 37  
     @Override
 38  
     protected void declareBeans(BSFManager manager, Rule rule, RouteContext context) throws BSFException {
 39  
         // define the standard beans
 40  0
         super.declareBeans(manager, rule, context);
 41  
         // define our special rule helper class
 42  0
         RuleHelper rh = new RuleHelper(rule, context);
 43  0
         manager.declareBean("metarule", rh, RuleHelper.class); // backwards compatibility with existing KRAMetaRuleExpression usage
 44  0
         manager.declareBean("rulehelper", rh, RuleHelper.class);
 45  0
     }
 46  
 
 47  
     /**
 48  
      * Helper class that is exposed to groovy scripts
 49  
      */
 50  0
     private static final class RuleHelper {
 51  
         private Rule rule;
 52  
         private WorkflowRuleAPI workflow;
 53  
         /**
 54  
          * Responsibilities accumulated during the evaluation
 55  
          */
 56  0
         private List<RuleResponsibility> responsibilities = new ArrayList<RuleResponsibility>();
 57  0
         private int responsibilityPriority = 0;
 58  
 
 59  0
         private RuleHelper(Rule rule, RouteContext context) {
 60  0
             this.workflow = new WorkflowRuleAPI(context);
 61  0
             this.rule = rule;
 62  0
         }
 63  
 
 64  
         /**
 65  
          * @return the accumulated responsibilities
 66  
          */
 67  
         public List<RuleResponsibility> getResponsibilities() {
 68  0
             return responsibilities;
 69  
         }
 70  
 
 71  
         /**
 72  
          * @param success whether the result should be successful
 73  
          * @return the RuleExpressionResult with success flag and accumulated responsibilities
 74  
          */
 75  
         public RuleExpressionResult getResult(boolean success) {
 76  0
             return new RuleExpressionResult(rule, success, responsibilities);
 77  
         }
 78  
 
 79  
         /**
 80  
          * Adds the responsibilities in the result to the list of accumulated responsibilities 
 81  
          * @param result a RuleExpressionResult whose responsibilities to accumulate
 82  
          */
 83  
         public void accumulate(RuleExpressionResult result) {
 84  0
             if (result.getResponsibilities() == null || result.getResponsibilities().size() == 0) return;
 85  
 
 86  0
             Integer curPriority = Integer.valueOf(responsibilityPriority);
 87  0
             for (RuleResponsibility responsibility: result.getResponsibilities()) {
 88  0
                 responsibility.setPriority(curPriority);
 89  0
                 responsibilities.add(responsibility);
 90  
             }
 91  
             // increment responsibilityPriority for next rule expression result responsibilities
 92  0
             responsibilityPriority++;
 93  0
         }
 94  
 
 95  
         /**
 96  
          * Evaluates a named rule accumulating responsibilities regardless of rule success
 97  
          * @param ruleName the name of the rule to evaluate
 98  
          * @return whether the rule was successful
 99  
          */
 100  
         public boolean evalRule(String ruleName) throws WorkflowException {
 101  0
             RuleExpressionResult result = workflow.invokeRule(ruleName);
 102  0
             accumulate(result);
 103  0
             return result.isSuccess();
 104  
         }
 105  
 
 106  
         /**
 107  
          * Evaluates a named rule and if it is successful accumulates the rule responsibilities
 108  
          * @param ruleName the name of the rule to evaluate
 109  
          * @param accumOnSuccess whether to accumulate the rules responsibilities on success (true), or on failure (false)
 110  
          * @return whether the rule was successful
 111  
          */
 112  
         public boolean evalRule(String ruleName, boolean accumOnSuccess) throws WorkflowException {
 113  0
             RuleExpressionResult result = workflow.invokeRule(ruleName);
 114  0
             if (accumOnSuccess == result.isSuccess()) {
 115  0
                 accumulate(result);
 116  
             }
 117  0
             return result.isSuccess();
 118  
         }
 119  
 
 120  
     }
 121  
 }