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 org.apache.log4j.Logger; |
20 | |
import org.kuali.rice.kew.actionrequest.ActionRequestValue; |
21 | |
import org.kuali.rice.kew.actionrequest.KimGroupRecipient; |
22 | |
import org.kuali.rice.kew.actionrequest.KimPrincipalRecipient; |
23 | |
import org.kuali.rice.kew.actionrequest.Recipient; |
24 | |
import org.kuali.rice.kew.actionrequest.service.ActionRequestService; |
25 | |
import org.kuali.rice.kew.actiontaken.ActionTakenValue; |
26 | |
import org.kuali.rice.kew.api.KewApiServiceLocator; |
27 | |
import org.kuali.rice.kew.api.WorkflowRuntimeException; |
28 | |
import org.kuali.rice.kew.api.document.attribute.DocumentAttributeIndexingQueue; |
29 | |
import org.kuali.rice.kew.engine.RouteContext; |
30 | |
import org.kuali.rice.kew.exception.InvalidActionTakenException; |
31 | |
import org.kuali.rice.kew.messaging.MessageServiceNames; |
32 | |
import org.kuali.rice.kew.messaging.RouteDocumentMessageService; |
33 | |
import org.kuali.rice.kew.postprocessor.DocumentRouteStatusChange; |
34 | |
import org.kuali.rice.kew.postprocessor.PostProcessor; |
35 | |
import org.kuali.rice.kew.postprocessor.ProcessDocReport; |
36 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
37 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
38 | |
import org.kuali.rice.kim.api.identity.principal.PrincipalContract; |
39 | |
import org.kuali.rice.kim.api.services.KimApiServiceLocator; |
40 | |
import org.kuali.rice.ksb.messaging.service.KSBXMLService; |
41 | |
|
42 | |
import javax.xml.namespace.QName; |
43 | |
import java.util.List; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public abstract class ActionTakenEvent { |
55 | |
|
56 | 0 | private static final Logger LOG = Logger.getLogger(ActionTakenEvent.class); |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
private String actionTakenCode; |
62 | |
|
63 | |
protected final String annotation; |
64 | |
|
65 | |
protected DocumentRouteHeaderValue routeHeader; |
66 | |
|
67 | |
private final PrincipalContract principal; |
68 | |
|
69 | |
private final boolean runPostProcessorLogic; |
70 | |
|
71 | |
private List<String> groupIdsForPrincipal; |
72 | |
|
73 | |
|
74 | 0 | private boolean queueDocumentAfterAction = true; |
75 | |
|
76 | |
|
77 | |
public ActionTakenEvent(String actionTakenCode, DocumentRouteHeaderValue routeHeader, PrincipalContract principal) { |
78 | 0 | this(actionTakenCode, routeHeader, principal, null, true); |
79 | 0 | } |
80 | |
|
81 | |
public ActionTakenEvent(String actionTakenCode, DocumentRouteHeaderValue routeHeader, PrincipalContract principal, String annotation) { |
82 | 0 | this(actionTakenCode, routeHeader, principal, annotation, true); |
83 | 0 | } |
84 | |
|
85 | 0 | public ActionTakenEvent(String actionTakenCode, DocumentRouteHeaderValue routeHeader, PrincipalContract principal, String annotation, boolean runPostProcessorLogic) { |
86 | 0 | this.actionTakenCode = actionTakenCode; |
87 | 0 | this.routeHeader = routeHeader; |
88 | 0 | this.principal = principal; |
89 | 0 | this.annotation = annotation == null ? "" : annotation; |
90 | 0 | this.runPostProcessorLogic = runPostProcessorLogic; |
91 | 0 | this.queueDocumentAfterAction = true; |
92 | 0 | } |
93 | |
|
94 | |
public ActionRequestService getActionRequestService() { |
95 | 0 | return (ActionRequestService) KEWServiceLocator.getService(KEWServiceLocator.ACTION_REQUEST_SRV); |
96 | |
} |
97 | |
|
98 | |
public DocumentRouteHeaderValue getRouteHeader() { |
99 | 0 | return routeHeader; |
100 | |
} |
101 | |
|
102 | |
public void setRouteHeader(DocumentRouteHeaderValue routeHeader) { |
103 | 0 | this.routeHeader = routeHeader; |
104 | 0 | } |
105 | |
|
106 | |
public PrincipalContract getPrincipal() { |
107 | 0 | return principal; |
108 | |
} |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
protected String getActionPerformedCode() { |
118 | 0 | return getActionTakenCode(); |
119 | |
} |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
protected boolean isActionValid() { |
126 | 0 | return org.apache.commons.lang.StringUtils.isEmpty(validateActionRules()); |
127 | |
} |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
public abstract String validateActionRules(); |
135 | |
public abstract String validateActionRules(List<ActionRequestValue> actionRequests); |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
public List<ActionRequestValue> filterActionRequestsByCode(List<ActionRequestValue> actionRequests, String requestCode) { |
145 | 0 | return getActionRequestService().filterActionRequestsByCode(actionRequests, getPrincipal().getPrincipalId(), getGroupIdsForPrincipal(), requestCode); |
146 | |
} |
147 | |
|
148 | |
protected boolean isActionCompatibleRequest(List<ActionRequestValue> requests) { |
149 | 0 | LOG.debug("isActionCompatibleRequest() Default method = returning true"); |
150 | 0 | return true; |
151 | |
} |
152 | |
|
153 | |
public void performAction() throws InvalidActionTakenException { |
154 | 0 | recordAction(); |
155 | 0 | if (queueDocumentAfterAction) { |
156 | 0 | queueDocumentProcessing(); |
157 | |
} |
158 | |
|
159 | 0 | } |
160 | |
|
161 | |
protected abstract void recordAction() throws InvalidActionTakenException; |
162 | |
|
163 | |
public void performDeferredAction() { |
164 | |
|
165 | 0 | } |
166 | |
|
167 | |
protected void updateSearchableAttributesIfPossible() { |
168 | |
|
169 | |
|
170 | 0 | RouteContext routeContext = RouteContext.getCurrentRouteContext(); |
171 | 0 | if (routeHeader.getDocumentType().hasSearchableAttributesOld() && !routeContext.isSearchIndexingRequestedForContext()) { |
172 | 0 | routeContext.requestSearchIndexingForContext(); |
173 | 0 | DocumentAttributeIndexingQueue queue = KewApiServiceLocator.getDocumentAttributeIndexingQueue(routeHeader.getDocumentType().getApplicationId()); |
174 | 0 | queue.indexDocument(getDocumentId()); |
175 | |
} |
176 | 0 | } |
177 | |
|
178 | |
protected void notifyActionTaken(ActionTakenValue actionTaken) { |
179 | 0 | if (!isRunPostProcessorLogic()) { |
180 | 0 | return; |
181 | |
} |
182 | 0 | if (actionTaken == null) { |
183 | 0 | return; |
184 | |
} |
185 | |
try { |
186 | 0 | LOG.debug("Notifying post processor of action taken"); |
187 | 0 | PostProcessor postProcessor = routeHeader.getDocumentType().getPostProcessor(); |
188 | 0 | ProcessDocReport report = postProcessor.doActionTaken(new org.kuali.rice.kew.postprocessor.ActionTakenEvent(routeHeader.getDocumentId(), routeHeader.getAppDocId(), actionTaken)); |
189 | 0 | if (!report.isSuccess()) { |
190 | 0 | LOG.warn(report.getMessage(), report.getProcessException()); |
191 | 0 | throw new InvalidActionTakenException(report.getMessage()); |
192 | |
} |
193 | |
|
194 | 0 | } catch (Exception ex) { |
195 | 0 | processPostProcessorException(ex); |
196 | 0 | } |
197 | 0 | } |
198 | |
|
199 | |
protected void notifyStatusChange(String newStatusCode, String oldStatusCode) throws InvalidActionTakenException { |
200 | 0 | if (!isRunPostProcessorLogic()) { |
201 | 0 | return; |
202 | |
} |
203 | 0 | DocumentRouteStatusChange statusChangeEvent = new DocumentRouteStatusChange(routeHeader.getDocumentId(), routeHeader.getAppDocId(), oldStatusCode, newStatusCode); |
204 | |
try { |
205 | 0 | LOG.debug("Notifying post processor of status change " + oldStatusCode + "->" + newStatusCode); |
206 | 0 | PostProcessor postProcessor = routeHeader.getDocumentType().getPostProcessor(); |
207 | 0 | ProcessDocReport report = postProcessor.doRouteStatusChange(statusChangeEvent); |
208 | 0 | if (!report.isSuccess()) { |
209 | 0 | LOG.warn(report.getMessage(), report.getProcessException()); |
210 | 0 | throw new InvalidActionTakenException(report.getMessage()); |
211 | |
} |
212 | 0 | } catch (Exception ex) { |
213 | 0 | processPostProcessorException(ex); |
214 | 0 | } |
215 | 0 | } |
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
protected void queueDocumentProcessing() { |
221 | 0 | QName documentServiceName = new QName(getRouteHeader().getDocumentType().getApplicationId(), MessageServiceNames.DOCUMENT_ROUTING_SERVICE); |
222 | 0 | KSBXMLService documentRoutingService = (KSBXMLService) MessageServiceNames.getServiceAsynchronously(documentServiceName, getRouteHeader()); |
223 | |
try { |
224 | |
|
225 | 0 | RouteDocumentMessageService.RouteMessageXmlElement element = new RouteDocumentMessageService.RouteMessageXmlElement(getDocumentId(),isRunPostProcessorLogic(), RouteContext.getCurrentRouteContext().isSearchIndexingRequestedForContext()); |
226 | 0 | String content = element.translate(); |
227 | 0 | documentRoutingService.invoke(content); |
228 | 0 | } catch (Exception e) { |
229 | 0 | processPostProcessorException(e); |
230 | 0 | } |
231 | 0 | } |
232 | |
|
233 | |
protected ActionTakenValue saveActionTaken() { |
234 | 0 | return saveActionTaken(Boolean.TRUE); |
235 | |
} |
236 | |
|
237 | |
protected ActionTakenValue saveActionTaken(Boolean currentInd) { |
238 | 0 | return saveActionTaken(currentInd, null); |
239 | |
} |
240 | |
|
241 | |
protected ActionTakenValue saveActionTaken(Recipient delegator) { |
242 | 0 | return saveActionTaken(Boolean.TRUE, delegator); |
243 | |
} |
244 | |
|
245 | |
protected ActionTakenValue saveActionTaken(Boolean currentInd, Recipient delegator) { |
246 | 0 | ActionTakenValue val = new ActionTakenValue(); |
247 | 0 | val.setActionTaken(getActionTakenCode()); |
248 | 0 | val.setAnnotation(annotation); |
249 | 0 | val.setDocVersion(routeHeader.getDocVersion()); |
250 | 0 | val.setDocumentId(routeHeader.getDocumentId()); |
251 | 0 | val.setPrincipalId(principal.getPrincipalId()); |
252 | 0 | if (delegator instanceof KimPrincipalRecipient) { |
253 | 0 | val.setDelegatorPrincipalId(((KimPrincipalRecipient)delegator).getPrincipalId()); |
254 | 0 | } else if (delegator instanceof KimGroupRecipient) { |
255 | 0 | val.setDelegatorGroupId(((KimGroupRecipient) delegator).getGroupId()); |
256 | |
} |
257 | |
|
258 | 0 | val.setCurrentIndicator(currentInd); |
259 | 0 | KEWServiceLocator.getActionTakenService().saveActionTaken(val); |
260 | 0 | return val; |
261 | |
} |
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
protected Recipient findDelegatorForActionRequests(List actionRequests) { |
267 | 0 | return getActionRequestService().findDelegator(actionRequests); |
268 | |
} |
269 | |
|
270 | |
public String getActionTakenCode() { |
271 | 0 | return actionTakenCode; |
272 | |
} |
273 | |
|
274 | |
protected void setActionTakenCode(String string) { |
275 | 0 | actionTakenCode = string; |
276 | 0 | } |
277 | |
|
278 | |
protected String getDocumentId() { |
279 | 0 | return this.routeHeader.getDocumentId(); |
280 | |
} |
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
protected boolean isRunPostProcessorLogic() { |
287 | 0 | return this.runPostProcessorLogic; |
288 | |
} |
289 | |
|
290 | |
protected List<String> getGroupIdsForPrincipal() { |
291 | 0 | if (groupIdsForPrincipal == null) { |
292 | 0 | groupIdsForPrincipal = KimApiServiceLocator.getGroupService().getGroupIdsForPrincipal(getPrincipal().getPrincipalId()); |
293 | |
} |
294 | 0 | return groupIdsForPrincipal; |
295 | |
} |
296 | |
|
297 | |
|
298 | |
public void setQueueDocumentAfterAction(boolean queueDocumentAfterAction) { |
299 | 0 | this.queueDocumentAfterAction = queueDocumentAfterAction; |
300 | 0 | } |
301 | |
|
302 | |
private void processPostProcessorException(Exception e) { |
303 | 0 | if (e instanceof RuntimeException) { |
304 | 0 | throw (RuntimeException)e; |
305 | |
} |
306 | 0 | throw new WorkflowRuntimeException(e); |
307 | |
} |
308 | |
|
309 | |
|
310 | |
} |