001/**
002 * Copyright 2005-2017 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kew.impl.document;
017
018import org.apache.commons.lang.StringUtils;
019import org.apache.log4j.Logger;
020import org.joda.time.DateTime;
021import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
022import org.kuali.rice.core.api.exception.RiceIllegalStateException;
023import org.kuali.rice.kew.actionrequest.ActionRequestValue;
024import org.kuali.rice.kew.actiontaken.ActionTakenValue;
025import org.kuali.rice.kew.api.action.ActionRequest;
026import org.kuali.rice.kew.api.action.ActionTaken;
027import org.kuali.rice.kew.api.document.Document;
028import org.kuali.rice.kew.api.document.DocumentContent;
029import org.kuali.rice.kew.api.document.DocumentDetail;
030import org.kuali.rice.kew.api.document.DocumentLink;
031import org.kuali.rice.kew.api.document.DocumentStatus;
032import org.kuali.rice.kew.api.document.WorkflowDocumentService;
033import org.kuali.rice.kew.api.document.node.RouteNodeInstance;
034import org.kuali.rice.kew.api.document.search.DocumentSearchCriteria;
035import org.kuali.rice.kew.api.document.search.DocumentSearchResults;
036import org.kuali.rice.kew.doctype.bo.DocumentType;
037import org.kuali.rice.kew.dto.DTOConverter;
038import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
039import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValueContent;
040import org.kuali.rice.kew.routeheader.DocumentStatusTransition;
041import org.kuali.rice.kew.service.KEWServiceLocator;
042
043import javax.jws.WebParam;
044import java.math.BigDecimal;
045import java.sql.Timestamp;
046import java.util.ArrayList;
047import java.util.Collection;
048import java.util.Collections;
049import java.util.List;
050
051/**
052 * 
053 * @author Kuali Rice Team (rice.collab@kuali.org)
054 *
055 */
056public class WorkflowDocumentServiceImpl implements WorkflowDocumentService {
057
058        private static final Logger LOG = Logger.getLogger(WorkflowDocumentServiceImpl.class);
059        
060        @Override
061        public Document getDocument(String documentId) {
062                if (StringUtils.isBlank(documentId)) {
063                        throw new RiceIllegalArgumentException("documentId was blank or null");
064                }
065                DocumentRouteHeaderValue documentBo = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentId);
066                return DocumentRouteHeaderValue.to(documentBo);
067        }
068
069        @Override
070        public boolean doesDocumentExist(String documentId) {
071            if (StringUtils.isBlank(documentId)) {
072            throw new RiceIllegalArgumentException("documentId was blank or null");
073        }
074            DocumentRouteHeaderValue documentBo = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentId);
075            return documentBo != null;
076        }
077
078    @Override
079    public String getDocumentTypeName(String documentId) {
080        if (StringUtils.isBlank(documentId)) {
081            throw new RiceIllegalArgumentException("documentId was blank or null");
082        }
083        DocumentType documentType = KEWServiceLocator.getDocumentTypeService().findByDocumentId(documentId);
084        if (documentType == null) {
085            throw new RiceIllegalArgumentException("Failed to determine document type name for document with id "
086                    + documentId
087                    + ". Perhaps document does not exist?");
088        }
089        return documentType.getName();
090    }
091
092
093    @Override
094    public DocumentDetail getDocumentDetailByAppId(String documentTypeName, String appId) {
095        if (StringUtils.isEmpty(documentTypeName)) {
096            throw new RiceIllegalArgumentException("documentTypeName was blank or null");
097        }
098        if (StringUtils.isEmpty(appId)) {
099            throw new RiceIllegalArgumentException("appId was blank or null");
100        }
101
102        Collection documentIds = KEWServiceLocator.getRouteHeaderService().findByDocTypeAndAppId(documentTypeName, appId);
103        if(documentIds==null||documentIds.isEmpty()){
104            throw new RiceIllegalStateException("No RouteHeader Ids found for documentTypName: " + documentTypeName + ", appId: " + appId);
105        }
106        if(documentIds.size()>1){
107            throw new RiceIllegalStateException("Multiple RouteHeader Ids found for documentTypName: " + documentTypeName + ", appId: " + appId);
108                }
109
110        return getDocumentDetail((String)documentIds.iterator().next());
111        }
112
113    public RouteNodeInstance getRouteNodeInstance(String nodeInstanceId)  {
114        if (StringUtils.isEmpty(nodeInstanceId)) {
115            throw new RiceIllegalArgumentException("nodeInstanceId was blank or null");
116        }
117        if ( LOG.isDebugEnabled() ) {
118                LOG.debug("Fetching RouteNodeInstanceVO [id="+nodeInstanceId+"]");
119        }
120        org.kuali.rice.kew.engine.node.RouteNodeInstance nodeInstance = KEWServiceLocator.getRouteNodeService().findRouteNodeInstanceById(nodeInstanceId);
121        return org.kuali.rice.kew.engine.node.RouteNodeInstance.to(nodeInstance);
122    }
123
124    @Override
125    public DocumentStatus getDocumentStatus(String documentId) {
126        if (StringUtils.isEmpty(documentId)) {
127            throw new RiceIllegalArgumentException("documentId was blank or null");
128        }
129        String documentStatus = KEWServiceLocator.getRouteHeaderService().getDocumentStatus(documentId);
130        if (StringUtils.isEmpty(documentStatus)) {
131            throw new RiceIllegalStateException("DocumentStatus not found for documentId: " + documentId);
132        }
133        return DocumentStatus.fromCode(documentStatus);
134    }
135
136    @Override
137    public String getApplicationDocumentId(String documentId) {
138        if (StringUtils.isEmpty(documentId)) {
139            throw new RiceIllegalArgumentException("documentId was blank or null");
140        }
141                return KEWServiceLocator.getRouteHeaderService().getAppDocId(documentId);
142        }
143
144    @Override
145    public String getApplicationDocumentStatus(String documentId)
146            throws RiceIllegalArgumentException {
147        if (StringUtils.isEmpty(documentId)) {
148            throw new RiceIllegalArgumentException("documentId was blank or null");
149        }
150        return KEWServiceLocator.getRouteHeaderService().getAppDocStatus(documentId);
151    }
152
153    @Override
154    public DocumentSearchResults documentSearch(String principalId, DocumentSearchCriteria criteria) {
155        if (criteria == null) {
156            throw new RiceIllegalArgumentException("criteria was null");
157        }
158        return KEWServiceLocator.getDocumentSearchService().lookupDocuments(principalId, criteria);
159    }
160
161    @Override
162    public DocumentSearchResults documentSearchSaveable(String principalId, DocumentSearchCriteria criteria, boolean saveSearch) {
163        if (criteria == null) {
164            throw new RiceIllegalArgumentException("criteria was null");
165        }
166        return KEWServiceLocator.getDocumentSearchService().lookupDocuments(principalId, criteria, saveSearch);
167    }
168
169    @Override
170    public List<String> getSearchableAttributeStringValuesByKey(String documentId, String key) {
171        if (StringUtils.isEmpty(documentId)) {
172            throw new RiceIllegalArgumentException("documentId was blank or null");
173        }
174        if (StringUtils.isEmpty(key)) {
175            throw new RiceIllegalArgumentException("key was blank or null");
176        }
177                return KEWServiceLocator.getRouteHeaderService().getSearchableAttributeStringValuesByKey(documentId, key);
178        }
179
180    @Override
181        public List<DateTime> getSearchableAttributeDateTimeValuesByKey(String documentId, String key) {
182                if (StringUtils.isEmpty(documentId)) {
183            throw new RiceIllegalArgumentException("documentId was blank or null");
184        }
185        if (StringUtils.isEmpty(key)) {
186            throw new RiceIllegalArgumentException("key was blank or null");
187        }
188
189        List<Timestamp> results = KEWServiceLocator.getRouteHeaderService().getSearchableAttributeDateTimeValuesByKey(documentId, key);
190        if (results == null) {
191            return null;
192        }
193        List<DateTime> dateTimes = new ArrayList<DateTime>();
194
195                for(Timestamp time : results) {
196            dateTimes.add(new DateTime(time.getTime()));
197        }
198        return dateTimes;
199        }
200
201    @Override
202        public List<BigDecimal> getSearchableAttributeFloatValuesByKey(String documentId, String key) {
203        if (StringUtils.isEmpty(documentId)) {
204            throw new RiceIllegalArgumentException("documentId was blank or null");
205        }
206        if (StringUtils.isEmpty(key)) {
207            throw new RiceIllegalArgumentException("key was blank or null");
208        }
209                return KEWServiceLocator.getRouteHeaderService().getSearchableAttributeFloatValuesByKey(documentId, key);
210        }
211
212    @Override
213    public List<Long> getSearchableAttributeLongValuesByKey(String documentId, String key) {
214        if (StringUtils.isEmpty(documentId)) {
215            throw new RiceIllegalArgumentException("documentId was blank or null");
216        }
217        if (StringUtils.isEmpty(key)) {
218            throw new RiceIllegalArgumentException("key was blank or null");
219        }
220                return KEWServiceLocator.getRouteHeaderService().getSearchableAttributeLongValuesByKey(documentId, key);
221        }
222        
223        @Override
224        public DocumentContent getDocumentContent(String documentId) {
225                if (StringUtils.isBlank(documentId)) {
226                        throw new RiceIllegalArgumentException("documentId was blank or null");
227                }
228                DocumentRouteHeaderValueContent content = KEWServiceLocator.getRouteHeaderService().getContent(documentId);
229                return DocumentRouteHeaderValueContent.to(content);
230        }
231
232        @Override
233        public List<ActionRequest> getRootActionRequests(String documentId) {
234        if (StringUtils.isBlank(documentId)) {
235                        throw new RiceIllegalArgumentException("documentId was blank or null");
236                }
237                List<ActionRequest> actionRequests = new ArrayList<ActionRequest>();
238                List<ActionRequestValue> actionRequestBos = KEWServiceLocator.getActionRequestService().findAllRootActionRequestsByDocumentId(documentId);
239                for (ActionRequestValue actionRequestBo : actionRequestBos) {
240                        actionRequests.add(ActionRequestValue.to(actionRequestBo));
241                }
242                return Collections.unmodifiableList(actionRequests);
243        }
244        
245        @Override
246        public List<ActionRequest> getPendingActionRequests(String documentId) {
247                if (StringUtils.isBlank(documentId)) {
248                        throw new RiceIllegalArgumentException("documentId was blank or null");
249                }
250                List<ActionRequest> actionRequests = new ArrayList<ActionRequest>();
251                List<ActionRequestValue> actionRequestBos = KEWServiceLocator.getActionRequestService().findAllPendingRequests(documentId);
252                for (ActionRequestValue actionRequestBo : actionRequestBos) {
253                        actionRequests.add(ActionRequestValue.to(actionRequestBo));
254                }
255                return Collections.unmodifiableList(actionRequests);
256        }
257        
258        @Override
259        public List<ActionRequest> getActionRequestsForPrincipalAtNode(String documentId, String nodeName,
260            String principalId) {
261        if (StringUtils.isBlank(documentId)) {
262                throw new RiceIllegalArgumentException("documentId was null or blank");
263        }
264        if ( LOG.isDebugEnabled() ) {
265                LOG.debug("Fetching ActionRequests [docId="+documentId+", nodeName="+nodeName+", principalId="+principalId+"]");
266        }
267        List<ActionRequestValue> actionRequestBos = KEWServiceLocator.getActionRequestService().findAllActionRequestsByDocumentId(documentId);
268        List<ActionRequestValue> matchingActionRequests = new ArrayList<ActionRequestValue>();
269        for (ActionRequestValue actionRequestValue : actionRequestBos) {
270            if (actionRequestMatches(actionRequestValue, nodeName, principalId)) {
271                matchingActionRequests.add(actionRequestValue);
272            }
273        }
274        List<ActionRequest> actionRequests = new ArrayList<ActionRequest>(matchingActionRequests.size());
275        for (ActionRequestValue matchingActionRequest : matchingActionRequests) {
276                actionRequests.add(ActionRequestValue.to(matchingActionRequest));
277        }
278        return actionRequests;
279    }
280        
281    protected boolean actionRequestMatches(ActionRequestValue actionRequest, String nodeName, String principalId) {
282        boolean matchesUserId = true;  // assume a match in case user is empty
283        boolean matchesNodeName = true;  // assume a match in case node name is empty
284        if (StringUtils.isNotBlank(nodeName)) {
285            matchesNodeName = nodeName.equals(actionRequest.getPotentialNodeName());
286        }
287        if (principalId != null) {
288            matchesUserId = actionRequest.isRecipientRoutedRequest(principalId);
289        }
290        return matchesNodeName && matchesUserId;
291    }
292
293
294        @Override
295        public List<ActionTaken> getActionsTaken(String documentId) {
296        if (StringUtils.isEmpty(documentId)) {
297            throw new RiceIllegalArgumentException("documentId is null or empty.");
298        }
299                List<ActionTaken> actionTakens = new ArrayList<ActionTaken>();
300                Collection<ActionTakenValue> actionTakenBos = KEWServiceLocator.getActionTakenService().findByDocumentId(documentId);
301                for (ActionTakenValue actionTakenBo : actionTakenBos) {
302                        actionTakens.add(ActionTakenValue.to(actionTakenBo));
303                }
304                return actionTakens;
305        }
306
307    @Override
308    public List<ActionTaken> _getActionsTaken(String documentId) {
309        return getActionsTaken(documentId);
310    }
311
312    @Override
313    public List<ActionTaken> getAllActionsTaken(String documentId){
314        if(StringUtils.isEmpty(documentId)){
315            throw new RiceIllegalArgumentException("documentId is null or empty.");
316        }
317
318                List<ActionTaken> actionsTaken = new ArrayList<ActionTaken>();
319        Collection<ActionTakenValue> actionTakenBos = KEWServiceLocator.getActionTakenService().findByDocumentIdIgnoreCurrentInd(documentId);
320                for (ActionTakenValue actionTakenBo : actionTakenBos) {
321                        actionsTaken.add(ActionTakenValue.to(actionTakenBo));
322                }
323       return actionsTaken;
324    }
325        
326        @Override
327        public DocumentDetail getDocumentDetail(@WebParam(name = "documentId") String documentId) {
328                if (StringUtils.isBlank(documentId)) {
329            throw new RiceIllegalArgumentException("documentId was null or blank");
330        }
331        if ( LOG.isDebugEnabled() ) {
332                LOG.debug("Fetching DocumentDetail [id="+documentId+"]");
333        }
334        DocumentRouteHeaderValue document = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentId);
335        if (document == null) {
336                return null;
337        }
338        DocumentDetail documentDetailVO = DTOConverter.convertDocumentDetailNew(document);
339        if ( LOG.isDebugEnabled() ) {
340                LOG.debug("Returning DocumentDetailVO [id=" + documentId + "]");
341        }
342        return documentDetailVO;
343        }
344
345    @Override
346    public List<org.kuali.rice.kew.api.document.DocumentStatusTransition> getDocumentStatusTransitionHistory(String documentId) {
347                if (StringUtils.isBlank(documentId)) {
348            throw new RiceIllegalArgumentException("documentId was null or blank");
349        }
350        if ( LOG.isDebugEnabled() ) {
351            LOG.debug("Fetching document status transition history [id="+documentId+"]");
352        }
353        DocumentRouteHeaderValue document = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentId);;
354
355        List<DocumentStatusTransition> list = document.getAppDocStatusHistory();
356
357        List<org.kuali.rice.kew.api.document.DocumentStatusTransition> transitionHistory = new ArrayList<org.kuali.rice.kew.api.document.DocumentStatusTransition>(list.size());
358
359        for (DocumentStatusTransition transition : list) {
360            transitionHistory.add(DocumentStatusTransition.to(transition));
361        }
362        return transitionHistory;
363    }
364        
365        @Override
366        public List<RouteNodeInstance> getRouteNodeInstances(String documentId) {
367        if (StringUtils.isBlank(documentId)) {
368            throw new RiceIllegalArgumentException("documentId was null or blank");
369        }
370
371        if ( LOG.isDebugEnabled() ) {
372                LOG.debug("Fetching RouteNodeInstances [documentId=" + documentId + "]");
373        }
374        DocumentRouteHeaderValue documentBo = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentId);
375        if (documentBo == null) {
376                return Collections.emptyList();
377        }
378        return convertRouteNodeInstances(KEWServiceLocator.getRouteNodeService().getFlattenedNodeInstances(documentBo, true));
379    }   
380        
381        @Override
382        public List<RouteNodeInstance> getActiveRouteNodeInstances(String documentId) {
383                if (StringUtils.isBlank(documentId)) {
384            throw new RiceIllegalArgumentException("documentId was null or blank");
385        }
386
387        if ( LOG.isDebugEnabled() ) {
388                LOG.debug("Fetching active RouteNodeInstances [documentId=" + documentId + "]");
389        }
390        return convertRouteNodeInstances(KEWServiceLocator.getRouteNodeService().getActiveNodeInstances(documentId));
391        }
392
393    @Override
394    public List<RouteNodeInstance> getTerminalRouteNodeInstances(String documentId) {
395        if (StringUtils.isBlank(documentId)) {
396            throw new RiceIllegalArgumentException("documentId was null or blank");
397        }
398
399        if ( LOG.isDebugEnabled() ) {
400                LOG.debug("Fetching terminal RouteNodeInstanceVOs [docId=" + documentId + "]");
401        }
402        return convertRouteNodeInstances(KEWServiceLocator.getRouteNodeService().getTerminalNodeInstances(documentId));
403    }
404
405    public List<RouteNodeInstance> getCurrentRouteNodeInstances(String documentId) {
406        if (StringUtils.isBlank(documentId)) {
407            throw new RiceIllegalArgumentException("documentId was null or blank");
408        }
409
410        if ( LOG.isDebugEnabled() ) {
411                LOG.debug("Fetching current RouteNodeInstanceVOs [docId=" + documentId + "]");
412        }
413        return convertRouteNodeInstances(KEWServiceLocator.getRouteNodeService().getCurrentNodeInstances(documentId));
414    }
415    
416    public List<String> getActiveRouteNodeNames(String documentId) {
417        if (StringUtils.isBlank(documentId)) {
418            throw new RiceIllegalArgumentException("documentId was null or blank");
419        }
420        
421        final List<String> nodes = KEWServiceLocator.getRouteNodeService().getActiveRouteNodeNames(documentId);
422        return nodes != null ? Collections.unmodifiableList(nodes) : Collections.<String>emptyList();
423    }
424
425    public List<String> getActiveSimpleRouteNodeNames(String documentId) {
426        if (StringUtils.isBlank(documentId)) {
427            throw new RiceIllegalArgumentException("documentId was null or blank");
428        }
429
430        final List<String> nodes = KEWServiceLocator.getRouteNodeService().getActiveSimpleRouteNodeNames(documentId);
431        return nodes != null ? Collections.unmodifiableList(nodes) : Collections.<String>emptyList();
432    }
433    
434    public List<String> getTerminalRouteNodeNames(String documentId) {
435        if (StringUtils.isBlank(documentId)) {
436            throw new RiceIllegalArgumentException("documentId was null or blank");
437        }
438        
439        final List<String> nodes = KEWServiceLocator.getRouteNodeService().getTerminalRouteNodeNames(documentId);
440        return nodes != null ? Collections.unmodifiableList(nodes) : Collections.<String>emptyList();
441    }
442
443    public List<String> getCurrentRouteNodeNames(String documentId) {
444        if (StringUtils.isBlank(documentId)) {
445            throw new RiceIllegalArgumentException("documentId was null or blank");
446        }
447        
448        final List<String> nodes = KEWServiceLocator.getRouteNodeService().getCurrentRouteNodeNames(documentId);
449        return nodes != null ? Collections.unmodifiableList(nodes) : Collections.<String>emptyList();
450    }
451
452    public List<String> getCurrentSimpleRouteNodeNames(String documentId) {
453        if (StringUtils.isBlank(documentId)) {
454            throw new RiceIllegalArgumentException("documentId was null or blank");
455        }
456
457        final List<String> nodes = KEWServiceLocator.getRouteNodeService().getCurrentSimpleRouteNodeNames(documentId);
458        return nodes != null ? Collections.unmodifiableList(nodes) : Collections.<String>emptyList();
459    }
460
461
462    private List<RouteNodeInstance> convertRouteNodeInstances(List<org.kuali.rice.kew.engine.node.RouteNodeInstance> routeNodeInstanceBos) {
463                List<RouteNodeInstance> routeNodeInstances = new ArrayList<RouteNodeInstance>();
464        for (org.kuali.rice.kew.engine.node.RouteNodeInstance routeNodeInstanceBo : routeNodeInstanceBos) {
465                routeNodeInstances.add(org.kuali.rice.kew.engine.node.RouteNodeInstance.to(routeNodeInstanceBo));
466        }
467        return Collections.unmodifiableList(routeNodeInstances);
468        }
469        
470        @Override
471        public List<String> getPreviousRouteNodeNames(String documentId) {
472
473                if (StringUtils.isBlank(documentId)) {
474            throw new RiceIllegalArgumentException("documentId was null or blank");
475        }
476        if ( LOG.isDebugEnabled() ) {
477                        LOG.debug("Fetching previous node names [documentId=" + documentId + "]");
478                }
479        return new ArrayList<String>(KEWServiceLocator.getRouteNodeService().findPreviousNodeNames(documentId));
480        }
481
482    @Override
483    public List<String> getPrincipalIdsWithPendingActionRequestByActionRequestedAndDocId(String actionRequestedCd, String documentId){
484        if (StringUtils.isEmpty(actionRequestedCd)) {
485            throw new RiceIllegalArgumentException("actionRequestCd was blank or null");
486        }
487        if (StringUtils.isEmpty(documentId)) {
488            throw new RiceIllegalArgumentException("documentId was blank or null");
489        }
490        return KEWServiceLocator.getActionRequestService().
491                                getPrincipalIdsWithPendingActionRequestByActionRequestedAndDocId(actionRequestedCd, documentId);
492    }
493
494    @Override
495    public String getDocumentInitiatorPrincipalId(String documentId) {
496        if (StringUtils.isEmpty(documentId)) {
497            throw new RiceIllegalArgumentException("documentId was blank or null");
498        }
499
500        DocumentRouteHeaderValue header = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentId, false);
501        if ( header == null) {
502                return null;
503        }
504        return header.getInitiatorWorkflowId();
505    }
506
507    @Override
508    public String getRoutedByPrincipalIdByDocumentId(String documentId) {
509        if (StringUtils.isEmpty(documentId)) {
510            throw new RiceIllegalArgumentException("documentId was blank or null");
511        }
512
513        DocumentRouteHeaderValue header = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentId, false);
514        if ( header == null) {
515                return null;
516        }
517        return header.getRoutedByUserWorkflowId();
518    }
519
520        @Override
521        public DocumentLink addDocumentLink(DocumentLink documentLink) throws RiceIllegalArgumentException {
522                if (documentLink == null) {
523                        throw new RiceIllegalArgumentException("documentLink was null");
524                }
525                if (documentLink.getId() != null) {
526                        throw new RiceIllegalArgumentException("the given documentLink already has an id, cannot add a document link with an existing id");
527                }
528                org.kuali.rice.kew.documentlink.DocumentLink documentLinkBo = org.kuali.rice.kew.documentlink.DocumentLink.from(documentLink);
529                documentLinkBo = KEWServiceLocator.getDocumentLinkService().saveDocumentLink(documentLinkBo);
530                return org.kuali.rice.kew.documentlink.DocumentLink.to(documentLinkBo);
531        }
532
533        @Override
534        public DocumentLink deleteDocumentLink(String documentLinkId) throws RiceIllegalArgumentException {
535                if (StringUtils.isBlank(documentLinkId)) {
536                        throw new RiceIllegalArgumentException("documentLinkId was null or blank");
537                }
538                org.kuali.rice.kew.documentlink.DocumentLink documentLinkBo = KEWServiceLocator.getDocumentLinkService().getDocumentLink(documentLinkId);
539                if (documentLinkBo == null) {
540                        throw new RiceIllegalStateException("Failed to locate document link with the given documentLinkId: " + documentLinkId);
541                }
542                KEWServiceLocator.getDocumentLinkService().deleteDocumentLink(documentLinkBo);
543                return org.kuali.rice.kew.documentlink.DocumentLink.to(documentLinkBo);
544        }
545            
546        @Override
547        public List<DocumentLink> deleteDocumentLinksByDocumentId(String originatingDocumentId) throws RiceIllegalArgumentException {
548                if (StringUtils.isBlank(originatingDocumentId)) {
549                        throw new RiceIllegalArgumentException("originatingDocumentId was null or blank");
550                }
551                List<org.kuali.rice.kew.documentlink.DocumentLink> documentLinkBos = KEWServiceLocator.getDocumentLinkService().getLinkedDocumentsByDocId(originatingDocumentId);
552                if (documentLinkBos == null || documentLinkBos.isEmpty()) {
553                        return Collections.emptyList();
554                }
555                List<DocumentLink> deletedDocumentLinks = new ArrayList<DocumentLink>();
556                for (org.kuali.rice.kew.documentlink.DocumentLink documentLinkBo : documentLinkBos) {
557                        deletedDocumentLinks.add(org.kuali.rice.kew.documentlink.DocumentLink.to(documentLinkBo));
558                        KEWServiceLocator.getDocumentLinkService().deleteDocumentLink(documentLinkBo);
559                }
560                return Collections.unmodifiableList(deletedDocumentLinks);
561    }
562            
563        @Override
564        public List<DocumentLink> getOutgoingDocumentLinks(String originatingDocumentId) throws RiceIllegalArgumentException {
565                if (StringUtils.isBlank(originatingDocumentId)) {
566                        throw new RiceIllegalArgumentException("originatingDocumentId was null or blank");
567                }
568                List<org.kuali.rice.kew.documentlink.DocumentLink> outgoingDocumentLinkBos = KEWServiceLocator.getDocumentLinkService().getLinkedDocumentsByDocId(originatingDocumentId);
569                List<DocumentLink> outgoingDocumentLinks = new ArrayList<DocumentLink>();
570                for (org.kuali.rice.kew.documentlink.DocumentLink outgoingDocumentLinkBo : outgoingDocumentLinkBos) {
571                        outgoingDocumentLinks.add(org.kuali.rice.kew.documentlink.DocumentLink.to(outgoingDocumentLinkBo));
572                }
573                return Collections.unmodifiableList(outgoingDocumentLinks);
574    }
575        
576        @Override
577        public List<DocumentLink> getIncomingDocumentLinks(String destinationDocumentId) throws RiceIllegalArgumentException {
578                if (StringUtils.isBlank(destinationDocumentId)) {
579                        throw new RiceIllegalArgumentException("destinationDocumentId was null or blank");
580                }
581                List<org.kuali.rice.kew.documentlink.DocumentLink> incomingDocumentLinkBos = KEWServiceLocator.getDocumentLinkService().getOutgoingLinkedDocumentsByDocId(destinationDocumentId);
582                List<DocumentLink> incomingDocumentLinks = new ArrayList<DocumentLink>();
583                for (org.kuali.rice.kew.documentlink.DocumentLink incomingDocumentLinkBo : incomingDocumentLinkBos) {
584                        incomingDocumentLinks.add(org.kuali.rice.kew.documentlink.DocumentLink.to(incomingDocumentLinkBo));
585                }
586                return Collections.unmodifiableList(incomingDocumentLinks);
587    }
588            
589        @Override
590        public DocumentLink getDocumentLink(String documentLinkId) throws RiceIllegalArgumentException {
591                if (StringUtils.isBlank(documentLinkId)) {
592                        throw new RiceIllegalArgumentException("documentLinkId was null or blank");
593                }
594                org.kuali.rice.kew.documentlink.DocumentLink documentLinkBo = KEWServiceLocator.getDocumentLinkService().getDocumentLink(documentLinkId);
595                return org.kuali.rice.kew.documentlink.DocumentLink.to(documentLinkBo);
596    }
597        
598}