View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    *
4    *
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.opensource.org/licenses/ecl2.php
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.actions;
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
24  import org.kuali.rice.kew.actiontaken.ActionTakenValue;
25  import org.kuali.rice.kew.doctype.bo.DocumentType;
26  import org.kuali.rice.kew.exception.InvalidActionTakenException;
27  import org.kuali.rice.kew.exception.WorkflowException;
28  import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
29  import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
30  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
31  import org.kuali.rice.kew.service.KEWServiceLocator;
32  import org.kuali.rice.kew.util.Utilities;
33  import org.kuali.rice.kim.bo.entity.KimPrincipal;
34  
35  
36  /**
37   * Super class for all super user action takens.
38   *
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   */
41  public abstract class SuperUserActionTakenEvent extends ActionTakenEvent {
42  
43      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SuperUserActionTakenEvent.class);
44  
45      protected String superUserAction;
46      //protected DocumentRouteStatusChange event;
47      private ActionRequestValue actionRequest;
48      public static String AUTHORIZATION = "general.routing.superuser.notAuthorized";
49  
50      public SuperUserActionTakenEvent(String actionTakenCode, DocumentRouteHeaderValue routeHeader, KimPrincipal principal) {
51          super(actionTakenCode, routeHeader, principal);
52      }
53  
54      public SuperUserActionTakenEvent(String actionTakenCode, DocumentRouteHeaderValue routeHeader, KimPrincipal principal, String annotation, boolean runPostProcessor) {
55          super(actionTakenCode, routeHeader, principal, annotation, runPostProcessor);
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 (!Utilities.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          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     }
103 
104     protected abstract void markDocument() throws WorkflowException;
105 
106     protected void processActionRequests() throws InvalidActionTakenException {
107         LOG.debug("Processing pending action requests");
108 
109         ActionTakenValue actionTaken = saveActionTaken();
110 
111         List<ActionRequestValue> actionRequests = getActionRequestService().findPendingByDoc(getRouteHeaderId());
112 
113         for (ActionRequestValue actionRequest : actionRequests)
114         {
115             getActionRequestService().deactivateRequest(actionTaken, actionRequest);
116         }
117 
118         notifyActionTaken(actionTaken);
119     }
120 
121     public ActionRequestValue getActionRequest() {
122         return actionRequest;
123     }
124 
125     public void setActionRequest(ActionRequestValue actionRequest) {
126         this.actionRequest = actionRequest;
127     }
128 
129 }