Coverage Report - org.kuali.rice.core.api.criteria.QueryByCriteria
 
Classes in this File Line Coverage Branch Coverage Complexity
QueryByCriteria
0%
0/21
N/A
1
QueryByCriteria$1
N/A
N/A
1
QueryByCriteria$Builder
0%
0/19
N/A
1
QueryByCriteria$Constants
0%
0/2
N/A
1
QueryByCriteria$Elements
0%
0/1
N/A
1
 
 1  
 /*
 2  
  * Copyright 2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 1.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/ecl1.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.api.criteria;
 17  
 
 18  
 import java.util.Collection;
 19  
 
 20  
 import javax.xml.bind.annotation.XmlAccessType;
 21  
 import javax.xml.bind.annotation.XmlAccessorType;
 22  
 import javax.xml.bind.annotation.XmlAnyElement;
 23  
 import javax.xml.bind.annotation.XmlElement;
 24  
 import javax.xml.bind.annotation.XmlRootElement;
 25  
 import javax.xml.bind.annotation.XmlType;
 26  
 
 27  
 import org.apache.commons.lang.builder.EqualsBuilder;
 28  
 import org.apache.commons.lang.builder.HashCodeBuilder;
 29  
 import org.apache.commons.lang.builder.ToStringBuilder;
 30  
 import org.kuali.rice.core.api.CoreConstants;
 31  
 import org.kuali.rice.core.api.mo.ModelObjectComplete;
 32  
 import org.w3c.dom.Element;
 33  
 
 34  
 /**
 35  
  * Defines a criteria-based query.  Consists of a {@link Criteria} definition
 36  
  * as well as a set of additional properties which control paging and other
 37  
  * aspects of the results which should be returned from the query.
 38  
  * 
 39  
  * <p>In order to construct a new {@link QueryByCriteria}, the {@link Builder}
 40  
  * should be used.  This contains an internal {@link CriteriaBuilder} which
 41  
  * can be used to define the {@link Criteria} for use by the query.
 42  
  * 
 43  
  * <p>This class specifies nothing regarding how the query will be executed.
 44  
  * It is expected that an instance will be constructed and then passed to code
 45  
  * which understands how to execute the desired query.
 46  
  * 
 47  
  * <p>This class is mapped for use by JAXB and can therefore be used by clients
 48  
  * as part of remotable service definitions.
 49  
  * 
 50  
  * @param <T> the type of the class which is being queried for based on the
 51  
  * specified criteria
 52  
  * 
 53  
  * @see Criteria
 54  
  * @see CriteriaBuilder
 55  
  * 
 56  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 57  
  * 
 58  
  */
 59  
 @XmlRootElement(name = QueryByCriteria.Constants.ROOT_ELEMENT_NAME)
 60  
 @XmlAccessorType(XmlAccessType.NONE)
 61  
 @XmlType(name = QueryByCriteria.Constants.TYPE_NAME, propOrder = {
 62  
                 QueryByCriteria.Elements.CRITERIA,
 63  
                 QueryByCriteria.Elements.START_AT_INDEX,
 64  
                 QueryByCriteria.Elements.MAX_RESULTS,
 65  
                 QueryByCriteria.Elements.COUNT_FLAG,
 66  
                 CoreConstants.CommonElements.FUTURE_ELEMENTS })
 67  0
 public final class QueryByCriteria<T> implements ModelObjectComplete {
 68  
 
 69  
         private static final long serialVersionUID = 2210627777648920180L;
 70  
 
 71  
         @XmlElement(name = Elements.CRITERIA, required = true)
 72  
         private final Criteria criteria;
 73  
         
 74  
         @XmlElement(name = Elements.START_AT_INDEX, required = false)
 75  
         private final Integer startAtIndex;
 76  
                 
 77  
         @XmlElement(name = Elements.MAX_RESULTS, required = false)
 78  
         private final Integer maxResults;
 79  
         
 80  
         @XmlElement(name = Elements.COUNT_FLAG, required = true)
 81  
         private final CountFlag countFlag;
 82  
 
 83  0
         @SuppressWarnings("unused")
 84  
         @XmlAnyElement
 85  
         private final Collection<Element> _futureElements = null;
 86  
 
 87  0
         private QueryByCriteria() {
 88  0
                 this.criteria = null;
 89  0
                 this.startAtIndex = null;
 90  0
                 this.maxResults = null;
 91  0
                 this.countFlag = null;
 92  0
         }
 93  
 
 94  0
         private QueryByCriteria(Builder<T> builder) {
 95  0
                 this.criteria = builder.getCriteriaBuilder().build();
 96  0
                 this.startAtIndex = builder.getStartAtIndex();
 97  0
                 this.maxResults = builder.getMaxResults();
 98  0
                 this.countFlag = builder.getCountFlag();
 99  0
         }
 100  
 
 101  
         /**
 102  
          * Returns the {@link Criteria} which will be used to execute the query.
 103  
          * 
 104  
          * @return the criteria defined on the query, should never be null
 105  
          */
 106  
         public Criteria getCriteria() {
 107  0
                 return this.criteria;
 108  
         }
 109  
 
 110  
         /**
 111  
          * Returns the optional zero-based "start" index for rows returned.  When
 112  
          * this query is executed, this property should be read to determine the
 113  
          * first row which should be returned.  If the given index is beyond the
 114  
          * end of the result set, then the resulting query should effectively
 115  
          * return no rows (as opposed to producing an index-based out of bounds
 116  
          * error).  If the value is null, then the results should start with the
 117  
          * first row returned from the query.
 118  
          * 
 119  
          * @return the starting row index requested by this query, or null if
 120  
          * the results should start at the beginning of the result set
 121  
          */
 122  
         public Integer getStartAtIndex() {
 123  0
                 return this.startAtIndex;
 124  
         }
 125  
 
 126  
         /**
 127  
          * Returns the maximum number of results that this query is requesting
 128  
          * to receive.  If null, then the query should return all rows, or as
 129  
          * many as it can.
 130  
          * 
 131  
          * @return the maximum number of results to return from the query
 132  
          */
 133  
         public Integer getMaxResults() {
 134  0
                 return this.maxResults;
 135  
         }
 136  
 
 137  
         /**
 138  
          * Indicates whether or not a total row count should be returned with the
 139  
          * query.  See {@link CountFlag} for more information on what each of these
 140  
          * flags means.  This will never return null and defaults to
 141  
          * {@link CountFlag#NONE}.
 142  
          * 
 143  
          * @return the flag specifying whether or not a total row count should be
 144  
          * produced by the query
 145  
          */
 146  
         public CountFlag getCountFlag() {
 147  0
                 return this.countFlag;
 148  
         }
 149  
 
 150  
         @Override
 151  
         public int hashCode() {
 152  0
                 return HashCodeBuilder.reflectionHashCode(this,
 153  
                                 Constants.HASH_CODE_EQUALS_EXCLUDE);
 154  
         }
 155  
 
 156  
         @Override
 157  
         public boolean equals(Object obj) {
 158  0
                 return EqualsBuilder.reflectionEquals(obj, this,
 159  
                                 Constants.HASH_CODE_EQUALS_EXCLUDE);
 160  
         }
 161  
 
 162  
         @Override
 163  
         public String toString() {
 164  0
                 return ToStringBuilder.reflectionToString(this);
 165  
         }
 166  
 
 167  
         public static final class Builder<T> {
 168  
 
 169  
                 private final CriteriaBuilder<T> criteriaBuilder;
 170  
                 private Integer startAtIndex;
 171  
                 private Integer maxResults;
 172  
                 private boolean includeCount;
 173  
                 private CountFlag countFlag;
 174  
 
 175  0
                 private Builder(Class<T> queryClass) {
 176  0
                         this.criteriaBuilder = CriteriaBuilder.newCriteriaBuilder(queryClass);
 177  0
                         this.countFlag = CountFlag.NONE;
 178  0
                 }
 179  
 
 180  
                 public static <T> Builder<T> create(Class<T> queryClass) {
 181  0
                         return new Builder<T>(queryClass);
 182  
                 }
 183  
 
 184  
                 public QueryByCriteria<T> build() {
 185  0
                         return new QueryByCriteria<T>(this);
 186  
                 }
 187  
 
 188  
                 public Integer getStartAtIndex() {
 189  0
                         return this.startAtIndex;
 190  
                 }
 191  
 
 192  
                 public void setStartAtIndex(Integer startAtIndex) {
 193  0
                         this.startAtIndex = startAtIndex;
 194  0
                 }
 195  
 
 196  
                 public Integer getMaxResults() {
 197  0
                         return this.maxResults;
 198  
                 }
 199  
 
 200  
                 public void setMaxResults(Integer maxResults) {
 201  0
                         this.maxResults = maxResults;
 202  0
                 }
 203  
 
 204  
                 public boolean isIncludeCount() {
 205  0
                         return this.includeCount;
 206  
                 }
 207  
 
 208  
                 public void setIncludeCount(boolean includeCount) {
 209  0
                         this.includeCount = includeCount;
 210  0
                 }
 211  
 
 212  
                 public CountFlag getCountFlag() {
 213  0
                         return this.countFlag;
 214  
                 }
 215  
 
 216  
                 public void setCountFlag(CountFlag countFlag) {
 217  0
                         this.countFlag = countFlag;
 218  0
                 }
 219  
 
 220  
                 public CriteriaBuilder<T> getCriteriaBuilder() {
 221  0
                         return this.criteriaBuilder;
 222  
                 }
 223  
 
 224  
         }
 225  
 
 226  
         /**
 227  
          * Defines some internal constants used on this class.
 228  
          */
 229  0
         static class Constants {
 230  
                 final static String ROOT_ELEMENT_NAME = "queryByCriteria";
 231  
                 final static String TYPE_NAME = "QueryByCriteriaType";
 232  0
                 final static String[] HASH_CODE_EQUALS_EXCLUDE = { CoreConstants.CommonElements.FUTURE_ELEMENTS };
 233  
         }
 234  
 
 235  
         /**
 236  
          * A private class which exposes constants which define the XML element
 237  
          * names to use when this object is marshaled to XML.
 238  
          */
 239  0
         static class Elements {
 240  
                 final static String CRITERIA = "criteria";
 241  
                 final static String START_AT_INDEX = "startAtIndex";
 242  
                 final static String MAX_RESULTS = "maxResults";
 243  
                 final static String COUNT_FLAG = "countFlag";
 244  
         }
 245  
 
 246  
 }