View Javadoc

1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.actions;
17  
18  import org.apache.log4j.MDC;
19  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
20  import org.kuali.rice.kew.actiontaken.ActionTakenValue;
21  import org.kuali.rice.kew.api.exception.WorkflowException;
22  import org.kuali.rice.kew.engine.node.ProcessDefinitionBo;
23  import org.kuali.rice.kew.api.exception.InvalidActionTakenException;
24  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
25  import org.kuali.rice.kew.service.KEWServiceLocator;
26  import org.kuali.rice.kew.api.KewApiConstants;
27  import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
28  
29  import java.sql.Timestamp;
30  import java.util.List;
31  
32  
33  /**
34   * Action that puts the document in routing.
35   *
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   *
38   */
39  public class RouteDocumentAction extends ActionTakenEvent {
40      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RouteDocumentAction.class);
41  
42      public RouteDocumentAction(DocumentRouteHeaderValue rh, PrincipalContract principal) {
43          super(KewApiConstants.ACTION_TAKEN_COMPLETED_CD, rh, principal);
44      }
45  
46      public RouteDocumentAction(DocumentRouteHeaderValue rh, PrincipalContract principal, String annotation) {
47          super(KewApiConstants.ACTION_TAKEN_COMPLETED_CD, rh, principal, annotation);
48      }
49  
50      /* (non-Javadoc)
51       * @see org.kuali.rice.kew.actions.ActionTakenEvent#getActionPerformedCode()
52       */
53      @Override
54      public String getActionPerformedCode() {
55          return KewApiConstants.ACTION_TAKEN_ROUTED_CD;
56      }
57  
58      /* (non-Javadoc)
59       * @see org.kuali.rice.kew.actions.ActionTakenEvent#isActionCompatibleRequest(java.util.List)
60       */
61      @Override
62      public String validateActionRules() {
63      	// check state before checking kim
64          if (!getRouteHeader().isValidActionToTake(getActionPerformedCode())) {
65              return "Document is not in a state to be routed";
66          }
67      	if (! KEWServiceLocator.getDocumentTypePermissionService().canRoute(getPrincipal().getPrincipalId(), getRouteHeader())) {
68      		return "User is not authorized to Route document";
69      	}
70          return "";
71      }
72  
73      @Override
74      public String validateActionRules(List<ActionRequestValue> actionRequests) {
75      	return validateActionRules();
76      }
77      
78      /**
79       * 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
80       * @throws org.kuali.rice.kew.api.exception.InvalidActionTakenException
81       */
82      public void recordAction() throws InvalidActionTakenException {
83          MDC.put("docId", getRouteHeader().getDocumentId());
84          updateSearchableAttributesIfPossible();
85  
86          LOG.debug("Routing document : " + annotation);
87  
88          LOG.debug("Checking to see if the action is legal");
89          String errorMessage = validateActionRules();
90          if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
91              throw new InvalidActionTakenException(errorMessage);
92          }
93  
94  
95          // we want to check that the "RouteDocument" command is valid here, not the "Complete" command (which is in our Action's action taken code)
96          LOG.debug("Record the routing action");
97          ActionTakenValue actionTaken = saveActionTaken();
98  
99          //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 }