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.dto.ActionTakenEventDTO;
 35  
 import org.kuali.rice.kew.dto.DeleteEventDTO;
 36  
 import org.kuali.rice.kew.dto.DocumentRouteLevelChangeDTO;
 37  
 import org.kuali.rice.kew.dto.DocumentRouteStatusChangeDTO;
 38  
 import org.kuali.rice.kew.dto.DocumentTypeDTO;
 39  
 import org.kuali.rice.kew.dto.RouteHeaderDTO;
 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  
 
 47  
 
 48  0
 public class EDocLiteDatabasePostProcessor extends EDocLitePostProcessor {
 49  
 
 50  0
         private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(EDocLiteDatabasePostProcessor.class);
 51  
 
 52  
            public boolean doRouteStatusChange(DocumentRouteStatusChangeDTO event) throws RemoteException {
 53  0
                 LOG.debug("doRouteStatusChange: " + event);
 54  0
                 String documentId = event.getDocumentId();
 55  0
                 super.postEvent(documentId, event, "statusChange");
 56  0
                 Document doc = getEDLContent(documentId);
 57  0
                 if (LOG.isDebugEnabled()) {
 58  0
                 LOG.debug("Submitting doc: " + XmlJotter.jotNode(doc));
 59  
             }
 60  0
                         extractEDLData(documentId, getNodeNames(event.getDocumentId()), doc);
 61  0
                 return super.doRouteStatusChange(event);
 62  
             }
 63  
 
 64  
             public boolean doActionTaken(ActionTakenEventDTO event) throws RemoteException {
 65  0
                 LOG.debug("doActionTaken: " + event);
 66  0
                 String documentId = event.getDocumentId();
 67  0
                 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  0
                           if (KEWConstants.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  
             public boolean doDeleteRouteHeader(DeleteEventDTO event) throws RemoteException {
 79  0
                 LOG.debug("doDeleteRouteHeader: " + event);
 80  0
                 super.postEvent(event.getDocumentId(), event, "deleteRouteHeader");
 81  0
                 return super.doDeleteRouteHeader(event);
 82  
             }
 83  
 
 84  
             public boolean doRouteLevelChange(DocumentRouteLevelChangeDTO event) throws RemoteException {
 85  0
                 LOG.debug("doRouteLevelChange: " + event);
 86  0
                 String documentId = event.getDocumentId();
 87  0
                 super.postEvent(documentId, event, "routeLevelChange");
 88  0
                 Document doc = getEDLContent(documentId);
 89  0
             if (LOG.isDebugEnabled()) {
 90  0
                 LOG.debug("Submitting doc: " + XmlJotter.jotNode(doc));
 91  
             }
 92  0
                         extractEDLData(documentId, new String[] {event.getNewNodeName()}, doc);
 93  0
                 return super.doRouteLevelChange(event);
 94  
             }
 95  
 
 96  
 //            public static Document getEDLContent(DocumentRouteHeaderValue routeHeader) throws Exception {
 97  
 //                String content = routeHeader.getDocContent();
 98  
 //                Document doc =  DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(content)));
 99  
 //                return doc;
 100  
 //            }
 101  
 
 102  
             private String[] getNodeNames(String documentId) {
 103  
                     try {
 104  0
                     RouteNodeInstanceDTO[] activeNodeInstances = new WorkflowInfo().getActiveNodeInstances(documentId);
 105  0
                     if (activeNodeInstances == null || activeNodeInstances.length == 0) {
 106  0
                         activeNodeInstances = new WorkflowInfo().getTerminalNodeInstances(documentId);
 107  
                     }
 108  0
                     String[] nodeNames = new String[(activeNodeInstances == null ? 0 : activeNodeInstances.length)];
 109  0
                     for (int index = 0; index < activeNodeInstances.length; index++) {
 110  0
                         nodeNames[index] = activeNodeInstances[index].getName();
 111  
                     }
 112  0
                     return nodeNames;
 113  0
                     } catch (WorkflowException e) {
 114  0
                             throw new RuntimeException(e);
 115  
                     }
 116  
             }
 117  
 
 118  
             private void extractEDLData(String documentId, String[] nodeNames, Document documentContent) {
 119  
                     try {
 120  0
                     RouteHeaderDTO routeHeader = new WorkflowInfo().getRouteHeader(documentId);
 121  0
                     DocumentTypeDTO documentType = new WorkflowInfo().getDocType(routeHeader.getDocTypeId());
 122  0
                     DumpDTO dump = getExtractService().getDumpByDocumentId(routeHeader.getDocumentId());
 123  0
                     if (dump == null) {
 124  0
                             dump = new DumpDTO();
 125  
                     }
 126  0
                     dump.setDocId(routeHeader.getDocumentId());
 127  0
                         dump.setDocCreationDate(new Timestamp(routeHeader.getDateCreated().getTimeInMillis()));
 128  0
                     dump.setDocCurrentNodeName(StringUtils.join(nodeNames, ","));
 129  0
                         dump.setDocDescription(documentType.getDocTypeDescription());
 130  0
                         if (routeHeader.getDateLastModified() != null) {
 131  0
                                 dump.setDocModificationDate(new Timestamp(routeHeader.getDateLastModified().getTimeInMillis()));
 132  
                         }
 133  0
                         dump.setDocInitiatorId(routeHeader.getInitiatorPrincipalId());
 134  0
                         dump.setDocRouteStatusCode(routeHeader.getDocRouteStatus());
 135  0
                         dump.setDocTypeName(documentType.getName());
 136  
 
 137  0
                         List<FieldDTO> fields = dump.getFields();
 138  0
                         fields.clear();
 139  
 
 140  0
                         List fieldElements = setExtractFields(routeHeader, 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  
 
 161  
         private ExtractService getExtractService() {
 162  0
                 return EdlFrameworkServiceLocator.getExtractService();
 163  
         }
 164  
 
 165  
 
 166  
         private static Element getRootElement(Document docContent) {
 167  0
                 return XmlHelper.buildJDocument(docContent).getRootElement();
 168  
         }
 169  
 
 170  
         private List setExtractFields(RouteHeaderDTO routeHeader, Document documentContent) {
 171  0
                 Element rootElement = getRootElement(documentContent);
 172  0
                 List<Element> fields = new ArrayList<Element>();
 173  0
                 Collection<Element> fieldElements = XmlHelper.findElements(rootElement, "field");
 174  0
         Iterator<Element> elementIter = fieldElements.iterator();
 175  0
         while (elementIter.hasNext()) {
 176  0
                 Element field = elementIter.next();
 177  0
                 Element version = field.getParentElement();
 178  0
                 if (version.getAttribute("current").getValue().equals("true")) {
 179  0
                        if (field.getAttribute("name")!= null) {
 180  0
                                fields.add(field);
 181  
                        }
 182  
                 }
 183  0
         }
 184  0
         return fields;
 185  
         }
 186  
 
 187  
 }