1 15 package org.josql.functions; 16 17 import java.io.File ; 18 19 import java.util.Random ; 20 import java.util.List ; 21 import java.util.Date ; 22 import java.util.Calendar ; 23 import java.util.Map ; 24 import java.util.HashMap ; 25 26 import com.gentlyweb.utils.Getter; 27 28 import org.josql.Query; 29 import org.josql.QueryExecutionException; 30 31 import org.josql.expressions.Expression; 32 33 public class MiscellaneousFunctions extends AbstractFunctionHandler 34 { 35 36 public static final String HANDLER_ID = "_internal_misc"; 37 38 private Map accessorCache = new HashMap (); 39 40 private Random rand = new Random (); 41 42 49 public Date now (boolean zeroTime) 50 { 51 52 Date d = null; 53 54 if (zeroTime) 55 { 56 57 Calendar c = Calendar.getInstance (); 58 59 c.set (Calendar.HOUR_OF_DAY, 60 0); 61 c.set (Calendar.MINUTE, 62 0); 63 c.set (Calendar.SECOND, 64 0); 65 c.set (Calendar.MILLISECOND, 66 0); 67 68 d = c.getTime (); 69 70 } else { 71 72 d = new Date (); 73 74 } 75 76 return d; 77 78 } 79 80 public void cache (List allobjs, 81 Getter get) 82 throws QueryExecutionException 83 { 84 85 int s = allobjs.size (); 86 87 for (int i = 0; i < s; i++) 88 { 89 90 Object o = allobjs.get (i); 91 92 try 93 { 94 95 this.q.setSaveValue (o, 96 get.getValue (o)); 97 98 } catch (Exception e) { 99 100 throw new QueryExecutionException ("Unable to get value from accessor: " + 101 get + 102 " from object: " + 103 i, 104 e); 105 106 } 107 108 } 109 110 } 111 112 public void cache (List allobjs, 113 Expression exp) 114 throws QueryExecutionException 115 { 116 117 Object co = this.q.getCurrentObject (); 118 119 int s = allobjs.size (); 120 121 for (int i = 0; i < s; i++) 122 { 123 124 Object o = allobjs.get (i); 125 126 this.q.setCurrentObject (o); 127 128 try 129 { 130 131 this.q.setSaveValue (o, 132 exp.getValue (o, 133 q)); 134 135 } catch (Exception e) { 136 137 throw new QueryExecutionException ("Unable to get value from expression: " + 138 exp + 139 " from object: " + 140 i, 141 e); 142 143 } 144 145 } 146 147 this.q.setCurrentObject (co); 148 149 } 150 151 public double abs (Number d) 152 { 153 154 return Math.abs (d.doubleValue ()); 155 156 } 157 158 public int random () 159 { 160 161 return this.rand.nextInt (); 162 163 } 164 165 public int random (Number n) 166 { 167 168 return this.rand.nextInt (n.intValue ()); 169 170 } 171 172 public double randomDouble () 173 { 174 175 return this.rand.nextDouble (); 176 177 } 178 179 public Object saveValue (Object saveValueName) 180 { 181 182 return this.q.getSaveValue (saveValueName); 183 184 } 185 186 public Object savevalue (Object saveValueName) 187 { 188 189 return this.q.getSaveValue (saveValueName); 190 191 } 192 193 public Object save_value (Object saveValueName) 194 { 195 196 return this.q.getSaveValue (saveValueName); 197 198 } 199 200 public String fileExtension (Object f) 201 { 202 203 if (f == null) 204 { 205 206 return null; 207 208 } 209 210 String n = null; 211 212 if (f instanceof String ) 213 { 214 215 n = (String ) f; 216 217 } 218 219 if (f instanceof File ) 220 { 221 222 File fi = (File ) f; 223 224 if (fi.isDirectory ()) 225 { 226 227 return null; 228 229 } 230 231 n = ((File ) f).getName (); 232 233 } 234 235 return n.substring (n.lastIndexOf (".") + 1); 236 237 } 238 239 247 public Object accessor (Expression oExp, 248 Expression accExp) 249 throws Exception  250 { 251 252 Object o = null; 254 255 try 256 { 257 258 o = oExp.getValue (this.q.getCurrentObject (), 259 this.q); 260 261 } catch (Exception e) { 262 263 throw new QueryExecutionException ("Unable to evaluate expression: " + 264 oExp + 265 " to get object."); 266 267 } 268 269 Object a = null; 270 271 try 272 { 273 274 a = accExp.getValue (this.q.getCurrentObject (), 275 this.q); 276 277 } catch (Exception e) { 278 279 throw new QueryExecutionException ("Unable to evaluate expression: " + 280 accExp + 281 " to get accessor."); 282 283 } 284 285 if (a == null) 286 { 287 288 throw new QueryExecutionException ("Accessor expression: " + 289 accExp + 290 " evaluates to null for object: " + 291 o + 292 " returned from expression: " + 293 oExp); 294 295 } 296 297 return this.accessor (o, 299 a.toString ()); 300 301 } 302 303 311 public Object accessor (Object o, 312 String acc) 313 throws Exception  314 { 315 316 if (o == null) 317 { 318 319 return null; 320 321 } 322 323 Getter g = (Getter) this.accessorCache.get (o.getClass ().getName () + acc); 325 326 if (g == null) 327 { 328 329 g = new Getter (acc, 331 o.getClass ()); 332 333 this.accessorCache.put (o.getClass ().getName () + acc, 334 g); 335 336 } 337 338 return g.getValue (o); 339 340 } 341 342 public Object ifThen (Expression ifcond, 343 Expression thenVal) 344 throws QueryExecutionException 345 { 346 347 if (ifcond.isTrue (this.q.getCurrentObject (), 348 this.q)) 349 { 350 351 return thenVal.getValue (this.q.getCurrentObject (), 352 this.q); 353 354 } 355 356 return null; 357 358 } 359 360 public Object ifThenElse (Expression ifcond, 361 Expression thenVal, 362 Expression elseVal) 363 throws QueryExecutionException 364 { 365 366 Object i = this.ifThen (ifcond, 367 thenVal); 368 369 if (i == null) 370 { 371 372 return elseVal.getValue (this.q.getCurrentObject (), 373 this.q); 374 375 } 376 377 return i; 378 379 } 380 381 public Object eval (Expression exp) 382 throws QueryExecutionException 383 { 384 385 return exp.getValue (this.q.getCurrentObject (), 386 this.q); 387 388 } 389 390 409 public Boolean instanceOf (Expression obj, 410 Expression clazz) 411 throws QueryExecutionException 412 { 413 414 return Boolean.valueOf (clazz.getValue (this.q.getCurrentObject (), 415 q).getClass ().isInstance (obj.getValue (this.q.getCurrentObject (), 416 q))); 417 418 } 419 420 } 421 | Popular Tags |