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 super();
47 this.entityManager = entityManager;
48 this.businessObjectDao = businessObjectDao;
49 this.documentAdHocService = documentAdHocService;
50 }
51
52
53
54
55 @Override
56 public <T extends Document> T save(T document) throws DataAccessException {
57 T attachedDoc = (T) findByDocumentHeaderId(document.getClass(),document.getDocumentNumber());
58 if (attachedDoc == null) {
59 entityManager.persist(document.getDocumentHeader());
60 entityManager.persist(document);
61 return document;
62 }
63 OrmUtils.reattach(document, attachedDoc);
64 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 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 Criteria criteria = new Criteria(clazz.getName());
89 criteria.in(KRADPropertyConstants.DOCUMENT_NUMBER, idList);
90 List<T> list = new ArrayList<T>();
91 try {
92 list = new QueryByCriteria(entityManager, criteria).toQuery().getResultList();
93 } catch (PersistenceException e) {
94 e.printStackTrace();
95 }
96 for (T doc : list) {
97 documentAdHocService.addAdHocs(doc);
98 entityManager.refresh(doc);
99 }
100 return list;
101 }
102
103 @Override
104 public BusinessObjectDao getBusinessObjectDao() {
105 return businessObjectDao;
106 }
107
108 public void setBusinessObjectDao(BusinessObjectDao businessObjectDao) {
109 this.businessObjectDao = businessObjectDao;
110 }
111
112
113
114
115 public EntityManager getEntityManager() {
116 return this.entityManager;
117 }
118
119
120
121
122 public void setEntityManager(EntityManager entityManager) {
123 this.entityManager = entityManager;
124 }
125
126
127
128
129 @Override
130 public DocumentAdHocService getDocumentAdHocService() {
131 return this.documentAdHocService;
132 }
133
134
135
136
137
138 public void setDocumentAdHocService(DocumentAdHocService dahs) {
139 this.documentAdHocService = dahs;
140 }
141
142 }