1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.core.impl.persistence.dao; |
18 | |
|
19 | |
import org.apache.log4j.Logger; |
20 | |
import org.apache.ojb.broker.query.Criteria; |
21 | |
import org.apache.ojb.broker.query.Query; |
22 | |
import org.apache.ojb.broker.query.QueryByCriteria; |
23 | |
import org.apache.ojb.broker.query.QueryFactory; |
24 | |
import org.kuali.rice.core.api.config.property.Config; |
25 | |
import org.kuali.rice.core.api.config.property.ConfigContext; |
26 | |
import org.kuali.rice.core.framework.persistence.platform.DatabasePlatform; |
27 | |
import org.kuali.rice.core.framework.persistence.dao.GenericDao; |
28 | |
import org.kuali.rice.core.framework.persistence.ojb.SuffixableQueryByCriteria; |
29 | |
import org.kuali.rice.core.util.RiceConstants; |
30 | |
import org.springframework.dao.DataAccessException; |
31 | |
import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport; |
32 | |
|
33 | |
import java.util.Collection; |
34 | |
import java.util.Iterator; |
35 | |
import java.util.List; |
36 | |
import java.util.Map; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 0 | public class GenericDaoOjb extends PersistenceBrokerDaoSupport implements GenericDao { |
46 | 0 | private static final Logger LOG = Logger.getLogger(GenericDaoOjb.class); |
47 | |
|
48 | 0 | private boolean useSelectForUpdate = true; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public void setUseSelectForUpdate(boolean useSelectForUpdate) { |
54 | 0 | this.useSelectForUpdate = useSelectForUpdate; |
55 | 0 | } |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
public Object findById(Class clazz, Object id) { |
61 | 0 | return getPersistenceBrokerTemplate().getObjectById(clazz, id); |
62 | |
} |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
public Object findByPrimaryKey(Class clazz, Map primaryKeys) { |
68 | 0 | Criteria criteria = buildCriteria(primaryKeys); |
69 | |
|
70 | 0 | return getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(clazz, criteria)); |
71 | |
} |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public Object findByUniqueKey(Class clazz, Map uniqueKeys) { |
77 | 0 | Criteria criteria = buildCriteria(uniqueKeys); |
78 | |
|
79 | 0 | return getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(clazz, criteria)); |
80 | |
} |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public Collection findAll(Class clazz) { |
86 | 0 | return getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(clazz, (Criteria) null)); |
87 | |
} |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
public Collection findAllOrderBy(Class clazz, String sortField, |
93 | |
boolean sortAscending) { |
94 | 0 | QueryByCriteria queryByCriteria = new QueryByCriteria(clazz, (Criteria) null); |
95 | |
|
96 | 0 | if (sortAscending) { |
97 | 0 | queryByCriteria.addOrderByAscending(sortField); |
98 | |
} else { |
99 | 0 | queryByCriteria.addOrderByDescending(sortField); |
100 | |
} |
101 | |
|
102 | 0 | return getPersistenceBrokerTemplate().getCollectionByQuery( |
103 | |
queryByCriteria); |
104 | |
} |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
public Collection findMatching(Class clazz, Map fieldValues) { |
110 | 0 | Criteria criteria = buildCriteria(fieldValues); |
111 | |
|
112 | 0 | return getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(clazz, criteria)); |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
public int countMatching(Class clazz, Map fieldValues) { |
119 | 0 | Criteria criteria = buildCriteria(fieldValues); |
120 | |
|
121 | 0 | return getPersistenceBrokerTemplate().getCount(QueryFactory.newQuery(clazz, criteria)); |
122 | |
} |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
public int countMatching(Class clazz, Map positiveFieldValues, |
128 | |
Map negativeFieldValues) { |
129 | 0 | Criteria criteria = buildCriteria(positiveFieldValues); |
130 | 0 | Criteria negativeCriteria = buildNegativeCriteria(negativeFieldValues); |
131 | 0 | criteria.addAndCriteria(negativeCriteria); |
132 | 0 | return getPersistenceBrokerTemplate().getCount(QueryFactory.newQuery(clazz, criteria)); |
133 | |
} |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
public Collection findMatchingOrderBy(Class clazz, Map fieldValues, |
139 | |
String sortField, boolean sortAscending) { |
140 | 0 | Criteria criteria = buildCriteria(fieldValues); |
141 | 0 | QueryByCriteria queryByCriteria = new QueryByCriteria(clazz, criteria); |
142 | |
|
143 | 0 | if (sortAscending) { |
144 | 0 | queryByCriteria.addOrderByAscending(sortField); |
145 | |
} else { |
146 | 0 | queryByCriteria.addOrderByDescending(sortField); |
147 | |
} |
148 | |
|
149 | 0 | return getPersistenceBrokerTemplate().getCollectionByQuery(queryByCriteria); |
150 | |
} |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
public void save(Object bo) throws DataAccessException { |
156 | 0 | getPersistenceBrokerTemplate().store(bo); |
157 | 0 | } |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
public void save(List businessObjects) throws DataAccessException { |
163 | 0 | for (Iterator i = businessObjects.iterator(); i.hasNext();) { |
164 | 0 | Object bo = i.next(); |
165 | 0 | getPersistenceBrokerTemplate().store(bo); |
166 | 0 | } |
167 | 0 | } |
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
public void delete(Object bo) { |
173 | 0 | getPersistenceBrokerTemplate().delete(bo); |
174 | 0 | } |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
public void delete(List<Object> boList) { |
180 | 0 | for (Object bo : boList) { |
181 | 0 | getPersistenceBrokerTemplate().delete(bo); |
182 | |
} |
183 | 0 | } |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
public void deleteMatching(Class clazz, Map fieldValues) { |
189 | 0 | Criteria criteria = buildCriteria(fieldValues); |
190 | |
|
191 | 0 | getPersistenceBrokerTemplate().deleteByQuery(QueryFactory.newQuery(clazz, criteria)); |
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | 0 | getPersistenceBrokerTemplate().clearCache(); |
197 | 0 | } |
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
public Object retrieve(Object object) { |
203 | 0 | return getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQueryByIdentity(object)); |
204 | |
} |
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
public Collection findMatchingByExample(Object object) { |
210 | 0 | return getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQueryByExample(object)); |
211 | |
} |
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
public Collection findMatching(Class clazz, Criteria criteria) { |
217 | 0 | return findMatching(clazz, criteria, false, RiceConstants.NO_WAIT); |
218 | |
|
219 | |
|
220 | |
} |
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
public Collection findMatching(Class clazz, Criteria criteria, boolean selectForUpdate, long wait) { |
226 | |
Query query; |
227 | 0 | if (selectForUpdate && !useSelectForUpdate) { |
228 | 0 | LOG.warn("Pessimistic locking was requested but select for update is disabled"); |
229 | |
} |
230 | 0 | if (selectForUpdate && useSelectForUpdate) { |
231 | 0 | SuffixableQueryByCriteria q = new SuffixableQueryByCriteria(clazz, criteria); |
232 | |
|
233 | 0 | Config config = ConfigContext.getCurrentContextConfig(); |
234 | 0 | DatabasePlatform platform = null; |
235 | |
try { |
236 | 0 | platform = (DatabasePlatform) Class.forName(config.getProperty(Config.DATASOURCE_PLATFORM)).newInstance(); |
237 | 0 | } catch (Exception e) { |
238 | 0 | throw new RuntimeException(e.getMessage(), e); |
239 | 0 | } |
240 | 0 | q.setQuerySuffix(" " + platform.getSelectForUpdateSuffix(wait)); |
241 | 0 | query = q; |
242 | 0 | } else { |
243 | 0 | query = QueryFactory.newQuery(clazz, criteria); |
244 | |
} |
245 | 0 | return getPersistenceBrokerTemplate().getCollectionByQuery(query); |
246 | |
|
247 | |
} |
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
private Criteria buildCriteria(Map fieldValues) { |
257 | 0 | Criteria criteria = new Criteria(); |
258 | 0 | for (Iterator i = fieldValues.entrySet().iterator(); i.hasNext();) { |
259 | 0 | Map.Entry e = (Map.Entry) i.next(); |
260 | |
|
261 | 0 | String key = (String) e.getKey(); |
262 | 0 | Object value = e.getValue(); |
263 | 0 | if (value instanceof Collection) { |
264 | 0 | criteria.addIn(key, (Collection) value); |
265 | |
} else { |
266 | 0 | criteria.addEqualTo(key, value); |
267 | |
} |
268 | 0 | } |
269 | |
|
270 | 0 | return criteria; |
271 | |
} |
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
private Criteria buildNegativeCriteria(Map negativeFieldValues) { |
281 | 0 | Criteria criteria = new Criteria(); |
282 | 0 | for (Iterator i = negativeFieldValues.entrySet().iterator(); i.hasNext();) { |
283 | 0 | Map.Entry e = (Map.Entry) i.next(); |
284 | |
|
285 | 0 | String key = (String) e.getKey(); |
286 | 0 | Object value = e.getValue(); |
287 | 0 | if (value instanceof Collection) { |
288 | 0 | criteria.addNotIn(key, (Collection) value); |
289 | |
} else { |
290 | 0 | criteria.addNotEqualTo(key, value); |
291 | |
} |
292 | 0 | } |
293 | |
|
294 | 0 | return criteria; |
295 | |
} |
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
@Override |
303 | |
public Collection findMatching(Class clazz, |
304 | |
org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria criteria) { |
305 | |
|
306 | 0 | return null; |
307 | |
} |
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
@Override |
315 | |
public Collection findMatching(Class clazz, |
316 | |
org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria criteria, |
317 | |
boolean selectForUpdate, long wait) { |
318 | |
|
319 | 0 | return null; |
320 | |
} |
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
@Override |
329 | |
public Collection findMatching(Class clazz, Map criteria, |
330 | |
boolean selectForUpdate, long wait) { |
331 | |
|
332 | 0 | LOG.info("*******************************calling GenericDaoOjb.findMatching(Class clazz, Map criteria, boolean selectForUpdate, long wait)"); |
333 | 0 | Criteria c = buildCriteria(criteria); |
334 | 0 | return findMatching(clazz, c, selectForUpdate, wait); |
335 | |
} |
336 | |
} |