001: package com.jofti.query;
002:
003: import com.jofti.api.IndexQuery;
004: import com.jofti.core.QueryId;
005: import com.jofti.core.QueryType;
006: import com.jofti.util.ReflectionUtil;
007:
008: /**
009: *
010: *
011: * A utility class to enable simple queries to be done easily. The Query matches
012: * ranges for particular field. This is equivalent to (>= and <=) or (> and <).<p>
013: *
014: *
015: * This query cannot be combined with any other. iF you want to construct more complex queries use the @link com.jofti.query.Query class.
016: <p>
017: * @author steve Woodcock
018: */
019:
020: public class MatchRangeQuery implements IndexQuery, QueryId {
021:
022: public final Object alias;
023: int hashCode = 0;
024: Class className;
025: String propertyName;
026: Comparable startValue;
027: Comparable endValue;
028: boolean inclusive = true;
029:
030: static final QueryType QUERY_ID = QueryType.RANGE_QUERY;
031:
032: /**
033: * Construct a query supplying the classname of the object type to be returned, the field to be queried and the start and finish values to be used.
034: * <p>
035: * The field type in the object and the values must be of the same type. In addition the end value must
036: * be greater (comparaison >0 ) than the start value;
037: * <p>
038: * An example usage would be:
039: * <p>
040: * new MatchNSQuery(org.package.Myclass.class, "myProperty" ,10,20);
041: * <p>
042: * @param className - the class of object to be returned.
043: * @param propertyName - the field name to use
044: * @param startValue - the value that is used as the start search value.
045: * @param endValue - the value that is used as the end search value.
046: */
047: public MatchRangeQuery(Class className, String propertyName,
048: Comparable startValue, Comparable endValue) {
049: this (className, propertyName, startValue, endValue, null);
050: }
051:
052: public MatchRangeQuery(Class className, String propertyName,
053: Comparable startValue, Comparable endValue, Object alias) {
054: this .className = className;
055: this .propertyName = propertyName;
056: this .startValue = startValue;
057: this .endValue = endValue;
058: this .alias = alias;
059: }
060:
061: /**
062: * Construct a query supplying the classname of the object type to be returned, the field to be queried and the start and finish values to be used.
063: * <p>
064: * The field type in the object and the values must be of the same type. In addition the end value must
065: * be greater (comparaison >0 ) than the start value;
066: * <p>
067: * An example usage would be:
068: * <p>
069: * new MatchNSQuery("org.package.Myclass", "myProperty" ,10,20);
070: * <p>
071: * @param className - the class of object to be returned.
072: * @param propertyName - the field name to use
073: * @param startValue - the value that is used as the start search value.
074: * @param endValue - the value that is used as the end search value.
075: */
076: public MatchRangeQuery(String className, String propertyName,
077: Comparable startValue, Comparable endValue) {
078: this (className, propertyName, startValue, endValue, null);
079: }
080:
081: public MatchRangeQuery(String className, String propertyName,
082: Comparable startValue, Comparable endValue, Object alias) {
083: Class clazz = null;
084: try {
085: clazz = ReflectionUtil.classForName(className);
086: } catch (Exception e) {
087: throw new RuntimeException(e);
088: }
089: this .className = clazz;
090: this .propertyName = propertyName;
091: this .startValue = startValue;
092: this .endValue = endValue;
093: this .alias = alias;
094: }
095:
096: /**
097: * Construct a query supplying the classname of the object type to be returned, the field to be queried and the start and finish values to be used.
098: * <p>
099: * The field type in the object and the values must be of the same type. In addition the end value must
100: * be greater (comparaison >0 ) than the start value. thi method also allows the setting of search inclusivity.
101: * <p>
102: * An example usage would be:
103: * <p>
104: * new MatchNSQuery(org.package.Myclass.class, "myProperty" ,10,20, true);
105: * <p>
106: * @param className - the class of object to be returned.
107: * @param propertyName - the field name to use
108: * @param startValue - the value that is used as the start search value.
109: * @param endValue - the value that is used as the end search value.
110: * @param inclusive - whether a search is inclusive of values
111: */
112: public MatchRangeQuery(Class className, String propertyName,
113: Comparable startValue, Comparable endValue,
114: boolean inclusive) {
115: this (className, propertyName, startValue, endValue, null);
116: }
117:
118: public MatchRangeQuery(Class className, String propertyName,
119: Comparable startValue, Comparable endValue,
120: boolean inclusive, Object alias) {
121: this .className = className;
122: this .propertyName = propertyName;
123: this .startValue = startValue;
124: this .endValue = endValue;
125: this .inclusive = inclusive;
126: this .alias = alias;
127: }
128:
129: /**
130: * Construct a query supplying the classname of the object type to be returned, the field to be queried and the start and finish values to be used.
131: * <p>
132: * The field type in the object and the values must be of the same type. In addition the end value must
133: * be greater (comparaison >0 ) than the start value. thi method also allows the setting of search inclusivity.
134: * <p>
135: * An example usage would be:
136: * <p>
137: * new MatchNSQuery("org.package.Myclass", "myProperty" ,10,20, true);
138: * <p>
139: * @param className - the class of object to be returned.
140: * @param propertyName - the field name to use
141: * @param startValue - the value that is used as the start search value.
142: * @param endValue - the value that is used as the end search value.
143: * @param inclusive - whether a search is inclusive of values
144: */
145: public MatchRangeQuery(String className, String propertyName,
146: Comparable startValue, Comparable endValue,
147: boolean inclusive) {
148: this (className, propertyName, startValue, endValue, inclusive,
149: null);
150: }
151:
152: public MatchRangeQuery(String className, String propertyName,
153: Comparable startValue, Comparable endValue,
154: boolean inclusive, Object alias) {
155: Class clazz = null;
156: try {
157: clazz = ReflectionUtil.classForName(className);
158: } catch (Exception e) {
159: throw new RuntimeException(e);
160: }
161: this .className = clazz;
162: this .propertyName = propertyName;
163: this .startValue = startValue;
164: this .endValue = endValue;
165: this .inclusive = inclusive;
166: this .alias = alias;
167: }
168:
169: /**
170: * Construct a query supplying the start and finish values to be used.
171: * <p>
172: * The values must be of the same type. In addition the end value must
173: * be greater (comparaison >0 ) than the start value. This is a convenience method for classes (such as String,Integer etc)
174: * that have no property value as such. Instead the value is the class type. This method also allows the setting of search inclusivity.
175: * <p>
176: * An example usage would be:
177: * <p>
178: * new MatchRangeQuery(new Integer(10), new Integer(20), true);
179: * <p>
180: * @param startValue - the value that is used as the start search value.
181: * @param endValue - the value that is used as the end search value.
182: * @param inclusive - whether a search is inclusive of values
183: */
184: public MatchRangeQuery(Comparable startValue, Comparable endValue,
185: boolean inclusive) {
186: this (startValue, endValue, inclusive, null);
187: }
188:
189: public MatchRangeQuery(Comparable startValue, Comparable endValue,
190: boolean inclusive, Object alias) {
191: this .startValue = startValue;
192: this .endValue = endValue;
193: this .inclusive = inclusive;
194: this .alias = alias;
195: }
196:
197: /**
198: * @return Returns the className.
199: */
200: public Class getClassName() {
201: return className;
202: }
203:
204: /**
205: * @return Returns the propertyName.
206: */
207: public String getPropertyName() {
208: return propertyName;
209: }
210:
211: /**
212: * @return Returns the value.
213: */
214:
215: /**
216: * @return Returns the endValue.
217: */
218: public Comparable getEndValue() {
219: return endValue;
220: }
221:
222: /**
223: * @return Returns the startValue.
224: */
225: public Comparable getStartValue() {
226: return startValue;
227: }
228:
229: public boolean isInclusive() {
230: return inclusive;
231: }
232:
233: public void setInclusive(boolean inclusive) {
234: this .inclusive = inclusive;
235: }
236:
237: public QueryType getQueryType() {
238:
239: return QUERY_ID;
240: }
241:
242: public Object getAlias() {
243: return alias;
244: }
245:
246: public IndexQuery setParameter(String name, Object value) {
247: throw new UnsupportedOperationException(
248: "Parameters are not supported for convenience classes");
249: }
250:
251: /* (non-Javadoc)
252: * @see com.jofti.api.IndexQuery#setParameter(int, java.lang.Object)
253: */
254: public IndexQuery setParameter(int position, Object value) {
255: throw new UnsupportedOperationException(
256: "Parameters are not supported for convenience classes");
257:
258: }
259:
260: public IndexQuery setFirstResult(int firstResult) {
261: throw new UnsupportedOperationException(
262: "result limits are not supported for convenience classes");
263:
264: }
265:
266: public IndexQuery setMaxResults(int maxResults) {
267: throw new UnsupportedOperationException(
268: "result limits are not supported for convenience classes");
269:
270: }
271: }
|