1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
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 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 0 | public class ActionRegistryImpl implements ActionRegistry { |
47 | 0 | private static final Logger LOG = Logger.getLogger(ActionRegistryImpl.class); |
48 | |
|
49 | 0 | private static Map<String, String> actionMap = new HashMap<String, String>(); |
50 | |
static { |
51 | 0 | actionMap.put(KewApiConstants.ACTION_TAKEN_ACKNOWLEDGED_CD, AcknowledgeAction.class.getName()); |
52 | 0 | actionMap.put(KewApiConstants.ACTION_TAKEN_ADHOC_CD, AdHocAction.class.getName()); |
53 | 0 | actionMap.put(KewApiConstants.ACTION_TAKEN_ADHOC_REVOKED_CD, RevokeAdHocAction.class.getName()); |
54 | 0 | actionMap.put(KewApiConstants.ACTION_TAKEN_APPROVED_CD, ApproveAction.class.getName()); |
55 | 0 | actionMap.put(KewApiConstants.ACTION_TAKEN_BLANKET_APPROVE_CD, BlanketApproveAction.class.getName()); |
56 | 0 | actionMap.put(KewApiConstants.ACTION_TAKEN_CANCELED_CD, CancelAction.class.getName()); |
57 | 0 | actionMap.put(KewApiConstants.ACTION_TAKEN_COMPLETED_CD, CompleteAction.class.getName()); |
58 | 0 | actionMap.put(KewApiConstants.ACTION_TAKEN_ROUTED_CD, RouteDocumentAction.class.getName()); |
59 | 0 | actionMap.put(KewApiConstants.ACTION_TAKEN_DENIED_CD, DisapproveAction.class.getName()); |
60 | 0 | actionMap.put(KewApiConstants.ACTION_TAKEN_FYI_CD, ClearFYIAction.class.getName()); |
61 | 0 | actionMap.put(KewApiConstants.ACTION_TAKEN_LOG_DOCUMENT_ACTION_CD, LogDocumentActionAction.class.getName()); |
62 | 0 | actionMap.put(KewApiConstants.ACTION_TAKEN_MOVE_CD, MoveDocumentAction.class.getName()); |
63 | 0 | actionMap.put(KewApiConstants.ACTION_TAKEN_TAKE_WORKGROUP_AUTHORITY_CD, TakeWorkgroupAuthority.class.getName()); |
64 | 0 | actionMap.put(KewApiConstants.ACTION_TAKEN_RELEASE_WORKGROUP_AUTHORITY_CD, ReleaseWorkgroupAuthority.class.getName()); |
65 | 0 | actionMap.put(KewApiConstants.ACTION_TAKEN_RETURNED_TO_PREVIOUS_CD, ReturnToPreviousNodeAction.class.getName()); |
66 | 0 | actionMap.put(KewApiConstants.ACTION_TAKEN_SAVED_CD, SaveActionEvent.class.getName()); |
67 | |
|
68 | 0 | actionMap.put(KewApiConstants.ACTION_TAKEN_SU_ACTION_REQUEST_APPROVED_CD, SuperUserActionRequestApproveEvent.class.getName()); |
69 | |
|
70 | |
|
71 | 0 | actionMap.put(KewApiConstants.ACTION_TAKEN_SU_APPROVED_CD, SuperUserApproveEvent.class.getName()); |
72 | 0 | actionMap.put(KewApiConstants.ACTION_TAKEN_SU_CANCELED_CD, SuperUserCancelEvent.class.getName()); |
73 | 0 | actionMap.put(KewApiConstants.ACTION_TAKEN_SU_DISAPPROVED_CD, SuperUserDisapproveEvent.class.getName()); |
74 | 0 | actionMap.put(KewApiConstants.ACTION_TAKEN_SU_RETURNED_TO_PREVIOUS_CD, SuperUserReturnToPreviousNodeAction.class.getName()); |
75 | 0 | actionMap.put(KewApiConstants.ACTION_TAKEN_SU_ROUTE_LEVEL_APPROVED_CD, SuperUserNodeApproveEvent.class.getName()); |
76 | 0 | } |
77 | |
|
78 | |
public void registerAction(String actionCode, String actionClass) { |
79 | 0 | if (actionClass == null) { |
80 | 0 | throw new IllegalArgumentException("Action Code '" + actionCode + "' cannot be registered with a null action class."); |
81 | |
} |
82 | 0 | if (actionMap.containsKey(actionCode)) { |
83 | 0 | throw new WorkflowRuntimeException("Action Code is already in use. [" + |
84 | |
actionCode + ", " + actionClass + "]. "+ |
85 | |
"Please unregister the existing implementation first."); |
86 | |
} |
87 | 0 | actionMap.put(actionCode, actionClass); |
88 | 0 | } |
89 | |
|
90 | |
public void unregisterAction(String actionCode) { |
91 | 0 | actionMap.remove(actionCode); |
92 | 0 | } |
93 | |
|
94 | |
public Map getActionMap() { |
95 | 0 | return Collections.unmodifiableMap(actionMap); |
96 | |
} |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
public ActionTakenEvent createAction(String actionCode, List<DataDefinition> parameters) throws ResourceUnavailableException { |
102 | 0 | String actionClassName = actionMap.get(actionCode); |
103 | 0 | if (actionClassName == null) { |
104 | 0 | throw new IllegalArgumentException("No action has been registered for the given action code of '" + actionCode + "'."); |
105 | |
} |
106 | 0 | ObjectDefinition actionDefinition = new ObjectDefinition(actionClassName); |
107 | 0 | if (parameters != null && !parameters.isEmpty()) { |
108 | 0 | actionDefinition.setConstructorParameters(parameters); |
109 | |
} |
110 | |
try { |
111 | |
|
112 | |
|
113 | |
|
114 | 0 | ActionTakenEvent actionTaken = (ActionTakenEvent) ObjectDefinitionResolver.createObject(actionDefinition, ClassLoaderUtils.getDefaultClassLoader(), false); |
115 | 0 | if (actionTaken == null) { |
116 | |
|
117 | 0 | throw new ResourceUnavailableException("Could not locate action taken class '" + actionClassName + "'"); |
118 | |
} |
119 | 0 | return actionTaken; |
120 | 0 | } catch (Exception e) { |
121 | 0 | LOG.debug("createAction() Exception thrown while working with action class name '" + actionClassName + "'"); |
122 | 0 | if (e instanceof ResourceUnavailableException) { |
123 | 0 | throw (ResourceUnavailableException)e; |
124 | |
} |
125 | 0 | throw new ResourceUnavailableException(e); |
126 | |
} |
127 | |
} |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
public ValidActions getValidActions(PrincipalContract principal, DocumentRouteHeaderValue document) throws ResourceUnavailableException { |
133 | 0 | ValidActions validActions = new ValidActions(); |
134 | 0 | ArrayList<ActionRequestValue> activeRequests = new ArrayList<ActionRequestValue>(); |
135 | 0 | for ( ActionRequestValue ar : document.getActionRequests() ) { |
136 | 0 | if ( (ar.getCurrentIndicator() != null && ar.getCurrentIndicator()) && StringUtils.equals( ar.getStatus(), ActionRequestStatus.ACTIVATED.getCode() ) ) { |
137 | 0 | activeRequests.add(ar); |
138 | |
} |
139 | |
} |
140 | 0 | for (String actionTakenCode : actionMap.keySet()) |
141 | |
{ |
142 | 0 | List<DataDefinition> parameters = new ArrayList<DataDefinition>(); |
143 | 0 | parameters.add(new DataDefinition(document)); |
144 | 0 | parameters.add(new DataDefinition(principal)); |
145 | 0 | ActionTakenEvent actionEvent = createAction(actionTakenCode, parameters); |
146 | 0 | if (StringUtils.isEmpty(actionEvent.validateActionRules(activeRequests))) |
147 | |
{ |
148 | 0 | validActions.addActionTakenCode(actionTakenCode); |
149 | |
} |
150 | 0 | } |
151 | 0 | return validActions; |
152 | |
} |
153 | |
|
154 | |
public org.kuali.rice.kew.api.action.ValidActions getNewValidActions(PrincipalContract principal, DocumentRouteHeaderValue document) { |
155 | |
try { |
156 | 0 | org.kuali.rice.kew.api.action.ValidActions.Builder builder = org.kuali.rice.kew.api.action.ValidActions.Builder.create(); |
157 | 0 | List<ActionRequestValue> activeRequests = new ArrayList<ActionRequestValue>(); |
158 | 0 | for ( ActionRequestValue ar : document.getActionRequests() ) { |
159 | 0 | if ( (ar.getCurrentIndicator() != null && ar.getCurrentIndicator()) && StringUtils.equals( ar.getStatus(), ActionRequestStatus.ACTIVATED.getCode() ) ) { |
160 | 0 | activeRequests.add(ar); |
161 | |
} |
162 | |
} |
163 | 0 | for (String actionTakenCode : actionMap.keySet()) |
164 | |
{ |
165 | 0 | List<DataDefinition> parameters = new ArrayList<DataDefinition>(); |
166 | 0 | parameters.add(new DataDefinition(document)); |
167 | 0 | parameters.add(new DataDefinition(principal)); |
168 | 0 | ActionTakenEvent actionEvent = createAction(actionTakenCode, parameters); |
169 | 0 | if (StringUtils.isEmpty(actionEvent.validateActionRules(activeRequests))) |
170 | |
{ |
171 | 0 | builder.addValidAction(ActionType.fromCode(actionTakenCode)); |
172 | |
} |
173 | 0 | } |
174 | 0 | return builder.build(); |
175 | 0 | } catch (ResourceUnavailableException e) { |
176 | 0 | throw new WorkflowRuntimeException(e); |
177 | |
} |
178 | |
} |
179 | |
} |