View Javadoc
1   /**
2    * Copyright 2005-2014 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.core.api.exception.RiceIllegalStateException;
19  import org.kuali.rice.kew.api.KewApiServiceLocator;
20  import org.kuali.rice.kew.engine.RouteContext;
21  import org.kuali.rice.kew.engine.node.NodeState;
22  import org.kuali.rice.kew.engine.node.RouteNode;
23  import org.kuali.rice.kew.engine.node.RouteNodeInstance;
24  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
25  import org.kuali.rice.kew.api.KewApiConstants;
26  import org.kuali.rice.kew.util.Utilities;
27  
28  import java.sql.Timestamp;
29  import java.util.ArrayList;
30  import java.util.List;
31  import java.util.Map;
32  
33  
34  /**
35   * Rule selector that select a rule based on configured rule name 
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  public class NamedRuleSelector implements RuleSelector {
39      /**
40       * The route node config param consulted to determine the rule name to select
41       */
42      public static final String RULE_NAME_CFG_KEY = "ruleName";
43  
44      /**
45       * @return the name of the rule that should be selected
46       */
47      protected String getName(RouteContext context, DocumentRouteHeaderValue routeHeader,
48              RouteNodeInstance nodeInstance, String selectionCriterion, Timestamp effectiveDate) {
49          String ruleName = null;
50          RouteNode routeNodeDef = nodeInstance.getRouteNode();
51          // first check to see if there is a rule name configured on the node instance
52          NodeState ns = nodeInstance.getNodeState(KewApiConstants.RULE_NAME_NODE_STATE_KEY);
53          if (ns != null) {
54              ruleName = ns.getValue();
55          } else {
56              // otherwise check the node def
57              Map<String, String> routeNodeConfig = Utilities.getKeyValueCollectionAsMap(routeNodeDef.getConfigParams());
58              ruleName = routeNodeConfig.get(RULE_NAME_CFG_KEY);
59          }
60          return ruleName;
61      }
62  
63      public List<Rule> selectRules(RouteContext context, DocumentRouteHeaderValue routeHeader,
64              RouteNodeInstance nodeInstance, String selectionCriterion, Timestamp effectiveDate) {
65  
66          String ruleName = getName(context, routeHeader, nodeInstance, selectionCriterion, effectiveDate);
67  
68          if (ruleName == null) {
69              throw new RiceIllegalStateException("No 'ruleName' configuration parameter present on route node definition: " + nodeInstance.getRouteNode());
70          }
71  
72          org.kuali.rice.kew.api.rule.Rule ruleDef = KewApiServiceLocator.getRuleService().getRuleByName(ruleName);
73          if (ruleDef == null) {
74              throw new RiceIllegalStateException("No rule found with name '" + ruleName + "'");
75          }
76  
77          List<Rule> rules = new ArrayList<Rule>(1);
78          rules.add(new RuleImpl(ruleDef));
79          return rules;
80      }
81  }