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.kns.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 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public void saveDocumentLink(DocumentLink link) { |
107 | 0 | if(getLinkedDocument(link) == null) { |
108 | 0 | if (link.getDocLinkId() == null) { |
109 | 0 | entityManager.persist(link); |
110 | |
} else { |
111 | 0 | OrmUtils.merge(entityManager, link); |
112 | |
} |
113 | |
} |
114 | |
|
115 | 0 | DocumentLink rLink = DocumentLinkDaoUtil.reverseLink((DocumentLink)ObjectUtils.deepCopy(link)); |
116 | 0 | if(getLinkedDocument(rLink) == null) { |
117 | 0 | if (link.getDocLinkId() == null) { |
118 | 0 | entityManager.persist(rLink); |
119 | |
} else { |
120 | 0 | OrmUtils.merge(entityManager, rLink); |
121 | |
} |
122 | |
} |
123 | |
|
124 | 0 | } |
125 | |
|
126 | |
private void deleteSingleLinkFromOrgnDoc(DocumentLink link){ |
127 | 0 | DocumentLink cur = getLinkedDocument(link); |
128 | 0 | entityManager.remove(cur) ; |
129 | 0 | } |
130 | |
|
131 | |
} |