1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.dao.proxy;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.config.ConfigurationException;
20 import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
21 import org.kuali.rice.krad.bo.BusinessObject;
22 import org.kuali.rice.krad.bo.ModuleConfiguration;
23 import org.kuali.rice.krad.bo.PersistableBusinessObject;
24 import org.kuali.rice.krad.dao.BusinessObjectDao;
25 import org.kuali.rice.krad.dao.impl.BusinessObjectDaoJpa;
26 import org.kuali.rice.krad.dao.impl.BusinessObjectDaoOjb;
27 import org.kuali.rice.krad.service.KRADServiceLocator;
28 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
29 import org.kuali.rice.krad.service.KualiModuleService;
30 import org.kuali.rice.krad.service.ModuleService;
31 import org.springframework.transaction.annotation.Transactional;
32
33 import javax.persistence.EntityManager;
34 import java.util.ArrayList;
35 import java.util.Collection;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39
40 @Transactional
41 public class BusinessObjectDaoProxy implements BusinessObjectDao {
42
43 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BusinessObjectDaoProxy.class);
44
45 private BusinessObjectDao businessObjectDaoJpa;
46 private BusinessObjectDao businessObjectDaoOjb;
47 private static KualiModuleService kualiModuleService;
48 private static HashMap<String, BusinessObjectDao> boDaoValues = new HashMap<String, BusinessObjectDao>();
49
50 private BusinessObjectDao getDao(Class clazz) {
51 ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
52 if (moduleService != null) {
53 ModuleConfiguration moduleConfig = moduleService.getModuleConfiguration();
54 String dataSourceName = "";
55 EntityManager entityManager = null;
56 if (moduleConfig != null) {
57 dataSourceName = moduleConfig.getDataSourceName();
58 entityManager = moduleConfig.getEntityManager();
59 }
60
61 if (StringUtils.isNotEmpty(dataSourceName)) {
62 if (boDaoValues.get(dataSourceName) != null) {
63 return boDaoValues.get(dataSourceName);
64 } else {
65 if (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) {
66
67 if (entityManager != null) {
68 BusinessObjectDaoJpa boDaoJpa =
69 new BusinessObjectDaoJpa(entityManager, KRADServiceLocator
70 .getPersistenceStructureService());
71
72 boDaoValues.put(dataSourceName, boDaoJpa);
73 return boDaoJpa;
74 } else {
75 throw new ConfigurationException("EntityManager is null. EntityManager must be set in the Module Configuration bean in the appropriate spring beans xml. (see nested exception for details).");
76 }
77 } else {
78
79 BusinessObjectDaoOjb boDaoOjb = new BusinessObjectDaoOjb(
80 KRADServiceLocator.getPersistenceStructureService());
81 boDaoOjb.setJcdAlias(dataSourceName);
82
83 boDaoValues.put(dataSourceName, boDaoOjb);
84 return boDaoOjb;
85 }
86 }
87
88 }
89 }
90
91 return (OrmUtils.isJpaAnnotated(clazz) && OrmUtils.isJpaEnabled()) ? businessObjectDaoJpa : businessObjectDaoOjb;
92 }
93
94
95
96
97 public int countMatching(Class clazz, Map<String, ?> fieldValues) {
98 return getDao(clazz).countMatching(clazz, fieldValues);
99 }
100
101
102
103
104 public int countMatching(Class clazz, Map<String, ?> positiveFieldValues, Map<String, ?> negativeFieldValues) {
105 return getDao(clazz).countMatching(clazz, positiveFieldValues, negativeFieldValues);
106 }
107
108
109
110
111 public void delete(PersistableBusinessObject bo) {
112 if (bo != null) {
113 getDao(bo.getClass()).delete(bo);
114 }
115 }
116
117
118
119
120 public void delete(List<? extends PersistableBusinessObject> boList) {
121 if (!boList.isEmpty()) {
122 getDao(boList.get(0).getClass()).delete(boList);
123 }
124 }
125
126
127
128
129 public void deleteMatching(Class clazz, Map<String, ?> fieldValues) {
130 getDao(clazz).deleteMatching(clazz, fieldValues);
131 }
132
133
134
135
136 public <T extends BusinessObject> Collection<T> findAll(Class<T> clazz) {
137 return getDao(clazz).findAll(clazz);
138 }
139
140
141
142
143 public <T extends BusinessObject> Collection<T> findAllActive(Class<T> clazz) {
144 return getDao(clazz).findAllActive(clazz);
145 }
146
147
148
149
150 public <T extends BusinessObject> Collection<T> findAllInactive(Class<T> clazz) {
151 return getDao(clazz).findAllInactive(clazz);
152 }
153
154
155
156
157 public <T extends BusinessObject> Collection<T> findAllActiveOrderBy(Class<T> clazz, String sortField, boolean sortAscending) {
158 return getDao(clazz).findAllActiveOrderBy(clazz, sortField, sortAscending);
159 }
160
161
162
163
164 public <T extends BusinessObject> Collection<T> findAllOrderBy(Class<T> clazz, String sortField, boolean sortAscending) {
165 return getDao(clazz).findAllOrderBy(clazz, sortField, sortAscending);
166 }
167
168
169
170
171 public <T extends BusinessObject> T findBySinglePrimaryKey(Class<T> clazz, Object primaryKey) {
172 return getDao(clazz).findBySinglePrimaryKey(clazz, primaryKey);
173 }
174
175
176
177
178 public <T extends BusinessObject> T findByPrimaryKey(Class<T> clazz, Map<String, ?> primaryKeys) {
179 return getDao(clazz).findByPrimaryKey(clazz, primaryKeys);
180 }
181
182
183
184
185 public <T extends BusinessObject> Collection<T> findMatching(Class<T> clazz, Map<String, ?> fieldValues) {
186 return getDao(clazz).findMatching(clazz, fieldValues);
187 }
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206 public <T extends BusinessObject> Collection<T> findMatchingActive(Class<T> clazz, Map<String, ?> fieldValues) {
207 return getDao(clazz).findMatchingActive(clazz, fieldValues);
208 }
209
210
211
212
213 public <T extends BusinessObject> Collection<T> findMatchingOrderBy(Class<T> clazz, Map<String, ?> fieldValues, String sortField, boolean sortAscending) {
214 return getDao(clazz).findMatchingOrderBy(clazz, fieldValues, sortField, sortAscending);
215 }
216
217
218
219
220 public PersistableBusinessObject retrieve(PersistableBusinessObject object) {
221 return getDao(object.getClass()).retrieve(object);
222 }
223
224
225
226
227
228 public PersistableBusinessObject manageReadOnly(PersistableBusinessObject bo) {
229 return getDao(bo.getClass()).manageReadOnly(bo);
230 }
231
232
233
234
235
236 public <T extends BusinessObject> T findByPrimaryKeyUsingKeyObject(Class<T> clazz, Object pkObject) {
237 return getDao(clazz).findByPrimaryKeyUsingKeyObject(clazz, pkObject);
238 }
239
240
241
242
243 public PersistableBusinessObject save(PersistableBusinessObject bo) {
244 PersistableBusinessObject savedBo;
245 savedBo = getDao(bo.getClass()).save(bo);
246 return savedBo;
247 }
248
249
250
251
252 public List<? extends PersistableBusinessObject> save(List businessObjects) {
253 if (!businessObjects.isEmpty()) {
254 return getDao(businessObjects.get(0).getClass()).save(businessObjects);
255 }
256 return new ArrayList<PersistableBusinessObject>();
257 }
258
259 private static KualiModuleService getKualiModuleService() {
260 if (kualiModuleService == null) {
261 kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
262 }
263 return kualiModuleService;
264 }
265
266 public void setBusinessObjectDaoJpa(BusinessObjectDao businessObjectDaoJpa) {
267 this.businessObjectDaoJpa = businessObjectDaoJpa;
268 }
269
270 public void setBusinessObjectDaoOjb(BusinessObjectDao businessObjectDaoOjb) {
271 this.businessObjectDaoOjb = businessObjectDaoOjb;
272 }
273 }