View Javadoc

1   /**
2    * Copyright 2005-2013 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.PersistenceContext;
26  import javax.persistence.TypedQuery;
27  import java.util.List;
28  
29  /**
30   * A JPA-based implementation of the {@link DocumentLinkDAO}.
31   * 
32   * @author Kuali Rice Team (rice.collab@kuali.org)
33   */
34  public class DocumentLinkDAOJpa implements DocumentLinkDAO {
35  
36  	
37      @PersistenceContext(unitName = "kew")
38      private EntityManager entityManager;
39      private DataObjectService dataObjectService;
40  
41      @Override
42  	public void deleteDocumentLink(DocumentLink link) {
43          getDataObjectService().delete(link);
44          // see if a reverse link exists or not
45          DocumentLink reverseLink = getLinkedDocument(link.getDestDocId(), link.getOrgnDocId());
46          if (reverseLink != null) {
47              getDataObjectService().delete(reverseLink);
48          }
49  	}
50  
51      @Override
52  	public List<DocumentLink> getLinkedDocumentsByDocId(String docId) {
53          TypedQuery<DocumentLink> query =
54                  getEntityManager().createNamedQuery("DocumentLink.GetLinkedDocumentsByDocId", DocumentLink.class);
55          query.setParameter("orgnDocId",docId);
56          return query.getResultList();
57  	
58  	}
59  
60      @Override
61  	public List<DocumentLink> getOutgoingLinkedDocumentsByDocId(String docId) {
62          TypedQuery<DocumentLink> query =
63                  getEntityManager().createNamedQuery("DocumentLink.GetOutgoingLinkedDocumentsByDocId", DocumentLink.class);
64          query.setParameter("destDocId",docId);
65          return query.getResultList();
66  	}
67  
68      @Override
69  	public DocumentLink saveDocumentLink(DocumentLink link) {
70          link = saveIfNotExists(link);
71          // create the 2-way linked pair
72          saveIfNotExists(createReverseLink(link));
73          getDataObjectService().flush(DocumentLink.class);
74          return link;
75  	}
76  
77      protected DocumentLink saveIfNotExists(DocumentLink link) {
78          // if an existing link already exists for this, we pretty much just ignore the request to save since it's
79          // already there
80          DocumentLink existingLink = getLinkedDocument(link.getOrgnDocId(), link.getDestDocId());
81          if (existingLink == null) {
82              link = getDataObjectService().save(link);
83          } else {
84              link = existingLink;
85          }
86          return link;
87      }
88  
89      protected DocumentLink getLinkedDocument(String orgnDocId, String destDocId) {
90          TypedQuery<DocumentLink> query =
91                  getEntityManager().createNamedQuery("DocumentLink.GetLinkedDocument", DocumentLink.class);
92          query.setParameter("orgnDocId", orgnDocId);
93          query.setParameter("destDocId", destDocId);
94          try {
95              return query.getSingleResult();
96          } catch (NoResultException e) {
97              return null;
98          }
99      }
100 
101 
102     private DocumentLink createReverseLink(DocumentLink link) {
103         DocumentLink reverseLink = new DocumentLink();
104         reverseLink.setOrgnDocId(link.getDestDocId());
105         reverseLink.setDestDocId(link.getOrgnDocId());
106         return reverseLink;
107     }
108 
109 	@Override
110 	public DocumentLink getDocumentLink(String documentLinkId) {
111 		return getDataObjectService().find(DocumentLink.class,documentLinkId);
112 	}
113 
114 
115     public DataObjectService getDataObjectService() {
116         return dataObjectService;
117     }
118 
119     @Required
120     public void setDataObjectService(DataObjectService dataObjectService) {
121         this.dataObjectService = dataObjectService;
122     }
123 
124 
125     public EntityManager getEntityManager() {
126         return this.entityManager;
127     }
128 
129     public void setEntityManager(EntityManager entityManager) {
130         this.entityManager = entityManager;
131     }
132 	
133 }