001 /** 002 * Copyright 2005-2013 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 */ 016 package org.kuali.rice.kew.actions; 017 018 import org.apache.log4j.MDC; 019 import org.kuali.rice.kew.actionrequest.ActionRequestValue; 020 import org.kuali.rice.kew.actiontaken.ActionTakenValue; 021 import org.kuali.rice.kew.api.exception.WorkflowException; 022 import org.kuali.rice.kew.engine.node.ProcessDefinitionBo; 023 import org.kuali.rice.kew.api.exception.InvalidActionTakenException; 024 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; 025 import org.kuali.rice.kew.service.KEWServiceLocator; 026 import org.kuali.rice.kew.api.KewApiConstants; 027 import org.kuali.rice.kim.api.identity.principal.PrincipalContract; 028 029 import java.sql.Timestamp; 030 import java.util.List; 031 032 033 /** 034 * Action that puts the document in routing. 035 * 036 * @author Kuali Rice Team (rice.collab@kuali.org) 037 * 038 */ 039 public 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 public void recordAction() throws InvalidActionTakenException { 083 MDC.put("docId", getRouteHeader().getDocumentId()); 084 updateSearchableAttributesIfPossible(); 085 086 LOG.debug("Routing document : " + annotation); 087 088 LOG.debug("Checking to see if the action is legal"); 089 String errorMessage = validateActionRules(); 090 if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) { 091 throw new InvalidActionTakenException(errorMessage); 092 } 093 094 095 // we want to check that the "RouteDocument" command is valid here, not the "Complete" command (which is in our Action's action taken code) 096 LOG.debug("Record the routing action"); 097 ActionTakenValue actionTaken = saveActionTaken(); 098 099 //TODO this will get all pending AR's even if they haven't been in an action list... This seems bad 100 List<ActionRequestValue> actionRequests = getActionRequestService().findPendingByDoc(getDocumentId()); 101 LOG.debug("Deactivate all pending action requests"); 102 // deactivate any requests for the user that routed the document. 103 for (ActionRequestValue actionRequest : actionRequests) 104 { 105 // requests generated to the user who is routing the document should be deactivated 106 if ((getPrincipal().getPrincipalId().equals(actionRequest.getPrincipalId())) && (actionRequest.isActive())) 107 { 108 getActionRequestService().deactivateRequest(actionTaken, actionRequest); 109 } 110 // requests generated by a save action should be deactivated 111 else if (KewApiConstants.SAVED_REQUEST_RESPONSIBILITY_ID.equals(actionRequest.getResponsibilityId())) 112 { 113 getActionRequestService().deactivateRequest(actionTaken, actionRequest); 114 } 115 } 116 117 notifyActionTaken(actionTaken); 118 119 try { 120 String oldStatus = getRouteHeader().getDocRouteStatus(); 121 getRouteHeader().markDocumentEnroute(); 122 123 if (((ProcessDefinitionBo)getRouteHeader().getDocumentType().getProcesses().get(0)).getInitialRouteNode() == null) { 124 getRouteHeader().setApprovedDate(new Timestamp(System.currentTimeMillis())); 125 126 notifyStatusChange(getRouteHeader().getDocRouteStatus(), oldStatus); 127 oldStatus = getRouteHeader().getDocRouteStatus(); 128 129 getRouteHeader().markDocumentProcessed(); 130 131 notifyStatusChange(getRouteHeader().getDocRouteStatus(), oldStatus); 132 oldStatus = getRouteHeader().getDocRouteStatus(); 133 134 getRouteHeader().markDocumentFinalized(); 135 } 136 137 getRouteHeader().setRoutedByUserWorkflowId(getPrincipal().getPrincipalId()); 138 139 String newStatus = getRouteHeader().getDocRouteStatus(); 140 notifyStatusChange(newStatus, oldStatus); 141 KEWServiceLocator.getRouteHeaderService().saveRouteHeader(getRouteHeader()); 142 } catch (WorkflowException ex) { 143 LOG.warn(ex, ex); 144 throw new InvalidActionTakenException(ex.getMessage()); 145 } 146 147 } 148 }