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