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 java.sql.Timestamp; 019import java.util.List; 020 021import org.apache.log4j.MDC; 022import org.kuali.rice.kew.actionrequest.ActionRequestValue; 023import org.kuali.rice.kew.actiontaken.ActionTakenValue; 024import org.kuali.rice.kew.api.KewApiConstants; 025import org.kuali.rice.kew.api.exception.InvalidActionTakenException; 026import org.kuali.rice.kew.api.exception.WorkflowException; 027import org.kuali.rice.kew.engine.node.ProcessDefinitionBo; 028import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; 029import org.kuali.rice.kew.service.KEWServiceLocator; 030import org.kuali.rice.kim.api.identity.principal.PrincipalContract; 031 032 033/** 034 * Action that puts the document in routing. 035 * 036 * @author Kuali Rice Team (rice.collab@kuali.org) 037 * 038 */ 039public class RouteDocumentAction extends ActionTakenEvent { 040 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RouteDocumentAction.class); 041 042 public RouteDocumentAction(DocumentRouteHeaderValue rh, PrincipalContract principal) { 043 super(KewApiConstants.ACTION_TAKEN_COMPLETED_CD, rh, principal); 044 } 045 046 public RouteDocumentAction(DocumentRouteHeaderValue rh, PrincipalContract principal, String annotation) { 047 super(KewApiConstants.ACTION_TAKEN_COMPLETED_CD, rh, principal, annotation); 048 } 049 050 /* (non-Javadoc) 051 * @see org.kuali.rice.kew.actions.ActionTakenEvent#getActionPerformedCode() 052 */ 053 @Override 054 public String getActionPerformedCode() { 055 return KewApiConstants.ACTION_TAKEN_ROUTED_CD; 056 } 057 058 /* (non-Javadoc) 059 * @see org.kuali.rice.kew.actions.ActionTakenEvent#isActionCompatibleRequest(java.util.List) 060 */ 061 @Override 062 public String validateActionRules() { 063 // check state before checking kim 064 if (!getRouteHeader().isValidActionToTake(getActionPerformedCode())) { 065 return "Document is not in a state to be routed"; 066 } 067 if (! KEWServiceLocator.getDocumentTypePermissionService().canRoute(getPrincipal().getPrincipalId(), getRouteHeader())) { 068 return "User is not authorized to Route document"; 069 } 070 return ""; 071 } 072 073 @Override 074 public String validateActionRules(List<ActionRequestValue> actionRequests) { 075 return validateActionRules(); 076 } 077 078 /** 079 * Record the routing action. To route a document, it must be in the proper state. Previous requests and actions have no bearing on the outcome of this action, unless the 080 * @throws org.kuali.rice.kew.api.exception.InvalidActionTakenException 081 */ 082 @Override 083 public void recordAction() throws InvalidActionTakenException { 084 MDC.put("docId", getRouteHeader().getDocumentId()); 085 updateSearchableAttributesIfPossible(); 086 087 if ( LOG.isDebugEnabled() ) { 088 LOG.debug("Routing document : " + annotation); 089 } 090 091 LOG.debug("Checking to see if the action is legal"); 092 String errorMessage = validateActionRules(); 093 if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) { 094 throw new InvalidActionTakenException(errorMessage); 095 } 096 097 098 // we want to check that the "RouteDocument" command is valid here, not the "Complete" command (which is in our Action's action taken code) 099 LOG.debug("Record the routing action"); 100 ActionTakenValue actionTaken = saveActionTaken(); 101 102 //TODO this will get all pending AR's even if they haven't been in an action list... This seems bad 103 List<ActionRequestValue> actionRequests = getActionRequestService().findPendingByDoc(getDocumentId()); 104 LOG.debug("Deactivate all pending action requests"); 105 // deactivate any requests for the user that routed the document. 106 for (ActionRequestValue actionRequest : actionRequests) 107 { 108 // requests generated to the user who is routing the document should be deactivated 109 if ((getPrincipal().getPrincipalId().equals(actionRequest.getPrincipalId())) && (actionRequest.isActive())) 110 { 111 getActionRequestService().deactivateRequest(actionTaken, actionRequest); 112 } 113 // requests generated by a save action should be deactivated 114 else if (KewApiConstants.SAVED_REQUEST_RESPONSIBILITY_ID.equals(actionRequest.getResponsibilityId())) 115 { 116 getActionRequestService().deactivateRequest(actionTaken, actionRequest); 117 } 118 } 119 120 notifyActionTaken(actionTaken); 121 122 try { 123 String oldStatus = getRouteHeader().getDocRouteStatus(); 124 getRouteHeader().markDocumentEnroute(); 125 126 if (((ProcessDefinitionBo)getRouteHeader().getDocumentType().getProcesses().get(0)).getInitialRouteNode() == null) { 127 getRouteHeader().setApprovedDate(new Timestamp(System.currentTimeMillis())); 128 129 notifyStatusChange(getRouteHeader().getDocRouteStatus(), oldStatus); 130 oldStatus = getRouteHeader().getDocRouteStatus(); 131 132 getRouteHeader().markDocumentProcessed(); 133 134 notifyStatusChange(getRouteHeader().getDocRouteStatus(), oldStatus); 135 oldStatus = getRouteHeader().getDocRouteStatus(); 136 137 getRouteHeader().markDocumentFinalized(); 138 } 139 140 getRouteHeader().setRoutedByUserWorkflowId(getPrincipal().getPrincipalId()); 141 142 String newStatus = getRouteHeader().getDocRouteStatus(); 143 notifyStatusChange(newStatus, oldStatus); 144 DocumentRouteHeaderValue persistedRouteHeader = KEWServiceLocator. 145 getRouteHeaderService().saveRouteHeader(getRouteHeader()); 146 setRouteHeader(persistedRouteHeader); 147 } catch (WorkflowException ex) { 148 LOG.warn(ex, ex); 149 throw new InvalidActionTakenException(ex.getMessage()); 150 } 151 152 } 153}