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.PersistenceContext;
26 import javax.persistence.TypedQuery;
27 import java.util.List;
28
29
30
31
32
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
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
72 saveIfNotExists(createReverseLink(link));
73 getDataObjectService().flush(DocumentLink.class);
74 return link;
75 }
76
77 protected DocumentLink saveIfNotExists(DocumentLink link) {
78
79
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 }