KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > josql > expressions > SaveValue


1 /*
2  * Copyright 2004-2005 Gary Bentley
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may
5  * not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */

15 package org.josql.expressions;
16
17 import com.gentlyweb.utils.Getter;
18
19 import org.josql.Query;
20 import org.josql.QueryExecutionException;
21 import org.josql.QueryParseException;
22
23 import org.josql.internal.Utilities;
24
25 public class SaveValue extends ValueExpression
26 {
27
28     private String JavaDoc name = null;
29     private String JavaDoc acc = null;
30     private Getter get = null;
31
32     public Class JavaDoc getExpectedReturnType (Query q)
33                                     throws QueryParseException
34     {
35
36     // See if the save value is already present.
37
Object JavaDoc sv = q.getSaveValue (this.name);
38
39     if (sv != null)
40     {
41
42         if (this.acc != null)
43         {
44
45         // Init the getter.
46
try
47         {
48
49             this.get = new Getter (this.acc,
50                        sv.getClass ());
51
52         } catch (Exception JavaDoc e) {
53             
54             throw new QueryParseException ("Unable to create dynamic getter from instance of type: " +
55                            sv.getClass ().getName () +
56                            " for save value: " +
57                            this.name +
58                            " using accessor: " +
59                            this.acc,
60                            e);
61
62         }
63
64         return this.get.getType ();
65
66         }
67
68         return sv.getClass ();
69
70     }
71
72     // No idea what it could be...
73
return Object JavaDoc.class;
74
75     }
76
77     public void init (Query q)
78     {
79
80     // Nothing to do...
81

82     }
83
84     public String JavaDoc getName ()
85     {
86
87     return this.name;
88
89     }
90
91     public void setName (String JavaDoc name)
92     {
93
94     this.name = name;
95
96     }
97
98     public Object JavaDoc getValue (Object JavaDoc o,
99                 Query q)
100                         throws QueryExecutionException
101     {
102
103     Object JavaDoc v = q.getSaveValue (this.name);
104
105     if (v == null)
106     {
107
108         return v;
109
110     }
111
112     // See if we have an accessor...
113

114     if ((this.acc != null)
115         &&
116         (this.get == null)
117        )
118     {
119
120         try
121         {
122
123         this.get = new Getter (this.acc,
124                        v.getClass ());
125
126         } catch (Exception JavaDoc e) {
127
128         throw new QueryExecutionException ("Unable to create dynamic getter from instance of type: " +
129                            v.getClass ().getName () +
130                            " for save value: " +
131                            this.name +
132                            " using accessor: " +
133                            this.acc,
134                            e);
135
136         }
137
138     }
139
140     if (this.get != null)
141     {
142
143         try
144         {
145
146         v = this.get.getValue (v);
147
148         } catch (Exception JavaDoc e) {
149
150         throw new QueryExecutionException ("Unable to get value from instance of type: " +
151                            v.getClass ().getName () +
152                            " for save value: " +
153                            this.name +
154                            " using accessor: " +
155                            this.acc,
156                            e);
157
158         }
159
160     }
161
162     return v;
163
164     }
165
166     public boolean isTrue (Object JavaDoc o,
167                Query q)
168                        throws QueryExecutionException
169     {
170
171     o = this.getValue (o,
172                q);
173
174     if (o == null)
175     {
176         
177         return false;
178
179     }
180
181     if (Utilities.isNumber (o))
182     {
183
184         return Utilities.getDouble (o) > 0;
185
186     }
187
188     // Not null so return true...
189
return true;
190
191     }
192
193     public String JavaDoc getAccessor ()
194     {
195
196     return this.acc;
197
198     }
199
200     public void setAccessor (String JavaDoc acc)
201     {
202
203     this.acc = acc;
204
205     }
206
207     public Object JavaDoc evaluate (Object JavaDoc o,
208                 Query q)
209                         throws QueryExecutionException
210     {
211
212     return this.getValue (o,
213                   q);
214
215     }
216
217     public String JavaDoc toString ()
218     {
219
220     StringBuffer JavaDoc buf = new StringBuffer JavaDoc ();
221
222     buf.append ("@");
223     buf.append (this.name);
224
225     if (this.acc != null)
226     {
227
228         buf.append (".");
229         buf.append (this.acc);
230
231     }
232
233     if (this.isBracketed ())
234     {
235
236         buf.insert (0,
237             "(");
238         buf.append (")");
239
240     }
241
242     return buf.toString ();
243
244     }
245
246     public boolean hasFixedResult (Query q)
247     {
248
249     // A save value cannot have a fixed result.
250
return false;
251
252     }
253
254 }
255
Popular Tags