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.EDLContext;
 22  
 import org.kuali.rice.edl.impl.EDLModelComponent;
 23  
 import org.kuali.rice.edl.impl.EDLXmlUtils;
 24  
 import org.kuali.rice.edl.impl.RequestParser;
 25  
 import org.kuali.rice.edl.impl.UserAction;
 26  
 import org.kuali.rice.kew.api.WorkflowDocument;
 27  
 import org.kuali.rice.kew.api.WorkflowDocumentFactory;
 28  
 import org.kuali.rice.kew.api.WorkflowRuntimeException;
 29  
 import org.kuali.rice.kew.api.document.DocumentStatus;
 30  
 import org.kuali.rice.kew.exception.WorkflowException;
 31  
 import org.w3c.dom.Document;
 32  
 import org.w3c.dom.Element;
 33  
 
 34  
 /**
 35  
  * Used as a pre processor and post processor. As a pre processor this creates/fetches the workflow
 36  
  * document and sets it on request. As a post processor this takes appropriate user action on the
 37  
  * 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 = WorkflowDocumentFactory.createDocument(edlContext.getUserSession().getPrincipalId(), edlContext
 72  
                     .getEdocLiteAssociation().getEdlName());
 73  0
             document.setTitle("Routing Document Type '" + document.getDocumentTypeName() + "'");
 74  0
             document.getDocumentId();
 75  0
             LOG.info("Created document " + document.getDocumentId());
 76  
         } else {
 77  0
             document = (WorkflowDocument) requestParser.getAttribute(RequestParser.WORKFLOW_DOCUMENT_SESSION_KEY);
 78  0
             if (document == null) {
 79  0
                 String docId = (String) requestParser.getAttribute("docId");
 80  0
                 if (docId == null) {
 81  0
                     LOG.info("no document found for edl " + edlContext.getEdocLiteAssociation().getEdlName());
 82  0
                     return;
 83  
                 } else {
 84  0
                     document = WorkflowDocumentFactory
 85  
                             .loadDocument(edlContext.getUserSession().getPrincipalId(), docId);
 86  
                 }
 87  
             }
 88  
         }
 89  
 
 90  0
         requestParser.setAttribute(RequestParser.WORKFLOW_DOCUMENT_SESSION_KEY, document);
 91  0
     }
 92  
 
 93  
     private void doPostProcessWork(Document dom, EDLContext edlContext) throws Exception {
 94  0
         RequestParser requestParser = edlContext.getRequestParser();
 95  
         // if the document is in error then we don't want to execute the action!
 96  0
         if (edlContext.isInError()) {
 97  0
             return;
 98  
         }
 99  0
         WorkflowDocument document = (WorkflowDocument) edlContext.getRequestParser().getAttribute(
 100  
                 RequestParser.WORKFLOW_DOCUMENT_SESSION_KEY);
 101  0
         if (document == null) {
 102  0
             return;
 103  
         }
 104  
         //strip out the data element
 105  0
         Element dataElement = (Element) dom.getElementsByTagName(EDLXmlUtils.DATA_E).item(0);
 106  0
         String docContent = XmlJotter.jotNode(dataElement);//use the transformer on edlcontext
 107  0
         document.setApplicationContent(docContent);
 108  0
         takeAction(document, dom, edlContext);
 109  0
     }
 110  
 
 111  
     public static void takeAction(WorkflowDocument document, Document dom, EDLContext edlContext)
 112  
             throws WorkflowException {
 113  0
         RequestParser requestParser = edlContext.getRequestParser();
 114  0
         UserAction userAction = edlContext.getUserAction();
 115  0
         String annotation = requestParser.getParameterValue("annotation");
 116  0
         String action = userAction.getAction();
 117  0
         String previousNodeName = requestParser.getParameterValue("previousNode");
 118  
 
 119  0
         if (!userAction.isValidatableAction()) {
 120  
             // if the action's not validatable, clear the attribute definitions because we don't want to end up executing validateClientRoutingData()
 121  
             // 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
 122  0
             document.clearAttributeDefinitions();
 123  
         }
 124  
 
 125  0
         boolean actionTaken = true;
 126  
 
 127  0
         if (UserAction.ACTION_ROUTE.equals(action)) {
 128  0
             document.route(annotation);
 129  0
         } else if (UserAction.ACTION_APPROVE.equals(action)) {
 130  0
             document.approve(annotation);
 131  0
         } else if (UserAction.ACTION_DISAPPROVE.equals(action)) {
 132  0
             document.disapprove(annotation);
 133  0
         } else if (UserAction.ACTION_CANCEL.equals(action)) {
 134  0
             document.cancel(annotation);
 135  0
         } else if (UserAction.ACTION_BLANKETAPPROVE.equals(action)) {
 136  0
             document.blanketApprove(annotation);
 137  0
         } else if (UserAction.ACTION_FYI.equals(action)) {
 138  0
             document.fyi();
 139  0
         } else if (UserAction.ACTION_ACKNOWLEDGE.equals(action)) {
 140  0
             document.acknowledge(annotation);
 141  0
         } else if (UserAction.ACTION_SAVE.equals(action)) {
 142  0
             if (document.getStatus().equals(DocumentStatus.INITIATED)) {
 143  0
                 document.saveDocument(annotation);
 144  
             } else {
 145  0
                 document.saveDocumentData();
 146  
             }
 147  0
         } else if (UserAction.ACTION_COMPLETE.equals(action)) {
 148  0
             document.complete(annotation);
 149  0
         } else if (UserAction.ACTION_DELETE.equals(action)) {
 150  0
             document.delete();
 151  0
         } else if (UserAction.ACTION_RETURN_TO_PREVIOUS.equals(action)) {
 152  0
             document.returnToPreviousNode(annotation, previousNodeName);
 153  
         } else {
 154  0
             actionTaken = false;
 155  
         }
 156  
 
 157  0
         if (actionTaken) {
 158  0
             Element actionTakenElement = EDLXmlUtils.getOrCreateChildElement(dom.getDocumentElement(), ACTION_TAKEN,
 159  
                     true);
 160  0
             actionTakenElement.appendChild(dom.createTextNode(action));
 161  
         }
 162  0
     }
 163  
 
 164  
 }