View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.documentlink.dao.impl;
17  
18  import org.kuali.rice.kew.documentlink.DocumentLink;
19  import org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO;
20  import org.kuali.rice.krad.data.DataObjectService;
21  import org.springframework.beans.factory.annotation.Required;
22  
23  import javax.persistence.EntityManager;
24  import javax.persistence.NoResultException;
25  import javax.persistence.TypedQuery;
26  import java.util.List;
27  
28  /**
29   * A JPA-based implementation of the {@link DocumentLinkDAO}.
30   *
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   */
33  public class DocumentLinkDAOJpa implements DocumentLinkDAO {
34  
35      private EntityManager entityManager;
36      private DataObjectService dataObjectService;
37  
38      @Override
39      public void deleteDocumentLink(DocumentLink link) {
40          getDataObjectService().delete(link);
41          // see if a reverse link exists or not
42          DocumentLink reverseLink = getLinkedDocument(link.getDestDocId(), link.getOrgnDocId());
43          if (reverseLink != null) {
44              getDataObjectService().delete(reverseLink);
45          }
46      }
47  
48      @Override
49      public List<DocumentLink> getLinkedDocumentsByDocId(String docId) {
50          TypedQuery<DocumentLink> query =
51                  getEntityManager().createNamedQuery("DocumentLink.GetLinkedDocumentsByDocId", DocumentLink.class);
52          query.setParameter("orgnDocId",docId);
53          return query.getResultList();
54  
55      }
56  
57      @Override
58      public List<DocumentLink> getOutgoingLinkedDocumentsByDocId(String docId) {
59          TypedQuery<DocumentLink> query =
60                  getEntityManager().createNamedQuery("DocumentLink.GetOutgoingLinkedDocumentsByDocId", DocumentLink.class);
61          query.setParameter("destDocId",docId);
62          return query.getResultList();
63      }
64  
65      @Override
66      public DocumentLink saveDocumentLink(DocumentLink link) {
67          link = saveIfNotExists(link);
68          // create the 2-way linked pair
69          saveIfNotExists(createReverseLink(link));
70          getDataObjectService().flush(DocumentLink.class);
71          return link;
72      }
73  
74      protected DocumentLink saveIfNotExists(DocumentLink link) {
75          // if an existing link already exists for this, we pretty much just ignore the request to save since it's
76          // already there
77          DocumentLink existingLink = getLinkedDocument(link.getOrgnDocId(), link.getDestDocId());
78          if (existingLink == null) {
79              link = getDataObjectService().save(link);
80          } else {
81              link = existingLink;
82          }
83          return link;
84      }
85  
86      protected DocumentLink getLinkedDocument(String orgnDocId, String destDocId) {
87          TypedQuery<DocumentLink> query =
88                  getEntityManager().createNamedQuery("DocumentLink.GetLinkedDocument", DocumentLink.class);
89          query.setParameter("orgnDocId", orgnDocId);
90          query.setParameter("destDocId", destDocId);
91          try {
92              return query.getSingleResult();
93          } catch (NoResultException e) {
94              return null;
95          }
96      }
97  
98  
99      private DocumentLink createReverseLink(DocumentLink link) {
100         DocumentLink reverseLink = new DocumentLink();
101         reverseLink.setOrgnDocId(link.getDestDocId());
102         reverseLink.setDestDocId(link.getOrgnDocId());
103         return reverseLink;
104     }
105 
106     @Override
107     public DocumentLink getDocumentLink(String documentLinkId) {
108         return getDataObjectService().find(DocumentLink.class,documentLinkId);
109     }
110 
111 
112     public DataObjectService getDataObjectService() {
113         return dataObjectService;
114     }
115 
116     @Required
117     public void setDataObjectService(DataObjectService dataObjectService) {
118         this.dataObjectService = dataObjectService;
119     }
120 
121 
122     public EntityManager getEntityManager() {
123         return this.entityManager;
124     }
125 
126     public void setEntityManager(EntityManager entityManager) {
127         this.entityManager = entityManager;
128     }
129 
130 }