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