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