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 java.util.List;
19
20 import javax.persistence.EntityManager;
21 import javax.persistence.NoResultException;
22 import javax.persistence.PersistenceContext;
23
24 import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
25 import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria;
26 import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria;
27 import org.kuali.rice.kew.documentlink.DocumentLink;
28 import org.kuali.rice.kew.documentlink.dao.DocumentLinkDAO;
29 import org.kuali.rice.krad.util.ObjectUtils;
30
31
32
33
34
35
36
37 public class DocumentLinkDAOJpaImpl implements DocumentLinkDAO {
38
39
40 @PersistenceContext(unitName = "kew-unit")
41 private EntityManager entityManager;
42
43 public EntityManager getEntityManager() {
44 return this.entityManager;
45 }
46
47 public void setEntityManager(EntityManager entityManager) {
48 this.entityManager = entityManager;
49 }
50
51
52
53
54
55
56 public void deleteDocmentLinksByDocId(String docId) {
57 List<DocumentLink> links = getLinkedDocumentsByDocId(docId);
58 for(DocumentLink link: links){
59 deleteDocumentLink(link);
60 }
61 }
62
63
64
65
66
67
68 public void deleteDocumentLink(DocumentLink link) {
69 deleteSingleLinkFromOrgnDoc(link);
70 deleteSingleLinkFromOrgnDoc(DocumentLinkDaoUtil.reverseLink((DocumentLink)ObjectUtils.deepCopy(link)));
71 }
72
73
74
75
76
77
78 public DocumentLink getLinkedDocument(DocumentLink link) {
79 Criteria crit = new Criteria(DocumentLink.class.getName());
80 crit.eq("orgnDocId", link.getOrgnDocId());
81 crit.eq("destDocId", link.getDestDocId());
82 try {
83 return (DocumentLink) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
84 } catch (NoResultException e) {
85 return null;
86 }
87 }
88
89
90
91
92
93
94 public List<DocumentLink> getLinkedDocumentsByDocId(String docId) {
95 Criteria crit = new Criteria(DocumentLink.class.getName());
96 crit.eq("orgnDocId", docId);
97 return (List<DocumentLink>) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
98
99 }
100
101 public List<DocumentLink> getOutgoingLinkedDocumentsByDocId(String docId) {
102 Criteria crit = new Criteria(DocumentLink.class.getName());
103 crit.eq("destDocId", docId);
104 return (List<DocumentLink>) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
105 }
106
107
108
109
110
111
112 public void saveDocumentLink(DocumentLink link) {
113 DocumentLink linkedDocument = getLinkedDocument(link);
114 if(linkedDocument == null) {
115 if (link.getDocLinkId() == null) {
116 entityManager.persist(link);
117 } else {
118 OrmUtils.merge(entityManager, link);
119 }
120 } else {
121 link.setDocLinkId(linkedDocument.getDocLinkId());
122 }
123
124 DocumentLink rLink = DocumentLinkDaoUtil.reverseLink((DocumentLink)ObjectUtils.deepCopy(link));
125 if(getLinkedDocument(rLink) == null) {
126 if (link.getDocLinkId() == null) {
127 entityManager.persist(rLink);
128 } else {
129 OrmUtils.merge(entityManager, rLink);
130 }
131 }
132
133 }
134
135 private void deleteSingleLinkFromOrgnDoc(DocumentLink link){
136 DocumentLink cur = getLinkedDocument(link);
137 entityManager.remove(cur) ;
138 }
139
140 @Override
141 public DocumentLink getDocumentLink(Long documentLinkId) {
142 Criteria crit = new Criteria(DocumentLink.class.getName());
143 crit.eq("docLinkId", documentLinkId);
144 return (DocumentLink) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
145 }
146
147 }