1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
42
43
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
67 actionMap.put(KEWConstants.ACTION_TAKEN_SU_ACTION_REQUEST_APPROVED_CD, SuperUserActionRequestApproveEvent.class.getName());
68
69
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
98
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
111
112
113 ActionTakenEvent actionTaken = (ActionTakenEvent)ObjectDefinitionResolver.createObject(actionDefinition, ClassLoaderUtils.getDefaultClassLoader(), false);
114 if (actionTaken == null) {
115
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
129
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 }