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