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.apache.commons.lang.StringUtils;
19 import org.apache.ojb.broker.query.Criteria;
20 import org.apache.ojb.broker.query.QueryByCriteria;
21 import org.apache.ojb.broker.query.QueryFactory;
22 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
23 import org.kuali.rice.kns.service.KNSServiceLocator;
24 import org.kuali.rice.krad.bo.BusinessObject;
25 import org.kuali.rice.krad.bo.PersistableBusinessObject;
26 import org.kuali.rice.krad.dao.BusinessObjectDao;
27 import org.kuali.rice.krad.service.PersistenceStructureService;
28 import org.kuali.rice.krad.service.util.OjbCollectionAware;
29 import org.kuali.rice.krad.service.util.OjbCollectionHelper;
30 import org.kuali.rice.krad.util.KRADPropertyConstants;
31 import org.kuali.rice.krad.util.ObjectUtils;
32 import org.springframework.dao.DataAccessException;
33 import org.springframework.orm.ObjectRetrievalFailureException;
34
35 import java.lang.reflect.InvocationTargetException;
36 import java.util.Collection;
37 import java.util.HashMap;
38 import java.util.Iterator;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Set;
42
43
44
45
46
47
48
49 @Deprecated
50 public class BusinessObjectDaoOjb extends PlatformAwareDaoBaseOjb implements BusinessObjectDao, OjbCollectionAware {
51 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BusinessObjectDaoOjb.class);
52
53 private PersistenceStructureService persistenceStructureService;
54 private OjbCollectionHelper ojbCollectionHelper;
55
56
57
58
59 public BusinessObjectDaoOjb(PersistenceStructureService persistenceStructureService) {
60 this.persistenceStructureService = persistenceStructureService;
61 }
62
63
64
65
66 public <T extends BusinessObject> T findBySinglePrimaryKey(Class<T> clazz, Object primaryKey) {
67 if (primaryKey.getClass().getName().startsWith("java.lang.")
68 || primaryKey.getClass().getName().startsWith("java.sql.")
69 || primaryKey.getClass().getName().startsWith("java.math.")
70 || primaryKey.getClass().getName().startsWith("java.util.")) {
71 try {
72 return (T) getPersistenceBrokerTemplate().getObjectById(clazz, primaryKey);
73 } catch ( ObjectRetrievalFailureException ex ) {
74
75 return null;
76 }
77 } else {
78 Criteria criteria = buildCriteria(clazz, primaryKey);
79
80 return (T) getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(clazz, criteria));
81 }
82 }
83
84
85
86
87 public <T extends BusinessObject> T findByPrimaryKey(Class<T> clazz, Map<String, ?> primaryKeys) {
88 Criteria criteria = buildCriteria(primaryKeys);
89
90 return (T) getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(clazz, criteria));
91 }
92
93
94
95
96
97
98
99
100 public <T extends BusinessObject> Collection<T> findAll(Class<T> clazz) {
101 return (Collection<T>)getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(clazz, (Criteria) null));
102 }
103
104
105
106
107 public <T extends BusinessObject> Collection<T> findAllOrderBy(Class<T> clazz, String sortField, boolean sortAscending) {
108 QueryByCriteria queryByCriteria = new QueryByCriteria(clazz, (Criteria) null);
109
110 if (sortAscending) {
111 queryByCriteria.addOrderByAscending(sortField);
112 }
113 else {
114 queryByCriteria.addOrderByDescending(sortField);
115 }
116
117 return (Collection<T>)getPersistenceBrokerTemplate().getCollectionByQuery(queryByCriteria);
118 }
119
120
121
122
123
124
125 public <T extends BusinessObject> Collection<T> findMatching(Class<T> clazz, Map<String, ?> fieldValues) {
126 Criteria criteria = buildCriteria(fieldValues);
127
128 return (Collection<T>)getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(clazz, criteria));
129 }
130
131
132
133
134 public <T extends BusinessObject> Collection<T> findAllActive(Class<T> clazz) {
135 return (Collection<T>)getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(clazz, buildActiveCriteria()));
136 }
137
138
139
140
141 public <T extends BusinessObject> Collection<T> findAllInactive(Class<T> clazz) {
142 return (Collection<T>)getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(clazz, buildInactiveCriteria()));
143 }
144
145
146
147
148 public <T extends BusinessObject> Collection<T> findAllActiveOrderBy(Class<T> clazz, String sortField, boolean sortAscending) {
149 QueryByCriteria queryByCriteria = new QueryByCriteria(clazz, buildActiveCriteria());
150
151 if (sortAscending) {
152 queryByCriteria.addOrderByAscending(sortField);
153 }
154 else {
155 queryByCriteria.addOrderByDescending(sortField);
156 }
157
158 return (Collection<T>)getPersistenceBrokerTemplate().getCollectionByQuery(queryByCriteria);
159 }
160
161
162
163
164 public <T extends BusinessObject> Collection<T> findMatchingActive(Class<T> clazz, Map<String, ?> fieldValues) {
165 Criteria criteria = buildCriteria(fieldValues);
166 criteria.addAndCriteria(buildActiveCriteria());
167
168 return (Collection<T>)getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(clazz, criteria));
169 }
170
171
172
173
174
175
176 public int countMatching(Class clazz, Map<String, ?> fieldValues) {
177 Criteria criteria = buildCriteria(fieldValues);
178
179 return getPersistenceBrokerTemplate().getCount(QueryFactory.newQuery(clazz, criteria));
180 }
181
182
183
184
185
186
187 public int countMatching(Class clazz, Map<String, ?> positiveFieldValues, Map<String, ?> negativeFieldValues) {
188 Criteria criteria = buildCriteria(positiveFieldValues);
189 Criteria negativeCriteria = buildNegativeCriteria(negativeFieldValues);
190 criteria.addAndCriteria(negativeCriteria);
191 return getPersistenceBrokerTemplate().getCount(QueryFactory.newQuery(clazz, criteria));
192 }
193
194
195
196
197
198
199
200 public <T extends BusinessObject> Collection<T> findMatchingOrderBy(Class<T> clazz, Map<String, ?> fieldValues, String sortField, boolean sortAscending) {
201 Criteria criteria = buildCriteria(fieldValues);
202 QueryByCriteria queryByCriteria = new QueryByCriteria(clazz, criteria);
203
204 if (sortAscending) {
205 queryByCriteria.addOrderByAscending(sortField);
206 }
207 else {
208 queryByCriteria.addOrderByDescending(sortField);
209 }
210
211 return (Collection<T>)getPersistenceBrokerTemplate().getCollectionByQuery(queryByCriteria);
212 }
213
214
215
216
217
218
219 public PersistableBusinessObject save(PersistableBusinessObject bo) throws DataAccessException {
220
221
222
223 Set<String> boCollections = getPersistenceStructureService().listCollectionObjectTypes(bo.getClass()).keySet();
224 PersistableBusinessObject savedBo = null;
225 if (!boCollections.isEmpty()) {
226
227 savedBo = (PersistableBusinessObject) ObjectUtils.deepCopy(bo);
228 for (String boCollection : boCollections) {
229 if (getPersistenceStructureService().isCollectionUpdatable(savedBo.getClass(), boCollection)) {
230 savedBo.refreshReferenceObject(boCollection);
231 }
232 }
233 getOjbCollectionHelper().processCollections(this, bo, savedBo);
234 }
235
236 getPersistenceBrokerTemplate().store(bo);
237 return bo;
238 }
239
240
241
242
243
244
245 public List<? extends PersistableBusinessObject> save(List businessObjects) throws DataAccessException {
246 if ( LOG.isDebugEnabled() ) {
247 LOG.debug( "About to persist the following BOs:" );
248 for ( Object bo : businessObjects ) {
249 LOG.debug( " --->" + bo );
250 }
251 }
252 for (Iterator i = businessObjects.iterator(); i.hasNext();) {
253 Object bo = i.next();
254 getPersistenceBrokerTemplate().store(bo);
255 }
256 return businessObjects;
257 }
258
259
260
261
262
263
264
265
266
267 public void delete(Object bo) {
268 getPersistenceBrokerTemplate().delete(bo);
269 }
270
271
272
273
274 public void delete(List<? extends PersistableBusinessObject> boList) {
275 for (PersistableBusinessObject bo : boList) {
276 getPersistenceBrokerTemplate().delete(bo);
277 }
278 }
279
280
281
282
283
284 public void deleteMatching(Class clazz, Map<String, ?> fieldValues) {
285 Criteria criteria = buildCriteria(fieldValues);
286
287 getPersistenceBrokerTemplate().deleteByQuery(QueryFactory.newQuery(clazz, criteria));
288
289
290
291 getPersistenceBrokerTemplate().clearCache();
292 }
293
294
295
296
297 public Object retrieve(Object object) {
298 return getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQueryByIdentity(object));
299 }
300
301
302
303
304
305 public <T extends BusinessObject> T findByPrimaryKeyUsingKeyObject(Class<T> clazz, Object pkObject) {
306 throw new UnsupportedOperationException("OJB does not support this option");
307 }
308
309
310
311
312
313 public PersistableBusinessObject manageReadOnly(PersistableBusinessObject bo) {
314 return bo;
315 }
316
317
318
319
320
321
322
323 private Criteria buildCriteria(Map<String, ?> fieldValues) {
324 Criteria criteria = new Criteria();
325 for (Iterator i = fieldValues.entrySet().iterator(); i.hasNext();) {
326 Map.Entry<String, Object> e = (Map.Entry<String, Object>) i.next();
327
328 String key = e.getKey();
329 Object value = e.getValue();
330 if (value instanceof Collection) {
331 criteria.addIn(key, (Collection) value);
332 } else {
333 criteria.addEqualTo(key, value);
334 }
335 }
336
337 return criteria;
338 }
339
340
341 private <T extends BusinessObject> Criteria buildCriteria(Class<T> clazz, Object primaryKey) {
342 Map<String, Object> fieldValues = new HashMap<String, Object>();
343 List<String> fieldNames = getPersistenceStructureService().getPrimaryKeys(clazz);
344
345
346 for (String fieldName : fieldNames) {
347 Object fieldValue;
348
349 try {
350 fieldValue = primaryKey.getClass().getMethod("get" + StringUtils.capitalize(fieldName)).invoke(primaryKey);
351 fieldValues.put(fieldName, fieldValue);
352 } catch (IllegalArgumentException e) {
353 e.printStackTrace();
354 } catch (IllegalAccessException e) {
355 e.printStackTrace();
356 } catch (SecurityException e) {
357 e.printStackTrace();
358 } catch (InvocationTargetException e) {
359 e.printStackTrace();
360 } catch (NoSuchMethodException e) {
361 e.printStackTrace();
362 }
363 }
364 return this.buildCriteria(fieldValues);
365 }
366
367
368
369
370
371 private Criteria buildActiveCriteria(){
372 Criteria criteria = new Criteria();
373 criteria.addEqualTo(KRADPropertyConstants.ACTIVE, true);
374
375 return criteria;
376 }
377
378
379
380
381
382 private Criteria buildInactiveCriteria(){
383 Criteria criteria = new Criteria();
384 criteria.addEqualTo(KRADPropertyConstants.ACTIVE, false);
385
386 return criteria;
387 }
388
389
390
391
392
393
394
395 private Criteria buildNegativeCriteria(Map<String, ?> negativeFieldValues) {
396 Criteria criteria = new Criteria();
397 for (Iterator i = negativeFieldValues.entrySet().iterator(); i.hasNext();) {
398 Map.Entry<String, Object> e = (Map.Entry<String, Object>) i.next();
399
400 String key = e.getKey();
401 Object value = e.getValue();
402 if (value instanceof Collection) {
403 criteria.addNotIn(key, (Collection) value);
404 }
405 else {
406 criteria.addNotEqualTo(key, value);
407 }
408 }
409
410 return criteria;
411 }
412
413
414
415
416
417 protected PersistenceStructureService getPersistenceStructureService() {
418 return persistenceStructureService;
419 }
420
421
422
423
424
425 public void setPersistenceStructureService(PersistenceStructureService persistenceStructureService) {
426 this.persistenceStructureService = persistenceStructureService;
427 }
428
429
430
431
432
433
434 protected OjbCollectionHelper getOjbCollectionHelper() {
435 if (ojbCollectionHelper == null) {
436 ojbCollectionHelper = KNSServiceLocator.getOjbCollectionHelper();
437 }
438
439 return ojbCollectionHelper;
440 }
441
442
443
444
445
446
447 public void setOjbCollectionHelper(OjbCollectionHelper ojbCollectionHelper) {
448 this.ojbCollectionHelper = ojbCollectionHelper;
449 }
450 }