1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kew.edl; |
17 | |
|
18 | |
import java.io.StringReader; |
19 | |
import java.util.ArrayList; |
20 | |
import java.util.Iterator; |
21 | |
import java.util.List; |
22 | |
import java.util.ListIterator; |
23 | |
|
24 | |
import javax.xml.parsers.DocumentBuilderFactory; |
25 | |
|
26 | |
import org.apache.commons.lang.StringUtils; |
27 | |
import org.jdom.Attribute; |
28 | |
import org.jdom.Element; |
29 | |
import org.kuali.rice.kew.dto.RouteNodeInstanceDTO; |
30 | |
import org.kuali.rice.kew.edl.extract.Dump; |
31 | |
import org.kuali.rice.kew.edl.extract.ExtractService; |
32 | |
import org.kuali.rice.kew.edl.extract.Fields; |
33 | |
import org.kuali.rice.kew.exception.InvalidXmlException; |
34 | |
import org.kuali.rice.kew.exception.WorkflowException; |
35 | |
import org.kuali.rice.kew.exception.WorkflowRuntimeException; |
36 | |
import org.kuali.rice.kew.exception.WorkflowServiceErrorException; |
37 | |
import org.kuali.rice.kew.postprocessor.ActionTakenEvent; |
38 | |
import org.kuali.rice.kew.postprocessor.DeleteEvent; |
39 | |
import org.kuali.rice.kew.postprocessor.DocumentRouteLevelChange; |
40 | |
import org.kuali.rice.kew.postprocessor.DocumentRouteStatusChange; |
41 | |
import org.kuali.rice.kew.postprocessor.ProcessDocReport; |
42 | |
import org.kuali.rice.kew.routeheader.DocumentContent; |
43 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
44 | |
import org.kuali.rice.kew.routeheader.StandardDocumentContent; |
45 | |
import org.kuali.rice.kew.routeheader.service.RouteHeaderService; |
46 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
47 | |
import org.kuali.rice.kew.service.WorkflowInfo; |
48 | |
import org.kuali.rice.kew.util.KEWConstants; |
49 | |
import org.kuali.rice.kew.util.XmlHelper; |
50 | |
import org.w3c.dom.Document; |
51 | |
import org.xml.sax.InputSource; |
52 | |
|
53 | |
|
54 | |
|
55 | 0 | public class EDLDatabasePostProcessor extends EDocLitePostProcessor { |
56 | |
|
57 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(EDLDatabasePostProcessor.class); |
58 | |
|
59 | |
public ProcessDocReport doRouteStatusChange(DocumentRouteStatusChange event) throws Exception { |
60 | 0 | LOG.debug("doRouteStatusChange: " + event); |
61 | 0 | super.postEvent(event.getRouteHeaderId(), event, "statusChange"); |
62 | 0 | DocumentRouteHeaderValue val = KEWServiceLocator.getRouteHeaderService().getRouteHeader(event.getRouteHeaderId()); |
63 | 0 | Document doc = getEDLContent(val); |
64 | 0 | if (LOG.isDebugEnabled()) { |
65 | 0 | LOG.debug("Submitting doc: " + XmlHelper.jotNode(doc)); |
66 | |
} |
67 | 0 | DocumentRouteHeaderValue routeHeader = getRouteHeaderService().getRouteHeader(event.getRouteHeaderId()); |
68 | 0 | extractEDLData(routeHeader, getNodeNames(event.getRouteHeaderId())); |
69 | 0 | return super.doRouteStatusChange(event); |
70 | |
} |
71 | |
|
72 | |
public ProcessDocReport doActionTaken(ActionTakenEvent event) throws Exception { |
73 | 0 | LOG.debug("doActionTaken: " + event); |
74 | 0 | super.postEvent(event.getRouteHeaderId(), event, "actionTaken"); |
75 | |
|
76 | |
|
77 | 0 | if (KEWConstants.ACTION_TAKEN_SAVED_CD.equals(event.getActionTaken().getActionTaken())) { |
78 | 0 | DocumentRouteHeaderValue routeHeader = getRouteHeaderService().getRouteHeader(event.getRouteHeaderId()); |
79 | 0 | extractEDLData(routeHeader, getNodeNames(event.getRouteHeaderId())); |
80 | |
} |
81 | |
|
82 | 0 | return super.doActionTaken(event); |
83 | |
} |
84 | |
|
85 | |
public ProcessDocReport doDeleteRouteHeader(DeleteEvent event) throws Exception { |
86 | 0 | LOG.debug("doDeleteRouteHeader: " + event); |
87 | 0 | super.postEvent(event.getRouteHeaderId(), event, "deleteRouteHeader"); |
88 | 0 | return super.doDeleteRouteHeader(event); |
89 | |
} |
90 | |
|
91 | |
public ProcessDocReport doRouteLevelChange(DocumentRouteLevelChange event) throws Exception { |
92 | 0 | LOG.debug("doRouteLevelChange: " + event); |
93 | 0 | super.postEvent(event.getRouteHeaderId(), event, "routeLevelChange"); |
94 | 0 | DocumentRouteHeaderValue val = KEWServiceLocator.getRouteHeaderService().getRouteHeader(event.getRouteHeaderId()); |
95 | 0 | Document doc = getEDLContent(val); |
96 | 0 | if (LOG.isDebugEnabled()) { |
97 | 0 | LOG.debug("Submitting doc: " + XmlHelper.jotNode(doc)); |
98 | |
} |
99 | 0 | DocumentRouteHeaderValue routeHeader = getRouteHeaderService().getRouteHeader(event.getRouteHeaderId()); |
100 | 0 | extractEDLData(routeHeader, new String[] {event.getNewNodeName()}); |
101 | 0 | return super.doRouteLevelChange(event); |
102 | |
} |
103 | |
|
104 | |
public static Document getEDLContent(DocumentRouteHeaderValue routeHeader) throws Exception { |
105 | 0 | String content = routeHeader.getDocContent(); |
106 | 0 | Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(content))); |
107 | 0 | return doc; |
108 | |
} |
109 | |
|
110 | |
private RouteHeaderService getRouteHeaderService() { |
111 | 0 | return (RouteHeaderService) KEWServiceLocator.getService(KEWServiceLocator.DOC_ROUTE_HEADER_SRV); |
112 | |
} |
113 | |
|
114 | |
private String[] getNodeNames(Long documentId) throws WorkflowException { |
115 | 0 | RouteNodeInstanceDTO[] activeNodeInstances = new WorkflowInfo().getActiveNodeInstances(documentId); |
116 | 0 | if (activeNodeInstances == null || activeNodeInstances.length == 0) { |
117 | 0 | activeNodeInstances = new WorkflowInfo().getTerminalNodeInstances(documentId); |
118 | |
} |
119 | 0 | String[] nodeNames = new String[(activeNodeInstances == null ? 0 : activeNodeInstances.length)]; |
120 | 0 | for (int index = 0; index < activeNodeInstances.length; index++) { |
121 | 0 | nodeNames[index] = activeNodeInstances[index].getName(); |
122 | |
} |
123 | 0 | return nodeNames; |
124 | |
} |
125 | |
|
126 | |
private void extractEDLData(DocumentRouteHeaderValue routeHeader, String[] nodeNames) { |
127 | 0 | Dump dump = getExtractService().getDumpByDocumentId(routeHeader.getRouteHeaderId()); |
128 | 0 | if (dump == null) { |
129 | 0 | dump = new Dump(); |
130 | |
} |
131 | 0 | dump.setDocId(routeHeader.getRouteHeaderId()); |
132 | 0 | dump.setDocCreationDate(routeHeader.getCreateDate()); |
133 | 0 | dump.setDocCurrentNodeName(StringUtils.join(nodeNames, ",")); |
134 | 0 | dump.setDocDescription(routeHeader.getDocumentType().getDescription()); |
135 | 0 | dump.setDocModificationDate(routeHeader.getStatusModDate()); |
136 | 0 | dump.setDocInitiatorId(routeHeader.getInitiatorWorkflowId()); |
137 | 0 | dump.setDocRouteStatusCode(routeHeader.getDocRouteStatus()); |
138 | 0 | dump.setDocTypeName(routeHeader.getDocumentType().getName()); |
139 | |
|
140 | 0 | List<Fields> fields = dump.getFields(); |
141 | 0 | fields.clear(); |
142 | |
|
143 | 0 | List fieldElements = setExtractFields(routeHeader); |
144 | 0 | for (Iterator iter = fieldElements.iterator(); iter.hasNext();) { |
145 | 0 | Fields field = new Fields(); |
146 | 0 | field.setDump(dump); |
147 | 0 | field.setDocId(dump.getDocId()); |
148 | 0 | Element element = (Element)iter.next(); |
149 | 0 | Attribute attribute = element.getAttribute("name"); |
150 | 0 | field.setFieldName(attribute.getValue()); |
151 | 0 | field.setFieldValue(element.getChildText("value")); |
152 | 0 | fields.add(field); |
153 | 0 | } |
154 | 0 | dump.setFields(fields); |
155 | 0 | getExtractService().saveDump(dump); |
156 | 0 | } |
157 | |
|
158 | |
|
159 | |
private ExtractService getExtractService() { |
160 | 0 | return (ExtractService) KEWServiceLocator.getService(KEWServiceLocator.EXTRACT_SERVICE); |
161 | |
} |
162 | |
|
163 | |
|
164 | |
private static Element getRootElement(DocumentContent docContent) { |
165 | 0 | Element rootElement = null; |
166 | |
try { |
167 | 0 | rootElement = XmlHelper.buildJDocument(docContent.getDocument()).getRootElement(); |
168 | 0 | } catch (Exception e) { |
169 | 0 | throw new WorkflowServiceErrorException("Invalid XML submitted", new ArrayList<Object>()); |
170 | 0 | } |
171 | 0 | return rootElement; |
172 | |
} |
173 | |
|
174 | |
private List setExtractFields(DocumentRouteHeaderValue routeHeader) { |
175 | |
|
176 | |
|
177 | 0 | StandardDocumentContent standardDocContent = null; |
178 | |
try { |
179 | 0 | standardDocContent = new StandardDocumentContent(routeHeader.getDocumentContent().getDocumentContent()); |
180 | 0 | } catch (InvalidXmlException e) { |
181 | 0 | throw new WorkflowRuntimeException("Caught exception retrieving doc content", e); |
182 | 0 | } |
183 | 0 | Element rootElement = getRootElement(standardDocContent); |
184 | 0 | List<Element> fields = new ArrayList<Element>(); |
185 | 0 | List fieldElements = XmlHelper.findElements(rootElement, "field"); |
186 | 0 | ListIterator elementIter = fieldElements.listIterator(); |
187 | 0 | while (elementIter.hasNext()) { |
188 | 0 | Element field = (Element) elementIter.next(); |
189 | 0 | Element version = field.getParentElement(); |
190 | 0 | if (version.getAttribute("current").getValue().equals("true")) { |
191 | 0 | if (field.getAttribute("name")!= null) { |
192 | 0 | fields.add(field); |
193 | |
} |
194 | |
} |
195 | 0 | } |
196 | 0 | return fields; |
197 | |
} |
198 | |
|
199 | |
} |