1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
30
31
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
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
69 saveIfNotExists(createReverseLink(link));
70 getDataObjectService().flush(DocumentLink.class);
71 return link;
72 }
73
74 protected DocumentLink saveIfNotExists(DocumentLink link) {
75
76
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 }