001    /**
002     * Copyright 2005-2013 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.apache.log4j.Logger;
019    import org.kuali.rice.kew.engine.RouteContext;
020    import org.kuali.rice.kew.engine.node.RouteNodeInstance;
021    import org.kuali.rice.kew.engine.node.hierarchyrouting.HierarchyRoutingNode;
022    import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
023    import org.kuali.rice.kew.util.Utilities;
024    
025    import java.sql.Timestamp;
026    import java.util.Map;
027    
028    
029    /**
030     * Derives the rule name to select based on node instance state configured by a governing
031     * HierarchyRoutingNode and the HierarchyRoutingNode name.  E.g.:
032     * <pre>
033     * &lt;dynamic name="hierarchy"&gt;
034     *   &lt;type&gt;org.kuali.rice.kew.engine.node.hierarchyrouting.HierarchyRoutingNode&lt;/type&gt;
035     *   &lt;hierarchyProvider&gt;org.kuali.rice.kew.engine.node.hierarchyrouting.SimpleHierarchyProvider&lt;/hierarchyProvider&gt;
036     *   &lt;ruleSelector&gt;HierarchicalNamed&lt;/ruleSelector&gt;
037     * &lt;/dynamic&gt;
038     * </pre>
039     * If <code>org.kuali.rice.kew.engine.node.hierarchyrouting.SimpleHierarchyProvider</code> supplied stops named 'a', 'b', and 'c', the rules
040     * selected would be 'hierarchy-a', 'hierarchy-b', and 'hierarchy-c', respectively.
041     * @author Kuali Rice Team (rice.collab@kuali.org)
042     */
043    public class HierarchicalNamedRuleSelector extends NamedRuleSelector {
044        private static final Logger LOG = Logger.getLogger(HierarchicalNamedRuleSelector.class);
045        @Override
046        protected String getName(RouteContext context, DocumentRouteHeaderValue routeHeader, RouteNodeInstance nodeInstance,
047                String selectionCriterion, Timestamp effectiveDate) {
048            Map<String, String> cfgMap = Utilities.getKeyValueCollectionAsMap(nodeInstance.getState());
049            String stop_id = cfgMap.get(HierarchyRoutingNode.STOP_ID);
050            if (stop_id == null) {
051                LOG.warn("STOP ID from nodeInstance was NULL: " + nodeInstance);
052                return null;
053            }
054            LOG.info("STOP ID from nodeInstance: " + nodeInstance.getRouteNodeInstanceId() + ": " + stop_id);
055            return nodeInstance.getProcess().getRouteNode().getRouteNodeName() + "-" + stop_id;
056        }
057    }