001    /**
002     * Copyright 2005-2012 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 org.kuali.rice.core.api.exception.RiceIllegalStateException;
019    import org.kuali.rice.kew.api.KewApiServiceLocator;
020    import org.kuali.rice.kew.engine.RouteContext;
021    import org.kuali.rice.kew.engine.node.NodeState;
022    import org.kuali.rice.kew.engine.node.RouteNode;
023    import org.kuali.rice.kew.engine.node.RouteNodeInstance;
024    import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
025    import org.kuali.rice.kew.api.KewApiConstants;
026    import org.kuali.rice.kew.util.Utilities;
027    
028    import java.sql.Timestamp;
029    import java.util.ArrayList;
030    import java.util.List;
031    import java.util.Map;
032    
033    
034    /**
035     * Rule selector that select a rule based on configured rule name 
036     * @author Kuali Rice Team (rice.collab@kuali.org)
037     */
038    public class NamedRuleSelector implements RuleSelector {
039        /**
040         * The route node config param consulted to determine the rule name to select
041         */
042        public static final String RULE_NAME_CFG_KEY = "ruleName";
043    
044        /**
045         * @return the name of the rule that should be selected
046         */
047        protected String getName(RouteContext context, DocumentRouteHeaderValue routeHeader,
048                RouteNodeInstance nodeInstance, String selectionCriterion, Timestamp effectiveDate) {
049            String ruleName = null;
050            RouteNode routeNodeDef = nodeInstance.getRouteNode();
051            // first check to see if there is a rule name configured on the node instance
052            NodeState ns = nodeInstance.getNodeState(KewApiConstants.RULE_NAME_NODE_STATE_KEY);
053            if (ns != null) {
054                ruleName = ns.getValue();
055            } else {
056                // otherwise check the node def
057                Map<String, String> routeNodeConfig = Utilities.getKeyValueCollectionAsMap(routeNodeDef.getConfigParams());
058                ruleName = routeNodeConfig.get(RULE_NAME_CFG_KEY);
059            }
060            return ruleName;
061        }
062    
063        public List<Rule> selectRules(RouteContext context, DocumentRouteHeaderValue routeHeader,
064                RouteNodeInstance nodeInstance, String selectionCriterion, Timestamp effectiveDate) {
065    
066            String ruleName = getName(context, routeHeader, nodeInstance, selectionCriterion, effectiveDate);
067    
068            if (ruleName == null) {
069                throw new RiceIllegalStateException("No 'ruleName' configuration parameter present on route node definition: " + nodeInstance.getRouteNode());
070            }
071    
072            org.kuali.rice.kew.api.rule.Rule ruleDef = KewApiServiceLocator.getRuleService().getRuleByName(ruleName);
073            if (ruleDef == null) {
074                throw new RiceIllegalStateException("No rule found with name '" + ruleName + "'");
075            }
076    
077            List<Rule> rules = new ArrayList<Rule>(1);
078            rules.add(new RuleImpl(ruleDef));
079            return rules;
080        }
081    }