|  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.actionrequest.Recipient;  | 
  |  22 |     | 
   import org.kuali.rice.kew.actiontaken.ActionTakenValue;  | 
  |  23 |     | 
   import org.kuali.rice.kew.doctype.DocumentTypePolicy;  | 
  |  24 |     | 
   import org.kuali.rice.kew.exception.InvalidActionTakenException;  | 
  |  25 |     | 
   import org.kuali.rice.kew.exception.ResourceUnavailableException;  | 
  |  26 |     | 
   import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;  | 
  |  27 |     | 
   import org.kuali.rice.kew.service.KEWServiceLocator;  | 
  |  28 |     | 
   import org.kuali.rice.kew.util.KEWConstants;  | 
  |  29 |     | 
   import org.kuali.rice.kim.api.entity.principal.PrincipalContract;  | 
  |  30 |     | 
     | 
  |  31 |     | 
     | 
  |  32 |     | 
   import java.util.Iterator;  | 
  |  33 |     | 
   import java.util.List;  | 
  |  34 |     | 
     | 
  |  35 |     | 
     | 
  |  36 |     | 
     | 
  |  37 |     | 
     | 
  |  38 |     | 
     | 
  |  39 |     | 
     | 
  |  40 |     | 
     | 
  |  41 |     | 
     | 
  |  42 |     | 
     | 
  |  43 |     | 
     | 
  |  44 |     | 
     | 
  |  45 |     | 
     | 
  |  46 |     | 
   public class ApproveAction extends ActionTakenEvent { | 
  |  47 |    0 |        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ApproveAction.class);  | 
  |  48 |     | 
     | 
  |  49 |     | 
         | 
  |  50 |     | 
     | 
  |  51 |     | 
     | 
  |  52 |     | 
     | 
  |  53 |     | 
     | 
  |  54 |     | 
     | 
  |  55 |     | 
       public ApproveAction(DocumentRouteHeaderValue routeHeader, PrincipalContract principal) { | 
  |  56 |    0 |            super(KEWConstants.ACTION_TAKEN_APPROVED_CD, routeHeader, principal);  | 
  |  57 |    0 |        }  | 
  |  58 |     | 
     | 
  |  59 |     | 
         | 
  |  60 |     | 
     | 
  |  61 |     | 
     | 
  |  62 |     | 
     | 
  |  63 |     | 
     | 
  |  64 |     | 
     | 
  |  65 |     | 
     | 
  |  66 |     | 
     | 
  |  67 |     | 
       public ApproveAction(DocumentRouteHeaderValue routeHeader, PrincipalContract principal, String annotation) { | 
  |  68 |    0 |            super(KEWConstants.ACTION_TAKEN_APPROVED_CD, routeHeader, principal, annotation);  | 
  |  69 |    0 |        }  | 
  |  70 |     | 
     | 
  |  71 |     | 
         | 
  |  72 |     | 
     | 
  |  73 |     | 
     | 
  |  74 |     | 
       @Override  | 
  |  75 |     | 
       public String validateActionRules() { | 
  |  76 |    0 |            return validateActionRules(getActionRequestService().findAllPendingRequests(routeHeader.getDocumentId()));  | 
  |  77 |     | 
       }  | 
  |  78 |     | 
     | 
  |  79 |     | 
       public String validateActionRules(List<ActionRequestValue> actionRequests) { | 
  |  80 |    0 |            if (!getRouteHeader().isValidActionToTake(getActionPerformedCode())) { | 
  |  81 |    0 |                return "Document is not in a state to be approved";  | 
  |  82 |     | 
           }  | 
  |  83 |    0 |            List<ActionRequestValue> filteredActionRequests = filterActionRequestsByCode(actionRequests, KEWConstants.ACTION_REQUEST_APPROVE_REQ);  | 
  |  84 |    0 |            if (!isActionCompatibleRequest(filteredActionRequests)) { | 
  |  85 |    0 |                return "No request for the user is compatible " + "with the APPROVE action";  | 
  |  86 |     | 
           }  | 
  |  87 |    0 |            return "";  | 
  |  88 |     | 
       }  | 
  |  89 |     | 
     | 
  |  90 |     | 
         | 
  |  91 |     | 
     | 
  |  92 |     | 
     | 
  |  93 |     | 
       @Override  | 
  |  94 |     | 
       public boolean isActionCompatibleRequest(List<ActionRequestValue> requests) { | 
  |  95 |     | 
             | 
  |  96 |    0 |            if (requests.isEmpty()) { | 
  |  97 |    0 |                return true;  | 
  |  98 |     | 
           }  | 
  |  99 |     | 
     | 
  |  100 |     | 
             | 
  |  101 |    0 |            if (routeHeader.isStateInitiated() || routeHeader.isStateSaved()) { | 
  |  102 |    0 |                return true;  | 
  |  103 |     | 
           }  | 
  |  104 |     | 
     | 
  |  105 |    0 |            boolean actionCompatible = false;  | 
  |  106 |    0 |            Iterator ars = requests.iterator();  | 
  |  107 |    0 |            ActionRequestValue actionRequest = null;  | 
  |  108 |     | 
     | 
  |  109 |    0 |            while (ars.hasNext()) { | 
  |  110 |    0 |                actionRequest = (ActionRequestValue) ars.next();  | 
  |  111 |    0 |                String request = actionRequest.getActionRequested();  | 
  |  112 |     | 
     | 
  |  113 |     | 
                 | 
  |  114 |    0 |                if ( (KEWConstants.ACTION_REQUEST_FYI_REQ.equals(request)) ||  | 
  |  115 |     | 
                       (KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ.equals(request)) ||  | 
  |  116 |     | 
                       (KEWConstants.ACTION_REQUEST_APPROVE_REQ.equals(request)) ||  | 
  |  117 |     | 
                       (KEWConstants.ACTION_REQUEST_COMPLETE_REQ.equals(request)) ) { | 
  |  118 |    0 |                    actionCompatible = true;  | 
  |  119 |    0 |                    break;  | 
  |  120 |     | 
               }  | 
  |  121 |    0 |            }  | 
  |  122 |    0 |            return actionCompatible;  | 
  |  123 |     | 
       }  | 
  |  124 |     | 
     | 
  |  125 |     | 
         | 
  |  126 |     | 
     | 
  |  127 |     | 
     | 
  |  128 |     | 
     | 
  |  129 |     | 
     | 
  |  130 |     | 
     | 
  |  131 |     | 
     | 
  |  132 |     | 
     | 
  |  133 |     | 
     | 
  |  134 |     | 
     | 
  |  135 |     | 
       public void recordAction() throws InvalidActionTakenException { | 
  |  136 |    0 |            MDC.put("docId", getRouteHeader().getDocumentId()); | 
  |  137 |    0 |            updateSearchableAttributesIfPossible();  | 
  |  138 |    0 |            LOG.debug("Approving document : " + annotation); | 
  |  139 |     | 
     | 
  |  140 |    0 |            List actionRequests = getActionRequestService().findAllValidRequests(getPrincipal().getPrincipalId(), getDocumentId(), KEWConstants.ACTION_REQUEST_APPROVE_REQ);  | 
  |  141 |    0 |            if (actionRequests == null || actionRequests.isEmpty()) { | 
  |  142 |    0 |                DocumentTypePolicy allowUnrequested = getRouteHeader().getDocumentType().getAllowUnrequestedActionPolicy();  | 
  |  143 |    0 |                if (allowUnrequested != null) { | 
  |  144 |    0 |                        if (!allowUnrequested.getPolicyValue()) { | 
  |  145 |    0 |                                throw new InvalidActionTakenException("No request for the user is compatible " + "with the APPROVE action. " + "Doctype policy ALLOW_UNREQUESTED_ACTION is set to false and someone else likely just took action on the document."); | 
  |  146 |     | 
                       }  | 
  |  147 |     | 
               }  | 
  |  148 |     | 
           }  | 
  |  149 |    0 |            String errorMessage = validateActionRules(actionRequests);  | 
  |  150 |    0 |            if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) { | 
  |  151 |    0 |                throw new InvalidActionTakenException(errorMessage);  | 
  |  152 |     | 
           }  | 
  |  153 |     | 
     | 
  |  154 |    0 |            Recipient delegator = findDelegatorForActionRequests(actionRequests);  | 
  |  155 |     | 
     | 
  |  156 |    0 |            LOG.debug("Record the approve action"); | 
  |  157 |    0 |            ActionTakenValue actionTaken = saveActionTaken(delegator);  | 
  |  158 |     | 
     | 
  |  159 |    0 |            LOG.debug("Deactivate all pending action requests"); | 
  |  160 |    0 |            getActionRequestService().deactivateRequests(actionTaken, actionRequests);  | 
  |  161 |    0 |            notifyActionTaken(actionTaken);  | 
  |  162 |     | 
     | 
  |  163 |    0 |            boolean isException = getRouteHeader().isInException();  | 
  |  164 |    0 |            boolean isSaved = getRouteHeader().isStateSaved();  | 
  |  165 |    0 |            if (isException || isSaved) { | 
  |  166 |    0 |                String oldStatus = getRouteHeader().getDocRouteStatus();  | 
  |  167 |    0 |                LOG.debug("Moving document back to Enroute from "+KEWConstants.DOCUMENT_STATUSES.get(oldStatus)); | 
  |  168 |    0 |                getRouteHeader().markDocumentEnroute();  | 
  |  169 |    0 |                String newStatus = getRouteHeader().getDocRouteStatus();  | 
  |  170 |    0 |                notifyStatusChange(newStatus, oldStatus);  | 
  |  171 |    0 |                KEWServiceLocator.getRouteHeaderService().saveRouteHeader(getRouteHeader());  | 
  |  172 |     | 
           }  | 
  |  173 |    0 |        }  | 
  |  174 |     | 
   }  |