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