View Javadoc

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