1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kns.service.impl; |
17 | |
|
18 | |
import java.rmi.RemoteException; |
19 | |
import java.util.Arrays; |
20 | |
import java.util.Collections; |
21 | |
import java.util.List; |
22 | |
|
23 | |
import org.apache.commons.collections.ListUtils; |
24 | |
import org.apache.log4j.Logger; |
25 | |
import org.apache.ojb.broker.OptimisticLockException; |
26 | |
import org.kuali.rice.kew.dto.ActionTakenEventDTO; |
27 | |
import org.kuali.rice.kew.dto.AfterProcessEventDTO; |
28 | |
import org.kuali.rice.kew.dto.BeforeProcessEventDTO; |
29 | |
import org.kuali.rice.kew.dto.DeleteEventDTO; |
30 | |
import org.kuali.rice.kew.dto.DocumentLockingEventDTO; |
31 | |
import org.kuali.rice.kew.dto.DocumentRouteLevelChangeDTO; |
32 | |
import org.kuali.rice.kew.dto.DocumentRouteStatusChangeDTO; |
33 | |
import org.kuali.rice.kew.exception.WorkflowException; |
34 | |
import org.kuali.rice.kew.util.KEWConstants; |
35 | |
import org.kuali.rice.kns.UserSession; |
36 | |
import org.kuali.rice.kns.document.Document; |
37 | |
import org.kuali.rice.kns.service.DateTimeService; |
38 | |
import org.kuali.rice.kns.service.DocumentService; |
39 | |
import org.kuali.rice.kns.service.PostProcessorService; |
40 | |
import org.kuali.rice.kns.util.GlobalVariables; |
41 | |
import org.kuali.rice.kns.util.KNSConstants; |
42 | |
import org.kuali.rice.kns.util.ObjectUtils; |
43 | |
import org.springframework.transaction.annotation.Transactional; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
@Transactional |
52 | 0 | public class PostProcessorServiceImpl implements PostProcessorService { |
53 | |
|
54 | 0 | private static Logger LOG = Logger.getLogger(PostProcessorServiceImpl.class); |
55 | |
|
56 | |
private DocumentService documentService; |
57 | |
private DateTimeService dateTimeService; |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
public boolean doRouteStatusChange(DocumentRouteStatusChangeDTO statusChangeEvent) throws RemoteException { |
63 | |
try { |
64 | 0 | if ( LOG.isInfoEnabled() ) { |
65 | 0 | LOG.info(new StringBuffer("started handling route status change from ").append(statusChangeEvent.getOldRouteStatus()).append(" to ").append(statusChangeEvent.getNewRouteStatus()).append(" for document ").append(statusChangeEvent.getRouteHeaderId())); |
66 | |
} |
67 | 0 | establishGlobalVariables(); |
68 | 0 | Document document = documentService.getByDocumentHeaderId(statusChangeEvent.getRouteHeaderId().toString()); |
69 | 0 | if (document == null) { |
70 | 0 | if (!KEWConstants.ROUTE_HEADER_CANCEL_CD.equals(statusChangeEvent.getNewRouteStatus())) { |
71 | 0 | throw new RuntimeException("unable to load document " + statusChangeEvent.getRouteHeaderId()); |
72 | |
} |
73 | |
} |
74 | |
else { |
75 | 0 | document.doRouteStatusChange(statusChangeEvent); |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | 0 | if (!document.getDocumentHeader().getWorkflowDocument().stateIsSaved()) { |
85 | 0 | documentService.updateDocument(document); |
86 | |
} |
87 | |
} |
88 | 0 | if ( LOG.isInfoEnabled() ) { |
89 | 0 | LOG.info(new StringBuffer("finished handling route status change from ").append(statusChangeEvent.getOldRouteStatus()).append(" to ").append(statusChangeEvent.getNewRouteStatus()).append(" for document ").append(statusChangeEvent.getRouteHeaderId())); |
90 | |
} |
91 | |
} |
92 | 0 | catch (Exception e) { |
93 | 0 | logAndRethrow("route status", e); |
94 | 0 | } |
95 | 0 | return true; |
96 | |
} |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
public boolean doRouteLevelChange(DocumentRouteLevelChangeDTO levelChangeEvent) throws RemoteException { |
102 | |
|
103 | |
|
104 | |
|
105 | |
try { |
106 | 0 | if ( LOG.isDebugEnabled() ) { |
107 | 0 | LOG.debug(new StringBuffer("started handling route level change from ").append(levelChangeEvent.getOldNodeName()).append(" to ").append(levelChangeEvent.getNewNodeName()).append(" for document ").append(levelChangeEvent.getRouteHeaderId())); |
108 | |
} |
109 | 0 | establishGlobalVariables(); |
110 | 0 | Document document = documentService.getByDocumentHeaderId(levelChangeEvent.getRouteHeaderId().toString()); |
111 | 0 | if (document == null) { |
112 | 0 | throw new RuntimeException("unable to load document " + levelChangeEvent.getRouteHeaderId()); |
113 | |
} |
114 | 0 | document.populateDocumentForRouting(); |
115 | 0 | document.doRouteLevelChange(levelChangeEvent); |
116 | 0 | document.getDocumentHeader().getWorkflowDocument().saveRoutingData(); |
117 | 0 | if ( LOG.isDebugEnabled() ) { |
118 | 0 | LOG.debug(new StringBuffer("finished handling route level change from ").append(levelChangeEvent.getOldNodeName()).append(" to ").append(levelChangeEvent.getNewNodeName()).append(" for document ").append(levelChangeEvent.getRouteHeaderId())); |
119 | |
} |
120 | |
} |
121 | 0 | catch (Exception e) { |
122 | 0 | logAndRethrow("route level", e); |
123 | 0 | } |
124 | 0 | return true; |
125 | |
} |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
public boolean doDeleteRouteHeader(DeleteEventDTO event) throws RemoteException { |
131 | 0 | return true; |
132 | |
} |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public boolean doActionTaken(ActionTakenEventDTO event) throws RemoteException { |
138 | |
try { |
139 | 0 | if ( LOG.isDebugEnabled() ) { |
140 | 0 | LOG.debug(new StringBuffer("started doing action taken for action taken code").append(event.getActionTaken().getActionTaken()).append(" for document ").append(event.getRouteHeaderId())); |
141 | |
} |
142 | 0 | establishGlobalVariables(); |
143 | 0 | Document document = documentService.getByDocumentHeaderId(event.getRouteHeaderId().toString()); |
144 | 0 | if (ObjectUtils.isNull(document)) { |
145 | |
|
146 | 0 | if (!KEWConstants.ACTION_TAKEN_CANCELED.equals(event.getActionTaken())) { |
147 | 0 | LOG.warn("doActionTaken() Unable to load document with id " + event.getRouteHeaderId() + |
148 | |
" using action taken code '" + KEWConstants.ACTION_TAKEN_CD.get(event.getActionTaken().getActionTaken())); |
149 | |
|
150 | |
} |
151 | |
} else { |
152 | 0 | document.doActionTaken(event); |
153 | 0 | if ( LOG.isDebugEnabled() ) { |
154 | 0 | LOG.debug(new StringBuffer("finished doing action taken for action taken code").append(event.getActionTaken().getActionTaken()).append(" for document ").append(event.getRouteHeaderId())); |
155 | |
} |
156 | |
} |
157 | |
} |
158 | 0 | catch (Exception e) { |
159 | 0 | logAndRethrow("do action taken", e); |
160 | 0 | } |
161 | 0 | return true; |
162 | |
} |
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
public boolean afterProcess(AfterProcessEventDTO event) throws Exception { |
171 | |
try { |
172 | 0 | if ( LOG.isDebugEnabled() ) { |
173 | 0 | LOG.debug(new StringBuffer("started after process method for document ").append(event.getRouteHeaderId())); |
174 | |
} |
175 | 0 | establishGlobalVariables(); |
176 | 0 | Document document = documentService.getByDocumentHeaderId(event.getRouteHeaderId().toString()); |
177 | 0 | if (ObjectUtils.isNull(document)) { |
178 | |
|
179 | 0 | LOG.warn("afterProcess() Unable to load document with id " + event.getRouteHeaderId() + "... ignoring post processing"); |
180 | |
} else { |
181 | 0 | document.afterWorkflowEngineProcess(event.isSuccessfullyProcessed()); |
182 | 0 | if ( LOG.isDebugEnabled() ) { |
183 | 0 | LOG.debug(new StringBuffer("finished after process method for document ").append(event.getRouteHeaderId())); |
184 | |
} |
185 | |
} |
186 | |
} |
187 | 0 | catch (Exception e) { |
188 | 0 | logAndRethrow("after process", e); |
189 | 0 | } |
190 | 0 | return true; |
191 | |
} |
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
public boolean beforeProcess(BeforeProcessEventDTO event) throws Exception { |
200 | |
try { |
201 | 0 | if ( LOG.isDebugEnabled() ) { |
202 | 0 | LOG.debug(new StringBuffer("started before process method for document ").append(event.getRouteHeaderId())); |
203 | |
} |
204 | 0 | establishGlobalVariables(); |
205 | 0 | Document document = documentService.getByDocumentHeaderId(event.getRouteHeaderId().toString()); |
206 | 0 | if (ObjectUtils.isNull(document)) { |
207 | |
|
208 | 0 | LOG.warn("beforeProcess() Unable to load document with id " + event.getRouteHeaderId() + "... ignoring post processing"); |
209 | |
} else { |
210 | 0 | document.beforeWorkflowEngineProcess(); |
211 | 0 | if ( LOG.isDebugEnabled() ) { |
212 | 0 | LOG.debug(new StringBuffer("finished before process method for document ").append(event.getRouteHeaderId())); |
213 | |
} |
214 | |
} |
215 | |
} |
216 | 0 | catch (Exception e) { |
217 | 0 | logAndRethrow("before process", e); |
218 | 0 | } |
219 | 0 | return true; |
220 | |
} |
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
public Long[] getDocumentIdsToLock(DocumentLockingEventDTO event) throws Exception { |
229 | |
try { |
230 | 0 | if ( LOG.isDebugEnabled() ) { |
231 | 0 | LOG.debug(new StringBuffer("started get document ids to lock method for document ").append(event.getRouteHeaderId())); |
232 | |
} |
233 | 0 | establishGlobalVariables(); |
234 | 0 | Document document = documentService.getByDocumentHeaderId(event.getRouteHeaderId().toString()); |
235 | 0 | if (ObjectUtils.isNull(document)) { |
236 | |
|
237 | 0 | LOG.warn("getDocumentIdsToLock() Unable to load document with id " + event.getRouteHeaderId() + "... ignoring post processing"); |
238 | |
} else { |
239 | 0 | List<Long> documentIdsToLock = document.getWorkflowEngineDocumentIdsToLock(); |
240 | 0 | if ( LOG.isDebugEnabled() ) { |
241 | 0 | LOG.debug(new StringBuffer("finished get document ids to lock method for document ").append(event.getRouteHeaderId())); |
242 | |
} |
243 | 0 | if (documentIdsToLock == null) { |
244 | 0 | return null; |
245 | |
} |
246 | 0 | return documentIdsToLock.toArray(new Long[0]); |
247 | |
|
248 | |
} |
249 | |
} |
250 | 0 | catch (Exception e) { |
251 | 0 | logAndRethrow("before process", e); |
252 | 0 | } |
253 | 0 | return null; |
254 | |
} |
255 | |
|
256 | |
private void logAndRethrow(String changeType, Exception e) throws RuntimeException { |
257 | 0 | LOG.error("caught exception while handling " + changeType + " change", e); |
258 | 0 | logOptimisticDetails(5, e); |
259 | |
|
260 | 0 | throw new RuntimeException("post processor caught exception while handling " + changeType + " change: " + e.getMessage(), e); |
261 | |
} |
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
private void logOptimisticDetails(int depth, Throwable t) { |
270 | 0 | if ((depth > 0) && (t != null)) { |
271 | 0 | if (t instanceof OptimisticLockException) { |
272 | 0 | OptimisticLockException o = (OptimisticLockException) t; |
273 | |
|
274 | 0 | LOG.error("source of OptimisticLockException = " + o.getSourceObject().getClass().getName() + " ::= " + o.getSourceObject()); |
275 | 0 | } |
276 | |
else { |
277 | 0 | Throwable cause = t.getCause(); |
278 | 0 | if (cause != t) { |
279 | 0 | logOptimisticDetails(--depth, cause); |
280 | |
} |
281 | |
} |
282 | |
} |
283 | 0 | } |
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
public final void setDocumentService(DocumentService documentService) { |
290 | 0 | this.documentService = documentService; |
291 | 0 | } |
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
public final void setDateTimeService(DateTimeService dateTimeService) { |
298 | 0 | this.dateTimeService = dateTimeService; |
299 | 0 | } |
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
protected void establishGlobalVariables() throws WorkflowException { |
305 | 0 | if (GlobalVariables.getUserSession() == null) { |
306 | 0 | GlobalVariables.setUserSession(new UserSession(KNSConstants.SYSTEM_USER)); |
307 | |
} |
308 | 0 | GlobalVariables.clear(); |
309 | 0 | } |
310 | |
|
311 | |
} |