View Javadoc

1   /**
2    * Copyright 2005-2012 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.util.List;
30  
31  
32  /**
33   * Action that puts the document in routing.
34   *
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   *
37   */
38  public class RouteDocumentAction extends ActionTakenEvent {
39      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RouteDocumentAction.class);
40  
41      public RouteDocumentAction(DocumentRouteHeaderValue rh, PrincipalContract principal) {
42          super(KewApiConstants.ACTION_TAKEN_COMPLETED_CD, rh, principal);
43      }
44  
45      public RouteDocumentAction(DocumentRouteHeaderValue rh, PrincipalContract principal, String annotation) {
46          super(KewApiConstants.ACTION_TAKEN_COMPLETED_CD, rh, principal, annotation);
47      }
48  
49      /* (non-Javadoc)
50       * @see org.kuali.rice.kew.actions.ActionTakenEvent#getActionPerformedCode()
51       */
52      @Override
53      public String getActionPerformedCode() {
54          return KewApiConstants.ACTION_TAKEN_ROUTED_CD;
55      }
56  
57      /* (non-Javadoc)
58       * @see org.kuali.rice.kew.actions.ActionTakenEvent#isActionCompatibleRequest(java.util.List)
59       */
60      @Override
61      public String validateActionRules() {
62      	// check state before checking kim
63          if (!getRouteHeader().isValidActionToTake(getActionPerformedCode())) {
64              return "Document is not in a state to be routed";
65          }
66      	if (! KEWServiceLocator.getDocumentTypePermissionService().canRoute(getPrincipal().getPrincipalId(), getRouteHeader())) {
67      		return "User is not authorized to Route document";
68      	}
69          return "";
70      }
71  
72      @Override
73      public String validateActionRules(List<ActionRequestValue> actionRequests) {
74      	return validateActionRules();
75      }
76      
77      /**
78       * 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
79       * @throws org.kuali.rice.kew.api.exception.InvalidActionTakenException
80       */
81      public void recordAction() throws InvalidActionTakenException {
82          MDC.put("docId", getRouteHeader().getDocumentId());
83          updateSearchableAttributesIfPossible();
84  
85          LOG.debug("Routing document : " + annotation);
86  
87          LOG.debug("Checking to see if the action is legal");
88          String errorMessage = validateActionRules();
89          if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
90              throw new InvalidActionTakenException(errorMessage);
91          }
92  
93  
94          // we want to check that the "RouteDocument" command is valid here, not the "Complete" command (which is in our Action's action taken code)
95          LOG.debug("Record the routing action");
96          ActionTakenValue actionTaken = saveActionTaken();
97  
98          //TODO this will get all pending AR's even if they haven't been in an action list... This seems bad
99          List<ActionRequestValue> actionRequests = getActionRequestService().findPendingByDoc(getDocumentId());
100         LOG.debug("Deactivate all pending action requests");
101         // deactivate any requests for the user that routed the document.
102         for (ActionRequestValue actionRequest : actionRequests)
103         {
104             // requests generated to the user who is routing the document should be deactivated
105             if ((getPrincipal().getPrincipalId().equals(actionRequest.getPrincipalId())) && (actionRequest.isActive()))
106             {
107                 getActionRequestService().deactivateRequest(actionTaken, actionRequest);
108             }
109             // requests generated by a save action should be deactivated
110             else if (KewApiConstants.SAVED_REQUEST_RESPONSIBILITY_ID.equals(actionRequest.getResponsibilityId()))
111             {
112                 getActionRequestService().deactivateRequest(actionTaken, actionRequest);
113             }
114         }
115 
116             notifyActionTaken(actionTaken);
117 
118             try {
119                 String oldStatus = getRouteHeader().getDocRouteStatus();
120                 getRouteHeader().markDocumentEnroute();
121                 
122                 if (((ProcessDefinitionBo)getRouteHeader().getDocumentType().getProcesses().get(0)).getInitialRouteNode() == null) {
123                     getRouteHeader().markDocumentProcessed();
124                     getRouteHeader().markDocumentFinalized();
125                 }
126 
127                 getRouteHeader().setRoutedByUserWorkflowId(getPrincipal().getPrincipalId());
128 
129                 String newStatus = getRouteHeader().getDocRouteStatus();
130                 notifyStatusChange(newStatus, oldStatus);
131                 KEWServiceLocator.getRouteHeaderService().saveRouteHeader(getRouteHeader());
132             } catch (WorkflowException ex) {
133                 LOG.warn(ex, ex);
134 	            throw new InvalidActionTakenException(ex.getMessage());
135             }
136 
137     }
138 }