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 everything in the
012: * value array passed in for that particular field. For example, x IN [val1, val2, val3] is equivalent to testing [x = val1, or x= = val2, or x = val3].
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 or EJBQuery class.
016: *<p>
017: *@author Steve Woodcock
018: */
019: public class MatchInQuery implements IndexQuery, QueryId {
020:
021: int hashCode = 0;
022: Class className;
023: String propertyName;
024: Comparable[] values;
025: final public Object alias;
026: static final QueryType QUERY_ID = QueryType.IN_QUERY;
027:
028: /**
029: * Construct a query supplying the classname of the object type to be returned,and the
030: * field to be queried and the values to be used. <p>
031: *
032: * The field type in the object and each of the values must be of the same type or be able
033: * to be coerced using java's assingability rules.
034: *
035: * <p>
036: * An example usage would be:
037: * <p>
038: * new MatchInQuery(org.package.Myclass.class, "myProperty" ,[20,21,22]);
039: * <p>
040: * @param className - the class of object to be returned.
041: * @param propertyName - the field name to use
042: * @param values - the values that are used as the search value.
043: */
044: public MatchInQuery(Class className, String propertyName,
045: Comparable[] values) {
046: this (className, propertyName, values, null);
047: }
048:
049: public MatchInQuery(Class className, String propertyName,
050: Comparable[] values, Object alias) {
051: this .className = className;
052: this .propertyName = propertyName;
053: this .values = values;
054: this .alias = alias;
055: }
056:
057: /**
058: * Construct a query supplying the classname of the object type to be returned,and the
059: * field to be queried and the values to be used. <p>
060: *
061: * The field type in the object and each of the values must be of the same type or be able
062: * to be coerced using java's assingability rules.
063: *
064: * <p>
065: * An example usage would be:
066: * <p>
067: * new MatchInQuery("org.package.Myclass", "myProperty" ,[20,21,22]);
068: * <p>
069: * @param className - the class of object to be returned.
070: * @param propertyName - the field name to use
071: * @param values - the values that are used as the search value.
072: */
073:
074: public MatchInQuery(String className, String propertyName,
075: Comparable[] values) {
076: this (className, propertyName, values, null);
077: }
078:
079: public MatchInQuery(String className, String propertyName,
080: Comparable[] values, Object alias) {
081: Class clazz = null;
082: try {
083: clazz = ReflectionUtil.classForName(className);
084: } catch (Exception e) {
085: throw new RuntimeException(e);
086: }
087: this .className = clazz;
088: this .propertyName = propertyName;
089: this .values = values;
090: this .alias = alias;
091: }
092:
093: /**
094: * Construct a query supplying the values to be searched against.
095: * This is a convenience method for classes (such as String,Integer etc)
096: * that have no property value as such. Instead the value is the class type of the values
097: * in the array.
098: * <p>
099: *
100: * An example usage would be:
101: * <p>
102: * new MatchInQuery(["test","test1"]);
103: * <p>
104: * This is so you do not have to use the methods above in the manner of
105: * <p>
106: * new MatchInQuery("java.lang.String", null ,["test","test1"]);
107: * <p>
108: * @param values - the values that are used as the search value.
109: */
110: public MatchInQuery(Comparable[] values) {
111: this .values = values;
112: this .alias = null;
113: }
114:
115: /**
116: * @return Returns the className.
117: */
118: public Class getClassName() {
119: return className;
120: }
121:
122: /**
123: * @return Returns the propertyName.
124: */
125: public String getPropertyName() {
126: return propertyName;
127: }
128:
129: /**
130: * @return Returns the values.
131: */
132: public Comparable[] getValues() {
133: return values;
134: }
135:
136: public QueryType getQueryType() {
137: return QUERY_ID;
138: }
139:
140: public Object getAlias() {
141: return alias;
142: }
143:
144: public IndexQuery setParameter(String name, Object value) {
145: throw new UnsupportedOperationException(
146: "Parameters are not supported for convenience classes");
147: }
148:
149: /* (non-Javadoc)
150: * @see com.jofti.api.IndexQuery#setParameter(int, java.lang.Object)
151: */
152: public IndexQuery setParameter(int position, Object value) {
153: throw new UnsupportedOperationException(
154: "Parameters are not supported for convenience classes");
155:
156: }
157:
158: public IndexQuery setFirstResult(int firstResult) {
159: throw new UnsupportedOperationException(
160: "result limits are not supported for convenience classes");
161:
162: }
163:
164: public IndexQuery setMaxResults(int maxResults) {
165: throw new UnsupportedOperationException(
166: "result limits are not supported for convenience classes");
167:
168: }
169: }
|