View Javadoc
1   /**
2    * Copyright 2005-2015 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          actionMap.put(ActionType.RECALL.getCode(), RecallAction.class.getName());
77  	}
78  
79  	public void registerAction(String actionCode, String actionClass) {
80  		if (actionClass == null) {
81  			throw new IllegalArgumentException("Action Code '" + actionCode + "' cannot be registered with a null action class.");
82  		}
83  		if (actionMap.containsKey(actionCode)) {
84  			throw new WorkflowRuntimeException("Action Code is already in use.  [" +
85  					actionCode + ", " + actionClass + "].  "+
86  					"Please unregister the existing implementation first.");
87  		}
88  		actionMap.put(actionCode, actionClass);
89  	}
90  
91  	public void unregisterAction(String actionCode) {
92  		actionMap.remove(actionCode);
93  	}
94  
95  	public Map getActionMap() {
96  		return Collections.unmodifiableMap(actionMap);
97  	}
98  
99  	/* (non-Javadoc)
100 	 * @see org.kuali.rice.kew.actions.ActionRegistry#createAction(java.lang.String, java.util.List)
101 	 */
102 	public ActionTakenEvent createAction(String actionCode, List<DataDefinition> parameters) throws ResourceUnavailableException {
103 		String actionClassName = actionMap.get(actionCode);
104 		if (actionClassName == null) {
105 			throw new IllegalArgumentException("No action has been registered for the given action code of '" + actionCode + "'.");
106 		}
107 		ObjectDefinition actionDefinition = new ObjectDefinition(actionClassName);
108 		if (parameters != null && !parameters.isEmpty()) {
109 			actionDefinition.setConstructorParameters(parameters);
110 		}
111 		try {
112 			//ActionTakenEvent actionTaken = (ActionTakenEvent)GlobalResourceLoader.getResourceLoader().getObject(actionDefinition);
113 			// TODO ActionTakenEvent is not an interface so we can't fetch them through the GlobalResourceLoader, for now, just use
114 			// the ObjectDefinitionResolver
115 			ActionTakenEvent actionTaken = (ActionTakenEvent) ObjectDefinitionResolver.createObject(actionDefinition, ClassLoaderUtils.getDefaultClassLoader(), false);
116 			if (actionTaken == null) {
117 				// TODO the exception handling here is a bit wonky
118 				throw new ResourceUnavailableException("Could not locate action taken class '" + actionClassName + "'");
119 			}
120 			return actionTaken;
121 		} catch (Exception e) {
122             LOG.debug("createAction() Exception thrown while working with action class name '" + actionClassName + "'");
123 			if (e instanceof ResourceUnavailableException) {
124 				throw (ResourceUnavailableException)e;
125 			}
126 			throw new ResourceUnavailableException(e);
127 		}
128 	}
129 
130     public org.kuali.rice.kew.api.action.ValidActions getValidActions(PrincipalContract principal, DocumentRouteHeaderValue document) {
131     	try {
132     		org.kuali.rice.kew.api.action.ValidActions.Builder builder = org.kuali.rice.kew.api.action.ValidActions.Builder.create();
133     		List<ActionRequestValue> activeRequests = new ArrayList<ActionRequestValue>();
134             // this looks like ActionRequestServiceImpl.findAllPendingRequests
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     				builder.addValidAction(ActionType.fromCode(actionTakenCode));
149     			}
150     		}
151     		return builder.build();
152     	} catch (ResourceUnavailableException e) {
153     		throw new WorkflowRuntimeException(e);
154     	}
155     }
156 
157     @Override
158     public boolean isValidAction(String actionTypeCode, PrincipalContract principal,
159             DocumentRouteHeaderValue document) {
160         boolean validAction = false;
161         try {
162             List<ActionRequestValue> activeRequests = new ArrayList<ActionRequestValue>();
163             for (ActionRequestValue ar : document.getActionRequests()) {
164                 if ((ar.getCurrentIndicator() != null && ar.getCurrentIndicator()) && StringUtils.equals(ar.getStatus(),
165                         ActionRequestStatus.ACTIVATED.getCode())) {
166                     activeRequests.add(ar);
167                 }
168             }
169 
170             List<DataDefinition> parameters = new ArrayList<DataDefinition>();
171             parameters.add(new DataDefinition(document));
172             parameters.add(new DataDefinition(principal));
173 
174             ActionTakenEvent actionEvent = createAction(actionTypeCode, parameters);
175             if (StringUtils.isEmpty(actionEvent.validateActionRules(activeRequests))) {
176                 validAction = true;
177             }
178         } catch (ResourceUnavailableException e) {
179             throw new WorkflowRuntimeException(e);
180         }
181 
182         return validAction;
183     }
184 }