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