001 /*
002 * Copyright 2005-2007 The Kuali Foundation
003 *
004 *
005 * Licensed under the Educational Community License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.opensource.org/licenses/ecl2.php
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.kuali.rice.kew.actions;
018
019 import java.util.ArrayList;
020 import java.util.Collections;
021 import java.util.HashMap;
022 import java.util.Iterator;
023 import java.util.List;
024 import java.util.Map;
025
026 import org.apache.commons.lang.StringUtils;
027 import org.apache.log4j.Logger;
028 import org.kuali.rice.core.reflect.DataDefinition;
029 import org.kuali.rice.core.reflect.ObjectDefinition;
030 import org.kuali.rice.core.resourceloader.ObjectDefinitionResolver;
031 import org.kuali.rice.core.util.ClassLoaderUtils;
032 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
033 import org.kuali.rice.kew.exception.ResourceUnavailableException;
034 import org.kuali.rice.kew.exception.WorkflowRuntimeException;
035 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
036 import org.kuali.rice.kew.util.KEWConstants;
037 import org.kuali.rice.kim.bo.entity.KimPrincipal;
038
039
040 /**
041 * A simple implementation of an ActionRegistry which includes all of the default Workflow Actions.
042 *
043 * @author Kuali Rice Team (rice.collab@kuali.org)
044 */
045 public class ActionRegistryImpl implements ActionRegistry {
046 private static final Logger LOG = Logger.getLogger(ActionRegistryImpl.class);
047
048 private static Map<String, String> actionMap = new HashMap<String, String>();
049 static {
050 actionMap.put(KEWConstants.ACTION_TAKEN_ACKNOWLEDGED_CD, AcknowledgeAction.class.getName());
051 actionMap.put(KEWConstants.ACTION_TAKEN_ADHOC_CD, AdHocAction.class.getName());
052 actionMap.put(KEWConstants.ACTION_TAKEN_ADHOC_REVOKED_CD, RevokeAdHocAction.class.getName());
053 actionMap.put(KEWConstants.ACTION_TAKEN_APPROVED_CD, ApproveAction.class.getName());
054 actionMap.put(KEWConstants.ACTION_TAKEN_BLANKET_APPROVE_CD, BlanketApproveAction.class.getName());
055 actionMap.put(KEWConstants.ACTION_TAKEN_CANCELED_CD, CancelAction.class.getName());
056 actionMap.put(KEWConstants.ACTION_TAKEN_COMPLETED_CD, CompleteAction.class.getName());
057 actionMap.put(KEWConstants.ACTION_TAKEN_ROUTED_CD, RouteDocumentAction.class.getName());
058 actionMap.put(KEWConstants.ACTION_TAKEN_DENIED_CD, DisapproveAction.class.getName());
059 actionMap.put(KEWConstants.ACTION_TAKEN_FYI_CD, ClearFYIAction.class.getName());
060 actionMap.put(KEWConstants.ACTION_TAKEN_LOG_DOCUMENT_ACTION_CD, LogDocumentActionAction.class.getName());
061 actionMap.put(KEWConstants.ACTION_TAKEN_MOVE_CD, MoveDocumentAction.class.getName());
062 actionMap.put(KEWConstants.ACTION_TAKEN_TAKE_WORKGROUP_AUTHORITY_CD, TakeWorkgroupAuthority.class.getName());
063 actionMap.put(KEWConstants.ACTION_TAKEN_RELEASE_WORKGROUP_AUTHORITY_CD, ReleaseWorkgroupAuthority.class.getName());
064 actionMap.put(KEWConstants.ACTION_TAKEN_RETURNED_TO_PREVIOUS_CD, ReturnToPreviousNodeAction.class.getName());
065 actionMap.put(KEWConstants.ACTION_TAKEN_SAVED_CD, SaveActionEvent.class.getName());
066 //actionMap.put(KEWConstants.ACTION_TAKEN_SU_ACTION_REQUEST_ACKNOWLEDGED_CD, SuperUserActionRequestAcknowledgeEvent.class.getName());
067 actionMap.put(KEWConstants.ACTION_TAKEN_SU_ACTION_REQUEST_APPROVED_CD, SuperUserActionRequestApproveEvent.class.getName());
068 //actionMap.put(KEWConstants.ACTION_TAKEN_SU_ACTION_REQUEST_COMPLETED_CD, SuperUserActionRequestCompleteEvent.class.getName());
069 //actionMap.put(KEWConstants.ACTION_TAKEN_SU_ACTION_REQUEST_FYI_CD, SuperUserActionRequestFYIEvent.class.getName());
070 actionMap.put(KEWConstants.ACTION_TAKEN_SU_APPROVED_CD, SuperUserApproveEvent.class.getName());
071 actionMap.put(KEWConstants.ACTION_TAKEN_SU_CANCELED_CD, SuperUserCancelEvent.class.getName());
072 actionMap.put(KEWConstants.ACTION_TAKEN_SU_DISAPPROVED_CD, SuperUserDisapproveEvent.class.getName());
073 actionMap.put(KEWConstants.ACTION_TAKEN_SU_RETURNED_TO_PREVIOUS_CD, SuperUserReturnToPreviousNodeAction.class.getName());
074 actionMap.put(KEWConstants.ACTION_TAKEN_SU_ROUTE_LEVEL_APPROVED_CD, SuperUserNodeApproveEvent.class.getName());
075 }
076
077 public void registerAction(String actionCode, String actionClass) {
078 if (actionClass == null) {
079 throw new IllegalArgumentException("Action Code '" + actionCode + "' cannot be registered with a null action class.");
080 }
081 if (actionMap.containsKey(actionCode)) {
082 throw new WorkflowRuntimeException("Action Code is already in use. [" +
083 actionCode + ", " + actionClass + "]. "+
084 "Please unregister the existing implementation first.");
085 }
086 actionMap.put(actionCode, actionClass);
087 }
088
089 public void unregisterAction(String actionCode) {
090 actionMap.remove(actionCode);
091 }
092
093 public Map getActionMap() {
094 return Collections.unmodifiableMap(actionMap);
095 }
096
097 /* (non-Javadoc)
098 * @see org.kuali.rice.kew.actions.ActionRegistry#createAction(java.lang.String, java.util.List)
099 */
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 }