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.Collections;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.commons.lang.StringUtils;
27  import org.apache.log4j.Logger;
28  import org.kuali.rice.core.reflect.DataDefinition;
29  import org.kuali.rice.core.reflect.ObjectDefinition;
30  import org.kuali.rice.core.resourceloader.ObjectDefinitionResolver;
31  import org.kuali.rice.core.util.ClassLoaderUtils;
32  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
33  import org.kuali.rice.kew.exception.ResourceUnavailableException;
34  import org.kuali.rice.kew.exception.WorkflowRuntimeException;
35  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
36  import org.kuali.rice.kew.util.KEWConstants;
37  import org.kuali.rice.kim.bo.entity.KimPrincipal;
38  
39  
40  /**
41   * A simple implementation of an ActionRegistry which includes all of the default Workflow Actions.
42   *
43   * @author Kuali Rice Team (rice.collab@kuali.org)
44   */
45  public class ActionRegistryImpl implements ActionRegistry {
46      private static final Logger LOG = Logger.getLogger(ActionRegistryImpl.class);
47  
48  	private static Map<String, String> actionMap = new HashMap<String, String>();
49  	static {
50  		actionMap.put(KEWConstants.ACTION_TAKEN_ACKNOWLEDGED_CD, AcknowledgeAction.class.getName());
51  		actionMap.put(KEWConstants.ACTION_TAKEN_ADHOC_CD, AdHocAction.class.getName());
52  		actionMap.put(KEWConstants.ACTION_TAKEN_ADHOC_REVOKED_CD, RevokeAdHocAction.class.getName());
53  		actionMap.put(KEWConstants.ACTION_TAKEN_APPROVED_CD, ApproveAction.class.getName());
54  		actionMap.put(KEWConstants.ACTION_TAKEN_BLANKET_APPROVE_CD, BlanketApproveAction.class.getName());
55  		actionMap.put(KEWConstants.ACTION_TAKEN_CANCELED_CD, CancelAction.class.getName());
56  		actionMap.put(KEWConstants.ACTION_TAKEN_COMPLETED_CD, CompleteAction.class.getName());
57          actionMap.put(KEWConstants.ACTION_TAKEN_ROUTED_CD, RouteDocumentAction.class.getName());
58  		actionMap.put(KEWConstants.ACTION_TAKEN_DENIED_CD, DisapproveAction.class.getName());
59  		actionMap.put(KEWConstants.ACTION_TAKEN_FYI_CD, ClearFYIAction.class.getName());
60  		actionMap.put(KEWConstants.ACTION_TAKEN_LOG_DOCUMENT_ACTION_CD, LogDocumentActionAction.class.getName());
61  		actionMap.put(KEWConstants.ACTION_TAKEN_MOVE_CD, MoveDocumentAction.class.getName());
62  		actionMap.put(KEWConstants.ACTION_TAKEN_TAKE_WORKGROUP_AUTHORITY_CD, TakeWorkgroupAuthority.class.getName());
63  		actionMap.put(KEWConstants.ACTION_TAKEN_RELEASE_WORKGROUP_AUTHORITY_CD, ReleaseWorkgroupAuthority.class.getName());
64  		actionMap.put(KEWConstants.ACTION_TAKEN_RETURNED_TO_PREVIOUS_CD, ReturnToPreviousNodeAction.class.getName());
65  		actionMap.put(KEWConstants.ACTION_TAKEN_SAVED_CD, SaveActionEvent.class.getName());
66  		//actionMap.put(KEWConstants.ACTION_TAKEN_SU_ACTION_REQUEST_ACKNOWLEDGED_CD, SuperUserActionRequestAcknowledgeEvent.class.getName());
67  		actionMap.put(KEWConstants.ACTION_TAKEN_SU_ACTION_REQUEST_APPROVED_CD, SuperUserActionRequestApproveEvent.class.getName());
68  		//actionMap.put(KEWConstants.ACTION_TAKEN_SU_ACTION_REQUEST_COMPLETED_CD, SuperUserActionRequestCompleteEvent.class.getName());
69  		//actionMap.put(KEWConstants.ACTION_TAKEN_SU_ACTION_REQUEST_FYI_CD, SuperUserActionRequestFYIEvent.class.getName());
70  		actionMap.put(KEWConstants.ACTION_TAKEN_SU_APPROVED_CD, SuperUserApproveEvent.class.getName());
71  		actionMap.put(KEWConstants.ACTION_TAKEN_SU_CANCELED_CD, SuperUserCancelEvent.class.getName());
72  		actionMap.put(KEWConstants.ACTION_TAKEN_SU_DISAPPROVED_CD, SuperUserDisapproveEvent.class.getName());
73  		actionMap.put(KEWConstants.ACTION_TAKEN_SU_RETURNED_TO_PREVIOUS_CD, SuperUserReturnToPreviousNodeAction.class.getName());
74  		actionMap.put(KEWConstants.ACTION_TAKEN_SU_ROUTE_LEVEL_APPROVED_CD, SuperUserNodeApproveEvent.class.getName());
75  	}
76  
77  	public void registerAction(String actionCode, String actionClass) {
78  		if (actionClass == null) {
79  			throw new IllegalArgumentException("Action Code '" + actionCode + "' cannot be registered with a null action class.");
80  		}
81  		if (actionMap.containsKey(actionCode)) {
82  			throw new WorkflowRuntimeException("Action Code is already in use.  [" +
83  					actionCode + ", " + actionClass + "].  "+
84  					"Please unregister the existing implementation first.");
85  		}
86  		actionMap.put(actionCode, actionClass);
87  	}
88  
89  	public void unregisterAction(String actionCode) {
90  		actionMap.remove(actionCode);
91  	}
92  
93  	public Map getActionMap() {
94  		return Collections.unmodifiableMap(actionMap);
95  	}
96  
97  	/* (non-Javadoc)
98  	 * @see org.kuali.rice.kew.actions.ActionRegistry#createAction(java.lang.String, java.util.List)
99  	 */
100 	public ActionTakenEvent createAction(String actionCode, List<DataDefinition> parameters) throws ResourceUnavailableException {
101 		String actionClassName = actionMap.get(actionCode);
102 		if (actionClassName == null) {
103 			throw new IllegalArgumentException("No action has been registered for the given action code of '" + actionCode + "'.");
104 		}
105 		ObjectDefinition actionDefinition = new ObjectDefinition(actionClassName);
106 		if (parameters != null && !parameters.isEmpty()) {
107 			actionDefinition.setConstructorParameters(parameters);
108 		}
109 		try {
110 			//ActionTakenEvent actionTaken = (ActionTakenEvent)GlobalResourceLoader.getResourceLoader().getObject(actionDefinition);
111 			// TODO ActionTakenEvent is not an interface so we can't fetch them through the GlobalResourceLoader, for now, just use
112 			// the ObjectDefinitionResolver
113 			ActionTakenEvent actionTaken = (ActionTakenEvent)ObjectDefinitionResolver.createObject(actionDefinition, ClassLoaderUtils.getDefaultClassLoader(), false);
114 			if (actionTaken == null) {
115 				// TODO the exception handling here is a bit wonky
116 				throw new ResourceUnavailableException("Could not locate action taken class '" + actionClassName + "'");
117 			}
118 			return actionTaken;
119 		} catch (Exception e) {
120             LOG.debug("createAction() Exception thrown while working with action class name '" + actionClassName + "'");
121 			if (e instanceof ResourceUnavailableException) {
122 				throw (ResourceUnavailableException)e;
123 			}
124 			throw new ResourceUnavailableException(e);
125 		}
126 	}
127 
128     /* (non-Javadoc)
129      * @see org.kuali.rice.kew.actions.ActionValidationService#getValidActions(org.kuali.rice.kew.user.WorkflowUser, org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue)
130      */
131     public ValidActions getValidActions(KimPrincipal principal, DocumentRouteHeaderValue document) throws ResourceUnavailableException {
132         ValidActions validActions = new ValidActions();
133         ArrayList<ActionRequestValue> activeRequests = new ArrayList<ActionRequestValue>();
134         for ( ActionRequestValue ar : document.getActionRequests() ) {
135         	if ( (ar.getCurrentIndicator() != null && ar.getCurrentIndicator()) && StringUtils.equals( ar.getStatus(), KEWConstants.ACTION_REQUEST_ACTIVATED ) ) {
136         		activeRequests.add(ar);
137         	}
138         }
139         for (String actionTakenCode : actionMap.keySet())
140         {
141             List<DataDefinition> parameters = new ArrayList<DataDefinition>();
142             parameters.add(new DataDefinition(document));
143             parameters.add(new DataDefinition(principal));
144             ActionTakenEvent actionEvent = createAction(actionTakenCode, parameters);
145             if (StringUtils.isEmpty(actionEvent.validateActionRules(activeRequests)))
146             {
147                 validActions.addActionTakenCode(actionTakenCode);
148             }
149         }
150         return validActions;
151     }
152 }