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: public class SaveValue extends ValueExpression {
026:
027: private String name = null;
028: private String acc = null;
029: private Getter get = null;
030:
031: public Class getExpectedReturnType(Query q)
032: throws QueryParseException {
033:
034: // See if the save value is already present.
035: Object sv = q.getSaveValue(this .name);
036:
037: if (sv != null) {
038:
039: if (this .acc != null) {
040:
041: // Init the getter.
042: try {
043:
044: this .get = new Getter(this .acc, sv.getClass());
045:
046: } catch (Exception e) {
047:
048: throw new QueryParseException(
049: "Unable to create dynamic getter from instance of type: "
050: + sv.getClass().getName()
051: + " for save value: " + this .name
052: + " using accessor: " + this .acc, e);
053:
054: }
055:
056: return this .get.getType();
057:
058: }
059:
060: return sv.getClass();
061:
062: }
063:
064: // No idea what it could be...
065: return Object.class;
066:
067: }
068:
069: public void init(Query q) {
070:
071: // Nothing to do...
072:
073: }
074:
075: public String getName() {
076:
077: return this .name;
078:
079: }
080:
081: public void setName(String name) {
082:
083: this .name = name;
084:
085: }
086:
087: public Object getValue(Object o, Query q)
088: throws QueryExecutionException {
089:
090: Object v = q.getSaveValue(this .name);
091:
092: if (v == null) {
093:
094: return v;
095:
096: }
097:
098: // See if we have an accessor...
099:
100: if ((this .acc != null) && (this .get == null)) {
101:
102: try {
103:
104: this .get = new Getter(this .acc, v.getClass());
105:
106: } catch (Exception e) {
107:
108: throw new QueryExecutionException(
109: "Unable to create dynamic getter from instance of type: "
110: + v.getClass().getName()
111: + " for save value: " + this .name
112: + " using accessor: " + this .acc, e);
113:
114: }
115:
116: }
117:
118: if (this .get != null) {
119:
120: try {
121:
122: v = this .get.getValue(v);
123:
124: } catch (Exception e) {
125:
126: throw new QueryExecutionException(
127: "Unable to get value from instance of type: "
128: + v.getClass().getName()
129: + " for save value: " + this .name
130: + " using accessor: " + this .acc, e);
131:
132: }
133:
134: }
135:
136: return v;
137:
138: }
139:
140: public boolean isTrue(Object o, Query q)
141: throws QueryExecutionException {
142:
143: o = this .getValue(o, q);
144:
145: if (o == null) {
146:
147: return false;
148:
149: }
150:
151: if (Utilities.isNumber(o)) {
152:
153: return Utilities.getDouble(o) > 0;
154:
155: }
156:
157: // Not null so return true...
158: return true;
159:
160: }
161:
162: public String getAccessor() {
163:
164: return this .acc;
165:
166: }
167:
168: public void setAccessor(String acc) {
169:
170: this .acc = acc;
171:
172: }
173:
174: public Object evaluate(Object o, Query q)
175: throws QueryExecutionException {
176:
177: return this .getValue(o, q);
178:
179: }
180:
181: public String toString() {
182:
183: StringBuffer buf = new StringBuffer();
184:
185: buf.append("@");
186: buf.append(this .name);
187:
188: if (this .acc != null) {
189:
190: buf.append(".");
191: buf.append(this .acc);
192:
193: }
194:
195: if (this .isBracketed()) {
196:
197: buf.insert(0, "(");
198: buf.append(")");
199:
200: }
201:
202: return buf.toString();
203:
204: }
205:
206: public boolean hasFixedResult(Query q) {
207:
208: // A save value cannot have a fixed result.
209: return false;
210:
211: }
212:
213: }
|