Coverage Report - org.kuali.rice.kew.xml.DocumentTypeXmlParser
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentTypeXmlParser
0%
0/784
0%
0/346
12.692
DocumentTypeXmlParser$1
N/A
N/A
12.692
DocumentTypeXmlParser$DocTypeNode
0%
0/4
N/A
12.692
DocumentTypeXmlParser$RoutePathContext
0%
0/2
N/A
12.692
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.kew.xml;
 18  
 
 19  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.ACTIVE;
 20  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.APPLICATION_ID;
 21  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.APP_DOC_STATUSES;
 22  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.BLANKET_APPROVE_GROUP_NAME;
 23  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.BLANKET_APPROVE_POLICY;
 24  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.BLANKET_APPROVE_WORKGROUP_NAME;
 25  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.CUSTOM_EMAIL_STYLESHEET;
 26  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.DATA_ELEMENT;
 27  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.DEFAULT_EXCEPTION_GROUP_NAME;
 28  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.DEFAULT_EXCEPTION_WORKGROUP_NAME;
 29  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.DESCRIPTION;
 30  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.DOCUMENT_TYPE;
 31  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.DOCUMENT_TYPES;
 32  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.DOCUMENT_TYPE_OVERWRITE_MODE;
 33  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.DOC_HANDLER;
 34  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.DOC_SEARCH_HELP_URL;
 35  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.EXCEPTION_GROUP_NAME;
 36  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.EXCEPTION_WORKGROUP;
 37  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.EXCEPTION_WORKGROUP_NAME;
 38  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.HELP_DEFINITION_URL;
 39  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.INITIAL_NODE;
 40  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.LABEL;
 41  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.NAMESPACE;
 42  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.NOTIFICATION_FROM_ADDRESS;
 43  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.PARENT;
 44  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.POLICIES;
 45  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.POLICY;
 46  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.POST_PROCESSOR_NAME;
 47  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.PROCESS_NAME;
 48  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.REPORTING_GROUP_NAME;
 49  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.REPORTING_WORKGROUP_NAME;
 50  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.ROUTE_NODES;
 51  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.ROUTE_PATH;
 52  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.ROUTE_PATHS;
 53  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.ROUTING_VERSION;
 54  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.SECURITY;
 55  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.SERVICE_NAMESPACE;
 56  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.STATUS;
 57  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.SUPER_USER_GROUP_NAME;
 58  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.SUPER_USER_WORKGROUP_NAME;
 59  
 
 60  
 import java.io.BufferedInputStream;
 61  
 import java.io.ByteArrayInputStream;
 62  
 import java.io.IOException;
 63  
 import java.io.InputStream;
 64  
 import java.util.ArrayList;
 65  
 import java.util.HashMap;
 66  
 import java.util.HashSet;
 67  
 import java.util.Iterator;
 68  
 import java.util.LinkedList;
 69  
 import java.util.List;
 70  
 import java.util.Map;
 71  
 import java.util.Set;
 72  
 
 73  
 import javax.xml.parsers.ParserConfigurationException;
 74  
 import javax.xml.xpath.XPath;
 75  
 import javax.xml.xpath.XPathConstants;
 76  
 import javax.xml.xpath.XPathExpressionException;
 77  
 
 78  
 import org.apache.commons.lang.StringUtils;
 79  
 import org.kuali.rice.core.api.services.CoreApiServiceLocator;
 80  
 import org.kuali.rice.core.util.xml.XmlException;
 81  
 import org.kuali.rice.core.util.xml.XmlHelper;
 82  
 import org.kuali.rice.core.util.xml.XmlJotter;
 83  
 import org.kuali.rice.kew.doctype.ApplicationDocumentStatus;
 84  
 import org.kuali.rice.kew.doctype.DocumentTypeAttribute;
 85  
 import org.kuali.rice.kew.doctype.DocumentTypePolicy;
 86  
 import org.kuali.rice.kew.doctype.DocumentTypePolicyEnum;
 87  
 import org.kuali.rice.kew.doctype.bo.DocumentType;
 88  
 import org.kuali.rice.kew.document.DocumentTypeMaintainable;
 89  
 import org.kuali.rice.kew.engine.node.ActivationTypeEnum;
 90  
 import org.kuali.rice.kew.engine.node.BranchPrototype;
 91  
 import org.kuali.rice.kew.engine.node.NodeType;
 92  
 import org.kuali.rice.kew.engine.node.Process;
 93  
 import org.kuali.rice.kew.engine.node.RoleNode;
 94  
 import org.kuali.rice.kew.engine.node.RouteNode;
 95  
 import org.kuali.rice.kew.engine.node.RouteNodeConfigParam;
 96  
 import org.kuali.rice.kew.exception.InvalidParentDocTypeException;
 97  
 import org.kuali.rice.kew.exception.WorkflowException;
 98  
 import org.kuali.rice.kew.exception.WorkflowRuntimeException;
 99  
 import org.kuali.rice.kew.export.KewExportDataSet;
 100  
 import org.kuali.rice.kew.role.RoleRouteModule;
 101  
 import org.kuali.rice.kew.rule.FlexRM;
 102  
 import org.kuali.rice.kew.rule.bo.RuleAttribute;
 103  
 import org.kuali.rice.kew.rule.bo.RuleTemplate;
 104  
 import org.kuali.rice.kew.rule.xmlrouting.XPathHelper;
 105  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 106  
 import org.kuali.rice.kew.util.KEWConstants;
 107  
 import org.kuali.rice.kew.util.Utilities;
 108  
 import org.kuali.rice.kim.api.group.Group;
 109  
 import org.kuali.rice.kim.api.services.IdentityManagementService;
 110  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 111  
 import org.kuali.rice.kns.exception.GroupNotFoundException;
 112  
 import org.kuali.rice.kns.maintenance.Maintainable;
 113  
 import org.kuali.rice.kns.util.MaintenanceUtils;
 114  
 import org.kuali.rice.kns.util.ObjectUtils;
 115  
 import org.w3c.dom.Document;
 116  
 import org.w3c.dom.Element;
 117  
 import org.w3c.dom.NamedNodeMap;
 118  
 import org.w3c.dom.Node;
 119  
 import org.w3c.dom.NodeList;
 120  
 import org.xml.sax.SAXException;
 121  
 
 122  
 
 123  
 /**
 124  
  * A parser for parsing an XML file into {@link DocumentType}s.
 125  
  *
 126  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 127  
  */
 128  0
 public class DocumentTypeXmlParser {
 129  
 
 130  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DocumentTypeXmlParser.class);
 131  
 
 132  
     private static final String NEXT_NODE_EXP = "./@nextNode";
 133  
     private static final String PARENT_NEXT_NODE_EXP = "../@nextNode";
 134  
     private static final String NEXT_DOC_STATUS_EXP = "./@nextAppDocStatus";
 135  
     /**
 136  
      * Default route node activation type to use if omitted
 137  
      */
 138  
     private static final String DEFAULT_ACTIVATION_TYPE = "S";
 139  
 
 140  
     public List docTypeRouteNodes;
 141  
     private Map nodesMap;
 142  
     private XPath xpath;
 143  
     private Group defaultExceptionWorkgroup;
 144  
     
 145  
     protected XPath getXPath() {
 146  0
         if (this.xpath == null) {
 147  0
             this.xpath = XPathHelper.newXPath();
 148  
         }
 149  0
         return xpath;
 150  
     }
 151  
     
 152  
     public List parseDocumentTypes(InputStream input) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException, WorkflowException, GroupNotFoundException {
 153  0
         Document routeDocument=XmlHelper.trimXml(input);
 154  0
         Map documentTypesByName = new HashMap();
 155  0
         for (Iterator iterator = parseAllDocumentTypes(routeDocument).iterator(); iterator.hasNext();) {
 156  0
             DocumentType type = (DocumentType) iterator.next();
 157  0
             documentTypesByName.put(type.getName(), type);
 158  0
         }
 159  0
         return new ArrayList(documentTypesByName.values());
 160  
     }
 161  
 
 162  
     /**
 163  
      * Parses all document types, both standard and routing.
 164  
      * 
 165  
      * @param routeDocument The DOM document to parse.
 166  
      * @return A list containing the desired document types.
 167  
      */
 168  
     private List<DocumentType> parseAllDocumentTypes(Document routeDocument) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException, WorkflowException, GroupNotFoundException {
 169  
             // A mapping from the names of uninitialized parent doc types to the child nodes that depend on the parent doc.
 170  0
             Map<String,List<DocTypeNode>> pendingChildDocs = new HashMap<String,List<DocTypeNode>>();
 171  
             // A mapping from the names of uninitialized parent doc types to the names of the dependent children.
 172  0
             Map<String,List<String>> pendingChildNames = new HashMap<String,List<String>>();
 173  
             // A stack containing Iterators over the various lists of unprocessed nodes; this allows for faster parent-child resolution
 174  
             // without having to use recursion.
 175  0
         List<Iterator<DocTypeNode>> docInitStack = new ArrayList<Iterator<DocTypeNode>>();
 176  
         // The first List of document types.
 177  0
         List<DocTypeNode> initialList = new ArrayList<DocTypeNode>();
 178  
             // The current size of the stack.
 179  0
             int stackLen = 0;
 180  
             // The current Iterator instance.
 181  0
             Iterator<DocTypeNode> currentIter = null;
 182  
         // The current document type node.
 183  0
         DocTypeNode currDocNode = null;
 184  
 
 185  0
         List<DocumentType> docTypeBeans = new ArrayList<DocumentType>();
 186  
                 
 187  
         // Acquire the "standard" and "routing" document types.
 188  0
         NodeList initialNodes = null;
 189  0
         xpath = XPathHelper.newXPath();
 190  0
         initialNodes = (NodeList) getXPath().evaluate("/" + DATA_ELEMENT + "/" + DOCUMENT_TYPES + "/" + DOCUMENT_TYPE, routeDocument, XPathConstants.NODESET);
 191  
         // Take each NodeList's nodes and insert them into a List implementation.
 192  0
         for (int j = 0; j < initialNodes.getLength(); j++) {
 193  0
             Node documentTypeNode = initialNodes.item(j);
 194  0
             boolean docIsStandard = true;
 195  
             try {
 196  0
                 String xpathModeExpression = "./@" + DOCUMENT_TYPE_OVERWRITE_MODE;
 197  0
                 if (XmlHelper.pathExists(xpath, xpathModeExpression, documentTypeNode)) {
 198  0
                     String overwriteMode = (String) getXPath().evaluate(xpathModeExpression, documentTypeNode, XPathConstants.STRING);
 199  0
                     docIsStandard = !StringUtils.equalsIgnoreCase("true", overwriteMode);
 200  
                 }
 201  0
             } catch (XPathExpressionException xpee) {
 202  0
                 LOG.error("Error trying to check for '" + DOCUMENT_TYPE_OVERWRITE_MODE + "' attribute on document type element", xpee);
 203  0
                 throw xpee;
 204  0
             }
 205  0
                 initialList.add(new DocTypeNode(documentTypeNode, docIsStandard));
 206  
         }
 207  
 
 208  
         // Setup the Iterator instance to start with.
 209  0
         currentIter = initialList.iterator();
 210  
         
 211  
         // Keep looping until all Iterators are complete or an uncaught exception is thrown.
 212  0
         while (stackLen >= 0) {
 213  
                 // Determine the action to take based on whether there are remaining nodes in the present iterator.
 214  0
                 if (currentIter.hasNext()) {
 215  
                         // If the current iterator still has more nodes, process the next one.
 216  0
                         String newParentName = null;
 217  0
                         currDocNode = currentIter.next();
 218  
                         // Initialize the document, and catch any child initialization problems.
 219  
                         try {
 220  
                                 // Take appropriate action based on whether the document is a standard one or a routing one.
 221  0
                                 DocumentType docType = parseDocumentType(!currDocNode.isStandard, currDocNode.docNode); 
 222  
                                 // Insert into appropriate position in the final list, based on the doc type's location in the XML file's list.
 223  0
                                       docTypeBeans.add(docType);
 224  
                             // Store the document's name for reference.
 225  0
                             newParentName = docType.getName();
 226  
                         }
 227  0
                         catch (InvalidParentDocTypeException exc) {
 228  
                                 // If the parent document has not been processed yet, then store the child document.
 229  0
                             List<DocTypeNode> tempList = null;
 230  0
                             List<String> tempStrList = null;
 231  0
                             String parentName = exc.getParentName();
 232  0
                             String childName = exc.getChildName();
 233  0
                             if (parentName == null || childName == null) { // Make sure the parent & child documents' names are defined.
 234  0
                                     throw exc;
 235  
                             }
 236  0
                             tempList = pendingChildDocs.get(parentName);
 237  0
                             tempStrList = pendingChildNames.get(parentName);
 238  0
                             if (tempList == null) { // Initialize a new child document list if necessary.
 239  0
                                     tempList = new ArrayList<DocTypeNode>();
 240  0
                                     tempStrList = new ArrayList<String>();
 241  0
                                     pendingChildDocs.put(parentName, tempList);
 242  0
                                     pendingChildNames.put(parentName, tempStrList);
 243  
                             }
 244  0
                                 tempList.add(currDocNode);
 245  0
                                 tempStrList.add(childName);
 246  0
                         }
 247  
                         
 248  
                     // Check for any delayed child documents that are dependent on the current document.
 249  0
                         List<DocTypeNode> childrenToProcess = pendingChildDocs.remove(newParentName);
 250  0
                         pendingChildNames.remove(newParentName);
 251  0
                         if (childrenToProcess != null) {
 252  0
                                 LOG.info("'" + newParentName + "' has children that were delayed; now processing them...");
 253  
                                 // If there are any pending children, push the old Iterator onto the stack and process the new Iterator on the next
 254  
                                 // iteration of the loop.
 255  0
                                 stackLen++;
 256  0
                                 docInitStack.add(currentIter);
 257  0
                                 currentIter = childrenToProcess.iterator();
 258  
                         }
 259  0
                 }
 260  
                 else {
 261  
                         // If the current Iterator has reached its end, discard it and pop the next one (if any) from the stack.
 262  0
                         stackLen--;
 263  0
                         currentIter = ((stackLen >= 0) ? docInitStack.remove(stackLen) : null);
 264  
                  }
 265  
         }
 266  
         
 267  
         // Throw an error if there are still any uninitialized child documents.
 268  0
         if (pendingChildDocs.size() > 0) {
 269  0
                 StringBuilder errMsg = new StringBuilder("Invalid parent document types: ");
 270  
                 // Construct the error message.
 271  0
                 for (Iterator<String> unknownParents = pendingChildNames.keySet().iterator(); unknownParents.hasNext();) {
 272  0
                         String currParent = unknownParents.next();
 273  0
                         errMsg.append("Invalid parent doc type '").append(currParent).append("' is needed by child doc types ");
 274  0
                         for (Iterator<String> failedChildren = pendingChildNames.get(currParent).iterator(); failedChildren.hasNext();) {
 275  0
                                 String currChild = failedChildren.next();
 276  0
                                 errMsg.append('\'').append(currChild).append((failedChildren.hasNext()) ? "', " : "'; ");
 277  0
                         }
 278  0
                 }
 279  
                 // Throw the exception.
 280  0
                 throw new InvalidParentDocTypeException(null, null, errMsg.toString());
 281  
         }
 282  
         
 283  0
             return docTypeBeans;
 284  
     }
 285  
 
 286  
     private DocumentType parseDocumentType(boolean isOverwrite, Node documentTypeNode) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException, WorkflowException, GroupNotFoundException {
 287  0
         DocumentType documentType = getFullDocumentType(isOverwrite, documentTypeNode);
 288  
 //        parseStructure(isOverwrite, documentTypeNode, documentType, new RoutePathContext());
 289  
         // reset variables
 290  0
         docTypeRouteNodes = null;
 291  0
         nodesMap = null;
 292  0
         xpath = null;
 293  0
         defaultExceptionWorkgroup = null;
 294  
 
 295  0
         LOG.debug("Saving document type " + documentType.getName());
 296  0
         routeDocumentType(documentType);
 297  0
         return documentType;
 298  
     }
 299  
 
 300  
     private DocumentType getFullDocumentType(boolean isOverwrite, Node documentTypeNode) throws XPathExpressionException, GroupNotFoundException, XmlException, WorkflowException, SAXException, IOException, ParserConfigurationException {
 301  0
         DocumentType documentType = getDocumentType(isOverwrite, documentTypeNode);
 302  
         /*
 303  
          * The following code does not need to apply the isOverwrite mode logic because it already checks to see if each node
 304  
          * is available on the ingested XML. If the node is ingested then it doesn't matter if we're in overwrite mode or not
 305  
          * the ingested code should save.
 306  
          */
 307  0
         NodeList policiesList = (NodeList) getXPath().evaluate("./" + POLICIES, documentTypeNode, XPathConstants.NODESET);
 308  0
         if (policiesList.getLength() > 1) {
 309  
             // more than one <policies> tag is invalid
 310  0
             throw new XmlException("More than one " + POLICIES + " node is present in a document type node");
 311  
         }
 312  0
         else if (policiesList.getLength() > 0) {
 313  
             // if there is exactly one <policies> tag then parse it and use the values
 314  0
             NodeList policyNodes = (NodeList) getXPath().evaluate("./" + POLICY, policiesList.item(0), XPathConstants.NODESET);
 315  0
             documentType.setPolicies(getDocumentTypePolicies(policyNodes, documentType));
 316  
         }
 317  
 
 318  0
         NodeList attributeList = (NodeList) getXPath().evaluate("./attributes", documentTypeNode, XPathConstants.NODESET);
 319  0
         if (attributeList.getLength() > 1) {
 320  0
             throw new XmlException("More than one attributes node is present in a document type node");
 321  
         }
 322  0
         else if (attributeList.getLength() > 0) {
 323  0
             NodeList attributeNodes = (NodeList) getXPath().evaluate("./attribute", attributeList.item(0), XPathConstants.NODESET);
 324  0
             documentType.setDocumentTypeAttributes(getDocumentTypeAttributes(attributeNodes, documentType));
 325  
         }
 326  
 
 327  0
         NodeList securityList = (NodeList) getXPath().evaluate("./" + SECURITY, documentTypeNode, XPathConstants.NODESET);
 328  0
         if (securityList.getLength() > 1) {
 329  0
             throw new XmlException("More than one " + SECURITY + " node is present in a document type node");
 330  
         }
 331  0
         else if (securityList.getLength() > 0) {
 332  
            try {
 333  0
              Node securityNode = securityList.item(0);
 334  0
              String securityText = XmlJotter.jotNode(securityNode);
 335  0
              documentType.setDocumentTypeSecurityXml(securityText);
 336  
            }
 337  0
            catch (Exception e) {
 338  0
              throw new XmlException(e);
 339  0
            }
 340  
         }
 341  0
         parseStructure(isOverwrite, documentTypeNode, documentType, new RoutePathContext());
 342  0
         return documentType;
 343  
     }
 344  
 
 345  
     private void parseStructure(boolean isOverwrite, Node documentTypeNode, DocumentType documentType, RoutePathContext context) throws XPathExpressionException, XmlException, GroupNotFoundException {
 346  
         // TODO have a validation function that takes an xpath statement and blows chunks if that
 347  
         // statement returns false
 348  0
         boolean hasRoutePathsElement = false;
 349  
         try {
 350  0
             hasRoutePathsElement = XmlHelper.pathExists(xpath, "./" + ROUTE_PATHS, documentTypeNode);
 351  0
         } catch (XPathExpressionException xpee) {
 352  0
             LOG.error("Error obtaining document type " + ROUTE_PATHS, xpee);
 353  0
             throw xpee;
 354  0
         }
 355  0
         boolean hasRouteNodesElement = false;
 356  
         try {
 357  0
             hasRouteNodesElement = XmlHelper.pathExists(xpath, "./" + ROUTE_NODES, documentTypeNode);
 358  0
         } catch (XPathExpressionException xpee) {
 359  0
             LOG.error("Error obtaining document type " + ROUTE_NODES, xpee);
 360  0
             throw xpee;
 361  0
         }
 362  
 
 363  
         // check to see if we're in overwrite mode
 364  0
         if (isOverwrite) {
 365  
             // since we're in overwrite mode, if we don't have a routeNodes element or a routePaths element we simply return
 366  0
             if (!hasRouteNodesElement && !hasRoutePathsElement) {
 367  0
                 return;
 368  
             }
 369  
             // if we have route nodes and route paths we're going to overwrite all existing processes so we can clear the current list
 370  0
             else if (hasRouteNodesElement && hasRoutePathsElement) {
 371  0
                 documentType.setProcesses(new ArrayList());
 372  
             }
 373  
             // check to see if we have one but not the other element of routePaths and routeNodes
 374  0
             else if (!hasRouteNodesElement || !hasRoutePathsElement) {
 375  
                 // throw an exception since an ingestion can only have neither or both of the routePaths and routeNodes elements
 376  0
                 throw new XmlException("A overwriting document type ingestion can not have only one of the " + ROUTE_PATHS + " and " + ROUTE_NODES + " elements.  Either both or neither should be defined.");
 377  
             }
 378  
         }
 379  
 
 380  
         NodeList processNodes;
 381  
 
 382  
         try {
 383  0
             if (XmlHelper.pathExists(xpath, "./" + ROUTE_PATHS + "/" + ROUTE_PATH, documentTypeNode)) {
 384  0
                 processNodes = (NodeList) getXPath().evaluate("./" + ROUTE_PATHS + "/" + ROUTE_PATH, documentTypeNode, XPathConstants.NODESET);
 385  
             } else {
 386  0
                 if (hasRoutePathsElement) {
 387  0
                     createEmptyProcess(documentType);
 388  
                 }
 389  0
                 return;
 390  
             }
 391  0
         } catch (XPathExpressionException xpee) {
 392  0
             LOG.error("Error obtaining document type routePaths", xpee);
 393  0
             throw xpee;
 394  0
         }
 395  
 
 396  0
         createProcesses(processNodes, documentType);
 397  
 
 398  0
         NodeList nodeList = null;
 399  
         try {
 400  0
             nodeList = (NodeList) getXPath().evaluate("./" + ROUTE_PATHS + "/" + ROUTE_PATH + "/start", documentTypeNode, XPathConstants.NODESET);
 401  0
         } catch (XPathExpressionException xpee) {
 402  0
             LOG.error("Error obtaining document type routePath start", xpee);
 403  0
             throw xpee;
 404  0
         }
 405  0
         if (nodeList.getLength() > 1) {
 406  0
             throw new XmlException("More than one start node is present in route path");
 407  0
         } else if (nodeList.getLength() == 0) {
 408  0
             throw new XmlException("No start node is present in route path");
 409  
         }
 410  
         try {
 411  0
             nodeList = (NodeList) getXPath().evaluate(".//" + ROUTE_NODES, documentTypeNode, XPathConstants.NODESET);
 412  0
         } catch (XPathExpressionException xpee) {
 413  0
             LOG.error("Error obtaining document type routeNodes", xpee);
 414  0
             throw xpee;
 415  0
         }
 416  0
         if (nodeList.getLength() > 1) {
 417  0
             throw new XmlException("More than one routeNodes node is present in documentType node");
 418  0
         } else if (nodeList.getLength() == 0) {
 419  0
             throw new XmlException("No routeNodes node is present in documentType node");
 420  
         }
 421  0
         Node routeNodesNode = nodeList.item(0);
 422  0
         checkForOrphanedRouteNodes(documentTypeNode, routeNodesNode);
 423  
 
 424  
         // passed validation.
 425  0
         nodesMap = new HashMap();
 426  0
         for (int index = 0; index < processNodes.getLength(); index++) {
 427  0
             Node processNode = processNodes.item(index);
 428  
             String startName;
 429  
             try {
 430  0
                 startName = (String) getXPath().evaluate("./start/@name", processNode, XPathConstants.STRING);
 431  0
             } catch (XPathExpressionException xpee) {
 432  0
                 LOG.error("Error obtaining routePath start name attribute", xpee);
 433  0
                 throw xpee;
 434  0
             }
 435  0
             String processName = KEWConstants.PRIMARY_PROCESS_NAME;
 436  0
             if (org.apache.commons.lang.StringUtils.isEmpty(startName)) {
 437  
                 try {
 438  0
                     startName = (String) getXPath().evaluate("./@" + INITIAL_NODE, processNode, XPathConstants.STRING);
 439  0
                 } catch (XPathExpressionException xpee) {
 440  0
                     LOG.error("Error obtaining routePath initialNode attribute", xpee);
 441  0
                     throw xpee;
 442  0
                 }
 443  
                 try {
 444  0
                     processName = (String) getXPath().evaluate("./@" + PROCESS_NAME, processNode, XPathConstants.STRING);
 445  0
                 } catch (XPathExpressionException xpee) {
 446  0
                     LOG.error("Error obtaining routePath processName attribute", xpee);
 447  0
                     throw xpee;
 448  0
                 }
 449  0
                 if (org.apache.commons.lang.StringUtils.isEmpty(startName)) {
 450  0
                     throw new XmlException("Invalid routePath: no initialNode attribute defined!");
 451  
                 }
 452  
             }
 453  0
             RouteNode routeNode = createRouteNode(null, startName, processNode, routeNodesNode, documentType, context);
 454  0
             if (routeNode != null) {
 455  0
                 Process process = documentType.getNamedProcess(processName);
 456  0
                 process.setInitialRouteNode(routeNode);
 457  
             }
 458  
         }
 459  
 
 460  0
     }
 461  
 
 462  
     private DocumentType getDocumentType(boolean isOverwrite, Node documentTypeNode) throws XPathExpressionException, GroupNotFoundException, XmlException, WorkflowException, SAXException, IOException, ParserConfigurationException {
 463  0
         DocumentType documentType = null;
 464  0
         String documentTypeName = getDocumentTypeNameFromNode(documentTypeNode);
 465  0
         DocumentType previousDocumentType = KEWServiceLocator.getDocumentTypeService().findByName(documentTypeName);
 466  0
         if (isOverwrite) {
 467  
             // we don't need the isOverwrite value to be passed here because we're only temporarily creating this document type in memory
 468  0
             documentType = generateNewDocumentTypeFromExisting(documentTypeName);
 469  
             // export the document type that exists in the database
 470  
         }
 471  
         // if we don't have a valid value for documentType create a brand new instance
 472  0
         if (ObjectUtils.isNull(documentType)) {
 473  0
             documentType = new DocumentType();
 474  
         }
 475  0
         documentType.setName(documentTypeName);
 476  
 
 477  
         // set the description on the document type
 478  
         // the description is always taken from the previous document type unless specified in the ingested file
 479  0
         String description = null;
 480  
         try {
 481  0
             description = (String) getXPath().evaluate("./" + DESCRIPTION, documentTypeNode, XPathConstants.STRING);
 482  0
         } catch (XPathExpressionException xpee) {
 483  0
             LOG.error("Error obtaining document type description", xpee);
 484  0
             throw xpee;
 485  0
         }
 486  
         // if the ingestion has produced a valid value then set it on the document
 487  0
         if (StringUtils.isNotBlank(description)) {
 488  0
             documentType.setDescription(description);
 489  
         }
 490  
         // at this point we know the ingested value is blank
 491  0
         else if (!isOverwrite) {
 492  
             // if this is not an overwrite we need to check the previous document type version for a value to pull forward
 493  0
             if ( (ObjectUtils.isNotNull(previousDocumentType)) && (StringUtils.isNotBlank(previousDocumentType.getDescription())) ) {
 494  
                 // keep the same value from the previous version of the document type from the database
 495  0
                 description = previousDocumentType.getDescription();
 496  
             }
 497  0
             documentType.setDescription(description);
 498  
         }
 499  
 
 500  
         // set the label on the document type
 501  0
         String label = null;
 502  
         try {
 503  0
             label = (String) getXPath().evaluate("./" + LABEL, documentTypeNode, XPathConstants.STRING);
 504  0
         } catch (XPathExpressionException xpee) {
 505  0
             LOG.error("Error obtaining document type label", xpee);
 506  0
             throw xpee;
 507  0
         }
 508  
         // if the ingestion has produced a valid value then set it on the document
 509  0
         if (StringUtils.isNotBlank(label)) {
 510  0
             documentType.setLabel(label);
 511  
         }
 512  
         // at this point we know the ingested value is blank
 513  0
         else if (!isOverwrite) {
 514  
             // if this is not an overwrite we need to check the previous document type version for a value to pull forward
 515  0
             if (ObjectUtils.isNotNull(previousDocumentType) && StringUtils.isNotBlank(previousDocumentType.getLabel())) {
 516  
                 // keep the same value from the previous version of the document type from the database
 517  0
                 label = previousDocumentType.getLabel();
 518  
             } else {
 519  
                 // otherwise set it to undefined
 520  0
                 label = KEWConstants.DEFAULT_DOCUMENT_TYPE_LABEL;
 521  
             }
 522  0
             documentType.setLabel(label);
 523  
         }
 524  
 
 525  
         // set the post processor class on the document type
 526  
         try {
 527  
             /*
 528  
              * - if the element tag is ingested... take whatever value is given, even if it's empty 
 529  
              * - we disregard the isOverwrite because if the element tag does not exist in the ingestion
 530  
              * then the documentType should already carry the value from the previous document type
 531  
              */
 532  0
             if (XmlHelper.pathExists(xpath, "./" + POST_PROCESSOR_NAME, documentTypeNode)) {
 533  0
                 String postProcessor = (String) getXPath().evaluate("./" + POST_PROCESSOR_NAME, documentTypeNode, XPathConstants.STRING);
 534  0
                 if (StringUtils.isEmpty(postProcessor)) {
 535  0
                     documentType.setPostProcessorName(KEWConstants.POST_PROCESSOR_NON_DEFINED_VALUE);
 536  
                 } else {
 537  0
                     documentType.setPostProcessorName((String) getXPath().evaluate("./" + POST_PROCESSOR_NAME, documentTypeNode, XPathConstants.STRING));
 538  
                 }    
 539  
             }
 540  
                 
 541  0
         } catch (XPathExpressionException xpee) {
 542  0
             LOG.error("Error obtaining document type postProcessorName", xpee);
 543  0
             throw xpee;
 544  0
         }
 545  
 
 546  
         // set the document handler URL on the document type
 547  
         try {
 548  
             /*
 549  
              * - if the element tag is ingested... take whatever value is given, even if it's empty 
 550  
              * - we disregard the isOverwrite because if the element tag does not exist in the ingestion
 551  
              * then the documentType should already carry the value from the previous document type
 552  
              */
 553  0
             if (XmlHelper.pathExists(xpath, "./" + DOC_HANDLER, documentTypeNode)) {
 554  0
                 documentType.setUnresolvedDocHandlerUrl((String) getXPath().evaluate("./" + DOC_HANDLER, documentTypeNode, XPathConstants.STRING));
 555  
             }
 556  0
         } catch (XPathExpressionException xpee) {
 557  0
             LOG.error("Error obtaining document type docHandler", xpee);
 558  0
             throw xpee;
 559  0
         }
 560  
 
 561  
         // set the help definition URL on the document type
 562  0
         String helpDefUrl = null;
 563  
         try {
 564  0
             helpDefUrl = (String) getXPath().evaluate("./" + HELP_DEFINITION_URL, documentTypeNode, XPathConstants.STRING);
 565  0
         } catch (XPathExpressionException xpee) {
 566  0
             LOG.error("Error obtaining document type help definition url", xpee);
 567  0
             throw xpee;
 568  0
         }
 569  
         // if the ingestion has produced a valid value then set it on the document
 570  0
         if (StringUtils.isNotBlank(helpDefUrl)) {
 571  0
             documentType.setUnresolvedHelpDefinitionUrl(helpDefUrl);
 572  
         }
 573  
         // at this point we know the ingested value is blank
 574  0
         else if (!isOverwrite) {
 575  
             // if this is not an overwrite, we need to check the previous document type version for a value to pull forward
 576  0
             if ( (ObjectUtils.isNotNull(previousDocumentType)) && (StringUtils.isNotBlank(previousDocumentType.getUnresolvedHelpDefinitionUrl())) ) {
 577  
                 // keep the same value from the previous version of the document type from the database
 578  0
                 helpDefUrl = previousDocumentType.getUnresolvedHelpDefinitionUrl();
 579  
             }
 580  0
             documentType.setUnresolvedHelpDefinitionUrl(helpDefUrl);
 581  
         }
 582  
         
 583  
         // set the doc search help URL on the document type
 584  0
         String docSearchHelpUrl = null;
 585  
         try {
 586  0
             docSearchHelpUrl = (String) getXPath().evaluate("./" + DOC_SEARCH_HELP_URL, documentTypeNode, XPathConstants.STRING);
 587  0
         } catch (XPathExpressionException xpee) {
 588  0
             LOG.error("Error obtaining document type document search help url", xpee);
 589  0
             throw xpee;
 590  0
         }
 591  
         // if the ingestion has produced a valid value then set it on the document
 592  0
         if (StringUtils.isNotBlank(docSearchHelpUrl)) {
 593  0
             documentType.setUnresolvedDocSearchHelpUrl(docSearchHelpUrl);
 594  
         }
 595  
         // at this point we know the ingested value is blank
 596  0
         else if (!isOverwrite) {
 597  
             // if this is not an overwrite, we need to check the previous document type version for a value to pull forward
 598  0
             if ( (ObjectUtils.isNotNull(previousDocumentType)) && (StringUtils.isNotBlank(previousDocumentType.getUnresolvedDocSearchHelpUrl())) ) {
 599  
                 // keep the same value from the previous version of the document type from the database
 600  0
                 docSearchHelpUrl = previousDocumentType.getUnresolvedDocSearchHelpUrl();
 601  
             }
 602  0
             documentType.setUnresolvedDocSearchHelpUrl(docSearchHelpUrl);
 603  
         }
 604  
 
 605  
         // set the application id on the document type
 606  
         try {
 607  
             /*
 608  
              * - if the element tag is ingested... take whatever value is given, even if it's empty 
 609  
              * - we disregard the isOverwrite because if the element tag does not exist in the ingestion
 610  
              * then the documentType should already carry the value from the previous document type
 611  
              */
 612  0
                 if (XmlHelper.pathExists(xpath, "./" + APPLICATION_ID, documentTypeNode)) {
 613  0
                         documentType.setActualApplicationId((String) getXPath().evaluate("./" + APPLICATION_ID, documentTypeNode, XPathConstants.STRING));
 614  0
                 } else if (XmlHelper.pathExists(xpath, "./" + SERVICE_NAMESPACE, documentTypeNode)) {
 615  0
                 documentType.setActualApplicationId((String) getXPath().evaluate("./" + SERVICE_NAMESPACE, documentTypeNode, XPathConstants.STRING));
 616  0
                 LOG.warn(SERVICE_NAMESPACE + " element was set on document type XML but is deprecated and will be removed in a future version, please use " + APPLICATION_ID + " instead.");
 617  
             }
 618  0
         } catch (XPathExpressionException xpee) {
 619  0
             LOG.error("Error obtaining document type applicationId", xpee);
 620  0
             throw xpee;
 621  0
         }
 622  
 
 623  
         // set the notification from address on the document type
 624  
         try {
 625  
             /*
 626  
              * - if the element tag is ingested... take whatever value is given, even if it's empty 
 627  
              * - we disregard the isOverwrite because if the element tag does not exist in the ingestion
 628  
              * then the documentType should already carry the value from the previous document type
 629  
              */
 630  0
             if (XmlHelper.pathExists(xpath, "./" + NOTIFICATION_FROM_ADDRESS, documentTypeNode)) {
 631  0
                 documentType.setActualNotificationFromAddress((String) getXPath().evaluate("./" + NOTIFICATION_FROM_ADDRESS, documentTypeNode, XPathConstants.STRING));
 632  
             }
 633  0
         } catch (XPathExpressionException xpee) {
 634  0
             LOG.error("Error obtaining document type " + NOTIFICATION_FROM_ADDRESS, xpee);
 635  0
             throw xpee;
 636  0
         }
 637  
 
 638  
         try {
 639  
             /*
 640  
              * - if the element tag is ingested... take whatever value is given, even if it's empty 
 641  
              * - we disregard the isOverwrite because if the element tag does not exist in the ingestion
 642  
              * then the documentType should already carry the value from the previous document type
 643  
              */
 644  0
             if (XmlHelper.pathExists(xpath, "./" + CUSTOM_EMAIL_STYLESHEET, documentTypeNode)) {
 645  0
                 documentType.setCustomEmailStylesheet((String) getXPath().evaluate("./" + CUSTOM_EMAIL_STYLESHEET, documentTypeNode, XPathConstants.STRING));
 646  
             }
 647  0
         } catch (XPathExpressionException xpee) {
 648  0
             LOG.error("Error obtaining document type " + CUSTOM_EMAIL_STYLESHEET, xpee);
 649  0
             throw xpee;
 650  0
         }
 651  
 
 652  
         // any ingested document type by default becomes the current document type
 653  0
         documentType.setCurrentInd(Boolean.TRUE);
 654  
 
 655  
         // set up the default exception workgroup for the document type
 656  0
         String exceptionWg = null;
 657  0
         String exceptionWgName = null;
 658  0
         String exceptionWgNamespace = null;
 659  
         try {
 660  0
                 if (XmlHelper.pathExists(xpath, "./" + DEFAULT_EXCEPTION_GROUP_NAME, documentTypeNode)) {
 661  0
                         exceptionWgName = (String) getXPath().evaluate("./" + DEFAULT_EXCEPTION_GROUP_NAME, documentTypeNode, XPathConstants.STRING);
 662  0
                         exceptionWgNamespace = (String) getXPath().evaluate("./" + DEFAULT_EXCEPTION_GROUP_NAME + "/@" + NAMESPACE, documentTypeNode, XPathConstants.STRING);
 663  0
                         exceptionWg = exceptionWgName;
 664  
                 } else {
 665  0
                         exceptionWg = (String) getXPath().evaluate("./" + DEFAULT_EXCEPTION_WORKGROUP_NAME, documentTypeNode, XPathConstants.STRING);
 666  
                 }
 667  0
         } catch (XPathExpressionException xpee) {
 668  0
             LOG.error("Error obtaining document type " + DEFAULT_EXCEPTION_GROUP_NAME, xpee);
 669  0
             throw xpee;
 670  0
         }
 671  
         // we don't need to take the isOverwrite into account here because this ingestion method is a shortcut to use the same workgroup in all route nodes
 672  0
         if (StringUtils.isNotBlank(exceptionWg)) {
 673  0
                 if (StringUtils.isNotBlank(exceptionWgName)) { // Found a "defaultExceptionGroupName" element.
 674  
                         // allow core config parameter replacement in documenttype workgroups
 675  0
                         exceptionWgName = Utilities.substituteConfigParameters(exceptionWgName).trim();
 676  0
                         exceptionWgNamespace = Utilities.substituteConfigParameters(exceptionWgNamespace).trim();
 677  
                 } else { // Found a deprecated "defaultExceptionWorkgroupName" element.
 678  0
                         LOG.warn((new StringBuilder(160)).append("Document Type XML is using deprecated element '").append(DEFAULT_EXCEPTION_WORKGROUP_NAME).append(
 679  
                                         "', please use '").append(DEFAULT_EXCEPTION_GROUP_NAME).append("' instead.").toString());
 680  
                     // allow core config parameter replacement in documenttype workgroups
 681  0
                     exceptionWg = Utilities.substituteConfigParameters(exceptionWg);
 682  0
                     exceptionWgName = Utilities.parseGroupName(exceptionWg);
 683  0
                     exceptionWgNamespace = Utilities.parseGroupNamespaceCode(exceptionWg);
 684  
                 }
 685  0
             Group exceptionGroup = getIdentityManagementService().getGroupByName(exceptionWgNamespace, exceptionWgName);
 686  0
             if(exceptionGroup == null) {
 687  0
                        throw new WorkflowRuntimeException("Exception workgroup name " + exceptionWgName + " does not exist");
 688  
             }
 689  0
             documentType.setDefaultExceptionWorkgroup(exceptionGroup);
 690  0
             defaultExceptionWorkgroup = exceptionGroup;
 691  
         }
 692  
 
 693  
         // set up the active indicator on the document type
 694  
         try {
 695  
              // if the element tag is ingested... take whatever value is given
 696  0
             if (XmlHelper.pathExists(xpath, "./" + ACTIVE, documentTypeNode)) {
 697  0
                 documentType.setActive(Boolean.valueOf((String) getXPath().evaluate("./" + ACTIVE, documentTypeNode, XPathConstants.STRING)));
 698  
             } 
 699  
             // if isOverwrite is false set the default value
 700  0
             else if (!isOverwrite) {
 701  0
                 documentType.setActive(Boolean.TRUE);
 702  
             }
 703  0
         } catch (XPathExpressionException xpee) {
 704  0
             LOG.error("Error obtaining document type active flag", xpee);
 705  0
             throw xpee;
 706  0
         }
 707  
 
 708  
         // check for a parent document type for the ingested document type
 709  0
         boolean parentElementExists = false;
 710  
         try {
 711  0
             parentElementExists = XmlHelper.pathExists(xpath, "./" + PARENT, documentTypeNode);
 712  0
         } catch (XPathExpressionException xpee) {
 713  0
             LOG.error("Error obtaining document type parent", xpee);
 714  0
             throw xpee;
 715  0
         }
 716  
         /*
 717  
          * - if the element tag is ingested... take whatever value is given 
 718  
          * - we disregard the isOverwrite because if the element tag does not exist in the ingestion
 719  
          * then the documentType should already carry the value from the previous document type
 720  
          */
 721  0
         if (parentElementExists) {
 722  
             // the tag was ingested so we'll use whatever the user attempted to set
 723  0
             String parentDocumentTypeName = null;
 724  
             try {
 725  0
                 parentDocumentTypeName = (String) getXPath().evaluate("./" + PARENT, documentTypeNode, XPathConstants.STRING);
 726  0
             } catch (XPathExpressionException xpee) {
 727  0
                 LOG.error("Error obtaining document type parent", xpee);
 728  0
                 throw xpee;
 729  0
             }
 730  0
             DocumentType parentDocumentType = KEWServiceLocator.getDocumentTypeService().findByName(parentDocumentTypeName);
 731  0
             if (parentDocumentType == null) {
 732  
                 //throw new XmlException("Invalid parent document type: '" + parentDocumentTypeName + "'");
 733  0
                 LOG.info("Parent document type '" + parentDocumentTypeName +
 734  
                         "' could not be found; attempting to delay processing of '" + documentTypeName + "'...");
 735  0
                 throw new InvalidParentDocTypeException(parentDocumentTypeName, documentTypeName,
 736  
                         "Invalid parent document type: '" + parentDocumentTypeName + "'");
 737  
             }
 738  0
             documentType.setDocTypeParentId(parentDocumentType.getDocumentTypeId());
 739  
         }
 740  
 
 741  
         // set the super user workgroup name on the document type
 742  
         try {
 743  
             /*
 744  
              * - if the element tag is ingested... take whatever value is given
 745  
              * - we disregard the isOverwrite because if the element tag does not exist in the ingestion
 746  
              * then the documentType should already carry the value from the previous document type
 747  
              */
 748  0
                 if (XmlHelper.pathExists(xpath, "./" + SUPER_USER_GROUP_NAME, documentTypeNode)) {
 749  0
                         documentType.setSuperUserWorkgroupNoInheritence(retrieveValidKimGroup("./" + SUPER_USER_GROUP_NAME, documentTypeNode, false));
 750  
                 }
 751  0
                 else if (XmlHelper.pathExists(xpath, "./" + SUPER_USER_WORKGROUP_NAME, documentTypeNode)) {
 752  0
                         LOG.warn((new StringBuilder(160)).append("Document Type XML is using deprecated element '").append(SUPER_USER_WORKGROUP_NAME).append(
 753  
                                         "', please use '").append(SUPER_USER_GROUP_NAME).append("' instead.").toString());
 754  0
                 documentType.setSuperUserWorkgroupNoInheritence(retrieveValidKimGroup("./" + SUPER_USER_WORKGROUP_NAME, documentTypeNode, true));
 755  
             }
 756  0
         } catch (XPathExpressionException xpee) {
 757  0
             LOG.error("Error obtaining document type " + SUPER_USER_GROUP_NAME, xpee);
 758  0
             throw xpee;
 759  0
         }
 760  
 
 761  
         // set the blanket approve workgroup name on the document type
 762  0
         String blanketWorkGroup = null;
 763  0
         String blanketGroupName = null;
 764  0
         String blanketNamespace = null;
 765  0
         String blanketApprovePolicy = null;
 766  
         try {
 767  
             // check if the blanket approve workgroup name element tag was set on the ingested document type and get value if it was
 768  0
                 if (XmlHelper.pathExists(xpath, "./" + BLANKET_APPROVE_GROUP_NAME, documentTypeNode)) {
 769  0
                         blanketGroupName = (String) getXPath().evaluate("./" + BLANKET_APPROVE_GROUP_NAME, documentTypeNode, XPathConstants.STRING);
 770  0
                         blanketNamespace = (String) getXPath().evaluate("./" + BLANKET_APPROVE_GROUP_NAME + "/@" + NAMESPACE, documentTypeNode, XPathConstants.STRING);
 771  0
                         blanketWorkGroup = blanketGroupName;
 772  
                 }
 773  0
                 else if (XmlHelper.pathExists(xpath, "./" + BLANKET_APPROVE_WORKGROUP_NAME, documentTypeNode)) {
 774  0
                 blanketWorkGroup = (String) getXPath().evaluate("./" + BLANKET_APPROVE_WORKGROUP_NAME, documentTypeNode, XPathConstants.STRING);
 775  
             }
 776  0
         } catch (XPathExpressionException xpee) {
 777  0
             LOG.error("Error obtaining document type " + BLANKET_APPROVE_GROUP_NAME, xpee);
 778  0
             throw xpee;
 779  0
         }
 780  
         try {
 781  
             // check if the blanket approve policy element tag was set on the ingested document type and get value if it was
 782  0
             if (XmlHelper.pathExists(xpath, "./" + BLANKET_APPROVE_POLICY, documentTypeNode)) {
 783  0
                 blanketApprovePolicy =(String) getXPath().evaluate("./" + BLANKET_APPROVE_POLICY, documentTypeNode, XPathConstants.STRING);
 784  
             }
 785  0
         } catch (XPathExpressionException xpee) {
 786  0
             LOG.error("Error obtaining document type " + BLANKET_APPROVE_POLICY, xpee);
 787  0
             throw xpee;
 788  0
         }
 789  
         // first check to see if the user ingested both a workgroup name and a policy
 790  0
         if (StringUtils.isNotBlank(blanketWorkGroup) && StringUtils.isNotBlank(blanketApprovePolicy)) {
 791  0
             throw new XmlException("Only one of the blanket approve xml tags can be set");
 792  
         }
 793  0
         else if (StringUtils.isNotBlank(blanketWorkGroup)) {
 794  0
             if (isOverwrite) {
 795  
                 // if overwrite mode is on we need to make sure we clear out the blanket approve policy in case that was the previous document type's method
 796  0
                 documentType.setBlanketApprovePolicy(null);
 797  
             }
 798  0
             if (StringUtils.isNotBlank(blanketGroupName)) { // Found a "blanketApproveGroupName" element.
 799  0
                     documentType.setBlanketApproveWorkgroup(retrieveValidKimGroupUsingGroupNameAndNamespace(blanketGroupName, blanketNamespace));
 800  
             } else { // Found a deprecated "blanketApproveWorkgroupName" element.
 801  0
                     LOG.warn((new StringBuilder(160)).append("Document Type XML is using deprecated element '").append(BLANKET_APPROVE_WORKGROUP_NAME).append(
 802  
                                         "', please use '").append(BLANKET_APPROVE_GROUP_NAME).append("' instead.").toString());
 803  0
                     documentType.setBlanketApproveWorkgroup(retrieveValidKimGroupUsingUnparsedGroupName(blanketWorkGroup));
 804  
             }
 805  
         }
 806  0
         else if (StringUtils.isNotBlank(blanketApprovePolicy)) {
 807  0
             if (isOverwrite) {
 808  
                 // if overwrite mode is on we need to make sure we clear out the blanket approve workgroup in case that was the previous document type's method
 809  0
                 documentType.setBlanketApproveWorkgroup(null);
 810  
             }
 811  0
             documentType.setBlanketApprovePolicy(blanketApprovePolicy);
 812  
         }
 813  
 
 814  
         // set the reporting workgroup name on the document type
 815  
         try {
 816  0
                 if (XmlHelper.pathExists(xpath, "./" + REPORTING_GROUP_NAME, documentTypeNode)) {
 817  0
                         documentType.setReportingWorkgroup(retrieveValidKimGroup("./" + REPORTING_GROUP_NAME, documentTypeNode, false));
 818  
                 }
 819  0
                 else if (XmlHelper.pathExists(xpath, "./" + REPORTING_WORKGROUP_NAME, documentTypeNode)) {
 820  0
                         LOG.warn((new StringBuilder(160)).append("Document Type XML is using deprecated element '").append(REPORTING_WORKGROUP_NAME).append(
 821  
                                         "', please use '").append(REPORTING_GROUP_NAME).append("' instead.").toString());
 822  0
                 documentType.setReportingWorkgroup(retrieveValidKimGroup("./" + REPORTING_WORKGROUP_NAME, documentTypeNode, true));
 823  
             }
 824  0
         } catch (XPathExpressionException xpee) {
 825  0
             LOG.error("Error obtaining document type " + REPORTING_GROUP_NAME, xpee);
 826  0
             throw xpee;
 827  0
         }
 828  
 
 829  
         // set the routing version on the document type
 830  
         try {
 831  
             /*
 832  
              * - if the element tag is ingested... take whatever value is given 
 833  
              * - we disregard the isOverwrite because if the element tag does not exist in the ingestion
 834  
              * then the documentType should already carry the value from the previous document type
 835  
              */
 836  0
             if (XmlHelper.pathExists(xpath, "./" + ROUTING_VERSION, documentTypeNode)) {
 837  
                 String version;
 838  
                 try {
 839  0
                     version = (String) getXPath().evaluate("./" + ROUTING_VERSION, documentTypeNode, XPathConstants.STRING);
 840  0
                 } catch (XPathExpressionException xpee) {
 841  0
                     LOG.error("Error obtaining document type routingVersion", xpee);
 842  0
                     throw xpee;
 843  0
                 }
 844  
                 // verify that the routing version is one of the two valid values
 845  0
                 if (!(version.equals(KEWConstants.ROUTING_VERSION_ROUTE_LEVEL) || version.equals(KEWConstants.ROUTING_VERSION_NODAL))) {
 846  0
                     throw new WorkflowRuntimeException("Invalid routing version on document type: " + version);
 847  
                 }
 848  0
                 documentType.setRoutingVersion(version);
 849  
             }
 850  0
         } catch (XPathExpressionException xpee) {
 851  0
             LOG.error("Error obtaining document type routingVersion", xpee);
 852  0
             throw xpee;
 853  0
         }
 854  
 
 855  
         // set the valid application document statuses for the document type
 856  
         /*
 857  
          * The following code does not need to apply the isOverwrite mode logic because it already checks to see if each node
 858  
          * is available on the ingested XML. If the node is ingested then it doesn't matter if we're in overwrite mode or not
 859  
          * the ingested code should save.
 860  
          */
 861  0
         NodeList appDocStatusList = (NodeList) getXPath().evaluate("./" + APP_DOC_STATUSES, documentTypeNode, XPathConstants.NODESET);
 862  0
         if (appDocStatusList.getLength() > 1) {
 863  
             // more than one <validDocumentStatuses> tag is invalid
 864  0
             throw new XmlException("More than one " + APP_DOC_STATUSES + " node is present in a document type node");
 865  
         }
 866  0
         else if (appDocStatusList.getLength() > 0) {
 867  
             // if there is exactly one <validDocumentStatuses> tag then parse it and use the values
 868  
 //                NodeList statusNodes = appDocStatusList.item(0).getChildNodes();
 869  0
             NodeList statusNodes = (NodeList) getXPath().evaluate("./" + STATUS, appDocStatusList.item(0), XPathConstants.NODESET);
 870  0
             documentType.setValidApplicationStatuses(getDocumentTypeStatuses(statusNodes, documentType));
 871  
         }
 872  
         
 873  
         
 874  0
         return documentType;
 875  
     }
 876  
 
 877  
     private Group retrieveValidKimGroup(String xpathExpression, Node documentTypeNode, boolean deprecatedGroupElement) throws XPathExpressionException, GroupNotFoundException {
 878  
         String groupName;
 879  0
         String groupNamespace = null;
 880  
         try {
 881  0
             groupName = (String) getXPath().evaluate(xpathExpression, documentTypeNode, XPathConstants.STRING);
 882  
             // If not using the deprecated "namespaceCode:GroupName" format for group names, obtain the namespace from the element's "namespace" attribute.
 883  0
             if (!deprecatedGroupElement) {
 884  0
                     groupNamespace = (String) getXPath().evaluate(xpathExpression + "/@" + NAMESPACE, documentTypeNode, XPathConstants.STRING);
 885  
             }
 886  0
         } catch (XPathExpressionException xpee) {
 887  0
             LOG.error("Error obtaining document type workgroup using xpath expression: " + xpathExpression, xpee);
 888  0
             throw xpee;
 889  0
         }
 890  
         // Use the appropriate method to retrieve the group, based on whether or not the deprecated "namespaceCode:groupName" naming pattern is in use. 
 891  0
         return (deprecatedGroupElement) ?
 892  
                         retrieveValidKimGroupUsingUnparsedGroupName(groupName) : retrieveValidKimGroupUsingGroupNameAndNamespace(groupName, groupNamespace);
 893  
     }
 894  
 
 895  
     private Group retrieveValidKimGroupUsingGroupNameAndNamespace(String groupName, String groupNamespace) throws GroupNotFoundException {
 896  
             // allow core config parameter replacement in documenttype workgroups
 897  0
             groupName = Utilities.substituteConfigParameters(groupName).trim();
 898  0
             groupNamespace = Utilities.substituteConfigParameters(groupNamespace).trim();
 899  0
             return retrieveValidKimGroupUsingProcessedGroupNameAndNamespace(groupName, groupNamespace);
 900  
     }
 901  
     
 902  
     private Group retrieveValidKimGroupUsingUnparsedGroupName(String unparsedGroupName) throws GroupNotFoundException {
 903  
         // allow core config parameter replacement in documenttype workgroups
 904  0
         unparsedGroupName = Utilities.substituteConfigParameters(unparsedGroupName);
 905  0
         String groupName = Utilities.parseGroupName(unparsedGroupName);
 906  0
         String groupNamespace = Utilities.parseGroupNamespaceCode(unparsedGroupName);
 907  0
         return retrieveValidKimGroupUsingProcessedGroupNameAndNamespace(groupName, groupNamespace);
 908  
     }
 909  
 
 910  
     private Group retrieveValidKimGroupUsingProcessedGroupNameAndNamespace(String groupName, String groupNamespace) throws GroupNotFoundException {
 911  0
         Group workgroup = getIdentityManagementService().getGroupByName(groupNamespace, groupName);
 912  0
         if (workgroup == null) {
 913  0
             throw new GroupNotFoundException("Valid Workgroup could not be found... Namespace: " + groupNamespace + "  Name: " + groupName);
 914  
         }
 915  0
         return workgroup;
 916  
     }
 917  
     
 918  
     public DocumentType generateNewDocumentTypeFromExisting(String documentTypeName) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException, GroupNotFoundException, WorkflowException {
 919  
         // export the document type that exists in the database
 920  0
         DocumentType docTypeFromDatabase = KEWServiceLocator.getDocumentTypeService().findByName(documentTypeName);
 921  0
         if (ObjectUtils.isNotNull(docTypeFromDatabase)) {
 922  0
             KewExportDataSet kewExportDataSet = new KewExportDataSet();
 923  0
             kewExportDataSet.getDocumentTypes().add(docTypeFromDatabase);
 924  0
             byte[] xmlBytes = CoreApiServiceLocator.getXmlExporterService().export(kewExportDataSet.createExportDataSet());
 925  
             // use the exported document type from the database to generate the new document type
 926  0
             Document tempDocument = XmlHelper.trimXml(new BufferedInputStream(new ByteArrayInputStream(xmlBytes)));
 927  0
             Node documentTypeNode = (Node) getXPath().evaluate("/" + DATA_ELEMENT + "/" + DOCUMENT_TYPES + "/" + DOCUMENT_TYPE, tempDocument, XPathConstants.NODE);
 928  0
             return getFullDocumentType(false, documentTypeNode);
 929  
         }
 930  0
         return null;
 931  
     }
 932  
 
 933  
     private void routeDocumentType(DocumentType documentType) {
 934  0
         DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName(documentType.getName());
 935  
         // if the docType exists then check locking
 936  0
         if (ObjectUtils.isNotNull(docType)) {
 937  0
             Maintainable docTypeMaintainable = new DocumentTypeMaintainable();
 938  0
             docTypeMaintainable.setBusinessObject(docType);
 939  0
             docTypeMaintainable.setBoClass(docType.getClass());
 940  
             // below will throw a ValidationException if a valid locking document exists
 941  0
             MaintenanceUtils.checkForLockingDocument(docTypeMaintainable, true);
 942  
         }
 943  0
         KEWServiceLocator.getDocumentTypeService().versionAndSave(documentType);
 944  0
     }
 945  
 
 946  
     private String getDocumentTypeNameFromNode(Node documentTypeNode) throws XPathExpressionException {
 947  
         try {
 948  0
             return (String) getXPath().evaluate("./name", documentTypeNode, XPathConstants.STRING);
 949  0
         } catch (XPathExpressionException xpee) {
 950  0
             LOG.error("Error obtaining document type name", xpee);
 951  0
             throw xpee;
 952  
         }
 953  
     }
 954  
 
 955  
     /**
 956  
      * Checks for route nodes that are declared but never used and throws an XmlException if one is discovered.
 957  
      */
 958  
     private void checkForOrphanedRouteNodes(Node documentTypeNode, Node routeNodesNode) throws XPathExpressionException, XmlException {
 959  0
         NodeList nodesInPath = (NodeList) getXPath().evaluate("./routePaths/routePath//*/@name", documentTypeNode, XPathConstants.NODESET);
 960  0
         List<String> nodeNamesInPath = new ArrayList<String>(nodesInPath.getLength());
 961  0
         for (int index = 0; index < nodesInPath.getLength(); index++) {
 962  0
             Node nameNode = nodesInPath.item(index);
 963  0
             nodeNamesInPath.add(nameNode.getNodeValue());
 964  
         }
 965  
 
 966  0
         NodeList declaredNodes = (NodeList) getXPath().evaluate("./*/@name", routeNodesNode, XPathConstants.NODESET);
 967  0
         List<String> declaredNodeNames = new ArrayList<String>(declaredNodes.getLength());
 968  0
         for (int index = 0; index < declaredNodes.getLength(); index++) {
 969  0
             Node nameNode = declaredNodes.item(index);
 970  0
             declaredNodeNames.add(nameNode.getNodeValue());
 971  
         }
 972  
 
 973  
         // now compare the declared nodes to the ones actually used
 974  0
         List<String> orphanedNodes = new ArrayList<String>();
 975  0
         for (String declaredNode : declaredNodeNames) {
 976  0
             boolean foundNode = false;
 977  0
             for (String nodeInPath : nodeNamesInPath) {
 978  0
                 if (nodeInPath.equals(declaredNode)) {
 979  0
                     foundNode = true;
 980  0
                     break;
 981  
                 }
 982  
             }
 983  0
             if (!foundNode) {
 984  0
                 orphanedNodes.add(declaredNode);
 985  
             }
 986  0
         }
 987  0
         if (!orphanedNodes.isEmpty()) {
 988  0
             String message = "The following nodes were declared but never used: ";
 989  0
             for (Iterator iterator = orphanedNodes.iterator(); iterator.hasNext();) {
 990  0
                 String orphanedNode = (String) iterator.next();
 991  0
                 message += orphanedNode + (iterator.hasNext() ? ", " : "");
 992  0
             }
 993  0
             throw new XmlException(message);
 994  
         }
 995  0
     }
 996  
 
 997  
     private void createProcesses(NodeList processNodes, DocumentType documentType) {
 998  0
         for (int index = 0; index < processNodes.getLength(); index++) {
 999  0
             Node processNode = processNodes.item(index);
 1000  0
             NamedNodeMap attributes = processNode.getAttributes();
 1001  0
             Node processNameNode = attributes.getNamedItem(PROCESS_NAME);
 1002  0
             String processName = (processNameNode == null ? null : processNameNode.getNodeValue());
 1003  0
             Process process = new Process();
 1004  0
             if (org.apache.commons.lang.StringUtils.isEmpty(processName)) {
 1005  0
                 process.setInitial(true);
 1006  0
                 process.setName(KEWConstants.PRIMARY_PROCESS_NAME);
 1007  
             } else {
 1008  0
                 process.setInitial(false);
 1009  0
                 process.setName(processName);
 1010  
             }
 1011  0
             process.setDocumentType(documentType);
 1012  0
             documentType.addProcess(process);
 1013  
         }
 1014  0
     }
 1015  
     
 1016  
     private void createEmptyProcess(DocumentType documentType) {
 1017  0
             Process process = new Process();
 1018  
             
 1019  0
             process.setInitial(true);
 1020  0
             process.setName(KEWConstants.PRIMARY_PROCESS_NAME);
 1021  
 
 1022  0
             process.setDocumentType(documentType);
 1023  0
             documentType.addProcess(process);
 1024  0
     }
 1025  
 
 1026  
     private RouteNode createRouteNode(RouteNode previousRouteNode, String nodeName, Node routePathNode, Node routeNodesNode, DocumentType documentType, RoutePathContext context) throws XPathExpressionException, XmlException, GroupNotFoundException {
 1027  0
         if (nodeName == null) return null;
 1028  
 
 1029  
         Node currentNode;
 1030  
         try {
 1031  0
             currentNode = (Node) getXPath().evaluate(".//*[@name = '" + nodeName + "']", routePathNode, XPathConstants.NODE);
 1032  0
         } catch (XPathExpressionException xpee) {
 1033  0
             LOG.error("Error obtaining routePath for routeNode", xpee);
 1034  0
             throw xpee;
 1035  0
         }
 1036  0
         if (currentNode == null) {
 1037  0
             String message = "Next node '" + nodeName + "' for node '" + previousRouteNode.getRouteNodeName() + "' not found!";
 1038  0
             LOG.error(message);
 1039  0
             throw new XmlException(message);
 1040  
         }
 1041  
         boolean nodeIsABranch;
 1042  
         try {
 1043  0
             nodeIsABranch = ((Boolean) getXPath().evaluate("self::node()[local-name() = 'branch']", currentNode, XPathConstants.BOOLEAN)).booleanValue();
 1044  0
         } catch (XPathExpressionException xpee) {
 1045  0
             LOG.error("Error testing whether node is a branch", xpee);
 1046  0
             throw xpee;
 1047  0
         }
 1048  0
         if (nodeIsABranch) {
 1049  0
             throw new XmlException("Next node cannot be a branch node");
 1050  
         }
 1051  
 
 1052  
         String localName;
 1053  
         try {
 1054  0
             localName = (String) getXPath().evaluate("local-name(.)", currentNode, XPathConstants.STRING);
 1055  0
         } catch (XPathExpressionException xpee) {
 1056  0
             LOG.error("Error obtaining node local-name", xpee);
 1057  0
             throw xpee;
 1058  0
         }
 1059  0
         RouteNode currentRouteNode = null;
 1060  0
         if (nodesMap.containsKey(nodeName)) {
 1061  0
             currentRouteNode = (RouteNode) nodesMap.get(nodeName);
 1062  
         } else {
 1063  0
             String nodeExpression = ".//*[@name='" + nodeName + "']";
 1064  0
             currentRouteNode = makeRouteNodePrototype(localName, nodeName, nodeExpression, routeNodesNode, documentType, context);
 1065  
         }
 1066  
 
 1067  0
         if ("split".equalsIgnoreCase(localName)) {
 1068  0
             getSplitNextNodes(currentNode, routePathNode, currentRouteNode, routeNodesNode, documentType, context);
 1069  
         }
 1070  
 
 1071  0
         if (previousRouteNode != null) {
 1072  0
             previousRouteNode.getNextNodes().add(currentRouteNode);
 1073  0
             nodesMap.put(previousRouteNode.getRouteNodeName(), previousRouteNode);
 1074  0
             currentRouteNode.getPreviousNodes().add(previousRouteNode);
 1075  
         }
 1076  
 
 1077  0
         String nextNodeName = null;
 1078  
         boolean hasNextNodeAttrib;
 1079  
         try {
 1080  0
             hasNextNodeAttrib = ((Boolean) getXPath().evaluate(NEXT_NODE_EXP, currentNode, XPathConstants.BOOLEAN)).booleanValue();
 1081  0
         } catch (XPathExpressionException xpee) {
 1082  0
             LOG.error("Error obtaining node nextNode attrib", xpee);
 1083  0
             throw xpee;
 1084  0
         }
 1085  0
         if (hasNextNodeAttrib) {
 1086  
             // if the node has a nextNode but is not a split node, the nextNode is used for its node
 1087  0
             if (!"split".equalsIgnoreCase(localName)) {
 1088  
                 try {
 1089  0
                     nextNodeName = (String) getXPath().evaluate(NEXT_NODE_EXP, currentNode, XPathConstants.STRING);
 1090  0
                 } catch (XPathExpressionException xpee) {
 1091  0
                     LOG.error("Error obtaining node nextNode attrib", xpee);
 1092  0
                     throw xpee;
 1093  0
                 }
 1094  0
                 createRouteNode(currentRouteNode, nextNodeName, routePathNode, routeNodesNode, documentType, context);
 1095  
             } else {
 1096  
                 // if the node has a nextNode but is a split node, the nextNode must be used for that split node's join node
 1097  0
                 nodesMap.put(currentRouteNode.getRouteNodeName(), currentRouteNode);
 1098  
             }
 1099  
         } else {
 1100  
             // if the node has no nextNode of its own and is not a join which gets its nextNode from its parent split node
 1101  0
             if (!"join".equalsIgnoreCase(localName)) {
 1102  0
                 nodesMap.put(currentRouteNode.getRouteNodeName(), currentRouteNode);
 1103  
                 // if join has a parent nextNode (on its split node) and join has not already walked this path
 1104  
             } else {
 1105  
                 boolean parentHasNextNodeAttrib;
 1106  
                 try {
 1107  0
                     parentHasNextNodeAttrib = ((Boolean) getXPath().evaluate(PARENT_NEXT_NODE_EXP, currentNode, XPathConstants.BOOLEAN)).booleanValue();
 1108  0
                 } catch (XPathExpressionException xpee) {
 1109  0
                     LOG.error("Error obtaining parent node nextNode attrib", xpee);
 1110  0
                     throw xpee;
 1111  0
                 }
 1112  0
                 if (parentHasNextNodeAttrib && !nodesMap.containsKey(nodeName)) {
 1113  
                     try {
 1114  0
                         nextNodeName = (String) getXPath().evaluate(PARENT_NEXT_NODE_EXP, currentNode, XPathConstants.STRING);
 1115  0
                     } catch (XPathExpressionException xpee) {
 1116  0
                         LOG.error("Error obtaining parent node nextNode attrib", xpee);
 1117  0
                         throw xpee;
 1118  0
                     }
 1119  0
                     createRouteNode(currentRouteNode, nextNodeName, routePathNode, routeNodesNode, documentType, context);
 1120  
                 } else {
 1121  
                     // if join's parent split node does not have a nextNode
 1122  0
                     nodesMap.put(currentRouteNode.getRouteNodeName(), currentRouteNode);
 1123  
                 }
 1124  
             }
 1125  
         }
 1126  
         
 1127  
         // handle nextAppDocStatus route node attribute
 1128  0
         String nextDocStatusName = null;
 1129  
         boolean hasNextDocStatus;
 1130  
         try {
 1131  0
             hasNextDocStatus = ((Boolean) getXPath().evaluate(NEXT_DOC_STATUS_EXP, currentNode, XPathConstants.BOOLEAN)).booleanValue();
 1132  0
         } catch (XPathExpressionException xpee) {
 1133  0
             LOG.error("Error obtaining node nextAppDocStatus attrib", xpee);
 1134  0
             throw xpee;
 1135  0
         }
 1136  0
         if (hasNextDocStatus){
 1137  
                 try {
 1138  0
                         nextDocStatusName = (String) getXPath().evaluate(NEXT_DOC_STATUS_EXP, currentNode, XPathConstants.STRING);
 1139  0
                 } catch (XPathExpressionException xpee) {
 1140  0
                         LOG.error("Error obtaining node nextNode attrib", xpee);
 1141  0
                         throw xpee;
 1142  0
                 }
 1143  
                 
 1144  
                 //validate against allowable values if defined
 1145  0
                 if (documentType.getValidApplicationStatuses() != null  && documentType.getValidApplicationStatuses().size() > 0){
 1146  0
                         Iterator iter = documentType.getValidApplicationStatuses().iterator();
 1147  0
                         boolean statusValidated = false;
 1148  0
                         while (iter.hasNext())
 1149  
                         {
 1150  0
                                 ApplicationDocumentStatus myAppDocStat = (ApplicationDocumentStatus) iter.next();
 1151  0
                                 if (nextDocStatusName.compareToIgnoreCase(myAppDocStat.getStatusName()) == 0)
 1152  
                                 {
 1153  0
                                         statusValidated = true;
 1154  0
                                         break;
 1155  
                                 }
 1156  0
                         }
 1157  0
                         if (!statusValidated){
 1158  0
                                         XmlException xpee = new XmlException("AppDocStatus value " +  nextDocStatusName + " not allowable.");
 1159  0
                                         LOG.error("Error validating nextAppDocStatus name: " +  nextDocStatusName + " against acceptable values.", xpee);
 1160  0
                                         throw xpee; 
 1161  
                         }
 1162  
                 }
 1163  0
                 currentRouteNode.setNextDocStatus(nextDocStatusName);
 1164  
         }
 1165  
         
 1166  0
         return currentRouteNode;
 1167  
     }
 1168  
 
 1169  
     private void getSplitNextNodes(Node splitNode, Node routePathNode, RouteNode splitRouteNode, Node routeNodesNode, DocumentType documentType, RoutePathContext context) throws XPathExpressionException, XmlException, GroupNotFoundException {
 1170  
         NodeList splitBranchNodes;
 1171  
         try {
 1172  0
             splitBranchNodes = (NodeList) getXPath().evaluate("./branch", splitNode, XPathConstants.NODESET);
 1173  0
         } catch (XPathExpressionException xpee) {
 1174  0
             LOG.error("Error obtaining split node branch", xpee);
 1175  0
             throw xpee;
 1176  0
         }
 1177  0
         for (int i = 0; i < splitBranchNodes.getLength(); i++) {
 1178  
             String branchName;
 1179  
             try {
 1180  0
                 branchName = (String) getXPath().evaluate("./@name", splitBranchNodes.item(i), XPathConstants.STRING);
 1181  0
             } catch (XPathExpressionException xpee) {
 1182  0
                 LOG.error("Error obtaining branch name attribute", xpee);
 1183  0
                 throw xpee;
 1184  0
             }
 1185  
             String name;
 1186  
             try {
 1187  0
                 name = (String) getXPath().evaluate("./*[1]/@name", splitBranchNodes.item(i), XPathConstants.STRING);
 1188  0
             } catch (XPathExpressionException xpee) {
 1189  0
                 LOG.error("Error obtaining first split branch node name", xpee);
 1190  0
                 throw xpee;
 1191  0
             }
 1192  0
             context.branch = new BranchPrototype();
 1193  0
             context.branch.setName(branchName);
 1194  
 
 1195  0
             createRouteNode(splitRouteNode, name, routePathNode, routeNodesNode, documentType, context);
 1196  
         }
 1197  0
     }
 1198  
 
 1199  
     private RouteNode makeRouteNodePrototype(String nodeTypeName, String nodeName, String nodeExpression, Node routeNodesNode, DocumentType documentType, RoutePathContext context) throws XPathExpressionException, GroupNotFoundException, XmlException {
 1200  
         NodeList nodeList;
 1201  
         try {
 1202  0
             nodeList = (NodeList) getXPath().evaluate(nodeExpression, routeNodesNode, XPathConstants.NODESET);
 1203  0
         } catch (XPathExpressionException xpee) {
 1204  0
             LOG.error("Error evaluating node expression: '" + nodeExpression + "'");
 1205  0
             throw xpee;
 1206  0
         }
 1207  0
         if (nodeList.getLength() > 1) {
 1208  0
             throw new XmlException("More than one node under routeNodes has the same name of '" + nodeName + "'");
 1209  
         }
 1210  0
         if (nodeList.getLength() == 0) {
 1211  0
             throw new XmlException("No node definition was found with the name '" + nodeName + "'");
 1212  
         }
 1213  0
         Node node = nodeList.item(0);
 1214  
 
 1215  0
         RouteNode routeNode = new RouteNode();
 1216  
         // set fields that all route nodes of all types should have defined
 1217  0
         routeNode.setDocumentType(documentType);
 1218  0
         routeNode.setRouteNodeName((String) getXPath().evaluate("./@name", node, XPathConstants.STRING));
 1219  0
         routeNode.setContentFragment(XmlJotter.jotNode(node));
 1220  
 
 1221  0
         if (XmlHelper.pathExists(xpath, "./activationType", node)) {
 1222  0
             routeNode.setActivationType(ActivationTypeEnum.parse((String) getXPath().evaluate("./activationType", node, XPathConstants.STRING)).getCode());
 1223  
         } else {
 1224  0
             routeNode.setActivationType(DEFAULT_ACTIVATION_TYPE);
 1225  
         }
 1226  
 
 1227  0
         Group exceptionWorkgroup = defaultExceptionWorkgroup;
 1228  
 
 1229  0
         String exceptionWg = null;
 1230  0
         String exceptionWorkgroupName = null;
 1231  0
         String exceptionWorkgroupNamespace = null;
 1232  
 
 1233  0
         if (XmlHelper.pathExists(xpath, "./" + EXCEPTION_GROUP_NAME, node)) {
 1234  0
                 exceptionWorkgroupName = Utilities.substituteConfigParameters(
 1235  
                                 (String) getXPath().evaluate("./" + EXCEPTION_GROUP_NAME, node, XPathConstants.STRING)).trim();
 1236  0
                 exceptionWorkgroupNamespace = Utilities.substituteConfigParameters(
 1237  
                                 (String) getXPath().evaluate("./" + EXCEPTION_GROUP_NAME + "/@" + NAMESPACE, node, XPathConstants.STRING)).trim();
 1238  
         }
 1239  0
         if (org.apache.commons.lang.StringUtils.isEmpty(exceptionWorkgroupName) && XmlHelper.pathExists(xpath, "./" + EXCEPTION_WORKGROUP_NAME, node)) {
 1240  0
                 LOG.warn((new StringBuilder(160)).append("Document Type XML is using deprecated element '").append(EXCEPTION_WORKGROUP_NAME).append(
 1241  
                                 "', please use '").append(EXCEPTION_GROUP_NAME).append("' instead.").toString());
 1242  
                 // for backward compatibility we also need to be able to support exceptionWorkgroupName
 1243  0
                 exceptionWg = Utilities.substituteConfigParameters((String) getXPath().evaluate("./" + EXCEPTION_WORKGROUP_NAME, node, XPathConstants.STRING));
 1244  0
                 exceptionWorkgroupName = Utilities.parseGroupName(exceptionWg);
 1245  0
                 exceptionWorkgroupNamespace = Utilities.parseGroupNamespaceCode(exceptionWg);
 1246  
         }
 1247  0
         if (org.apache.commons.lang.StringUtils.isEmpty(exceptionWorkgroupName) && XmlHelper.pathExists(xpath, "./" + EXCEPTION_WORKGROUP, node)) {
 1248  0
                 LOG.warn((new StringBuilder(160)).append("Document Type XML is using deprecated element '").append(EXCEPTION_WORKGROUP).append(
 1249  
                                 "', please use '").append(EXCEPTION_GROUP_NAME).append("' instead.").toString());
 1250  
             // for backward compatibility we also need to be able to support exceptionWorkgroup
 1251  0
             exceptionWg = Utilities.substituteConfigParameters((String) getXPath().evaluate("./" + EXCEPTION_WORKGROUP, node, XPathConstants.STRING));
 1252  0
             exceptionWorkgroupName = Utilities.parseGroupName(exceptionWg);
 1253  0
             exceptionWorkgroupNamespace = Utilities.parseGroupNamespaceCode(exceptionWg);
 1254  
         }
 1255  0
         if (org.apache.commons.lang.StringUtils.isEmpty(exceptionWorkgroupName)) {
 1256  0
             if (routeNode.getDocumentType().getDefaultExceptionWorkgroup() != null) {
 1257  0
                 exceptionWorkgroupName = routeNode.getDocumentType().getDefaultExceptionWorkgroup().getName();
 1258  0
                 exceptionWorkgroupNamespace = routeNode.getDocumentType().getDefaultExceptionWorkgroup().getNamespaceCode();
 1259  
             }
 1260  
         }
 1261  0
         if (!org.apache.commons.lang.StringUtils.isEmpty(exceptionWorkgroupName)) {
 1262  0
             exceptionWorkgroup = getIdentityManagementService().getGroupByName(exceptionWorkgroupNamespace, exceptionWorkgroupName);
 1263  0
             if (exceptionWorkgroup == null) {
 1264  0
                 throw new GroupNotFoundException("Could not locate exception workgroup with namespace '" + exceptionWorkgroupNamespace + "' and name '" + exceptionWorkgroupName + "'");
 1265  
             }
 1266  
         }
 1267  0
         if (exceptionWorkgroup != null) {
 1268  0
             routeNode.setExceptionWorkgroupName(exceptionWorkgroup.getName());
 1269  0
             routeNode.setExceptionWorkgroupId(exceptionWorkgroup.getId());
 1270  
         }
 1271  
 
 1272  0
         if (((Boolean) getXPath().evaluate("./mandatoryRoute", node, XPathConstants.BOOLEAN)).booleanValue()) {
 1273  0
             routeNode.setMandatoryRouteInd(Boolean.valueOf((String)getXPath().evaluate("./mandatoryRoute", node, XPathConstants.STRING)));
 1274  
         } else {
 1275  0
             routeNode.setMandatoryRouteInd(Boolean.FALSE);
 1276  
         }
 1277  0
         if (((Boolean) getXPath().evaluate("./finalApproval", node, XPathConstants.BOOLEAN)).booleanValue()) {
 1278  0
             routeNode.setFinalApprovalInd(Boolean.valueOf((String)getXPath().evaluate("./finalApproval", node, XPathConstants.STRING)));
 1279  
         } else {
 1280  0
             routeNode.setFinalApprovalInd(Boolean.FALSE);
 1281  
         }
 1282  
 
 1283  
         // for every simple child element of the node, store a config parameter of the element name and text content
 1284  0
         NodeList children = node.getChildNodes();
 1285  0
         for (int i = 0; i < children.getLength(); i++) {
 1286  0
             Node n = children.item(i);
 1287  0
             if (n instanceof Element) {
 1288  0
                 Element e = (Element) n;
 1289  0
                 String name = e.getNodeName();
 1290  0
                 String content = getTextContent(e);
 1291  0
                 routeNode.getConfigParams().add(new RouteNodeConfigParam(routeNode, name, content));
 1292  
             }
 1293  
         }
 1294  
 
 1295  
         // make sure a default rule selector is set
 1296  0
         Map<String, String> cfgMap = Utilities.getKeyValueCollectionAsMap(routeNode.getConfigParams());
 1297  0
         if (!cfgMap.containsKey(RouteNode.RULE_SELECTOR_CFG_KEY)) {
 1298  0
             routeNode.getConfigParams().add(new RouteNodeConfigParam(routeNode, RouteNode.RULE_SELECTOR_CFG_KEY, FlexRM.DEFAULT_RULE_SELECTOR));
 1299  
         }
 1300  
 
 1301  0
         if (((Boolean) getXPath().evaluate("./ruleTemplate", node, XPathConstants.BOOLEAN)).booleanValue()) {
 1302  0
             String ruleTemplateName = (String) getXPath().evaluate("./ruleTemplate", node, XPathConstants.STRING);
 1303  0
             RuleTemplate ruleTemplate = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(ruleTemplateName);
 1304  0
             if (ruleTemplate == null) {
 1305  0
                 throw new XmlException("Rule template for node '" + routeNode.getRouteNodeName() + "' not found: " + ruleTemplateName);
 1306  
             }
 1307  0
             routeNode.setRouteMethodName(ruleTemplateName);
 1308  0
             routeNode.setRouteMethodCode(KEWConstants.ROUTE_LEVEL_FLEX_RM);
 1309  0
         } else if (((Boolean) getXPath().evaluate("./routeModule", node, XPathConstants.BOOLEAN)).booleanValue()) {
 1310  0
             routeNode.setRouteMethodName((String) getXPath().evaluate("./routeModule", node, XPathConstants.STRING));
 1311  0
             routeNode.setRouteMethodCode(KEWConstants.ROUTE_LEVEL_ROUTE_MODULE);
 1312  
         }
 1313  
 
 1314  0
         String nodeType = null;
 1315  0
         if (((Boolean) getXPath().evaluate("./type", node, XPathConstants.BOOLEAN)).booleanValue()) {
 1316  0
             nodeType = (String) getXPath().evaluate("./type", node, XPathConstants.STRING);
 1317  
         } else {
 1318  0
             String localName = (String) getXPath().evaluate("local-name(.)", node, XPathConstants.STRING);
 1319  0
             if ("start".equalsIgnoreCase(localName)) {
 1320  0
                 nodeType = "org.kuali.rice.kew.engine.node.InitialNode";
 1321  0
             } else if ("split".equalsIgnoreCase(localName)) {
 1322  0
                 nodeType = "org.kuali.rice.kew.engine.node.SimpleSplitNode";
 1323  0
             } else if ("join".equalsIgnoreCase(localName)) {
 1324  0
                 nodeType = "org.kuali.rice.kew.engine.node.SimpleJoinNode";
 1325  0
             } else if ("requests".equalsIgnoreCase(localName)) {
 1326  0
                 nodeType = "org.kuali.rice.kew.engine.node.RequestsNode";
 1327  0
             } else if ("process".equalsIgnoreCase(localName)) {
 1328  0
                 nodeType = "org.kuali.rice.kew.engine.node.SimpleSubProcessNode";
 1329  0
             } else if (NodeType.ROLE.getName().equalsIgnoreCase(localName)) {
 1330  0
                 nodeType = RoleNode.class.getName();
 1331  
             }
 1332  
         }
 1333  0
         if (org.apache.commons.lang.StringUtils.isEmpty(nodeType)) {
 1334  0
             throw new XmlException("Could not determine node type for the node named '" + routeNode.getRouteNodeName() + "'");
 1335  
         }
 1336  0
         routeNode.setNodeType(nodeType);
 1337  
 
 1338  0
         String localName = (String) getXPath().evaluate("local-name(.)", node, XPathConstants.STRING);
 1339  0
         if ("split".equalsIgnoreCase(localName)) {
 1340  0
             context.splitNodeStack.addFirst(routeNode);
 1341  0
         } else if ("join".equalsIgnoreCase(localName) && context.splitNodeStack.size() != 0) {
 1342  
             // join node should have same branch prototype as split node
 1343  0
             RouteNode splitNode = (RouteNode)context.splitNodeStack.removeFirst();
 1344  0
             context.branch = splitNode.getBranch();
 1345  0
         } else if (NodeType.ROLE.getName().equalsIgnoreCase(localName)) {
 1346  0
             routeNode.setRouteMethodName(RoleRouteModule.class.getName());
 1347  0
             routeNode.setRouteMethodCode(KEWConstants.ROUTE_LEVEL_ROUTE_MODULE);
 1348  
         }
 1349  0
         routeNode.setBranch(context.branch);
 1350  
 
 1351  0
         return routeNode;
 1352  
     }
 1353  
 
 1354  
     private static String getTextContent(org.w3c.dom.Element element) {
 1355  0
                 NodeList children = element.getChildNodes();
 1356  0
                 Node node = children.item(0);
 1357  0
                 return node.getNodeValue();
 1358  
         }
 1359  
 
 1360  
     private List getDocumentTypePolicies(NodeList documentTypePolicies, DocumentType documentType) throws XPathExpressionException, XmlException {
 1361  0
         List policies = new ArrayList();
 1362  0
         Set policyNames = new HashSet();
 1363  
 
 1364  0
         for (int i = 0; i < documentTypePolicies.getLength(); i++) {
 1365  0
             DocumentTypePolicy policy = new DocumentTypePolicy();
 1366  0
             policy.setDocumentType(documentType);
 1367  
             try {
 1368  0
                 String policyName = (String) getXPath().evaluate("./name", documentTypePolicies.item(i), XPathConstants.STRING);
 1369  0
                 policy.setPolicyName(DocumentTypePolicyEnum.lookup(policyName).getName().toUpperCase());
 1370  0
             } catch (XPathExpressionException xpee) {
 1371  0
                 LOG.error("Error obtaining document type policy name", xpee);
 1372  0
                 throw xpee;
 1373  0
             }
 1374  
             try {
 1375  0
                 if (((Boolean) getXPath().evaluate("./value", documentTypePolicies.item(i), XPathConstants.BOOLEAN)).booleanValue()) {
 1376  0
                     policy.setPolicyValue(Boolean.valueOf((String) getXPath().evaluate("./value", documentTypePolicies.item(i), XPathConstants.STRING)));
 1377  
                 } else {
 1378  0
                     policy.setPolicyValue(Boolean.FALSE);
 1379  
                 }
 1380  0
             } catch (XPathExpressionException xpee) {
 1381  0
                 LOG.error("Error obtaining document type policy value", xpee);
 1382  0
                 throw xpee;
 1383  0
             }
 1384  
             try {
 1385  0
                     String policyStringValue = (String) getXPath().evaluate("./stringValue", documentTypePolicies.item(i), XPathConstants.STRING);
 1386  0
                     if (!StringUtils.isEmpty(policyStringValue)){
 1387  0
                             policy.setPolicyStringValue(policyStringValue.toUpperCase());
 1388  0
                             policy.setPolicyValue(Boolean.TRUE);
 1389  
                             // if DocumentStatusPolicy, check against allowable values
 1390  0
                             if (KEWConstants.DOCUMENT_STATUS_POLICY.equalsIgnoreCase(DocumentTypePolicyEnum.lookup(policy.getPolicyName()).getName())){
 1391  0
                                     boolean found = false;
 1392  0
                                     for (int index=0; index<KEWConstants.DOCUMENT_STATUS_POLICY_VALUES.length; index++) {
 1393  0
                                             if (KEWConstants.DOCUMENT_STATUS_POLICY_VALUES[index].equalsIgnoreCase(policyStringValue)){
 1394  0
                                                     found = true;
 1395  0
                                                     break;
 1396  
                                             }                                                    
 1397  
                                     }
 1398  0
                                     if (!found){
 1399  0
                                             throw new XmlException("Application Document Status string value: " + policyStringValue + " is invalid.");
 1400  
                                     }
 1401  0
                             }
 1402  
                             
 1403  
                     } else {
 1404  
                             //DocumentStatusPolicy requires a <stringValue> tag
 1405  0
                             if (KEWConstants.DOCUMENT_STATUS_POLICY.equalsIgnoreCase(DocumentTypePolicyEnum.lookup(policy.getPolicyName()).getName())){
 1406  0
                                     throw new XmlException("Application Document Status Policy requires a <stringValue>");
 1407  
                             }
 1408  
                     }
 1409  0
             } catch (XPathExpressionException xpee) {
 1410  0
                 LOG.error("Error obtaining document type policy string value", xpee);
 1411  0
                 throw xpee;
 1412  0
             }
 1413  
             
 1414  0
             if (!policyNames.add(policy.getPolicyName())) {
 1415  0
                 throw new XmlException("Policy '" + policy.getPolicyName() + "' has already been defined on this document");
 1416  
             } else {
 1417  0
                 policies.add(policy);
 1418  
             }
 1419  
         }
 1420  
 
 1421  0
         return policies;
 1422  
     }
 1423  
 
 1424  
     private List getDocumentTypeStatuses(NodeList documentTypeStatuses, DocumentType documentType) throws XPathExpressionException, XmlException {
 1425  0
         List statuses = new ArrayList();
 1426  0
         Set statusNames = new HashSet();
 1427  
 
 1428  0
         for (int i = 0; i < documentTypeStatuses.getLength(); i++) {
 1429  0
                 ApplicationDocumentStatus status = new ApplicationDocumentStatus();
 1430  0
                 status.setDocumentType(documentType);
 1431  0
                 Node myNode = documentTypeStatuses.item(i);  
 1432  0
                 String statusName = myNode.getFirstChild().getNodeValue();
 1433  0
                 status.setStatusName(statusName);
 1434  0
                 if (!statusNames.add(status.getStatusName())) {
 1435  0
                         throw new XmlException("Application Status '" + status.getStatusName() + "' has already been defined on this document");
 1436  
                 } else {
 1437  0
                         statuses.add(status);
 1438  
                 }      
 1439  
         }
 1440  0
         return statuses;
 1441  
     }
 1442  
 
 1443  
     private List getDocumentTypeAttributes(NodeList documentTypeAttributes, DocumentType documentType) throws XPathExpressionException, WorkflowException {
 1444  0
         List attributes = new ArrayList();
 1445  
 
 1446  0
         for (int i = 0; i < documentTypeAttributes.getLength(); i++) {
 1447  0
             DocumentTypeAttribute attribute = new DocumentTypeAttribute();
 1448  0
             attribute.setDocumentType(documentType);
 1449  
             String ruleAttributeName;
 1450  
             try {
 1451  0
                 ruleAttributeName = (String) getXPath().evaluate("./name", documentTypeAttributes.item(i), XPathConstants.STRING);
 1452  0
             } catch (XPathExpressionException xpee) {
 1453  0
                 LOG.error("Error obtaining rule attribute name", xpee);
 1454  0
                 throw xpee;
 1455  0
             }
 1456  0
             RuleAttribute ruleAttribute = KEWServiceLocator.getRuleAttributeService().findByName(ruleAttributeName);
 1457  0
             if (ruleAttribute == null) {
 1458  0
                 throw new WorkflowException("Could not find rule attribute: " + ruleAttributeName);
 1459  
             }
 1460  0
             attribute.setDocumentType(documentType);
 1461  0
             attribute.setRuleAttribute(ruleAttribute);
 1462  0
             attribute.setOrderIndex(i+1);
 1463  0
             attributes.add(attribute);
 1464  
         }
 1465  0
         return attributes;
 1466  
     }
 1467  
 
 1468  0
     private class RoutePathContext {
 1469  
         public BranchPrototype branch;
 1470  0
         public LinkedList splitNodeStack = new LinkedList();
 1471  
     }
 1472  
 
 1473  
     protected IdentityManagementService getIdentityManagementService() {
 1474  0
         return KimApiServiceLocator.getIdentityManagementService();
 1475  
     }
 1476  
 
 1477  
     /**
 1478  
      * This is a helper class for indicating if an unprocessed document type node is "standard" or "routing."
 1479  
      * 
 1480  
      * @author Kuali Rice Team (rice.collab@kuali.org)
 1481  
      */
 1482  0
     private class DocTypeNode {
 1483  
         /** The Node that needs to be converted into a doc type. */
 1484  
         public final Node docNode;
 1485  
         /** A flag for indicating the document's type; true indicates "standard," false indicates "routing." */
 1486  
         public final boolean isStandard;
 1487  
         
 1488  
         /**
 1489  
          * Constructs a DocTypeNode instance containing the specified Node and flag.
 1490  
          * 
 1491  
          * @param newNode The unprocessed document type.
 1492  
          * @param newFlag An indicator of what type of document this is (true for "standard," false for "routing").
 1493  
          */
 1494  0
         public DocTypeNode(Node newNode, boolean newFlag) {
 1495  0
             docNode = newNode;
 1496  0
             isStandard = newFlag;
 1497  0
         }
 1498  
     }
 1499  
 
 1500  
 }