Coverage Report - org.kuali.rice.kew.actions.CancelAction
 
Classes in this File Line Coverage Branch Coverage Complexity
CancelAction
0%
0/52
0%
0/18
3.667
 
 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 org.apache.log4j.MDC;
 20  
 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
 21  
 import org.kuali.rice.kew.actiontaken.ActionTakenValue;
 22  
 import org.kuali.rice.kew.exception.InvalidActionTakenException;
 23  
 import org.kuali.rice.kew.exception.WorkflowException;
 24  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 25  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 26  
 import org.kuali.rice.kew.util.KEWConstants;
 27  
 import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
 28  
 
 29  
 
 30  
 import java.util.Iterator;
 31  
 import java.util.List;
 32  
 
 33  
 
 34  
 /**
 35  
  * Cancels a document at the request of a client app.
 36  
  *
 37  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 38  
  */
 39  
 public class CancelAction extends ActionTakenEvent {
 40  
 
 41  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CancelAction.class);
 42  
 
 43  
     public CancelAction(DocumentRouteHeaderValue rh, PrincipalContract principal) {
 44  0
         super(KEWConstants.ACTION_TAKEN_CANCELED_CD, rh, principal);
 45  0
     }
 46  
 
 47  
     public CancelAction(DocumentRouteHeaderValue rh, PrincipalContract principal, String annotation) {
 48  0
         super(KEWConstants.ACTION_TAKEN_CANCELED_CD, rh, principal, annotation);
 49  0
     }
 50  
 
 51  
     /* (non-Javadoc)
 52  
      * @see org.kuali.rice.kew.actions.ActionTakenEvent#isActionCompatibleRequest(java.util.List)
 53  
      */
 54  
     @Override
 55  
     public String validateActionRules() {
 56  0
         return validateActionRules(getActionRequestService().findAllPendingRequests(routeHeader.getDocumentId()));
 57  
     }
 58  
 
 59  
     public String validateActionRules(List<ActionRequestValue> actionRequests) {
 60  
         // FYI delyea:  This is new validation check... was not being checked previously
 61  0
         if (!getRouteHeader().isValidActionToTake(getActionPerformedCode())) {
 62  0
             return "Document is not in a state to be cancelled";
 63  
         }
 64  0
         List<ActionRequestValue> filteredActionRequests = filterActionRequestsByCode(actionRequests, KEWConstants.ACTION_REQUEST_COMPLETE_REQ);
 65  0
         if (!isActionCompatibleRequest(filteredActionRequests)) {
 66  0
             return "No request for the user is compatible with the Cancel Action";
 67  
         }
 68  
             // check state before checking kim
 69  0
         if (! KEWServiceLocator.getDocumentTypePermissionService().canCancel(getPrincipal().getPrincipalId(), getDocumentId(), getRouteHeader().getDocumentType(), getRouteHeader().getCurrentNodeNames(), getRouteHeader().getDocRouteStatus(), getRouteHeader().getInitiatorWorkflowId())) {
 70  0
             return "User is not authorized to Cancel document";
 71  
         }
 72  0
         return "";
 73  
     }
 74  
 
 75  
     /* (non-Javadoc)
 76  
      * @see org.kuali.rice.kew.actions.ActionTakenEvent#isActionCompatibleRequest(java.util.List)
 77  
      */
 78  
     @Override
 79  
     public boolean isActionCompatibleRequest(List<ActionRequestValue> requests) {
 80  
 
 81  
         // can always cancel saved or initiated document
 82  0
         if (routeHeader.isStateInitiated() || routeHeader.isStateSaved()) {
 83  0
             return true;
 84  
         }
 85  
 
 86  0
         boolean actionCompatible = false;
 87  0
         Iterator ars = requests.iterator();
 88  0
         ActionRequestValue actionRequest = null;
 89  
 
 90  0
         while (ars.hasNext()) {
 91  0
             actionRequest = (ActionRequestValue) ars.next();
 92  0
             String request = actionRequest.getActionRequested();
 93  
 
 94  
             // APPROVE and COMPLETE request matches CANCEL Taken code
 95  0
             if ( (KEWConstants.ACTION_REQUEST_APPROVE_REQ.equals(request)) ||
 96  
                  (KEWConstants.ACTION_REQUEST_COMPLETE_REQ.equals(request)) ) {
 97  0
                 actionCompatible = true;
 98  0
                 break;
 99  
             }
 100  0
         }
 101  
 
 102  0
         return actionCompatible;
 103  
     }
 104  
 
 105  
     public void recordAction() throws InvalidActionTakenException {
 106  0
         MDC.put("docId", getRouteHeader().getDocumentId());
 107  0
         updateSearchableAttributesIfPossible();
 108  
 
 109  0
         LOG.debug("Canceling document : " + annotation);
 110  
 
 111  0
         List actionRequests = getActionRequestService().findAllValidRequests(getPrincipal().getPrincipalId(), getDocumentId(), KEWConstants.ACTION_REQUEST_COMPLETE_REQ);
 112  0
         LOG.debug("Checking to see if the action is legal");
 113  0
         String errorMessage = validateActionRules(actionRequests);
 114  0
         if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
 115  0
             throw new InvalidActionTakenException(errorMessage);
 116  
         }
 117  
 
 118  0
         LOG.debug("Record the cancel action");
 119  0
         ActionTakenValue actionTaken = saveActionTaken(findDelegatorForActionRequests(actionRequests));
 120  
 
 121  0
         LOG.debug("Deactivate all pending action requests");
 122  0
         actionRequests = getActionRequestService().findPendingByDoc(getDocumentId());
 123  
 
 124  0
         getActionRequestService().deactivateRequests(actionTaken, actionRequests);
 125  0
         notifyActionTaken(actionTaken);
 126  
 
 127  0
         LOG.debug("Canceling document");
 128  
 
 129  
         try {
 130  0
             String oldStatus = getRouteHeader().getDocRouteStatus();
 131  0
             getRouteHeader().markDocumentCanceled();
 132  0
             String newStatus = getRouteHeader().getDocRouteStatus();
 133  0
             KEWServiceLocator.getRouteHeaderService().saveRouteHeader(getRouteHeader());
 134  0
             notifyStatusChange(newStatus, oldStatus);
 135  0
         } catch (WorkflowException ex) {
 136  0
             LOG.warn(ex, ex);
 137  0
             throw new InvalidActionTakenException(ex.getMessage());
 138  0
         }
 139  0
     }
 140  
 }