001: /*
002: * Copyright 2004-2007 Gary Bentley
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may
005: * not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015: package org.josql.expressions;
016:
017: import com.gentlyweb.utils.Getter;
018:
019: import org.josql.Query;
020: import org.josql.QueryExecutionException;
021: import org.josql.QueryParseException;
022:
023: import org.josql.internal.Utilities;
024:
025: /**
026: * Represents an "accessor" into an object. An accessor is basically a dot separated list
027: * of method names, such as: <code>myObj.id.name</code>.
028: * <p>
029: * All of the methods referenced must have no arguments and be "public" in the referring class.
030: * You can use either the actual method name or the JavaBean naming convention.
031: * Thus: <code>myObj.id.name</code> might also be represented as: <code>getMyObj.getId.getName</code>.
032: */
033: public class Accessor extends ValueExpression {
034:
035: private String acc = null;
036: private Getter get = null;
037:
038: public Class getExpectedReturnType(Query q)
039: throws QueryParseException {
040:
041: return this .get.getType();
042:
043: }
044:
045: public void init(Query q) throws QueryParseException {
046:
047: // Now init the getter.
048: try {
049:
050: this .get = new Getter(this .acc, q.getFromObjectClass());
051:
052: } catch (Exception e) {
053:
054: throw new QueryParseException("Unable to create getter: "
055: + this .acc, e);
056:
057: }
058:
059: }
060:
061: public String getAccessor() {
062:
063: return this .acc;
064:
065: }
066:
067: public void setAccessor(String a) {
068:
069: this .acc = a;
070:
071: }
072:
073: public Getter getGetter() {
074:
075: return this .get;
076:
077: }
078:
079: public void setName(String name) {
080:
081: this .acc = name;
082:
083: }
084:
085: public boolean isTrue(Object o, Query q)
086: throws QueryExecutionException {
087:
088: o = this .evaluate(o, q);
089:
090: if (o == null) {
091:
092: return false;
093:
094: }
095:
096: if (Utilities.isNumber(o)) {
097:
098: return Utilities.getDouble(o) > 0;
099:
100: }
101:
102: if (o instanceof Boolean) {
103:
104: return ((Boolean) o).booleanValue();
105:
106: }
107:
108: // Not null so return true...
109: return true;
110:
111: }
112:
113: public boolean hasFixedResult(Query q) {
114:
115: // Well duh...
116: return false;
117:
118: }
119:
120: public Object evaluate(Object o, Query q)
121: throws QueryExecutionException {
122:
123: try {
124:
125: return this .get.getValue(o);
126:
127: } catch (Exception e) {
128:
129: throw new QueryExecutionException(
130: "Unable to get value from: " + this
131: + " passed in object type: "
132: + o.getClass().getName() + " expecting: "
133: + this .get.getType().getName(), e);
134:
135: }
136:
137: }
138:
139: public boolean equals(Object o) {
140:
141: if (o == null) {
142:
143: return false;
144:
145: }
146:
147: if (!(o instanceof Accessor)) {
148:
149: return false;
150:
151: }
152:
153: Accessor a = (Accessor) o;
154:
155: return this .acc.equals(a.getAccessor());
156:
157: }
158:
159: public String toString() {
160:
161: if (this .isBracketed()) {
162:
163: return "(" + this .acc + ")";
164:
165: }
166:
167: return this .acc + "[detail: " + this .get + "]";
168:
169: }
170:
171: }
|