View Javadoc

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.routemodule;
18  
19  import java.io.BufferedInputStream;
20  import java.io.ByteArrayInputStream;
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import javax.xml.parsers.DocumentBuilder;
27  import javax.xml.parsers.DocumentBuilderFactory;
28  import javax.xml.parsers.ParserConfigurationException;
29  
30  import org.kuali.rice.kew.exception.ResourceUnavailableException;
31  import org.kuali.rice.kew.exception.WorkflowException;
32  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
33  import org.kuali.rice.kew.service.KEWServiceLocator;
34  import org.kuali.rice.kew.util.KEWConstants;
35  import org.w3c.dom.Document;
36  import org.w3c.dom.NamedNodeMap;
37  import org.w3c.dom.Node;
38  import org.w3c.dom.NodeList;
39  import org.xml.sax.SAXException;
40  
41  
42  /**
43   * @author Kuali Rice Team (rice.collab@kuali.org)
44   */
45  public class TestRouteModuleXMLHelper {
46  
47      /**
48       * 
49       */
50      public static String toXML(TestDocContent docContent) {
51          StringBuffer buffer = new StringBuffer();
52          buffer.append("<docContent>");
53          for (Iterator rlIt = docContent.getRouteLevels().iterator(); rlIt.hasNext();) {
54              TestRouteLevel routeLevel = (TestRouteLevel) rlIt.next();
55              buffer.append("<routeLevel priority=\"").append(routeLevel.getPriority()).append("\">");
56              for (Iterator respIt = routeLevel.getResponsibilities().iterator(); respIt.hasNext();) {
57                  TestResponsibility responsibility = (TestResponsibility) respIt.next();
58                  buffer.append("<responsibility actionRequested=\"").append(responsibility.getActionRequested()).append("\" priority=\"").append(responsibility.getPriority()).append("\">");
59                  TestRecipient recipient = responsibility.getRecipient();
60                  buffer.append("<recipient type=\"").append(recipient.getType()).append("\" id=\"").append(recipient.getId()).append("\"/>");
61                  if (!responsibility.getDelegations().isEmpty()) {
62                      buffer.append("<delegations>");
63                  }
64                  for (Iterator delIt = responsibility.getDelegations().iterator(); delIt.hasNext();) {
65                      TestDelegation delegation = (TestDelegation) delIt.next();
66                      buffer.append("<delegation type=\"").append(delegation.getType()).append("\">");
67                      TestResponsibility delResp = delegation.getResponsibility();
68                      buffer.append("<delegateResponsibility>");
69                      TestRecipient delRecipient = delResp.getRecipient();
70                      buffer.append("<recipient type=\"").append(delRecipient.getType()).append("\" id=\"").append(delRecipient.getId()).append("\"/>");
71                      buffer.append("</delegateResponsibility>");
72                      buffer.append("</delegation>");
73                  }
74                  if (!responsibility.getDelegations().isEmpty()) {
75                      buffer.append("</delegations>");
76                  }
77                  buffer.append("</responsibility>");
78              }
79              buffer.append("</routeLevel>");
80          }
81          buffer.append("</docContent>");
82          return buffer.toString();
83      }
84      
85      public static TestRouteLevel parseCurrentRouteLevel(DocumentRouteHeaderValue routeHeader) throws ResourceUnavailableException, WorkflowException {
86          try {
87              DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
88              Document document = builder.parse(new BufferedInputStream(new ByteArrayInputStream(routeHeader.getDocContent().getBytes())));
89              NodeList nodes = document.getElementsByTagName(TestRouteModuleConstants.ROUTE_LEVEL_ELEMENT);
90              Node routeLevelNode = null;
91              Integer priority = null;
92              for (int index = 0; index < nodes.getLength(); index++) {
93                  Node node = nodes.item(index);
94                  NamedNodeMap nodeMap = node.getAttributes();
95                  Node priorityNode = nodeMap.getNamedItem(TestRouteModuleConstants.PRIORITY_ATTRIBUTE);
96                  try {
97                      priority = Integer.valueOf(priorityNode.getNodeValue());
98                  } catch (NumberFormatException e) {
99                      throw new WorkflowException("Invalid route level priority '"+priorityNode.getNodeValue()+"'", e);
100                 }
101                 if (priorityNode != null && routeHeader.getDocRouteLevel().equals(priority)) {
102                     routeLevelNode = node;
103                     break;
104                 }
105             }
106             if (routeLevelNode == null) {
107                 return null;
108             }
109             TestRouteLevel routeLevel = new TestRouteLevel();
110             routeLevel.setPriority(priority.intValue());
111             List responsibilityNodes = new ArrayList();
112             NodeList rlNodes = routeLevelNode.getChildNodes();
113             for (int index = 0; index < rlNodes.getLength(); index++) {
114                 Node node = rlNodes.item(index);
115                 if (node.getNodeName().equals(TestRouteModuleConstants.RESPONSIBILITY_ELEMENT)) {
116                     responsibilityNodes.add(node);
117                 }
118             }
119             routeLevel.setResponsibilities(parseResponsibilities(responsibilityNodes));
120             return routeLevel;
121         } catch (ParserConfigurationException e) {
122             throw new ResourceUnavailableException("Could not configure DOM parser for route level " + routeHeader.getDocRouteLevel());
123         } catch (IOException e) {
124             throw new ResourceUnavailableException("Could not parse XML doc content for route level " + routeHeader.getDocRouteLevel());
125         } catch (SAXException e) {
126             throw new ResourceUnavailableException("Could not parse XML doc content for route level " + routeHeader.getDocRouteLevel());
127         }
128     }
129     
130     private static List parseResponsibilities(List responsibilityNodes) throws WorkflowException {
131         List responsibilities = new ArrayList();
132         for (Iterator iterator = responsibilityNodes.iterator(); iterator.hasNext();) {
133             Node node = (Node) iterator.next();
134             responsibilities.add(parseResponsibility(node));
135         }
136         return responsibilities;
137     }
138     
139     private static TestResponsibility parseResponsibility(Node responsibilityNode) throws WorkflowException {
140         TestResponsibility responsibility = new TestResponsibility();
141         NamedNodeMap attributes = responsibilityNode.getAttributes();
142         Node actionRequestedNode = attributes.getNamedItem(TestRouteModuleConstants.ACTION_REQUESTED_ATTRIBUTE);
143         Node priorityNode = attributes.getNamedItem(TestRouteModuleConstants.PRIORITY_ATTRIBUTE);
144         responsibility.setActionRequested(actionRequestedNode.getNodeValue());
145         try {
146             responsibility.setPriority(Integer.parseInt(priorityNode.getNodeValue()));
147         } catch (NumberFormatException e) {
148             throw new WorkflowException("Invalid responsibility priority '"+priorityNode.getNodeValue()+"'", e);
149         }
150         NodeList childNodes = responsibilityNode.getChildNodes();
151         for (int index = 0; index < childNodes.getLength(); index++) {
152             Node node = childNodes.item(index);
153             if (node.getNodeName().equals(TestRouteModuleConstants.RECIPIENT_ELEMENT)) {
154                 responsibility.setRecipient(parseRecipient(node));
155             } else if (node.getNodeName().equals(TestRouteModuleConstants.DELEGATIONS_ELEMENT)) {
156                 responsibility.setDelegations(parseDelegations(node));
157             }
158         }
159         return responsibility;
160     }
161     
162     private static TestRecipient parseRecipient(Node recipientNode) {
163         TestRecipient recipient = new TestRecipient();
164         NamedNodeMap attributes = recipientNode.getAttributes();
165         recipient.setType(attributes.getNamedItem(TestRouteModuleConstants.TYPE_ATTRIBUTE).getNodeValue());
166         recipient.setId(attributes.getNamedItem(TestRouteModuleConstants.ID_ATTRIBUTE).getNodeValue());
167         return recipient;
168     }
169     
170     private static List parseDelegations(Node delegationsNode) {
171         List delegations = new ArrayList();
172         NodeList childNodes = delegationsNode.getChildNodes();
173         for (int index = 0; index < childNodes.getLength(); index++) {
174             Node node = childNodes.item(index);
175             if (node.getNodeName().equals(TestRouteModuleConstants.DELEGATION_ELEMENT)) {
176                 delegations.add(parseDelegation(node));
177             }
178         }
179         return delegations;
180     }
181     
182     private static TestDelegation parseDelegation(Node delegationNode) {
183         TestDelegation delegation = new TestDelegation();
184         NamedNodeMap attributes = delegationNode.getAttributes();
185         delegation.setType(attributes.getNamedItem(TestRouteModuleConstants.TYPE_ATTRIBUTE).getNodeValue());
186         NodeList childNodes = delegationNode.getChildNodes();
187         for (int index = 0; index < childNodes.getLength(); index++) {
188             Node node = childNodes.item(index);
189             if (node.getNodeName().equals(TestRouteModuleConstants.DELEGATE_RESPONSIBILITY_ELEMENT)) {
190                 delegation.setResponsibility(parseDelegateResponsibility(node));
191             }
192         }
193         return delegation;
194     }
195     
196     private static TestResponsibility parseDelegateResponsibility(Node delegateResponsibilityNode) {
197         TestResponsibility responsibility = new TestResponsibility();
198         NodeList childNodes = delegateResponsibilityNode.getChildNodes();
199         for (int index = 0; index < childNodes.getLength(); index++) {
200             Node node = childNodes.item(index);
201             if (node.getNodeName().equals(TestRouteModuleConstants.RECIPIENT_ELEMENT)) {
202                 responsibility.setRecipient(parseRecipient(node));
203             }
204         }
205         return responsibility;
206     }
207     
208 }