01: /*
02: * Copyright 2007 The Kuali Foundation.
03: *
04: * Licensed under the Educational Community License, Version 1.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.opensource.org/licenses/ecl1.php
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.kuali.kfs.dao.ojb;
17:
18: import java.lang.reflect.Field;
19: import java.util.Map;
20:
21: import org.apache.ojb.broker.PersistenceBroker;
22: import org.apache.ojb.broker.accesslayer.QueryCustomizerDefaultImpl;
23: import org.apache.ojb.broker.metadata.CollectionDescriptor;
24: import org.apache.ojb.broker.query.Criteria;
25: import org.apache.ojb.broker.query.Query;
26: import org.apache.ojb.broker.query.QueryByCriteria;
27: import org.kuali.core.util.ObjectUtils;
28:
29: public class OjbQueryCustomizer extends QueryCustomizerDefaultImpl {
30: // used to AND in additional criteria on a collection
31: private static final String FIELD_PREFIX = "parent.";
32:
33: @Override
34: public Query customizeQuery(Object arg0, PersistenceBroker arg1,
35: CollectionDescriptor arg2, QueryByCriteria arg3) {
36: // unfortunately OJB's default implementation has no getter for the map they construct
37: // by accessing this map, we can provide a more generic interface by looping through any attributes
38: // so, use reflection to get at the attribute anyway
39: Field field = null;
40: try {
41: field = this .getClass().getSuperclass().getDeclaredField(
42: "m_attributeList");
43: } catch (Exception e) {
44: throw new RuntimeException(e);
45: }
46: field.setAccessible(true);
47: Map<String, String> m_attributeList = null;
48: try {
49: m_attributeList = (Map) field.get(this );
50: } catch (Exception e) {
51: throw new RuntimeException(e);
52: }
53:
54: // now, do what we wanted to do to start with if we could've just gotten m_attributeList easily
55: Criteria criteria = arg3.getCriteria();
56: for (String key : m_attributeList.keySet()) {
57: // if beginning with FIELD_PREFIX is too hacky, or more flexibility is needed, another query customizer class can be
58: // made,
59: // and this method can be renamed to take a parameter to specify which we want to do
60: // (and the customizeQuery method here made to call the new method with the parameter).
61: // However, making another class would mean you couldn't intermix constants and field values,
62: // since OJB won't use have multiple query-customizers per collection-descriptor.
63: if (this.getAttribute(key).startsWith(FIELD_PREFIX)) {
64: criteria.addEqualTo(key, ObjectUtils.getPropertyValue(
65: arg0, this.getAttribute(key).substring(
66: FIELD_PREFIX.length())));
67: } else {
68: criteria.addEqualTo(key, this.getAttribute(key));
69: }
70: }
71: arg3.setCriteria(criteria);
72: return arg3;
73: }
74: }
|