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.kuali.rice.kew.actionrequest.ActionRequestValue;
19  import org.kuali.rice.kew.actiontaken.ActionTakenValue;
20  import org.kuali.rice.kew.api.exception.InvalidActionTakenException;
21  import org.kuali.rice.kew.doctype.bo.DocumentType;
22  import org.kuali.rice.kew.api.exception.WorkflowException;
23  import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
24  import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
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  
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  
34  /**
35   * Super class for all super user action takens.
36   *
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   */
39  abstract class SuperUserActionTakenEvent extends ActionTakenEvent {
40  
41      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SuperUserActionTakenEvent.class);
42  
43      protected final String superUserAction;
44      //protected DocumentRouteStatusChange event;
45      private ActionRequestValue actionRequest;
46      public static String AUTHORIZATION = "general.routing.superuser.notAuthorized";
47  
48      protected SuperUserActionTakenEvent(String actionTakenCode, String superUserAction, DocumentRouteHeaderValue routeHeader, PrincipalContract principal) {
49          super(actionTakenCode, routeHeader, principal);
50          this.superUserAction = superUserAction;
51      }
52  
53      protected SuperUserActionTakenEvent(String actionTakenCode, String superUserAction, DocumentRouteHeaderValue routeHeader, PrincipalContract principal, String annotation, boolean runPostProcessor) {
54          super(actionTakenCode, routeHeader, principal, annotation, runPostProcessor);
55          this.superUserAction = superUserAction;
56      }
57  
58      /* (non-Javadoc)
59       * @see org.kuali.rice.kew.actions.ActionTakenEvent#validateActionRules()
60       */
61      @Override
62      public String validateActionRules() {
63          DocumentType docType = getRouteHeader().getDocumentType();
64          if (!KEWServiceLocator.getDocumentTypePermissionService().canAdministerRouting(getPrincipal().getPrincipalId(), docType)) {
65              return "User not authorized for super user action";
66          }
67          return "";
68      }
69  
70      @Override
71      public String validateActionRules(List<ActionRequestValue> actionRequests) {
72      	return validateActionRules();
73      }
74  
75      public void recordAction() throws InvalidActionTakenException {
76  
77          String errorMessage = validateActionRules();
78          if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
79              LOG.info("User not authorized");
80              List<WorkflowServiceErrorImpl> errors = new ArrayList<WorkflowServiceErrorImpl>();
81              errors.add(new WorkflowServiceErrorImpl(errorMessage, AUTHORIZATION));
82              throw new WorkflowServiceErrorException(errorMessage, errors);
83          }
84  
85          ActionTakenValue actionTaken = processActionRequests();
86  
87          try {
88          	String oldStatus = getRouteHeader().getDocRouteStatus();
89          	//if the document is initiated then set it enroute so we can transition to any other status
90          	if (getRouteHeader().isStateInitiated()) {
91          		getRouteHeader().markDocumentEnroute();
92          		notifyStatusChange(getRouteHeader().getDocRouteStatus(), oldStatus);
93          	}
94              markDocument();
95              String newStatus = getRouteHeader().getDocRouteStatus();
96              notifyStatusChange(newStatus, oldStatus);
97          } catch (Exception ex) {
98              LOG.error("Caught Exception talking to post processor", ex);
99              throw new RuntimeException(ex.getMessage());
100         }
101         
102         processActionTaken(actionTaken);
103     }
104 
105     protected abstract void markDocument() throws WorkflowException;
106 
107     protected ActionTakenValue processActionRequests() throws InvalidActionTakenException {
108         LOG.debug("Processing pending action requests");
109 
110         ActionTakenValue actionTaken = saveActionTaken();
111 
112         List<ActionRequestValue> actionRequests = getActionRequestService().findPendingByDoc(getDocumentId());
113 
114         for (ActionRequestValue actionRequest : actionRequests)
115         {
116             getActionRequestService().deactivateRequest(actionTaken, actionRequest);
117         }
118 
119         notifyActionTaken(actionTaken);
120 
121         return actionTaken;
122     }
123 
124     /**
125      * Allows subclasses to perform any post-processing after the action has been taken
126      */
127     protected void processActionTaken(ActionTakenValue actionTaken) {
128         // no default impl
129     }
130 
131     public ActionRequestValue getActionRequest() {
132         return actionRequest;
133     }
134 
135     public void setActionRequest(ActionRequestValue actionRequest) {
136         this.actionRequest = actionRequest;
137     }
138 
139 }