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: * the value for particular field. This is equivalent to =.
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: *
023: * @author steve Woodcock
024: */
025: public class MatchNSQuery implements IndexQuery, INameSpaceAware,
026: QueryId {
027:
028: public final Object alias;
029: private int hashCode = 0;
030: Class className;
031: Object nameSpace;
032: String propertyName;
033: Comparable value;
034: static final QueryType QUERY_ID = QueryType.MATCH_NS_QUERY;
035:
036: /**
037: * Construct a query supplying the classname of the object type to be returned, the namespace
038: * under which to start the search. The field to be queried and the value to be used.
039: * <p>
040: * The field type in the object and the value must be of the same type.
041: * <p>
042: *
043: * An example usage (for JBossCache) would be:
044: * <p>
045: * new MatchNSQuery("org.package.Myclass", "/test", "myProperty" ,"some value");
046: * <p>
047: * @param className - the class of object to be returned.
048: * @param nameSpace - the name space to be searched.
049: * @param propertyName - the field name to use
050: * @param value - the value that is used as the search value.
051: */
052: public MatchNSQuery(Class className, Object nameSpace,
053: String propertyName, Comparable value) {
054: this (className, nameSpace, propertyName, value, null);
055: }
056:
057: public MatchNSQuery(Class className, Object nameSpace,
058: String propertyName, Comparable value, Object alias) {
059: this .className = className;
060: this .propertyName = propertyName;
061: this .value = value;
062: this .nameSpace = nameSpace;
063: this .alias = alias;
064: }
065:
066: public MatchNSQuery(String className, Object nameSpace,
067: String propertyName, Comparable value) {
068: this (className, nameSpace, propertyName, value, null);
069: }
070:
071: public MatchNSQuery(String className, Object nameSpace,
072: String propertyName, Comparable value, Object alias) {
073: Class clazz = null;
074: try {
075: clazz = ReflectionUtil.classForName(className);
076: } catch (Exception e) {
077: throw new RuntimeException(e);
078: }
079: this .className = clazz;
080: this .propertyName = propertyName;
081: this .value = value;
082: this .nameSpace = nameSpace;
083: this .alias = alias;
084: }
085:
086: /**
087: * Construct a query supplying the value to be searched against and the namespace
088: * under which to start the search. This is a convenience method for classes (such as String,Integer etc)
089: * that have no property value as such. Instead the value is the class type.
090: * <p>
091: *
092: * An example usage (for JBossCache) would be:
093: * <p>
094: * new MatchNSQuery("/test","some value");
095: * <p>
096: * This is so you do not have to use the methods above in the manner of
097: * <p>
098: * new MatchNSQuery("java.lang.String", "/test", null ,"some value");
099: * <p>
100:
101: * @param nameSpace - the name space to be searched.
102: * @param value - the value that is used as the search value.
103: */
104:
105: public MatchNSQuery(Object nameSpace, Comparable value) {
106: this (nameSpace, value, null);
107: }
108:
109: public MatchNSQuery(Object nameSpace, Comparable value, Object alias) {
110: this .value = value;
111: this .nameSpace = nameSpace;
112: this .alias = alias;
113:
114: }
115:
116: public MatchNSQuery(Object nameSpace) {
117: this (nameSpace, null);
118: }
119:
120: public MatchNSQuery(Object nameSpace, Object alias) {
121: this .nameSpace = nameSpace;
122: this .alias = alias;
123:
124: }
125:
126: /**
127: * @return Returns the className.
128: */
129: public Class getClassName() {
130: return className;
131: }
132:
133: /**
134: * @return Returns the propertyName.
135: */
136: public String getPropertyName() {
137: return propertyName;
138: }
139:
140: /**
141: * @return Returns the value.
142: */
143: public Comparable getValue() {
144: return value;
145: }
146:
147: /**
148: * @return Returns the wrapper.
149: */
150: public synchronized INameSpace getNameSpaceWrapper() {
151:
152: if (nameSpace instanceof INameSpace) {
153: return (INameSpace) nameSpace;
154: } else {
155: return new NameSpaceWrapper(nameSpace);
156: }
157: }
158:
159: /**
160: * @return Returns the nameSpace.
161: */
162: public synchronized Object getNameSpace() {
163: return nameSpace;
164: }
165:
166: /**
167: * @param nameSpace The nameSpace to set.
168: */
169: public synchronized void setNameSpace(Object nameSpace) {
170: this .nameSpace = nameSpace;
171: }
172:
173: public boolean equals(Object o) {
174: if (o instanceof MatchNSQuery) {
175: MatchNSQuery temp = (MatchNSQuery) o;
176: boolean result = nameSpace.equals(temp.nameSpace);
177: if (!result) {
178: return result;
179: }
180: if (className != null && temp.className != null) {
181: result = className.equals(temp.className);
182: if (!result) {
183: return result;
184: }
185: } else {
186: result = className == null & temp.className == null;
187: if (!result) {
188: return result;
189: }
190: }
191:
192: if (propertyName != null && temp.propertyName != null) {
193: result = propertyName.equals(temp.propertyName);
194: if (!result) {
195: return result;
196: }
197: } else {
198: result = propertyName == null
199: & temp.propertyName == null;
200: if (!result) {
201: return result;
202: }
203: }
204: if (value != null && temp.value != null) {
205: result = value.equals(temp.value);
206: if (!result) {
207: return result;
208: }
209: } else {
210: result = value == null & temp.value == null;
211: if (!result) {
212: return result;
213: }
214: }
215: return result;
216: }
217: return false;
218: }
219:
220: public int hashCode() {
221: if (hashCode == 0) {
222: int temp = nameSpace == null ? 0 : nameSpace.hashCode();
223: if (className != null) {
224: temp += className.hashCode();
225: }
226: if (propertyName != null) {
227: temp += propertyName.hashCode();
228: }
229: if (value != null) {
230: temp += value.hashCode();
231: }
232: hashCode = temp;
233:
234: }
235: return hashCode;
236: }
237:
238: public Object getAlias() {
239: return alias;
240: }
241:
242: public QueryType getQueryType() {
243:
244: return QUERY_ID;
245: }
246:
247: public IndexQuery setParameter(String name, Object value) {
248: throw new UnsupportedOperationException(
249: "Parameters are not supported for convenience classes");
250: }
251:
252: /* (non-Javadoc)
253: * @see com.jofti.api.IndexQuery#setParameter(int, java.lang.Object)
254: */
255: public IndexQuery setParameter(int position, Object value) {
256: throw new UnsupportedOperationException(
257: "Parameters are not supported for convenience classes");
258:
259: }
260:
261: public IndexQuery setFirstResult(int firstResult) {
262: throw new UnsupportedOperationException(
263: "Result limitation is not supported for convenience classes");
264: }
265:
266: public IndexQuery setMaxResults(int maxResults) {
267: throw new UnsupportedOperationException(
268: "Result limitation is not supported for convenience classes");
269: }
270: }
|