Coverage Report - org.kuali.rice.kew.edl.components.WorkflowDocumentActions
 
Classes in this File Line Coverage Branch Coverage Complexity
WorkflowDocumentActions
0%
0/75
0%
0/40
7.25
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.kew.edl.components;
 18  
 
 19  
 import org.apache.log4j.Logger;
 20  
 import org.kuali.rice.kew.dto.NetworkIdDTO;
 21  
 import org.kuali.rice.kew.edl.EDLContext;
 22  
 import org.kuali.rice.kew.edl.EDLModelComponent;
 23  
 import org.kuali.rice.kew.edl.EDLXmlUtils;
 24  
 import org.kuali.rice.kew.edl.RequestParser;
 25  
 import org.kuali.rice.kew.edl.UserAction;
 26  
 import org.kuali.rice.kew.exception.WorkflowException;
 27  
 import org.kuali.rice.kew.exception.WorkflowRuntimeException;
 28  
 import org.kuali.rice.kew.service.WorkflowDocument;
 29  
 import org.kuali.rice.kew.util.XmlHelper;
 30  
 import org.w3c.dom.Document;
 31  
 import org.w3c.dom.Element;
 32  
 
 33  
 
 34  
 /**
 35  
  * Used as a pre processor and post processor.
 36  
  * As a pre processor this creates/fetches the workflow document and sets it on request.
 37  
  * As a post processor this takes appropriate user action on the document if the document is not in error.
 38  
  *
 39  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 40  
  *
 41  
  */
 42  0
 public class WorkflowDocumentActions implements EDLModelComponent {
 43  
 
 44  0
         private static final Logger LOG = Logger.getLogger(WorkflowDocumentActions.class);
 45  
 
 46  
         public static final String ACTION_TAKEN = "actionTaken";
 47  
 
 48  
         boolean isPreProcessor;
 49  
 
 50  
         public void updateDOM(Document dom, Element configElement, EDLContext edlContext) {
 51  
 
 52  
                 try {
 53  0
                         isPreProcessor = configElement.getTagName().equals("preProcessor");
 54  0
                         if (isPreProcessor) {
 55  0
                                 doPreProcessWork(edlContext);
 56  
                         } else {
 57  0
                                 doPostProcessWork(dom, edlContext);
 58  
                         }
 59  0
                 } catch (Exception e) {
 60  0
                         throw new WorkflowRuntimeException(e);
 61  0
                 }
 62  
 
 63  0
         }
 64  
 
 65  
         private void doPreProcessWork(EDLContext edlContext) throws Exception {
 66  0
                 RequestParser requestParser = edlContext.getRequestParser();
 67  
 
 68  0
                 UserAction userAction = edlContext.getUserAction();
 69  0
                 WorkflowDocument document = null;
 70  0
                 if (UserAction.ACTION_CREATE.equals(userAction.getAction())) {
 71  0
                         document = new WorkflowDocument(edlContext.getUserSession().getPrincipalId(), edlContext.getEdocLiteAssociation().getEdlName());
 72  0
                         document.setTitle("Routing Document Type '" + document.getDocumentType() + "'");
 73  0
                         document.getRouteHeaderId();
 74  0
                         LOG.info("Created document " + document.getRouteHeaderId());
 75  
                 } else {
 76  0
                         document = (WorkflowDocument)requestParser.getAttribute(RequestParser.WORKFLOW_DOCUMENT_SESSION_KEY);
 77  0
                         if (document == null) {
 78  0
                                 String docId = (String) requestParser.getAttribute("docId");
 79  0
                                 if (docId == null) {
 80  0
                                         LOG.info("no document found for edl " + edlContext.getEdocLiteAssociation().getEdlName());
 81  0
                                         return;
 82  
                                 } else {
 83  0
                                         document = new WorkflowDocument(edlContext.getUserSession().getPrincipalId(), new Long(docId));
 84  
                                 }
 85  
                         }
 86  
                 }
 87  
 
 88  0
                 requestParser.setAttribute(RequestParser.WORKFLOW_DOCUMENT_SESSION_KEY, document);
 89  0
         }
 90  
 
 91  
         private void doPostProcessWork(Document dom, EDLContext edlContext) throws Exception {
 92  0
                 RequestParser requestParser = edlContext.getRequestParser();
 93  
                 // if the document is in error then we don't want to execute the action!
 94  0
                 if (edlContext.isInError()) {
 95  0
                         return;
 96  
                 }
 97  0
                 WorkflowDocument document = (WorkflowDocument)edlContext.getRequestParser().getAttribute(RequestParser.WORKFLOW_DOCUMENT_SESSION_KEY);
 98  0
                 if (document == null) {
 99  0
                         return;
 100  
                 }
 101  
                 //strip out the data element
 102  0
                 Element dataElement = (Element) dom.getElementsByTagName(EDLXmlUtils.DATA_E).item(0);
 103  0
                 String docContent = XmlHelper.writeNode(dataElement);//use the transformer on edlcontext
 104  0
                 document.setApplicationContent(docContent);
 105  0
                 takeAction(document, dom, edlContext);
 106  0
         }
 107  
 
 108  
         public static void takeAction(WorkflowDocument document, Document dom, EDLContext edlContext) throws WorkflowException {
 109  0
                     RequestParser requestParser = edlContext.getRequestParser();
 110  0
                     UserAction userAction = edlContext.getUserAction();
 111  0
                 String annotation = requestParser.getParameterValue("annotation");
 112  0
                 String action = userAction.getAction();
 113  0
                 String previousNodeName = requestParser.getParameterValue("previousNode");
 114  
 
 115  0
                 if (!userAction.isValidatableAction()) {
 116  
                         // if the action's not validatable, clear the attribute definitions because we don't want to end up executing validateClientRoutingData()
 117  
                         // TODO the problem here is that the XML is still updated on a cancel so we end up without any attribute content in the document content
 118  0
                         document.clearAttributeDefinitions();
 119  
                 }
 120  
 
 121  0
         boolean actionTaken = true;
 122  
 
 123  0
         if (UserAction.ACTION_ROUTE.equals(action)) {
 124  0
                 document.routeDocument(annotation);
 125  0
         } else if (UserAction.ACTION_APPROVE.equals(action)) {
 126  0
                 document.approve(annotation);
 127  0
         } else if (UserAction.ACTION_DISAPPROVE.equals(action)) {
 128  0
                 document.disapprove(annotation);
 129  0
         } else if (UserAction.ACTION_CANCEL.equals(action)) {
 130  0
                 document.cancel(annotation);
 131  0
         } else if (UserAction.ACTION_BLANKETAPPROVE.equals(action)) {
 132  0
                 document.blanketApprove(annotation);
 133  0
         } else if (UserAction.ACTION_FYI.equals(action)) {
 134  0
                 document.fyi();
 135  0
         } else if (UserAction.ACTION_ACKNOWLEDGE.equals(action)) {
 136  0
                 document.acknowledge(annotation);
 137  0
         } else if (UserAction.ACTION_SAVE.equals(action)) {
 138  0
             if (document.getStatusDisplayValue().equals("INITIATED")) {
 139  0
                 document.saveDocument(annotation);
 140  
             } else {
 141  0
                 document.saveRoutingData();
 142  
             }
 143  0
         } else if (UserAction.ACTION_COMPLETE.equals(action)) {
 144  0
                 document.complete(annotation);
 145  0
         } else if (UserAction.ACTION_DELETE.equals(action)) {
 146  0
                 document.delete();
 147  0
         } else if (UserAction.ACTION_RETURN_TO_PREVIOUS.equals(action)) {
 148  0
             document.returnToPreviousNode(annotation, previousNodeName);
 149  
         } else {
 150  0
             actionTaken = false;
 151  
         }
 152  
 
 153  0
         if (actionTaken) {
 154  0
             Element actionTakenElement = EDLXmlUtils.getOrCreateChildElement(dom.getDocumentElement(), ACTION_TAKEN, true);
 155  0
             actionTakenElement.appendChild(dom.createTextNode(action));
 156  
         }
 157  0
     }
 158  
 
 159  
 }