001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.kew.engine.node;
017
018import org.apache.commons.collections.CollectionUtils;
019import org.apache.commons.lang.StringUtils;
020import org.kuali.rice.core.api.exception.RiceRuntimeException;
021import org.kuali.rice.kew.doctype.bo.DocumentType;
022import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
023import org.kuali.rice.kew.rule.xmlrouting.XPathHelper;
024import org.w3c.dom.Document;
025import org.w3c.dom.Element;
026import org.w3c.dom.NodeList;
027import org.xml.sax.InputSource;
028
029import javax.xml.parsers.DocumentBuilder;
030import javax.xml.parsers.DocumentBuilderFactory;
031import javax.xml.xpath.XPathConstants;
032import java.io.StringReader;
033import java.util.ArrayList;
034import java.util.Collections;
035import java.util.Comparator;
036import java.util.HashMap;
037import java.util.HashSet;
038import java.util.List;
039import java.util.Map;
040import java.util.Set;
041
042/**
043 * A simple class for performing operations on RouteNode.  In particular, this class provides some
044 * convenience methods for processing custom RouteNode XML content fragments. 
045 * 
046 * @author Kuali Rice Team (rice.collab@kuali.org)
047 *
048 */
049public final class RouteNodeUtils {
050        
051        private RouteNodeUtils() {
052                throw new UnsupportedOperationException("do not call");
053        }
054
055        /**
056         * Searches a RouteNode's "contentFragment" (it's XML definition) for an XML element with
057         * the given name and returns it's value.
058         * 
059         * <p>For example, in a node with the following definition:
060         *
061         * <pre><routeNode name="...">
062         *   ...
063         *   <myCustomProperty>propertyValue</myCustomProperty>
064         * </routeNode></pre>
065         * 
066         * <p>An invocation of getValueOfCustomProperty(routeNode, "myCustomProperty") would return
067         * "propertyValue".
068         * 
069         * @param routeNode RouteNode to examine
070         * @param propertyName name of the XML element to search for
071         * 
072         * @return the value of the XML element, or null if it could not be located
073         */
074        public static String getValueOfCustomProperty(RouteNode routeNode, String propertyName) {
075                String contentFragment = routeNode.getContentFragment();
076                String elementValue = null;
077                if (!StringUtils.isBlank(contentFragment)) {
078                        try {
079                                DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
080                                Document document = db.parse(new InputSource(new StringReader(contentFragment)));       
081                                elementValue = XPathHelper.newXPath().evaluate("//" + propertyName, document);
082                        } catch (Exception e) {
083                                throw new RiceRuntimeException("Error when attempting to parse Route Node content fragment for property name: " + propertyName, e);
084                        }
085                }
086                return elementValue;
087        }
088
089    public static List<Element> getCustomRouteNodeElements(RouteNode routeNode, String elementName) {
090        String contentFragment = routeNode.getContentFragment();
091        List<Element> elements = new ArrayList<Element>();
092                NodeList nodeList = null;
093                if (!StringUtils.isBlank(contentFragment)) {
094                        try {
095                                DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
096                                Document document = db.parse(new InputSource(new StringReader(contentFragment)));
097                                nodeList = (NodeList)XPathHelper.newXPath().evaluate("//" + elementName, document, XPathConstants.NODESET);
098                        } catch (Exception e) {
099                                throw new RiceRuntimeException("Error when attempting to parse Route Node content fragment for element name: " + elementName, e);
100                        }
101                }
102        for (int index = 0; index < nodeList.getLength(); index++) {
103            Element element = (Element)nodeList.item(index);
104            elements.add(element);
105        }
106                return elements;
107    }
108
109    public static Element getCustomRouteNodeElement(RouteNode routeNode, String elementName) {
110        List<Element> elements = getCustomRouteNodeElements(routeNode, elementName);
111        if (CollectionUtils.isEmpty(elements)) {
112            return null;
113        } else if (elements.size() > 1) {
114            throw new RiceRuntimeException("More than one element found with the given name: " + elementName);
115        }
116        return elements.get(0);
117    }
118        
119        public static List<RouteNodeInstance> getFlattenedNodeInstances(DocumentRouteHeaderValue document, boolean includeProcesses) {
120        List<RouteNodeInstance> nodeInstances = new ArrayList<RouteNodeInstance>();
121        Set<String> visitedNodeInstanceIds = new HashSet<String>();
122        for (RouteNodeInstance initialNodeInstance : document.getInitialRouteNodeInstances())
123        {
124            flattenNodeInstanceGraph(nodeInstances, visitedNodeInstanceIds, initialNodeInstance, includeProcesses);
125        }
126        return nodeInstances;
127    }
128    
129    private static void flattenNodeInstanceGraph(List<RouteNodeInstance> nodeInstances, Set<String> visitedNodeInstanceIds, RouteNodeInstance nodeInstance, boolean includeProcesses) {
130        if (visitedNodeInstanceIds.contains(nodeInstance.getRouteNodeInstanceId())) {
131            return;
132        }
133        if (includeProcesses && nodeInstance.getProcess() != null) {
134            flattenNodeInstanceGraph(nodeInstances, visitedNodeInstanceIds, nodeInstance.getProcess(), includeProcesses);
135        }
136        visitedNodeInstanceIds.add(nodeInstance.getRouteNodeInstanceId());
137        nodeInstances.add(nodeInstance);
138        for (RouteNodeInstance nextNodeInstance : nodeInstance.getNextNodeInstances())
139        {
140            flattenNodeInstanceGraph(nodeInstances, visitedNodeInstanceIds, nextNodeInstance, includeProcesses);
141        }
142    }
143    
144    public static List<RouteNode> getFlattenedNodes(DocumentType documentType, boolean climbHierarchy) {
145        List<RouteNode> nodes = new ArrayList<RouteNode>();
146        if (!documentType.isRouteInherited() || climbHierarchy) {
147            for (Object o : documentType.getProcesses())
148            {
149                ProcessDefinitionBo process = (ProcessDefinitionBo) o;
150                nodes.addAll(getFlattenedNodes(process));
151            }
152        }
153        Collections.sort(nodes, new RouteNodeSorter());
154        return nodes;
155    }
156    
157    public static List<RouteNode> getFlattenedNodes(ProcessDefinitionBo process) {
158        Map<String, RouteNode> nodesMap = new HashMap<String, RouteNode>();
159        if (process.getInitialRouteNode() != null) {
160            flattenNodeGraph(nodesMap, process.getInitialRouteNode());
161            List<RouteNode> nodes = new ArrayList<RouteNode>(nodesMap.values());
162            Collections.sort(nodes, new RouteNodeSorter());
163            return nodes;
164        } else {
165            List<RouteNode> nodes = new ArrayList<RouteNode>();
166            nodes.add(new RouteNode());
167            return nodes;
168        }
169
170    }
171    
172    /**
173     * Recursively walks the node graph and builds up the map.  Uses a map because we will
174     * end up walking through duplicates, as is the case with Join nodes.
175     * @param nodes map
176     * @param node graph
177     */
178    private static void flattenNodeGraph(Map<String, RouteNode> nodes, RouteNode node) {
179        if (node != null) {
180            if (nodes.containsKey(node.getRouteNodeName())) {
181                return;
182            }
183            nodes.put(node.getRouteNodeName(), node);
184            for (RouteNode nextNode : node.getNextNodes())
185            {
186                flattenNodeGraph(nodes, nextNode);
187            }
188        } else {
189            return;
190        }
191    }
192    
193    /**
194     * Sorts by RouteNodeId or the order the nodes will be evaluated in *roughly*.  This is 
195     * for display purposes when rendering a flattened list of nodes.
196     * 
197 * @author Kuali Rice Team (rice.collab@kuali.org)
198     */
199    private static class RouteNodeSorter implements Comparator {
200        public int compare(Object arg0, Object arg1) {
201            RouteNode rn1 = (RouteNode)arg0;
202            RouteNode rn2 = (RouteNode)arg1;
203            return rn1.getRouteNodeId().compareTo(rn2.getRouteNodeId());
204        }
205    }
206    
207    public static List<RouteNodeInstance> getActiveNodeInstances(DocumentRouteHeaderValue document) {
208        List<RouteNodeInstance> flattenedNodeInstances = getFlattenedNodeInstances(document, true);
209        List<RouteNodeInstance> activeNodeInstances = new ArrayList<RouteNodeInstance>();
210        for (RouteNodeInstance nodeInstance : flattenedNodeInstances)
211        {
212            if (nodeInstance.isActive())
213            {
214                activeNodeInstances.add(nodeInstance);
215            }
216        }
217        return activeNodeInstances;
218    }
219    
220    public static RouteNodeInstance findRouteNodeInstanceById(String nodeInstanceId, DocumentRouteHeaderValue document) {
221        List<RouteNodeInstance> flattenedNodeInstances = getFlattenedNodeInstances(document, true);
222        RouteNodeInstance niRet = null;
223        for (RouteNodeInstance nodeInstance : flattenedNodeInstances)
224        {
225            if (nodeInstanceId.equals(nodeInstance.getRouteNodeInstanceId()))
226            {
227                niRet = nodeInstance;
228                break;
229            }
230        }
231        return niRet;
232    }
233    
234    
235        
236}