| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| CustomCollectionJoinQueryCustomizer |
|
| 2.0;2 |
| 1 | /* | |
| 2 | * Copyright 2007-2008 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.rice.core.ojb; | |
| 17 | ||
| 18 | import java.util.HashMap; | |
| 19 | import java.util.Map; | |
| 20 | ||
| 21 | import org.apache.commons.lang.StringUtils; | |
| 22 | import org.apache.ojb.broker.PersistenceBroker; | |
| 23 | import org.apache.ojb.broker.accesslayer.QueryCustomizer; | |
| 24 | import org.apache.ojb.broker.metadata.CollectionDescriptor; | |
| 25 | import org.apache.ojb.broker.query.Criteria; | |
| 26 | import org.apache.ojb.broker.query.Query; | |
| 27 | import org.apache.ojb.broker.query.QueryByCriteria; | |
| 28 | import org.kuali.rice.kns.util.ObjectUtils; | |
| 29 | ||
| 30 | 0 | public class CustomCollectionJoinQueryCustomizer implements QueryCustomizer { |
| 31 | // used to AND in additional criteria on a collection | |
| 32 | private static final String FIELD_PREFIX = "parent."; | |
| 33 | ||
| 34 | 0 | protected Map<String,String> attributeMap = new HashMap<String,String>(); |
| 35 | ||
| 36 | /** | |
| 37 | * @see org.apache.ojb.broker.metadata.AttributeContainer#addAttribute(java.lang.String, java.lang.String) | |
| 38 | */ | |
| 39 | public void addAttribute(String attributeName, String attributeValue) { | |
| 40 | 0 | attributeMap.put( attributeName, attributeValue ); |
| 41 | 0 | } |
| 42 | ||
| 43 | /** | |
| 44 | * @see org.apache.ojb.broker.metadata.AttributeContainer#getAttribute(java.lang.String) | |
| 45 | */ | |
| 46 | public String getAttribute(String attributeName) { | |
| 47 | 0 | return getAttribute( attributeName, null ); |
| 48 | } | |
| 49 | /** | |
| 50 | * @see org.apache.ojb.broker.metadata.AttributeContainer#getAttribute(java.lang.String, java.lang.String) | |
| 51 | */ | |
| 52 | public String getAttribute(String attributeName, String defaultValue) { | |
| 53 | 0 | String val = attributeMap.get( attributeName ); |
| 54 | 0 | if ( StringUtils.isBlank( val ) ) { |
| 55 | 0 | return defaultValue; |
| 56 | } | |
| 57 | 0 | return val; |
| 58 | } | |
| 59 | ||
| 60 | public Query customizeQuery(Object obj, PersistenceBroker pb, CollectionDescriptor collDesc, QueryByCriteria query) { | |
| 61 | ||
| 62 | // now, do what we wanted to do to start with if we could've just gotten m_attributeList easily | |
| 63 | 0 | Criteria criteria = query.getCriteria(); |
| 64 | 0 | for (String key : attributeMap.keySet()) { |
| 65 | 0 | String val = attributeMap.get( key ); |
| 66 | // if beginning with FIELD_PREFIX is too hacky, or more flexibility is needed, another query customizer class can be | |
| 67 | // made, | |
| 68 | // and this method can be renamed to take a parameter to specify which we want to do | |
| 69 | // (and the customizeQuery method here made to call the new method with the parameter). | |
| 70 | // However, making another class would mean you couldn't intermix constants and field values, | |
| 71 | // since OJB won't use have multiple query-customizers per collection-descriptor. | |
| 72 | 0 | if (val.startsWith(FIELD_PREFIX)) { |
| 73 | 0 | criteria.addEqualTo(key, ObjectUtils.getPropertyValue(obj, val.substring(FIELD_PREFIX.length()))); |
| 74 | } else { | |
| 75 | 0 | criteria.addEqualTo(key, this.getAttribute(key)); |
| 76 | } | |
| 77 | 0 | } |
| 78 | 0 | query.setCriteria(criteria); |
| 79 | 0 | return query; |
| 80 | } | |
| 81 | } |