Coverage Report - org.kuali.rice.edl.framework.workflow.EDocLiteDatabasePostProcessor
 
Classes in this File Line Coverage Branch Coverage Complexity
EDocLiteDatabasePostProcessor
0%
0/85
0%
0/28
3.222
 
 1  
 /*
 2  
  * Copyright 2008-2009 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 java.rmi.RemoteException;
 19  
 import java.sql.Timestamp;
 20  
 import java.util.ArrayList;
 21  
 import java.util.Collection;
 22  
 import java.util.Iterator;
 23  
 import java.util.List;
 24  
 
 25  
 import org.apache.commons.lang.StringUtils;
 26  
 import org.jdom.Attribute;
 27  
 import org.jdom.Element;
 28  
 import org.kuali.rice.core.util.xml.XmlHelper;
 29  
 import org.kuali.rice.core.util.xml.XmlJotter;
 30  
 import org.kuali.rice.edl.framework.extract.DumpDTO;
 31  
 import org.kuali.rice.edl.framework.extract.ExtractService;
 32  
 import org.kuali.rice.edl.framework.extract.FieldDTO;
 33  
 import org.kuali.rice.edl.framework.services.EdlFrameworkServiceLocator;
 34  
 import org.kuali.rice.kew.api.KewApiServiceLocator;
 35  
 import org.kuali.rice.kew.api.doctype.DocumentType;
 36  
 import org.kuali.rice.kew.dto.ActionTakenEventDTO;
 37  
 import org.kuali.rice.kew.dto.DeleteEventDTO;
 38  
 import org.kuali.rice.kew.dto.DocumentRouteLevelChangeDTO;
 39  
 import org.kuali.rice.kew.dto.DocumentRouteStatusChangeDTO;
 40  
 import org.kuali.rice.kew.dto.RouteNodeInstanceDTO;
 41  
 import org.kuali.rice.kew.exception.WorkflowException;
 42  
 import org.kuali.rice.kew.service.WorkflowInfo;
 43  
 import org.kuali.rice.kew.util.KEWConstants;
 44  
 import org.w3c.dom.Document;
 45  
 
 46  0
 public class EDocLiteDatabasePostProcessor extends EDocLitePostProcessor {
 47  
 
 48  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
 49  
             .getLogger(EDocLiteDatabasePostProcessor.class);
 50  
 
 51  
     public boolean doRouteStatusChange(DocumentRouteStatusChangeDTO 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  
     public boolean doActionTaken(ActionTakenEventDTO event) throws RemoteException {
 64  0
         LOG.debug("doActionTaken: " + event);
 65  0
         String documentId = event.getDocumentId();
 66  0
         super.postEvent(documentId, event, "actionTaken");
 67  
 
 68  
         // if the action requested is a save, go ahead and update the database with the most current information. -grpatter
 69  0
         if (KEWConstants.ACTION_TAKEN_SAVED_CD.equals(event.getActionTaken().getActionTaken())) {
 70  0
             Document doc = getEDLContent(documentId);
 71  0
             extractEDLData(documentId, getNodeNames(event.getDocumentId()), doc);
 72  
         }
 73  
 
 74  0
         return super.doActionTaken(event);
 75  
     }
 76  
 
 77  
     public boolean doDeleteRouteHeader(DeleteEventDTO event) throws RemoteException {
 78  0
         LOG.debug("doDeleteRouteHeader: " + event);
 79  0
         super.postEvent(event.getDocumentId(), event, "deleteRouteHeader");
 80  0
         return super.doDeleteRouteHeader(event);
 81  
     }
 82  
 
 83  
     public boolean doRouteLevelChange(DocumentRouteLevelChangeDTO event) throws RemoteException {
 84  0
         LOG.debug("doRouteLevelChange: " + event);
 85  0
         String documentId = event.getDocumentId();
 86  0
         super.postEvent(documentId, event, "routeLevelChange");
 87  0
         Document doc = getEDLContent(documentId);
 88  0
         if (LOG.isDebugEnabled()) {
 89  0
             LOG.debug("Submitting doc: " + XmlJotter.jotNode(doc));
 90  
         }
 91  0
         extractEDLData(documentId, new String[]{event.getNewNodeName()}, doc);
 92  0
         return super.doRouteLevelChange(event);
 93  
     }
 94  
 
 95  
     //            public static Document getEDLContent(DocumentRouteHeaderValue routeHeader) throws Exception {
 96  
     //                String content = routeHeader.getDocContent();
 97  
     //                Document doc =  DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(content)));
 98  
     //                return doc;
 99  
     //            }
 100  
 
 101  
     private String[] getNodeNames(String documentId) {
 102  
         try {
 103  0
             RouteNodeInstanceDTO[] activeNodeInstances = new WorkflowInfo().getActiveNodeInstances(documentId);
 104  0
             if (activeNodeInstances == null || activeNodeInstances.length == 0) {
 105  0
                 activeNodeInstances = new WorkflowInfo().getTerminalNodeInstances(documentId);
 106  
             }
 107  0
             String[] nodeNames = new String[(activeNodeInstances == null ? 0 : activeNodeInstances.length)];
 108  0
             for (int index = 0; index < activeNodeInstances.length; index++) {
 109  0
                 nodeNames[index] = activeNodeInstances[index].getName();
 110  
             }
 111  0
             return nodeNames;
 112  0
         } catch (WorkflowException e) {
 113  0
             throw new RuntimeException(e);
 114  
         }
 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  
 }