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