1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.impl.document;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.log4j.Logger;
20 import org.joda.time.DateTime;
21 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
22 import org.kuali.rice.core.api.exception.RiceIllegalStateException;
23 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
24 import org.kuali.rice.kew.actiontaken.ActionTakenValue;
25 import org.kuali.rice.kew.api.action.ActionRequest;
26 import org.kuali.rice.kew.api.action.ActionTaken;
27 import org.kuali.rice.kew.api.doctype.RouteNode;
28 import org.kuali.rice.kew.api.document.Document;
29 import org.kuali.rice.kew.api.document.DocumentContent;
30 import org.kuali.rice.kew.api.document.DocumentDetail;
31 import org.kuali.rice.kew.api.document.DocumentLink;
32 import org.kuali.rice.kew.api.document.DocumentStatus;
33 import org.kuali.rice.kew.api.document.WorkflowDocumentService;
34 import org.kuali.rice.kew.api.document.search.DocumentSearchCriteria;
35 import org.kuali.rice.kew.api.document.search.DocumentSearchResults;
36 import org.kuali.rice.kew.api.document.node.RouteNodeInstance;
37 import org.kuali.rice.kew.dto.DTOConverter;
38 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
39 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValueContent;
40 import org.kuali.rice.kew.routeheader.DocumentStatusTransition;
41 import org.kuali.rice.kew.service.KEWServiceLocator;
42
43 import javax.jws.WebParam;
44 import java.math.BigDecimal;
45 import java.sql.Timestamp;
46 import java.util.ArrayList;
47 import java.util.Collection;
48 import java.util.Collections;
49 import java.util.HashMap;
50 import java.util.LinkedHashSet;
51 import java.util.List;
52 import java.util.Map;
53 import java.util.Set;
54
55
56
57
58
59
60 public class WorkflowDocumentServiceImpl implements WorkflowDocumentService {
61
62 private static final Logger LOG = Logger.getLogger(WorkflowDocumentServiceImpl.class);
63
64 @Override
65 public Document getDocument(String documentId) {
66 if (StringUtils.isBlank(documentId)) {
67 throw new RiceIllegalArgumentException("documentId was blank or null");
68 }
69 DocumentRouteHeaderValue documentBo = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentId);
70 return DocumentRouteHeaderValue.to(documentBo);
71 }
72
73 @Override
74 public boolean doesDocumentExist(String documentId) {
75 if (StringUtils.isBlank(documentId)) {
76 throw new RiceIllegalArgumentException("documentId was blank or null");
77 }
78 DocumentRouteHeaderValue documentBo = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentId);
79 return documentBo != null;
80 }
81
82 @Override
83 public DocumentDetail getDocumentDetailByAppId(String documentTypeName, String appId) {
84 if (StringUtils.isEmpty(documentTypeName)) {
85 throw new RiceIllegalArgumentException("documentTypeName was blank or null");
86 }
87 if (StringUtils.isEmpty(appId)) {
88 throw new RiceIllegalArgumentException("appId was blank or null");
89 }
90
91 Collection documentIds = KEWServiceLocator.getRouteHeaderService().findByDocTypeAndAppId(documentTypeName, appId);
92 if(documentIds==null||documentIds.isEmpty()){
93 throw new RiceIllegalStateException("No RouteHeader Ids found for documentTypName: " + documentTypeName + ", appId: " + appId);
94 }
95 if(documentIds.size()>1){
96 throw new RiceIllegalStateException("Multiple RouteHeader Ids found for documentTypName: " + documentTypeName + ", appId: " + appId);
97 }
98
99 return getDocumentDetail((String)documentIds.iterator().next());
100 }
101
102 public RouteNodeInstance getRouteNodeInstance(String nodeInstanceId) {
103 if (StringUtils.isEmpty(nodeInstanceId)) {
104 throw new RiceIllegalArgumentException("nodeInstanceId was blank or null");
105 }
106 if ( LOG.isDebugEnabled() ) {
107 LOG.debug("Fetching RouteNodeInstanceVO [id="+nodeInstanceId+"]");
108 }
109 org.kuali.rice.kew.engine.node.RouteNodeInstance nodeInstance = KEWServiceLocator.getRouteNodeService().findRouteNodeInstanceById(nodeInstanceId);
110 return org.kuali.rice.kew.engine.node.RouteNodeInstance.to(nodeInstance);
111 }
112
113 @Override
114 public DocumentStatus getDocumentStatus(String documentId) {
115 if (StringUtils.isEmpty(documentId)) {
116 throw new RiceIllegalArgumentException("documentId was blank or null");
117 }
118 String documentStatus = KEWServiceLocator.getRouteHeaderService().getDocumentStatus(documentId);
119 if (StringUtils.isEmpty(documentStatus)) {
120 throw new RiceIllegalStateException("DocumentStatus not found for documentId: " + documentId);
121 }
122 return DocumentStatus.fromCode(documentStatus);
123 }
124
125 @Override
126 public String getApplicationDocumentId(String documentId) {
127 if (StringUtils.isEmpty(documentId)) {
128 throw new RiceIllegalArgumentException("documentId was blank or null");
129 }
130 return KEWServiceLocator.getRouteHeaderService().getAppDocId(documentId);
131 }
132
133 @Override
134 public DocumentSearchResults documentSearch(String principalId, DocumentSearchCriteria criteria) {
135 if (criteria == null) {
136 throw new RiceIllegalArgumentException("criteria was null");
137 }
138 return KEWServiceLocator.getDocumentSearchService().lookupDocuments(principalId, criteria);
139 }
140
141 @Override
142 public List<String> getSearchableAttributeStringValuesByKey(String documentId, String key) {
143 if (StringUtils.isEmpty(documentId)) {
144 throw new RiceIllegalArgumentException("documentId was blank or null");
145 }
146 if (StringUtils.isEmpty(key)) {
147 throw new RiceIllegalArgumentException("key was blank or null");
148 }
149 return KEWServiceLocator.getRouteHeaderService().getSearchableAttributeStringValuesByKey(documentId, key);
150 }
151
152 @Override
153 public List<DateTime> getSearchableAttributeDateTimeValuesByKey(String documentId, String key) {
154 if (StringUtils.isEmpty(documentId)) {
155 throw new RiceIllegalArgumentException("documentId was blank or null");
156 }
157 if (StringUtils.isEmpty(key)) {
158 throw new RiceIllegalArgumentException("key was blank or null");
159 }
160
161 List<Timestamp> results = KEWServiceLocator.getRouteHeaderService().getSearchableAttributeDateTimeValuesByKey(documentId, key);
162 if (results == null) {
163 return null;
164 }
165 List<DateTime> dateTimes = new ArrayList<DateTime>();
166
167 for(Timestamp time : results) {
168 dateTimes.add(new DateTime(time.getTime()));
169 }
170 return dateTimes;
171 }
172
173 @Override
174 public List<BigDecimal> getSearchableAttributeFloatValuesByKey(String documentId, String key) {
175 if (StringUtils.isEmpty(documentId)) {
176 throw new RiceIllegalArgumentException("documentId was blank or null");
177 }
178 if (StringUtils.isEmpty(key)) {
179 throw new RiceIllegalArgumentException("key was blank or null");
180 }
181 return KEWServiceLocator.getRouteHeaderService().getSearchableAttributeFloatValuesByKey(documentId, key);
182 }
183
184 @Override
185 public List<Long> getSearchableAttributeLongValuesByKey(String documentId, String key) {
186 if (StringUtils.isEmpty(documentId)) {
187 throw new RiceIllegalArgumentException("documentId was blank or null");
188 }
189 if (StringUtils.isEmpty(key)) {
190 throw new RiceIllegalArgumentException("key was blank or null");
191 }
192 return KEWServiceLocator.getRouteHeaderService().getSearchableAttributeLongValuesByKey(documentId, key);
193 }
194
195 @Override
196 public DocumentContent getDocumentContent(String documentId) {
197 if (StringUtils.isBlank(documentId)) {
198 throw new RiceIllegalArgumentException("documentId was blank or null");
199 }
200 DocumentRouteHeaderValueContent content = KEWServiceLocator.getRouteHeaderService().getContent(documentId);
201 return DocumentRouteHeaderValueContent.to(content);
202 }
203
204 @Override
205 public List<ActionRequest> getRootActionRequests(String documentId) {
206 if (StringUtils.isBlank(documentId)) {
207 throw new RiceIllegalArgumentException("documentId was blank or null");
208 }
209 List<ActionRequest> actionRequests = new ArrayList<ActionRequest>();
210 List<ActionRequestValue> actionRequestBos = KEWServiceLocator.getActionRequestService().findAllRootActionRequestsByDocumentId(documentId);
211 for (ActionRequestValue actionRequestBo : actionRequestBos) {
212 actionRequests.add(ActionRequestValue.to(actionRequestBo));
213 }
214 return Collections.unmodifiableList(actionRequests);
215 }
216
217 @Override
218 public List<ActionRequest> getActionRequestsForPrincipalAtNode(String documentId, String nodeName,
219 String principalId) {
220 if (StringUtils.isBlank(documentId)) {
221 throw new RiceIllegalArgumentException("documentId was null or blank");
222 }
223 if ( LOG.isDebugEnabled() ) {
224 LOG.debug("Fetching ActionRequests [docId="+documentId+", nodeName="+nodeName+", principalId="+principalId+"]");
225 }
226 List<ActionRequestValue> actionRequestBos = KEWServiceLocator.getActionRequestService().findAllActionRequestsByDocumentId(documentId);
227 List<ActionRequestValue> matchingActionRequests = new ArrayList<ActionRequestValue>();
228 for (ActionRequestValue actionRequestValue : actionRequestBos) {
229 if (actionRequestMatches(actionRequestValue, nodeName, principalId)) {
230 matchingActionRequests.add(actionRequestValue);
231 }
232 }
233 List<ActionRequest> actionRequests = new ArrayList<ActionRequest>(matchingActionRequests.size());
234 for (ActionRequestValue matchingActionRequest : matchingActionRequests) {
235 actionRequests.add(ActionRequestValue.to(matchingActionRequest));
236 }
237 return actionRequests;
238 }
239
240 protected boolean actionRequestMatches(ActionRequestValue actionRequest, String nodeName, String principalId) {
241 boolean matchesUserId = true;
242 boolean matchesNodeName = true;
243 if (StringUtils.isNotBlank(nodeName)) {
244 matchesNodeName = nodeName.equals(actionRequest.getPotentialNodeName());
245 }
246 if (principalId != null) {
247 matchesUserId = actionRequest.isRecipientRoutedRequest(principalId);
248 }
249 return matchesNodeName && matchesUserId;
250 }
251
252
253 @Override
254 public List<ActionTaken> getActionsTaken(String documentId) {
255 if (StringUtils.isEmpty(documentId)) {
256 throw new RiceIllegalArgumentException("documentId is null or empty.");
257 }
258 List<ActionTaken> actionTakens = new ArrayList<ActionTaken>();
259 Collection<ActionTakenValue> actionTakenBos = KEWServiceLocator.getActionTakenService().findByDocumentId(documentId);
260 for (ActionTakenValue actionTakenBo : actionTakenBos) {
261 actionTakens.add(ActionTakenValue.to(actionTakenBo));
262 }
263 return actionTakens;
264 }
265
266 @Override
267 public DocumentDetail getDocumentDetail(@WebParam(name = "documentId") String documentId) {
268 if (StringUtils.isBlank(documentId)) {
269 throw new RiceIllegalArgumentException("documentId was null or blank");
270 }
271 if ( LOG.isDebugEnabled() ) {
272 LOG.debug("Fetching DocumentDetail [id="+documentId+"]");
273 }
274 DocumentRouteHeaderValue document = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentId);
275 if (document == null) {
276 return null;
277 }
278 DocumentDetail documentDetailVO = DTOConverter.convertDocumentDetailNew(document);
279 if ( LOG.isDebugEnabled() ) {
280 LOG.debug("Returning DocumentDetailVO [id=" + documentId + "]");
281 }
282 return documentDetailVO;
283 }
284
285 @Override
286 public List<org.kuali.rice.kew.api.document.DocumentStatusTransition> getDocumentStatusTransitionHistory(String documentId) {
287 if (StringUtils.isBlank(documentId)) {
288 throw new RiceIllegalArgumentException("documentId was null or blank");
289 }
290 if ( LOG.isDebugEnabled() ) {
291 LOG.debug("Fetching document status transition history [id="+documentId+"]");
292 }
293 DocumentRouteHeaderValue document = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentId);;
294
295 List<DocumentStatusTransition> list = document.getAppDocStatusHistory();
296
297 List<org.kuali.rice.kew.api.document.DocumentStatusTransition> transitionHistory = new ArrayList<org.kuali.rice.kew.api.document.DocumentStatusTransition>(list.size());
298
299 for (DocumentStatusTransition transition : list) {
300 transitionHistory.add(DocumentStatusTransition.to(transition));
301 }
302 return transitionHistory;
303 }
304
305 @Override
306 public List<RouteNodeInstance> getRouteNodeInstances(String documentId) {
307 if (StringUtils.isBlank(documentId)) {
308 throw new RiceIllegalArgumentException("documentId was null or blank");
309 }
310
311 if ( LOG.isDebugEnabled() ) {
312 LOG.debug("Fetching RouteNodeInstances [documentId=" + documentId + "]");
313 }
314 DocumentRouteHeaderValue documentBo = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentId);
315 if (documentBo == null) {
316 return Collections.emptyList();
317 }
318 return convertRouteNodeInstances(KEWServiceLocator.getRouteNodeService().getFlattenedNodeInstances(documentBo, true));
319 }
320
321 @Override
322 public List<RouteNodeInstance> getActiveRouteNodeInstances(String documentId) {
323 if (StringUtils.isBlank(documentId)) {
324 throw new RiceIllegalArgumentException("documentId was null or blank");
325 }
326
327 if ( LOG.isDebugEnabled() ) {
328 LOG.debug("Fetching active RouteNodeInstances [documentId=" + documentId + "]");
329 }
330 return convertRouteNodeInstances(KEWServiceLocator.getRouteNodeService().getActiveNodeInstances(documentId));
331 }
332
333 @Override
334 public List<RouteNodeInstance> getTerminalRouteNodeInstances(String documentId) {
335 if (StringUtils.isBlank(documentId)) {
336 throw new RiceIllegalArgumentException("documentId was null or blank");
337 }
338
339 if ( LOG.isDebugEnabled() ) {
340 LOG.debug("Fetching terminal RouteNodeInstanceVOs [docId=" + documentId + "]");
341 }
342 return convertRouteNodeInstances(KEWServiceLocator.getRouteNodeService().getTerminalNodeInstances(documentId));
343 }
344
345 public List<RouteNodeInstance> getCurrentRouteNodeInstances(String documentId) {
346 if (StringUtils.isBlank(documentId)) {
347 throw new RiceIllegalArgumentException("documentId was null or blank");
348 }
349
350 if ( LOG.isDebugEnabled() ) {
351 LOG.debug("Fetching current RouteNodeInstanceVOs [docId=" + documentId + "]");
352 }
353 return convertRouteNodeInstances(KEWServiceLocator.getRouteNodeService().getCurrentNodeInstances(documentId));
354 }
355
356 private List<RouteNodeInstance> convertRouteNodeInstances(List<org.kuali.rice.kew.engine.node.RouteNodeInstance> routeNodeInstanceBos) {
357 List<RouteNodeInstance> routeNodeInstances = new ArrayList<RouteNodeInstance>();
358 for (org.kuali.rice.kew.engine.node.RouteNodeInstance routeNodeInstanceBo : routeNodeInstanceBos) {
359 routeNodeInstances.add(org.kuali.rice.kew.engine.node.RouteNodeInstance.to(routeNodeInstanceBo));
360 }
361 return Collections.unmodifiableList(routeNodeInstances);
362 }
363
364 @Override
365 public List<String> getPreviousRouteNodeNames(String documentId) {
366
367 if (StringUtils.isBlank(documentId)) {
368 throw new RiceIllegalArgumentException("documentId was null or blank");
369 }
370 if ( LOG.isDebugEnabled() ) {
371 LOG.debug("Fetching previous node names [documentId=" + documentId + "]");
372 }
373 return new ArrayList<String>(KEWServiceLocator.getRouteNodeService().findPreviousNodeNames(documentId));
374 }
375
376 @Override
377 public List<String> getPrincipalIdsWithPendingActionRequestByActionRequestedAndDocId(String actionRequestedCd, String documentId){
378 if (StringUtils.isEmpty(actionRequestedCd)) {
379 throw new RiceIllegalArgumentException("actionRequestCd was blank or null");
380 }
381 if (StringUtils.isEmpty(documentId)) {
382 throw new RiceIllegalArgumentException("documentId was blank or null");
383 }
384 return KEWServiceLocator.getActionRequestService().
385 getPrincipalIdsWithPendingActionRequestByActionRequestedAndDocId(actionRequestedCd, documentId);
386 }
387
388 @Override
389 public String getDocumentInitiatorPrincipalId(String documentId) {
390 if (StringUtils.isEmpty(documentId)) {
391 throw new RiceIllegalArgumentException("documentId was blank or null");
392 }
393
394 DocumentRouteHeaderValue header = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentId, false);
395 if ( header == null) {
396 return null;
397 }
398 return header.getInitiatorWorkflowId();
399 }
400
401 @Override
402 public String getRoutedByPrincipalIdByDocumentId(String documentId) {
403 if (StringUtils.isEmpty(documentId)) {
404 throw new RiceIllegalArgumentException("documentId was blank or null");
405 }
406
407 DocumentRouteHeaderValue header = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentId, false);
408 if ( header == null) {
409 return null;
410 }
411 return header.getRoutedByUserWorkflowId();
412 }
413
414 @Override
415 public DocumentLink addDocumentLink(DocumentLink documentLink) throws RiceIllegalArgumentException {
416 if (documentLink == null) {
417 throw new RiceIllegalArgumentException("documentLink was null");
418 }
419 if (documentLink.getId() != null) {
420 throw new RiceIllegalArgumentException("the given documentLink already has an id, cannot add a document link with an existing id");
421 }
422 org.kuali.rice.kew.documentlink.DocumentLink documentLinkBo = org.kuali.rice.kew.documentlink.DocumentLink.from(documentLink);
423 KEWServiceLocator.getDocumentLinkService().saveDocumentLink(documentLinkBo);
424 return org.kuali.rice.kew.documentlink.DocumentLink.to(documentLinkBo);
425 }
426
427 @Override
428 public DocumentLink deleteDocumentLink(String documentLinkId) throws RiceIllegalArgumentException {
429 if (StringUtils.isBlank(documentLinkId)) {
430 throw new RiceIllegalArgumentException("documentLinkId was null or blank");
431 }
432 org.kuali.rice.kew.documentlink.DocumentLink documentLinkBo = KEWServiceLocator.getDocumentLinkService().getDocumentLink(Long.valueOf(documentLinkId));
433 if (documentLinkBo == null) {
434 throw new RiceIllegalStateException("Failed to locate document link with the given documentLinkId: " + documentLinkId);
435 }
436 KEWServiceLocator.getDocumentLinkService().deleteDocumentLink(documentLinkBo);
437 return org.kuali.rice.kew.documentlink.DocumentLink.to(documentLinkBo);
438 }
439
440 @Override
441 public List<DocumentLink> deleteDocumentLinksByDocumentId(String originatingDocumentId) throws RiceIllegalArgumentException {
442 if (StringUtils.isBlank(originatingDocumentId)) {
443 throw new RiceIllegalArgumentException("originatingDocumentId was null or blank");
444 }
445 List<org.kuali.rice.kew.documentlink.DocumentLink> documentLinkBos = KEWServiceLocator.getDocumentLinkService().getLinkedDocumentsByDocId(originatingDocumentId);
446 if (documentLinkBos == null || documentLinkBos.isEmpty()) {
447 return Collections.emptyList();
448 }
449 List<DocumentLink> deletedDocumentLinks = new ArrayList<DocumentLink>();
450 for (org.kuali.rice.kew.documentlink.DocumentLink documentLinkBo : documentLinkBos) {
451 deletedDocumentLinks.add(org.kuali.rice.kew.documentlink.DocumentLink.to(documentLinkBo));
452 KEWServiceLocator.getDocumentLinkService().deleteDocumentLink(documentLinkBo);
453 }
454 return Collections.unmodifiableList(deletedDocumentLinks);
455 }
456
457 @Override
458 public List<DocumentLink> getOutgoingDocumentLinks(String originatingDocumentId) throws RiceIllegalArgumentException {
459 if (StringUtils.isBlank(originatingDocumentId)) {
460 throw new RiceIllegalArgumentException("originatingDocumentId was null or blank");
461 }
462 List<org.kuali.rice.kew.documentlink.DocumentLink> outgoingDocumentLinkBos = KEWServiceLocator.getDocumentLinkService().getLinkedDocumentsByDocId(originatingDocumentId);
463 List<DocumentLink> outgoingDocumentLinks = new ArrayList<DocumentLink>();
464 for (org.kuali.rice.kew.documentlink.DocumentLink outgoingDocumentLinkBo : outgoingDocumentLinkBos) {
465 outgoingDocumentLinks.add(org.kuali.rice.kew.documentlink.DocumentLink.to(outgoingDocumentLinkBo));
466 }
467 return Collections.unmodifiableList(outgoingDocumentLinks);
468 }
469
470 @Override
471 public List<DocumentLink> getIncomingDocumentLinks(String destinationDocumentId) throws RiceIllegalArgumentException {
472 if (StringUtils.isBlank(destinationDocumentId)) {
473 throw new RiceIllegalArgumentException("destinationDocumentId was null or blank");
474 }
475 List<org.kuali.rice.kew.documentlink.DocumentLink> incomingDocumentLinkBos = KEWServiceLocator.getDocumentLinkService().getOutgoingLinkedDocumentsByDocId(destinationDocumentId);
476 List<DocumentLink> incomingDocumentLinks = new ArrayList<DocumentLink>();
477 for (org.kuali.rice.kew.documentlink.DocumentLink incomingDocumentLinkBo : incomingDocumentLinkBos) {
478 incomingDocumentLinks.add(org.kuali.rice.kew.documentlink.DocumentLink.to(incomingDocumentLinkBo));
479 }
480 return Collections.unmodifiableList(incomingDocumentLinks);
481 }
482
483 @Override
484 public DocumentLink getDocumentLink(String documentLinkId) throws RiceIllegalArgumentException {
485 if (StringUtils.isBlank(documentLinkId)) {
486 throw new RiceIllegalArgumentException("documentLinkId was null or blank");
487 }
488 org.kuali.rice.kew.documentlink.DocumentLink documentLinkBo = KEWServiceLocator.getDocumentLinkService().getDocumentLink(Long.valueOf(documentLinkId));
489 return org.kuali.rice.kew.documentlink.DocumentLink.to(documentLinkBo);
490 }
491
492 }