1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.edl.framework.workflow; |
17 | |
|
18 | |
import org.apache.commons.lang.StringUtils; |
19 | |
import org.jdom.Attribute; |
20 | |
import org.jdom.Element; |
21 | |
import org.kuali.rice.core.api.util.xml.XmlHelper; |
22 | |
import org.kuali.rice.core.api.util.xml.XmlJotter; |
23 | |
import org.kuali.rice.edl.framework.extract.DumpDTO; |
24 | |
import org.kuali.rice.edl.framework.extract.ExtractService; |
25 | |
import org.kuali.rice.edl.framework.extract.FieldDTO; |
26 | |
import org.kuali.rice.edl.framework.services.EdlFrameworkServiceLocator; |
27 | |
import org.kuali.rice.kew.api.KewApiConstants; |
28 | |
import org.kuali.rice.kew.api.KewApiServiceLocator; |
29 | |
import org.kuali.rice.kew.api.doctype.DocumentType; |
30 | |
import org.kuali.rice.kew.api.document.node.RouteNodeInstance; |
31 | |
import org.kuali.rice.kew.framework.postprocessor.ActionTakenEvent; |
32 | |
import org.kuali.rice.kew.framework.postprocessor.DeleteEvent; |
33 | |
import org.kuali.rice.kew.framework.postprocessor.DocumentRouteLevelChange; |
34 | |
import org.kuali.rice.kew.framework.postprocessor.DocumentRouteStatusChange; |
35 | |
import org.kuali.rice.kew.framework.postprocessor.ProcessDocReport; |
36 | |
import org.w3c.dom.Document; |
37 | |
|
38 | |
import java.rmi.RemoteException; |
39 | |
import java.sql.Timestamp; |
40 | |
import java.util.ArrayList; |
41 | |
import java.util.Collection; |
42 | |
import java.util.Iterator; |
43 | |
import java.util.List; |
44 | |
|
45 | 0 | public class EDocLiteDatabasePostProcessor extends EDocLitePostProcessor { |
46 | |
|
47 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger |
48 | |
.getLogger(EDocLiteDatabasePostProcessor.class); |
49 | |
|
50 | |
@Override |
51 | |
public ProcessDocReport doRouteStatusChange(DocumentRouteStatusChange event) throws RemoteException { |
52 | 0 | LOG.debug("doRouteStatusChange: " + event); |
53 | 0 | String documentId = event.getDocumentId(); |
54 | 0 | super.postEvent(documentId, event, "statusChange"); |
55 | 0 | Document doc = getEDLContent(documentId); |
56 | 0 | if (LOG.isDebugEnabled()) { |
57 | 0 | LOG.debug("Submitting doc: " + XmlJotter.jotNode(doc)); |
58 | |
} |
59 | 0 | extractEDLData(documentId, getNodeNames(event.getDocumentId()), doc); |
60 | 0 | return super.doRouteStatusChange(event); |
61 | |
} |
62 | |
|
63 | |
@Override |
64 | |
public ProcessDocReport doActionTaken(ActionTakenEvent event) throws RemoteException { |
65 | 0 | LOG.debug("doActionTaken: " + event); |
66 | 0 | String documentId = event.getDocumentId(); |
67 | 0 | super.postEvent(documentId, event, "actionTaken"); |
68 | |
|
69 | |
|
70 | 0 | if (KewApiConstants.ACTION_TAKEN_SAVED_CD.equals(event.getActionTaken().getActionTaken())) { |
71 | 0 | Document doc = getEDLContent(documentId); |
72 | 0 | extractEDLData(documentId, getNodeNames(event.getDocumentId()), doc); |
73 | |
} |
74 | |
|
75 | 0 | return super.doActionTaken(event); |
76 | |
} |
77 | |
|
78 | |
@Override |
79 | |
public ProcessDocReport doDeleteRouteHeader(DeleteEvent event) throws RemoteException { |
80 | 0 | LOG.debug("doDeleteRouteHeader: " + event); |
81 | 0 | super.postEvent(event.getDocumentId(), event, "deleteRouteHeader"); |
82 | 0 | return super.doDeleteRouteHeader(event); |
83 | |
} |
84 | |
|
85 | |
@Override |
86 | |
public ProcessDocReport doRouteLevelChange(DocumentRouteLevelChange event) throws RemoteException { |
87 | 0 | LOG.debug("doRouteLevelChange: " + event); |
88 | 0 | String documentId = event.getDocumentId(); |
89 | 0 | super.postEvent(documentId, event, "routeLevelChange"); |
90 | 0 | Document doc = getEDLContent(documentId); |
91 | 0 | if (LOG.isDebugEnabled()) { |
92 | 0 | LOG.debug("Submitting doc: " + XmlJotter.jotNode(doc)); |
93 | |
} |
94 | 0 | extractEDLData(documentId, new String[]{event.getNewNodeName()}, doc); |
95 | 0 | return super.doRouteLevelChange(event); |
96 | |
} |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
private String[] getNodeNames(String documentId) { |
105 | 0 | List<RouteNodeInstance> activeNodeInstances = KewApiServiceLocator.getWorkflowDocumentService().getActiveRouteNodeInstances(documentId); |
106 | 0 | if (activeNodeInstances == null || activeNodeInstances.size() == 0) { |
107 | 0 | activeNodeInstances = KewApiServiceLocator.getWorkflowDocumentService().getTerminalRouteNodeInstances( |
108 | |
documentId); |
109 | |
} |
110 | 0 | String[] nodeNames = new String[(activeNodeInstances == null ? 0 : activeNodeInstances.size())]; |
111 | 0 | for (int index = 0; index < activeNodeInstances.size(); index++) { |
112 | 0 | nodeNames[index] = activeNodeInstances.get(index).getName(); |
113 | |
} |
114 | 0 | return nodeNames; |
115 | |
} |
116 | |
|
117 | |
private void extractEDLData(String documentId, String[] nodeNames, Document documentContent) { |
118 | |
try { |
119 | 0 | org.kuali.rice.kew.api.document.Document document = KewApiServiceLocator.getWorkflowDocumentService().getDocument(documentId); |
120 | 0 | DocumentType documentType = KewApiServiceLocator.getDocumentTypeService().getDocumentTypeById( |
121 | |
document.getDocumentTypeId()); |
122 | 0 | DumpDTO dump = getExtractService().getDumpByDocumentId(documentId); |
123 | 0 | if (dump == null) { |
124 | 0 | dump = new DumpDTO(); |
125 | |
} |
126 | 0 | dump.setDocId(documentId); |
127 | 0 | dump.setDocCreationDate(new Timestamp(document.getDateCreated().getMillis())); |
128 | 0 | dump.setDocCurrentNodeName(StringUtils.join(nodeNames, ",")); |
129 | 0 | dump.setDocDescription(documentType.getDescription()); |
130 | 0 | if (document.getDateLastModified() != null) { |
131 | 0 | dump.setDocModificationDate(new Timestamp(document.getDateLastModified().getMillis())); |
132 | |
} |
133 | 0 | dump.setDocInitiatorId(document.getInitiatorPrincipalId()); |
134 | 0 | dump.setDocRouteStatusCode(document.getStatus().getCode()); |
135 | 0 | dump.setDocTypeName(documentType.getName()); |
136 | |
|
137 | 0 | List<FieldDTO> fields = dump.getFields(); |
138 | 0 | fields.clear(); |
139 | |
|
140 | 0 | List fieldElements = setExtractFields(documentContent); |
141 | 0 | for (Iterator iter = fieldElements.iterator(); iter.hasNext();) { |
142 | 0 | FieldDTO field = new FieldDTO(); |
143 | 0 | field.setDocId(dump.getDocId()); |
144 | 0 | Element element = (Element) iter.next(); |
145 | 0 | Attribute attribute = element.getAttribute("name"); |
146 | 0 | field.setFieldName(attribute.getValue()); |
147 | 0 | field.setFieldValue(element.getChildText("value")); |
148 | 0 | fields.add(field); |
149 | 0 | } |
150 | 0 | dump.setFields(fields); |
151 | 0 | getExtractService().saveDump(dump); |
152 | 0 | } catch (Exception e) { |
153 | 0 | if (e instanceof RuntimeException) { |
154 | 0 | throw (RuntimeException) e; |
155 | |
} |
156 | 0 | throw new RuntimeException(e); |
157 | 0 | } |
158 | 0 | } |
159 | |
|
160 | |
private ExtractService getExtractService() { |
161 | 0 | return EdlFrameworkServiceLocator.getExtractService(); |
162 | |
} |
163 | |
|
164 | |
private static Element getRootElement(Document docContent) { |
165 | 0 | return XmlHelper.buildJDocument(docContent).getRootElement(); |
166 | |
} |
167 | |
|
168 | |
private List setExtractFields(Document documentContent) { |
169 | 0 | Element rootElement = getRootElement(documentContent); |
170 | 0 | List<Element> fields = new ArrayList<Element>(); |
171 | 0 | Collection<Element> fieldElements = XmlHelper.findElements(rootElement, "field"); |
172 | 0 | Iterator<Element> elementIter = fieldElements.iterator(); |
173 | 0 | while (elementIter.hasNext()) { |
174 | 0 | Element field = elementIter.next(); |
175 | 0 | Element version = field.getParentElement(); |
176 | 0 | if (version.getAttribute("current").getValue().equals("true")) { |
177 | 0 | if (field.getAttribute("name") != null) { |
178 | 0 | fields.add(field); |
179 | |
} |
180 | |
} |
181 | 0 | } |
182 | 0 | return fields; |
183 | |
} |
184 | |
|
185 | |
} |