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