1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.krad.workflow.service.impl; |
17 | |
|
18 | |
import org.apache.commons.lang.StringUtils; |
19 | |
import org.apache.commons.lang.time.StopWatch; |
20 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
21 | |
import org.kuali.rice.core.util.RiceKeyConstants; |
22 | |
import org.kuali.rice.kew.api.KewApiServiceLocator; |
23 | |
import org.kuali.rice.kew.api.WorkflowDocument; |
24 | |
import org.kuali.rice.kew.api.WorkflowDocumentFactory; |
25 | |
import org.kuali.rice.kew.api.action.ActionRequestType; |
26 | |
import org.kuali.rice.kew.api.action.ActionType; |
27 | |
import org.kuali.rice.kew.dto.RouteNodeInstanceDTO; |
28 | |
import org.kuali.rice.kew.exception.InvalidActionTakenException; |
29 | |
import org.kuali.rice.kew.exception.WorkflowException; |
30 | |
import org.kuali.rice.kew.service.WorkflowInfo; |
31 | |
import org.kuali.rice.kew.util.KEWConstants; |
32 | |
import org.kuali.rice.kim.api.group.Group; |
33 | |
import org.kuali.rice.kim.api.identity.principal.Principal; |
34 | |
import org.kuali.rice.kim.api.services.KimApiServiceLocator; |
35 | |
import org.kuali.rice.kim.bo.Person; |
36 | |
import org.kuali.rice.krad.bo.AdHocRouteRecipient; |
37 | |
import org.kuali.rice.krad.exception.UnknownDocumentIdException; |
38 | |
import org.kuali.rice.krad.service.KRADServiceLocator; |
39 | |
import org.kuali.rice.krad.util.GlobalVariables; |
40 | |
import org.kuali.rice.krad.workflow.service.KualiWorkflowInfo; |
41 | |
import org.kuali.rice.krad.workflow.service.WorkflowDocumentService; |
42 | |
import org.springframework.transaction.annotation.Transactional; |
43 | |
|
44 | |
import java.text.MessageFormat; |
45 | |
import java.util.ArrayList; |
46 | |
import java.util.HashSet; |
47 | |
import java.util.List; |
48 | |
import java.util.Set; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
@Transactional |
55 | 0 | public class WorkflowDocumentServiceImpl implements WorkflowDocumentService { |
56 | 0 | private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(WorkflowDocumentServiceImpl.class); |
57 | |
|
58 | |
private KualiWorkflowInfo workflowInfoService; |
59 | |
|
60 | |
@Override |
61 | |
public boolean workflowDocumentExists(String documentId) { |
62 | 0 | boolean exists = false; |
63 | |
|
64 | 0 | if (StringUtils.isBlank(documentId)) { |
65 | 0 | throw new IllegalArgumentException("invalid (blank) documentId"); |
66 | |
} |
67 | |
|
68 | 0 | exists = KewApiServiceLocator.getWorkflowDocumentService().doesDocumentExist(documentId); |
69 | |
|
70 | 0 | return exists; |
71 | |
} |
72 | |
|
73 | |
@Override |
74 | |
public WorkflowDocument createWorkflowDocument(String documentTypeName, Person person) { |
75 | 0 | String watchName = "createWorkflowDocument"; |
76 | 0 | StopWatch watch = new StopWatch(); |
77 | 0 | watch.start(); |
78 | 0 | if (LOG.isDebugEnabled()) { |
79 | 0 | LOG.debug(watchName + ": started"); |
80 | |
} |
81 | 0 | if (StringUtils.isBlank(documentTypeName)) { |
82 | 0 | throw new IllegalArgumentException("invalid (blank) documentTypeName"); |
83 | |
} |
84 | 0 | if (person == null) { |
85 | 0 | throw new IllegalArgumentException("invalid (null) person"); |
86 | |
} |
87 | |
|
88 | 0 | if (StringUtils.isBlank(person.getPrincipalName())) { |
89 | 0 | throw new IllegalArgumentException("invalid (empty) PrincipalName"); |
90 | |
} |
91 | |
|
92 | 0 | if (LOG.isDebugEnabled()) { |
93 | 0 | LOG.debug("creating workflowDoc(" + documentTypeName + "," + person.getPrincipalName() + ")"); |
94 | |
} |
95 | |
|
96 | 0 | WorkflowDocument document = WorkflowDocumentFactory.createDocument(person.getPrincipalId(), documentTypeName); |
97 | 0 | watch.stop(); |
98 | 0 | if (LOG.isDebugEnabled()) { |
99 | 0 | LOG.debug(watchName + ": " + watch.toString()); |
100 | |
} |
101 | |
|
102 | 0 | return document; |
103 | |
} |
104 | |
|
105 | |
@Override |
106 | |
public WorkflowDocument loadWorkflowDocument(String documentId, Person user) { |
107 | 0 | if (documentId == null) { |
108 | 0 | throw new IllegalArgumentException("invalid (null) documentHeaderId"); |
109 | |
} |
110 | 0 | if (user == null) { |
111 | 0 | throw new IllegalArgumentException("invalid (null) workflowUser"); |
112 | |
} |
113 | 0 | else if (StringUtils.isEmpty(user.getPrincipalName())) { |
114 | 0 | throw new IllegalArgumentException("invalid (empty) workflowUser"); |
115 | |
} |
116 | |
|
117 | 0 | if (LOG.isDebugEnabled()) { |
118 | 0 | LOG.debug("retrieving document(" + documentId + "," + user.getPrincipalName() + ")"); |
119 | |
} |
120 | |
|
121 | |
try { |
122 | 0 | return WorkflowDocumentFactory.loadDocument(user.getPrincipalId(), documentId); |
123 | 0 | } catch (IllegalArgumentException e) { |
124 | |
|
125 | 0 | throw new UnknownDocumentIdException("unable to locate document with documentHeaderId '" + documentId + "'"); |
126 | |
} |
127 | |
} |
128 | |
|
129 | |
@Override |
130 | |
public void acknowledge(WorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException { |
131 | 0 | if (LOG.isDebugEnabled()) { |
132 | 0 | LOG.debug("acknowleding document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')"); |
133 | |
} |
134 | |
|
135 | 0 | handleAdHocRouteRequests(workflowDocument, annotation, filterAdHocRecipients(adHocRecipients, new String[] { KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, KEWConstants.ACTION_REQUEST_FYI_REQ })); |
136 | 0 | workflowDocument.acknowledge(annotation); |
137 | 0 | } |
138 | |
|
139 | |
@Override |
140 | |
public void approve(WorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException { |
141 | 0 | if (LOG.isDebugEnabled()) { |
142 | 0 | LOG.debug("approving document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')"); |
143 | |
} |
144 | |
|
145 | 0 | handleAdHocRouteRequests(workflowDocument, annotation, filterAdHocRecipients(adHocRecipients, new String[] { KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, KEWConstants.ACTION_REQUEST_FYI_REQ, KEWConstants.ACTION_REQUEST_APPROVE_REQ })); |
146 | 0 | workflowDocument.approve(annotation); |
147 | 0 | } |
148 | |
|
149 | |
|
150 | |
@Override |
151 | |
public void superUserApprove(WorkflowDocument workflowDocument, String annotation) throws WorkflowException { |
152 | 0 | if ( LOG.isInfoEnabled() ) { |
153 | 0 | LOG.info("super user approve document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')"); |
154 | |
} |
155 | 0 | workflowDocument.superUserBlanketApprove(annotation); |
156 | 0 | } |
157 | |
|
158 | |
@Override |
159 | |
public void superUserCancel(WorkflowDocument workflowDocument, String annotation) throws WorkflowException { |
160 | 0 | LOG.info("super user cancel document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')"); |
161 | 0 | workflowDocument.superUserCancel(annotation); |
162 | 0 | } |
163 | |
|
164 | |
@Override |
165 | |
public void superUserDisapprove(WorkflowDocument workflowDocument, String annotation) throws WorkflowException { |
166 | 0 | if ( LOG.isInfoEnabled() ) { |
167 | 0 | LOG.info("super user disapprove document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')"); |
168 | |
} |
169 | 0 | workflowDocument.superUserDisapprove(annotation); |
170 | 0 | } |
171 | |
|
172 | |
@Override |
173 | |
public void blanketApprove(WorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException { |
174 | 0 | if (LOG.isDebugEnabled()) { |
175 | 0 | LOG.debug("blanket approving document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')"); |
176 | |
} |
177 | |
|
178 | 0 | handleAdHocRouteRequests(workflowDocument, annotation, filterAdHocRecipients(adHocRecipients, new String[] { KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, KEWConstants.ACTION_REQUEST_FYI_REQ })); |
179 | 0 | workflowDocument.blanketApprove(annotation); |
180 | 0 | } |
181 | |
|
182 | |
@Override |
183 | |
public void cancel(WorkflowDocument workflowDocument, String annotation) throws WorkflowException { |
184 | 0 | if (LOG.isDebugEnabled()) { |
185 | 0 | LOG.debug("canceling document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')"); |
186 | |
} |
187 | |
|
188 | 0 | workflowDocument.cancel(annotation); |
189 | 0 | } |
190 | |
|
191 | |
@Override |
192 | |
public void clearFyi(WorkflowDocument workflowDocument, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException { |
193 | 0 | if (LOG.isDebugEnabled()) { |
194 | 0 | LOG.debug("clearing FYI for document(" + workflowDocument.getDocumentId() + ")"); |
195 | |
} |
196 | |
|
197 | 0 | handleAdHocRouteRequests(workflowDocument, "", filterAdHocRecipients(adHocRecipients, new String[] { KEWConstants.ACTION_REQUEST_FYI_REQ })); |
198 | 0 | workflowDocument.fyi(); |
199 | 0 | } |
200 | |
|
201 | |
@Override |
202 | |
public void sendWorkflowNotification(WorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException { |
203 | 0 | sendWorkflowNotification(workflowDocument, annotation, adHocRecipients, null); |
204 | 0 | } |
205 | |
|
206 | |
@Override |
207 | |
public void sendWorkflowNotification(WorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients, String notificationLabel) throws WorkflowException { |
208 | 0 | if (LOG.isDebugEnabled()) { |
209 | 0 | LOG.debug("sending FYI for document(" + workflowDocument.getDocumentId() + ")"); |
210 | |
} |
211 | |
|
212 | 0 | handleAdHocRouteRequests(workflowDocument, annotation, adHocRecipients, notificationLabel); |
213 | 0 | } |
214 | |
|
215 | |
@Override |
216 | |
public void disapprove(WorkflowDocument workflowDocument, String annotation) throws WorkflowException { |
217 | 0 | if (LOG.isDebugEnabled()) { |
218 | 0 | LOG.debug("disapproving document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')"); |
219 | |
} |
220 | |
|
221 | 0 | workflowDocument.disapprove(annotation); |
222 | 0 | } |
223 | |
|
224 | |
@Override |
225 | |
public void route(WorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException { |
226 | 0 | if (LOG.isDebugEnabled()) { |
227 | 0 | LOG.debug("routing document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')"); |
228 | |
} |
229 | |
|
230 | 0 | handleAdHocRouteRequests(workflowDocument, annotation, filterAdHocRecipients(adHocRecipients, new String[] { KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, KEWConstants.ACTION_REQUEST_FYI_REQ, KEWConstants.ACTION_REQUEST_APPROVE_REQ })); |
231 | 0 | workflowDocument.route(annotation); |
232 | 0 | } |
233 | |
|
234 | |
@Override |
235 | |
public void save(WorkflowDocument workflowDocument, String annotation) throws WorkflowException { |
236 | 0 | if (workflowDocument.isValidAction(ActionType.SAVE)) { |
237 | 0 | if (LOG.isDebugEnabled()) { |
238 | 0 | LOG.debug("saving document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')"); |
239 | |
} |
240 | |
|
241 | 0 | workflowDocument.saveDocument(annotation); |
242 | |
} |
243 | |
else { |
244 | 0 | this.saveRoutingData(workflowDocument); |
245 | |
} |
246 | 0 | } |
247 | |
|
248 | |
@Override |
249 | |
public void saveRoutingData(WorkflowDocument workflowDocument) throws WorkflowException { |
250 | 0 | if (LOG.isDebugEnabled()) { |
251 | 0 | LOG.debug("saving document(" + workflowDocument.getDocumentId() + ")"); |
252 | |
} |
253 | |
|
254 | 0 | workflowDocument.saveDocumentData(); |
255 | 0 | } |
256 | |
|
257 | |
@Override |
258 | |
public String getCurrentRouteLevelName(WorkflowDocument workflowDocument) throws WorkflowException { |
259 | 0 | if (LOG.isDebugEnabled()) { |
260 | 0 | LOG.debug("getting current route level name for document(" + workflowDocument.getDocumentId()); |
261 | |
} |
262 | |
|
263 | 0 | WorkflowDocument freshCopyWorkflowDoc = loadWorkflowDocument(workflowDocument.getDocumentId(), GlobalVariables.getUserSession().getPerson()); |
264 | 0 | return getCurrentRouteNodeNames(freshCopyWorkflowDoc); |
265 | |
} |
266 | |
|
267 | |
|
268 | |
|
269 | |
@Override |
270 | |
public String getCurrentRouteNodeNames(WorkflowDocument workflowDocument) { |
271 | 0 | Set<String> nodeNames = workflowDocument.getNodeNames(); |
272 | 0 | if (nodeNames.isEmpty()) { |
273 | 0 | return ""; |
274 | |
} |
275 | 0 | StringBuilder builder = new StringBuilder(); |
276 | 0 | for (String nodeName : nodeNames) { |
277 | 0 | builder.append(nodeName).append(", "); |
278 | |
} |
279 | 0 | builder.setLength(builder.length() - 2); |
280 | 0 | return builder.toString(); |
281 | |
} |
282 | |
|
283 | |
private void handleAdHocRouteRequests(WorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException { |
284 | 0 | handleAdHocRouteRequests(workflowDocument, annotation, adHocRecipients, null); |
285 | 0 | } |
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
private void handleAdHocRouteRequests(WorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients, String notificationLabel) throws WorkflowException { |
298 | |
|
299 | 0 | if (adHocRecipients != null && adHocRecipients.size() > 0) { |
300 | 0 | String currentNode = null; |
301 | 0 | Set<String> currentNodes = workflowDocument.getNodeNames(); |
302 | 0 | if (currentNodes.isEmpty()) { |
303 | 0 | WorkflowInfo workflowInfo = new WorkflowInfo(); |
304 | 0 | RouteNodeInstanceDTO[] nodes = workflowInfo.getTerminalNodeInstances(workflowDocument.getDocumentId()); |
305 | 0 | currentNodes = new HashSet<String>(); |
306 | 0 | for (RouteNodeInstanceDTO node : nodes) { |
307 | 0 | currentNodes.add(node.getName()); |
308 | |
} |
309 | |
} |
310 | |
|
311 | 0 | currentNode = currentNodes.iterator().next(); |
312 | |
|
313 | 0 | for (AdHocRouteRecipient recipient : adHocRecipients) { |
314 | 0 | if (StringUtils.isNotEmpty(recipient.getId())) { |
315 | 0 | String newAnnotation = annotation; |
316 | 0 | if ( StringUtils.isBlank( annotation ) ) { |
317 | |
try { |
318 | 0 | String message = KRADServiceLocator.getKualiConfigurationService().getPropertyString(RiceKeyConstants.MESSAGE_ADHOC_ANNOTATION); |
319 | 0 | newAnnotation = MessageFormat.format(message, GlobalVariables.getUserSession().getPrincipalName() ); |
320 | 0 | } catch ( Exception ex ) { |
321 | 0 | LOG.warn("Unable to set annotation", ex ); |
322 | 0 | } |
323 | |
} |
324 | 0 | if (AdHocRouteRecipient.PERSON_TYPE.equals(recipient.getType())) { |
325 | |
|
326 | 0 | Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(recipient.getId()); |
327 | 0 | if (principal == null) { |
328 | 0 | throw new RiceRuntimeException("Could not locate principal with name '" + recipient.getId() + "'"); |
329 | |
} |
330 | 0 | workflowDocument.adHocToPrincipal(ActionRequestType.fromCode(recipient.getActionRequested()), currentNode, newAnnotation, principal.getPrincipalId(), "", true, notificationLabel); |
331 | 0 | } |
332 | |
else { |
333 | 0 | Group group = KimApiServiceLocator.getGroupService().getGroup(recipient.getId()); |
334 | 0 | if (group == null) { |
335 | 0 | throw new RiceRuntimeException("Could not locate group with id '" + recipient.getId() + "'"); |
336 | |
} |
337 | 0 | workflowDocument.adHocToGroup(ActionRequestType.fromCode(recipient.getActionRequested()), currentNode, newAnnotation, group.getId() , "", true, notificationLabel); |
338 | |
} |
339 | 0 | } |
340 | |
} |
341 | |
} |
342 | 0 | } |
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
|
348 | |
|
349 | |
|
350 | |
private List<AdHocRouteRecipient> filterAdHocRecipients(List<AdHocRouteRecipient> adHocRecipients, String[] validTypes) { |
351 | |
|
352 | 0 | List<AdHocRouteRecipient> realAdHocRecipients = new ArrayList<AdHocRouteRecipient>(); |
353 | 0 | if (adHocRecipients != null) { |
354 | 0 | for (AdHocRouteRecipient proposedRecipient : adHocRecipients) { |
355 | 0 | if (StringUtils.isNotBlank(proposedRecipient.getActionRequested())) { |
356 | 0 | for (int i = 0; i < validTypes.length; i++) { |
357 | 0 | if (validTypes[i].equals(proposedRecipient.getActionRequested())) { |
358 | 0 | realAdHocRecipients.add(proposedRecipient); |
359 | |
} |
360 | |
} |
361 | |
} |
362 | |
} |
363 | |
} |
364 | 0 | return realAdHocRecipients; |
365 | |
} |
366 | |
|
367 | |
|
368 | |
public void setWorkflowInfoService(KualiWorkflowInfo workflowInfoService) { |
369 | 0 | this.workflowInfoService = workflowInfoService; |
370 | 0 | } |
371 | |
|
372 | |
public KualiWorkflowInfo getWorkflowInfoService() { |
373 | 0 | return workflowInfoService; |
374 | |
} |
375 | |
} |