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