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