1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.krad.dao.impl; |
17 | |
|
18 | |
import org.kuali.rice.core.framework.persistence.jpa.OrmUtils; |
19 | |
import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria; |
20 | |
import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria; |
21 | |
import org.kuali.rice.krad.dao.BusinessObjectDao; |
22 | |
import org.kuali.rice.krad.dao.DocumentDao; |
23 | |
import org.kuali.rice.krad.document.Document; |
24 | |
import org.kuali.rice.krad.service.DocumentAdHocService; |
25 | |
import org.kuali.rice.krad.util.KRADPropertyConstants; |
26 | |
import org.springframework.dao.DataAccessException; |
27 | |
|
28 | |
import javax.persistence.EntityManager; |
29 | |
import javax.persistence.PersistenceContext; |
30 | |
import javax.persistence.PersistenceException; |
31 | |
import java.util.ArrayList; |
32 | |
import java.util.List; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public class DocumentDaoJpa implements DocumentDao { |
38 | |
|
39 | |
private BusinessObjectDao businessObjectDao; |
40 | |
private DocumentAdHocService documentAdHocService; |
41 | |
|
42 | |
@PersistenceContext |
43 | |
private EntityManager entityManager; |
44 | |
|
45 | |
public DocumentDaoJpa(EntityManager entityManager, BusinessObjectDao businessObjectDao, DocumentAdHocService documentAdHocService) { |
46 | 0 | super(); |
47 | 0 | this.entityManager = entityManager; |
48 | 0 | this.businessObjectDao = businessObjectDao; |
49 | 0 | this.documentAdHocService = documentAdHocService; |
50 | 0 | } |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
@Override |
56 | |
public <T extends Document> T save(T document) throws DataAccessException { |
57 | 0 | T attachedDoc = (T) findByDocumentHeaderId(document.getClass(),document.getDocumentNumber()); |
58 | 0 | if (attachedDoc == null) { |
59 | 0 | entityManager.persist(document.getDocumentHeader()); |
60 | 0 | entityManager.persist(document); |
61 | 0 | return document; |
62 | |
} |
63 | 0 | OrmUtils.reattach(document, attachedDoc); |
64 | 0 | return entityManager.merge(attachedDoc); |
65 | |
} |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
@Override |
75 | |
public <T extends Document> T findByDocumentHeaderId(Class<T> clazz, String id) { |
76 | 0 | return entityManager.find(clazz, id); |
77 | |
} |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
@Override |
87 | |
public <T extends Document> List<T> findByDocumentHeaderIds(Class<T> clazz, List<String> idList) { |
88 | 0 | Criteria criteria = new Criteria(clazz.getName()); |
89 | 0 | criteria.in(KRADPropertyConstants.DOCUMENT_NUMBER, idList); |
90 | 0 | List<T> list = new ArrayList<T>(); |
91 | |
try { |
92 | 0 | list = new QueryByCriteria(entityManager, criteria).toQuery().getResultList(); |
93 | 0 | } catch (PersistenceException e) { |
94 | 0 | e.printStackTrace(); |
95 | 0 | } |
96 | 0 | for (T doc : list) { |
97 | 0 | documentAdHocService.addAdHocs(doc); |
98 | 0 | entityManager.refresh(doc); |
99 | |
} |
100 | 0 | return list; |
101 | |
} |
102 | |
|
103 | |
@Override |
104 | |
public BusinessObjectDao getBusinessObjectDao() { |
105 | 0 | return businessObjectDao; |
106 | |
} |
107 | |
|
108 | |
public void setBusinessObjectDao(BusinessObjectDao businessObjectDao) { |
109 | 0 | this.businessObjectDao = businessObjectDao; |
110 | 0 | } |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
public EntityManager getEntityManager() { |
116 | 0 | return this.entityManager; |
117 | |
} |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
public void setEntityManager(EntityManager entityManager) { |
123 | 0 | this.entityManager = entityManager; |
124 | 0 | } |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
@Override |
130 | |
public DocumentAdHocService getDocumentAdHocService() { |
131 | 0 | return this.documentAdHocService; |
132 | |
} |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
public void setDocumentAdHocService(DocumentAdHocService dahs) { |
139 | 0 | this.documentAdHocService = dahs; |
140 | 0 | } |
141 | |
|
142 | |
} |