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