View Javadoc
1   /*
2    * Copyright 2007 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
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.ole.sys.dataaccess.impl;
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.rice.krad.util.ObjectUtils;
28  
29  public class OjbQueryCustomizer extends QueryCustomizerDefaultImpl {
30      // used to AND in additional criteria on a collection
31      protected static final String FIELD_PREFIX = "parent.";
32  
33      @Override
34      public Query customizeQuery(Object arg0, PersistenceBroker arg1, CollectionDescriptor arg2, QueryByCriteria arg3) {
35          // unfortunately OJB's default implementation has no getter for the map they construct
36          // by accessing this map, we can provide a more generic interface by looping through any attributes
37          // so, use reflection to get at the attribute anyway
38          Field field = null;
39          try {
40              field = this.getClass().getSuperclass().getDeclaredField("m_attributeList");
41          }
42          catch (Exception e) {
43              throw new RuntimeException(e);
44          }
45          field.setAccessible(true);
46          Map<String, String> m_attributeList = null;
47          try {
48              m_attributeList = (Map) field.get(this);
49          }
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(arg0, this.getAttribute(key).substring(FIELD_PREFIX.length())));
65              }
66              else {
67                  criteria.addEqualTo(key, this.getAttribute(key));
68              }
69          }
70          arg3.setCriteria(criteria);
71          return arg3;
72      }
73  }