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