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