|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  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 |     | 
     | 
  |  36 |     | 
     | 
  |  37 |     | 
     | 
  |  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 |     | 
         | 
  |  52 |     | 
     | 
  |  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 |     | 
             | 
  |  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 |     | 
                 | 
  |  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 |     | 
         | 
  |  76 |     | 
     | 
  |  77 |     | 
     | 
  |  78 |     | 
       @Override  | 
  |  79 |     | 
       public boolean isActionCompatibleRequest(List<ActionRequestValue> requests) { | 
  |  80 |     | 
     | 
  |  81 |     | 
             | 
  |  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 |     | 
                 | 
  |  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 |     | 
   }  |