Coverage Report - org.kuali.rice.edl.impl.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.edl.impl.components;
 18  
 
 19  
 import org.apache.log4j.Logger;
 20  
 import org.kuali.rice.core.util.xml.XmlJotter;
 21  
 import org.kuali.rice.edl.impl.*;
 22  
 import org.kuali.rice.kew.exception.WorkflowException;
 23  
 import org.kuali.rice.kew.exception.WorkflowRuntimeException;
 24  
 import org.kuali.rice.kew.service.WorkflowDocument;
 25  
 import org.w3c.dom.Document;
 26  
 import org.w3c.dom.Element;
 27  
 
 28  
 
 29  
 /**
 30  
  * Used as a pre processor and post processor.
 31  
  * As a pre processor this creates/fetches the workflow document and sets it on request.
 32  
  * As a post processor this takes appropriate user action on the document if the document is not in error.
 33  
  *
 34  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 35  
  *
 36  
  */
 37  0
 public class WorkflowDocumentActions implements EDLModelComponent {
 38  
 
 39  0
         private static final Logger LOG = Logger.getLogger(WorkflowDocumentActions.class);
 40  
 
 41  
         public static final String ACTION_TAKEN = "actionTaken";
 42  
 
 43  
         boolean isPreProcessor;
 44  
 
 45  
         public void updateDOM(Document dom, Element configElement, EDLContext edlContext) {
 46  
 
 47  
                 try {
 48  0
                         isPreProcessor = configElement.getTagName().equals("preProcessor");
 49  0
                         if (isPreProcessor) {
 50  0
                                 doPreProcessWork(edlContext);
 51  
                         } else {
 52  0
                                 doPostProcessWork(dom, edlContext);
 53  
                         }
 54  0
                 } catch (Exception e) {
 55  0
                         throw new WorkflowRuntimeException(e);
 56  0
                 }
 57  
 
 58  0
         }
 59  
 
 60  
         private void doPreProcessWork(EDLContext edlContext) throws Exception {
 61  0
                 RequestParser requestParser = edlContext.getRequestParser();
 62  
 
 63  0
                 UserAction userAction = edlContext.getUserAction();
 64  0
                 WorkflowDocument document = null;
 65  0
                 if (UserAction.ACTION_CREATE.equals(userAction.getAction())) {
 66  0
                         document = WorkflowDocument.createDocument(edlContext.getUserSession().getPrincipalId(), edlContext.getEdocLiteAssociation().getEdlName());
 67  0
                         document.setTitle("Routing Document Type '" + document.getDocumentType() + "'");
 68  0
                         document.getDocumentId();
 69  0
                         LOG.info("Created document " + document.getDocumentId());
 70  
                 } else {
 71  0
                         document = (WorkflowDocument)requestParser.getAttribute(RequestParser.WORKFLOW_DOCUMENT_SESSION_KEY);
 72  0
                         if (document == null) {
 73  0
                                 String docId = (String) requestParser.getAttribute("docId");
 74  0
                                 if (docId == null) {
 75  0
                                         LOG.info("no document found for edl " + edlContext.getEdocLiteAssociation().getEdlName());
 76  0
                                         return;
 77  
                                 } else {
 78  0
                                         document =  WorkflowDocument.loadDocument(edlContext.getUserSession().getPrincipalId(), docId);
 79  
                                 }
 80  
                         }
 81  
                 }
 82  
 
 83  0
                 requestParser.setAttribute(RequestParser.WORKFLOW_DOCUMENT_SESSION_KEY, document);
 84  0
         }
 85  
 
 86  
         private void doPostProcessWork(Document dom, EDLContext edlContext) throws Exception {
 87  0
                 RequestParser requestParser = edlContext.getRequestParser();
 88  
                 // if the document is in error then we don't want to execute the action!
 89  0
                 if (edlContext.isInError()) {
 90  0
                         return;
 91  
                 }
 92  0
                 WorkflowDocument document = (WorkflowDocument)edlContext.getRequestParser().getAttribute(RequestParser.WORKFLOW_DOCUMENT_SESSION_KEY);
 93  0
                 if (document == null) {
 94  0
                         return;
 95  
                 }
 96  
                 //strip out the data element
 97  0
                 Element dataElement = (Element) dom.getElementsByTagName(EDLXmlUtils.DATA_E).item(0);
 98  0
                 String docContent = XmlJotter.jotNode(dataElement);//use the transformer on edlcontext
 99  0
                 document.setApplicationContent(docContent);
 100  0
                 takeAction(document, dom, edlContext);
 101  0
         }
 102  
 
 103  
         public static void takeAction(WorkflowDocument document, Document dom, EDLContext edlContext) throws WorkflowException {
 104  0
                     RequestParser requestParser = edlContext.getRequestParser();
 105  0
                     UserAction userAction = edlContext.getUserAction();
 106  0
                 String annotation = requestParser.getParameterValue("annotation");
 107  0
                 String action = userAction.getAction();
 108  0
                 String previousNodeName = requestParser.getParameterValue("previousNode");
 109  
 
 110  0
                 if (!userAction.isValidatableAction()) {
 111  
                         // if the action's not validatable, clear the attribute definitions because we don't want to end up executing validateClientRoutingData()
 112  
                         // 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
 113  0
                         document.clearAttributeDefinitions();
 114  
                 }
 115  
 
 116  0
         boolean actionTaken = true;
 117  
 
 118  0
         if (UserAction.ACTION_ROUTE.equals(action)) {
 119  0
                 document.routeDocument(annotation);
 120  0
         } else if (UserAction.ACTION_APPROVE.equals(action)) {
 121  0
                 document.approve(annotation);
 122  0
         } else if (UserAction.ACTION_DISAPPROVE.equals(action)) {
 123  0
                 document.disapprove(annotation);
 124  0
         } else if (UserAction.ACTION_CANCEL.equals(action)) {
 125  0
                 document.cancel(annotation);
 126  0
         } else if (UserAction.ACTION_BLANKETAPPROVE.equals(action)) {
 127  0
                 document.blanketApprove(annotation);
 128  0
         } else if (UserAction.ACTION_FYI.equals(action)) {
 129  0
                 document.fyi();
 130  0
         } else if (UserAction.ACTION_ACKNOWLEDGE.equals(action)) {
 131  0
                 document.acknowledge(annotation);
 132  0
         } else if (UserAction.ACTION_SAVE.equals(action)) {
 133  0
             if (document.getStatusDisplayValue().equals("INITIATED")) {
 134  0
                 document.saveDocument(annotation);
 135  
             } else {
 136  0
                 document.saveRoutingData();
 137  
             }
 138  0
         } else if (UserAction.ACTION_COMPLETE.equals(action)) {
 139  0
                 document.complete(annotation);
 140  0
         } else if (UserAction.ACTION_DELETE.equals(action)) {
 141  0
                 document.delete();
 142  0
         } else if (UserAction.ACTION_RETURN_TO_PREVIOUS.equals(action)) {
 143  0
             document.returnToPreviousNode(annotation, previousNodeName);
 144  
         } else {
 145  0
             actionTaken = false;
 146  
         }
 147  
 
 148  0
         if (actionTaken) {
 149  0
             Element actionTakenElement = EDLXmlUtils.getOrCreateChildElement(dom.getDocumentElement(), ACTION_TAKEN, true);
 150  0
             actionTakenElement.appendChild(dom.createTextNode(action));
 151  
         }
 152  0
     }
 153  
 
 154  
 }