001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.kew.actions; 017 018import org.apache.log4j.MDC; 019import org.kuali.rice.kew.actionrequest.ActionRequestValue; 020import org.kuali.rice.kew.actiontaken.ActionTakenValue; 021import org.kuali.rice.kew.api.exception.InvalidActionTakenException; 022import org.kuali.rice.kew.doctype.DocumentTypePolicy; 023import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; 024import org.kuali.rice.kew.service.KEWServiceLocator; 025import org.kuali.rice.kew.api.KewApiConstants; 026import org.kuali.rice.kim.api.identity.principal.PrincipalContract; 027 028 029import java.util.Iterator; 030import java.util.List; 031 032 033/** 034 * CompleteAction records and process a complete action 035 * 036 * The routeheader is first checked to make sure the action is valid for the document. 037 * Next the user is checked to make sure he/she has not taken a previous action on this 038 * document at the actions responsibility or below. The action is recorded. Any requests 039 * related to this user are deactivated. 040 * 041 * @author Kuali Rice Team (rice.collab@kuali.org) 042 */ 043public class CompleteAction extends ActionTakenEvent { 044 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CompleteAction.class); 045 046 /** 047 * @param rh 048 * RouteHeader for the document upon which the action is taken. 049 * @param principal 050 * User taking the action. 051 */ 052 public CompleteAction(DocumentRouteHeaderValue rh, PrincipalContract principal) { 053 super(KewApiConstants.ACTION_TAKEN_COMPLETED_CD, rh, principal); 054 } 055 056 /** 057 * @param rh 058 * RouteHeader for the document upon which the action is taken. 059 * @param principal 060 * User taking the action. 061 * @param annotation 062 * User comment on the action taken 063 */ 064 public CompleteAction(DocumentRouteHeaderValue rh, PrincipalContract principal, String annotation) { 065 super(KewApiConstants.ACTION_TAKEN_COMPLETED_CD, rh, principal, annotation); 066 } 067 068 /* (non-Javadoc) 069 * @see org.kuali.rice.kew.actions.ActionTakenEvent#isActionCompatibleRequest(java.util.List) 070 */ 071 @Override 072 public String validateActionRules() { 073 return validateActionRules(getActionRequestService().findAllPendingRequests(routeHeader.getDocumentId())); 074 } 075 076 public String validateActionRules(List<ActionRequestValue> actionRequests) { 077 if (!getRouteHeader().isValidActionToTake(getActionPerformedCode())) { 078 return "Document is not in a state to be completed"; 079 } 080 List<ActionRequestValue> filteredActionRequests = filterActionRequestsByCode(actionRequests, KewApiConstants.ACTION_REQUEST_COMPLETE_REQ); 081 if (!isActionCompatibleRequest(filteredActionRequests)) { 082 return "No request for the user is compatible " + "with the COMPLETE action"; 083 } 084 return ""; 085 } 086 087 /* (non-Javadoc) 088 * @see org.kuali.rice.kew.actions.ActionTakenEvent#isActionCompatibleRequest(java.util.List) 089 */ 090 @Override 091 public boolean isActionCompatibleRequest(List requests) { 092 // we allow pre-approval 093 if (requests.isEmpty()) { 094 return true; 095 } 096 097 // can always cancel saved or initiated document 098 if (routeHeader.isStateInitiated() || routeHeader.isStateSaved()) { 099 return true; 100 } 101 102 boolean actionCompatible = false; 103 Iterator ars = requests.iterator(); 104 ActionRequestValue actionRequest = null; 105 106 while (ars.hasNext()) { 107 actionRequest = (ActionRequestValue) ars.next(); 108 String request = actionRequest.getActionRequested(); 109 110 // Complete action matches Complete, Approve, FYI, and ACK requests 111 if ( (KewApiConstants.ACTION_REQUEST_FYI_REQ.equals(request)) || 112 (KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ.equals(request)) || 113 (KewApiConstants.ACTION_REQUEST_APPROVE_REQ.equals(request)) || 114 (KewApiConstants.ACTION_REQUEST_COMPLETE_REQ.equals(request)) ) { 115 actionCompatible = true; 116 break; 117 } 118 } 119 return actionCompatible; 120 } 121 122 /** 123 * Records the complete action. - Checks to make sure the document status allows the action. - Checks that the user has not taken a previous action. - Deactivates the pending requests for this user - Records the action 124 * 125 * @throws org.kuali.rice.kew.api.exception.InvalidActionTakenException 126 * @throws org.kuali.rice.kew.api.exception.ResourceUnavailableException 127 */ 128 public void recordAction() throws InvalidActionTakenException { 129 MDC.put("docId", getRouteHeader().getDocumentId()); 130 updateSearchableAttributesIfPossible(); 131 LOG.debug("Completing document : " + annotation); 132 133 List actionRequests = getActionRequestService().findAllValidRequests(getPrincipal().getPrincipalId(), getDocumentId(), KewApiConstants.ACTION_REQUEST_COMPLETE_REQ); 134 if (actionRequests == null || actionRequests.isEmpty()) { 135 DocumentTypePolicy allowUnrequested = getRouteHeader().getDocumentType().getAllowUnrequestedActionPolicy(); 136 if (allowUnrequested != null) { 137 if (!allowUnrequested.getPolicyValue()) { 138 throw new InvalidActionTakenException("No request for the user is compatible " + "with the COMPLETE action. " + "Doctype policy ALLOW_UNREQUESTED_ACTION is set to false and someone else likely just took action on the document."); 139 } 140 } 141 } 142 LOG.debug("Checking to see if the action is legal"); 143 String errorMessage = validateActionRules(actionRequests); 144 if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) { 145 throw new InvalidActionTakenException(errorMessage); 146 } 147 148 LOG.debug("Record the complete action"); 149 ActionTakenValue actionTaken = saveActionTaken(findDelegatorForActionRequests(actionRequests)); 150 151 LOG.debug("Deactivate all pending action requests"); 152 getActionRequestService().deactivateRequests(actionTaken, actionRequests); 153 notifyActionTaken(actionTaken); 154 155 boolean isException = getRouteHeader().isInException(); 156 boolean isSaved = getRouteHeader().isStateSaved(); 157 if (isException || isSaved) { 158 String oldStatus = getRouteHeader().getDocRouteStatus(); 159 LOG.debug("Moving document back to Enroute from "+KewApiConstants.DOCUMENT_STATUSES.get(oldStatus)); 160 getRouteHeader().markDocumentEnroute(); 161 String newStatus = getRouteHeader().getDocRouteStatus(); 162 notifyStatusChange(newStatus, oldStatus); 163 KEWServiceLocator.getRouteHeaderService().saveRouteHeader(getRouteHeader()); 164 } 165 } 166 167}