1 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 name = null; 29 private String acc = null; 30 private Getter get = null; 31 32 public Class getExpectedReturnType (Query q) 33 throws QueryParseException 34 { 35 36 Object sv = q.getSaveValue (this.name); 38 39 if (sv != null) 40 { 41 42 if (this.acc != null) 43 { 44 45 try 47 { 48 49 this.get = new Getter (this.acc, 50 sv.getClass ()); 51 52 } catch (Exception 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 return Object .class; 74 75 } 76 77 public void init (Query q) 78 { 79 80 82 } 83 84 public String getName () 85 { 86 87 return this.name; 88 89 } 90 91 public void setName (String name) 92 { 93 94 this.name = name; 95 96 } 97 98 public Object getValue (Object o, 99 Query q) 100 throws QueryExecutionException 101 { 102 103 Object v = q.getSaveValue (this.name); 104 105 if (v == null) 106 { 107 108 return v; 109 110 } 111 112 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 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 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 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 return true; 190 191 } 192 193 public String getAccessor () 194 { 195 196 return this.acc; 197 198 } 199 200 public void setAccessor (String acc) 201 { 202 203 this.acc = acc; 204 205 } 206 207 public Object evaluate (Object o, 208 Query q) 209 throws QueryExecutionException 210 { 211 212 return this.getValue (o, 213 q); 214 215 } 216 217 public String toString () 218 { 219 220 StringBuffer buf = new StringBuffer (); 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 return false; 251 252 } 253 254 } 255 | Popular Tags |