Coverage Report - org.kuali.rice.kew.actions.RouteDocumentAction
 
Classes in this File Line Coverage Branch Coverage Complexity
RouteDocumentAction
0%
0/44
0%
0/16
3.167
 
 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.actions;
 18  
 
 19  
 import java.util.Iterator;
 20  
 import java.util.List;
 21  
 
 22  
 import org.apache.log4j.MDC;
 23  
 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
 24  
 import org.kuali.rice.kew.actiontaken.ActionTakenValue;
 25  
 import org.kuali.rice.kew.exception.InvalidActionTakenException;
 26  
 import org.kuali.rice.kew.exception.WorkflowException;
 27  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 28  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 29  
 import org.kuali.rice.kew.util.KEWConstants;
 30  
 import org.kuali.rice.kew.util.Utilities;
 31  
 import org.kuali.rice.kim.bo.entity.KimPrincipal;
 32  
 import org.kuali.rice.kew.engine.node.Process;
 33  
 
 34  
 
 35  
 /**
 36  
  * Action that puts the document in routing.
 37  
  *
 38  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 39  
  *
 40  
  */
 41  
 public class RouteDocumentAction extends ActionTakenEvent {
 42  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RouteDocumentAction.class);
 43  
 
 44  
     public RouteDocumentAction(DocumentRouteHeaderValue rh, KimPrincipal principal) {
 45  0
         super(KEWConstants.ACTION_TAKEN_COMPLETED_CD, rh, principal);
 46  0
     }
 47  
 
 48  
     public RouteDocumentAction(DocumentRouteHeaderValue rh, KimPrincipal principal, String annotation) {
 49  0
         super(KEWConstants.ACTION_TAKEN_COMPLETED_CD, rh, principal, annotation);
 50  0
     }
 51  
 
 52  
     /* (non-Javadoc)
 53  
      * @see org.kuali.rice.kew.actions.ActionTakenEvent#getActionPerformedCode()
 54  
      */
 55  
     @Override
 56  
     public String getActionPerformedCode() {
 57  0
         return KEWConstants.ACTION_TAKEN_ROUTED_CD;
 58  
     }
 59  
 
 60  
     /* (non-Javadoc)
 61  
      * @see org.kuali.rice.kew.actions.ActionTakenEvent#isActionCompatibleRequest(java.util.List)
 62  
      */
 63  
     @Override
 64  
     public String validateActionRules() {
 65  
             // check state before checking kim
 66  0
         if (!getRouteHeader().isValidActionToTake(getActionPerformedCode())) {
 67  0
             return "Document is not in a state to be routed";
 68  
         }
 69  0
             if (! KEWServiceLocator.getDocumentTypePermissionService().canRoute(getPrincipal().getPrincipalId(), getRouteHeader())) {
 70  0
                     return "User is not authorized to Route document";
 71  
             }
 72  0
         return "";
 73  
     }
 74  
 
 75  
     @Override
 76  
     public String validateActionRules(List<ActionRequestValue> actionRequests) {
 77  0
             return validateActionRules();
 78  
     }
 79  
     
 80  
     /**
 81  
      * Record the routing action. To route a document, it must be in the proper state. Previous requests and actions have no bearing on the outcome of this action, unless the
 82  
      * @throws org.kuali.rice.kew.exception.InvalidActionTakenException
 83  
      */
 84  
     public void recordAction() throws InvalidActionTakenException {
 85  0
         MDC.put("docId", getRouteHeader().getRouteHeaderId());
 86  0
         updateSearchableAttributesIfPossible();
 87  
 
 88  0
         LOG.debug("Routing document : " + annotation);
 89  
 
 90  0
         LOG.debug("Checking to see if the action is legal");
 91  0
         String errorMessage = validateActionRules();
 92  0
         if (!Utilities.isEmpty(errorMessage)) {
 93  0
             throw new InvalidActionTakenException(errorMessage);
 94  
         }
 95  
 
 96  
 
 97  
         // we want to check that the "RouteDocument" command is valid here, not the "Complete" command (which is in our Action's action taken code)
 98  0
         LOG.debug("Record the routing action");
 99  0
         ActionTakenValue actionTaken = saveActionTaken();
 100  
 
 101  
         //TODO this will get all pending AR's even if they haven't been in an action list... This seems bad
 102  0
         List<ActionRequestValue> actionRequests = getActionRequestService().findPendingByDoc(getRouteHeaderId());
 103  0
         LOG.debug("Deactivate all pending action requests");
 104  
         // deactivate any requests for the user that routed the document.
 105  0
         for (ActionRequestValue actionRequest : actionRequests)
 106  
         {
 107  
             // requests generated to the user who is routing the document should be deactivated
 108  0
             if ((getPrincipal().getPrincipalId().equals(actionRequest.getPrincipalId())) && (actionRequest.isActive()))
 109  
             {
 110  0
                 getActionRequestService().deactivateRequest(actionTaken, actionRequest);
 111  
             }
 112  
             // requests generated by a save action should be deactivated
 113  0
             else if (KEWConstants.SAVED_REQUEST_RESPONSIBILITY_ID.equals(actionRequest.getResponsibilityId()))
 114  
             {
 115  0
                 getActionRequestService().deactivateRequest(actionTaken, actionRequest);
 116  
             }
 117  
         }
 118  
 
 119  0
             notifyActionTaken(actionTaken);
 120  
 
 121  
             try {
 122  0
                 String oldStatus = getRouteHeader().getDocRouteStatus();
 123  0
                 getRouteHeader().markDocumentEnroute();
 124  
                 
 125  0
                 if (((Process)getRouteHeader().getDocumentType().getProcesses().get(0)).getInitialRouteNode() == null) {
 126  0
                     getRouteHeader().markDocumentApproved();
 127  0
                     getRouteHeader().markDocumentProcessed();
 128  0
                     getRouteHeader().markDocumentFinalized();
 129  
                 }
 130  
 
 131  0
                 getRouteHeader().setRoutedByUserWorkflowId(getPrincipal().getPrincipalId());
 132  
 
 133  0
                 String newStatus = getRouteHeader().getDocRouteStatus();
 134  0
                 notifyStatusChange(newStatus, oldStatus);
 135  0
                 KEWServiceLocator.getRouteHeaderService().saveRouteHeader(getRouteHeader());
 136  0
             } catch (WorkflowException ex) {
 137  0
                 LOG.warn(ex, ex);
 138  0
                     throw new InvalidActionTakenException(ex.getMessage());
 139  0
             }
 140  
 //        } else {
 141  
 //            LOG.warn("Document not in state to be routed.");
 142  
 //            throw new InvalidActionTakenException("Document is not in a state to be routed");
 143  
 //        }
 144  0
     }
 145  
 }