View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  public class EDocLiteDatabasePostProcessor extends EDocLitePostProcessor {
46  
47      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          LOG.debug("doRouteStatusChange: " + event);
53          String documentId = event.getDocumentId();
54          super.postEvent(documentId, event, "statusChange");
55          Document doc = getEDLContent(documentId);
56          if (LOG.isDebugEnabled()) {
57              LOG.debug("Submitting doc: " + XmlJotter.jotNode(doc));
58          }
59          extractEDLData(documentId, getNodeNames(event.getDocumentId()), doc);
60          return super.doRouteStatusChange(event);
61      }
62  
63      @Override
64      public ProcessDocReport doActionTaken(ActionTakenEvent event) throws RemoteException {
65          LOG.debug("doActionTaken: " + event);
66          String documentId = event.getDocumentId();
67          super.postEvent(documentId, event, "actionTaken");
68  
69          // if the action requested is a save, go ahead and update the database with the most current information. -grpatter
70          if (KewApiConstants.ACTION_TAKEN_SAVED_CD.equals(event.getActionTaken().getActionTaken())) {
71              Document doc = getEDLContent(documentId);
72              extractEDLData(documentId, getNodeNames(event.getDocumentId()), doc);
73          }
74  
75          return super.doActionTaken(event);
76      }
77  
78      @Override
79      public ProcessDocReport doDeleteRouteHeader(DeleteEvent event) throws RemoteException {
80          LOG.debug("doDeleteRouteHeader: " + event);
81          super.postEvent(event.getDocumentId(), event, "deleteRouteHeader");
82          return super.doDeleteRouteHeader(event);
83      }
84  
85      @Override
86      public ProcessDocReport doRouteLevelChange(DocumentRouteLevelChange event) throws RemoteException {
87          LOG.debug("doRouteLevelChange: " + event);
88          String documentId = event.getDocumentId();
89          super.postEvent(documentId, event, "routeLevelChange");
90          Document doc = getEDLContent(documentId);
91          if (LOG.isDebugEnabled()) {
92              LOG.debug("Submitting doc: " + XmlJotter.jotNode(doc));
93          }
94          extractEDLData(documentId, new String[]{event.getNewNodeName()}, doc);
95          return super.doRouteLevelChange(event);
96      }
97  
98      //	    public static Document getEDLContent(DocumentRouteHeaderValue routeHeader) throws Exception {
99      //	        String content = routeHeader.getDocContent();
100     //	        Document doc =  DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(content)));
101     //	        return doc;
102     //	    }
103 
104     private String[] getNodeNames(String documentId) {
105         List<RouteNodeInstance> activeNodeInstances = KewApiServiceLocator.getWorkflowDocumentService().getActiveRouteNodeInstances(documentId);
106         if (activeNodeInstances == null || activeNodeInstances.size() == 0) {
107             activeNodeInstances = KewApiServiceLocator.getWorkflowDocumentService().getTerminalRouteNodeInstances(
108                     documentId);
109         }
110         String[] nodeNames = new String[(activeNodeInstances == null ? 0 : activeNodeInstances.size())];
111         for (int index = 0; index < activeNodeInstances.size(); index++) {
112             nodeNames[index] = activeNodeInstances.get(index).getName();
113         }
114         return nodeNames;
115     }
116 
117     private void extractEDLData(String documentId, String[] nodeNames, Document documentContent) {
118         try {
119             org.kuali.rice.kew.api.document.Document document = KewApiServiceLocator.getWorkflowDocumentService().getDocument(documentId);
120             DocumentType documentType = KewApiServiceLocator.getDocumentTypeService().getDocumentTypeById(
121                     document.getDocumentTypeId());
122             DumpDTO dump = getExtractService().getDumpByDocumentId(documentId);
123             if (dump == null) {
124                 dump = new DumpDTO();
125             }
126             dump.setDocId(documentId);
127             dump.setDocCreationDate(new Timestamp(document.getDateCreated().getMillis()));
128             dump.setDocCurrentNodeName(StringUtils.join(nodeNames, ","));
129             dump.setDocDescription(documentType.getDescription());
130             if (document.getDateLastModified() != null) {
131                 dump.setDocModificationDate(new Timestamp(document.getDateLastModified().getMillis()));
132             }
133             dump.setDocInitiatorId(document.getInitiatorPrincipalId());
134             dump.setDocRouteStatusCode(document.getStatus().getCode());
135             dump.setDocTypeName(documentType.getName());
136 
137             List<FieldDTO> fields = dump.getFields();
138             fields.clear();
139 
140             List fieldElements = setExtractFields(documentContent);
141             for (Iterator iter = fieldElements.iterator(); iter.hasNext();) {
142                 FieldDTO field = new FieldDTO();
143                 field.setDocId(dump.getDocId());
144                 Element element = (Element) iter.next();
145                 Attribute attribute = element.getAttribute("name");
146                 field.setFieldName(attribute.getValue());
147                 field.setFieldValue(element.getChildText("value"));
148                 fields.add(field);
149             }
150             dump.setFields(fields);
151             getExtractService().saveDump(dump);
152         } catch (Exception e) {
153             if (e instanceof RuntimeException) {
154                 throw (RuntimeException) e;
155             }
156             throw new RuntimeException(e);
157         }
158     }
159 
160     private ExtractService getExtractService() {
161         return EdlFrameworkServiceLocator.getExtractService();
162     }
163 
164     private static Element getRootElement(Document docContent) {
165         return XmlHelper.buildJDocument(docContent).getRootElement();
166     }
167 
168     private List setExtractFields(Document documentContent) {
169         Element rootElement = getRootElement(documentContent);
170         List<Element> fields = new ArrayList<Element>();
171         Collection<Element> fieldElements = XmlHelper.findElements(rootElement, "field");
172         Iterator<Element> elementIter = fieldElements.iterator();
173         while (elementIter.hasNext()) {
174             Element field = elementIter.next();
175             Element version = field.getParentElement();
176             if (version.getAttribute("current").getValue().equals("true")) {
177                 if (field.getAttribute("name") != null) {
178                     fields.add(field);
179                 }
180             }
181         }
182         return fields;
183     }
184 
185 }