1 15 package org.josql.functions; 16 17 import java.util.Map ; 18 import java.util.HashMap ; 19 import java.util.Date ; 20 import java.util.GregorianCalendar ; 21 import java.util.Calendar ; 22 23 import java.text.SimpleDateFormat ; 24 25 import org.josql.Query; 26 import org.josql.QueryExecutionException; 27 28 32 public class ConversionFunctions extends AbstractFunctionHandler 33 { 34 35 public static final String HANDLER_ID = "_internal_conversion"; 36 37 40 public static final String MINUTE = "mi"; 41 42 45 public static final String DAY = "d"; 46 47 50 public static final String YEAR = "y"; 51 52 55 public static final String SECOND = "s"; 56 57 60 public static final String HOUR = "h"; 61 62 65 public static final String MONTH = "m"; 66 67 70 public static final String WEEK = "w"; 71 72 public static String DEFAULT_DATE_FORMAT_SPEC = "dd/MMM/yyyy"; 73 public static String DEFAULT_DATE_FORMAT_SPEC_2 = "dd-MMM-yyyy"; 74 public static String DEFAULT_DATE_FORMAT_SPEC_3 = "dd MMM yyyy"; 75 76 private static Map sdfs = new HashMap (); 77 private static Calendar cal = Calendar.getInstance (); 78 79 static 80 { 81 82 ConversionFunctions.sdfs.put (ConversionFunctions.DEFAULT_DATE_FORMAT_SPEC, 83 new SimpleDateFormat (ConversionFunctions.DEFAULT_DATE_FORMAT_SPEC)); 84 ConversionFunctions.sdfs.put (ConversionFunctions.DEFAULT_DATE_FORMAT_SPEC_2, 85 new SimpleDateFormat (ConversionFunctions.DEFAULT_DATE_FORMAT_SPEC_2)); 86 ConversionFunctions.sdfs.put (ConversionFunctions.DEFAULT_DATE_FORMAT_SPEC_3, 87 new SimpleDateFormat (ConversionFunctions.DEFAULT_DATE_FORMAT_SPEC_3)); 88 89 } 90 91 103 public int timeField (Object d, 104 String type) 105 throws QueryExecutionException 106 { 107 108 if ((!(d instanceof Date )) 109 && 110 (!(d instanceof Long )) 111 ) 112 { 113 114 throw new QueryExecutionException ("Value passed in is of type: " + 115 d.getClass ().getName () + 116 " only: " + 117 Long .class.getName () + 118 " or: " + 119 Date .class.getName () + 120 " are supported."); 121 122 } 123 124 Date date = null; 125 126 if (d instanceof Long ) 127 { 128 129 date = new Date (((Long ) d).longValue ()); 130 131 } 132 133 if (d instanceof Date ) 134 { 135 136 date = (Date ) d; 137 138 } 139 140 ConversionFunctions.cal.setTime (date); 141 142 type = type.toLowerCase (); 143 144 if (type.equals (ConversionFunctions.SECOND)) 145 { 146 147 return ConversionFunctions.cal.get (Calendar.SECOND); 148 149 } 150 151 if (type.equals (ConversionFunctions.MINUTE)) 152 { 153 154 return ConversionFunctions.cal.get (Calendar.MINUTE); 155 156 } 157 158 if (type.equals (ConversionFunctions.HOUR)) 159 { 160 161 return ConversionFunctions.cal.get (Calendar.HOUR_OF_DAY); 162 163 } 164 165 if (type.equals (ConversionFunctions.DAY)) 166 { 167 168 return ConversionFunctions.cal.get (Calendar.DATE); 169 170 } 171 172 if (type.equals (ConversionFunctions.WEEK)) 173 { 174 175 return ConversionFunctions.cal.get (Calendar.WEEK_OF_YEAR); 176 177 } 178 179 if (type.equals (ConversionFunctions.MONTH)) 180 { 181 182 return ConversionFunctions.cal.get (Calendar.MONTH); 183 184 } 185 186 if (type.equals (ConversionFunctions.YEAR)) 187 { 188 189 return ConversionFunctions.cal.get (Calendar.YEAR); 190 191 } 192 193 return -1; 195 196 } 197 198 public Date addTime (Date d, 199 Double amount, 200 String type) 201 { 202 203 int a = amount.intValue (); 204 205 long v = d.getTime (); 206 207 if (type.equals (ConversionFunctions.SECOND)) 208 { 209 210 v += (a * 1000); 211 212 return new Date (v); 213 214 } 215 216 if (type.equals (ConversionFunctions.MINUTE)) 217 { 218 219 v += (a * 60000); 220 221 return new Date (v); 222 223 } 224 225 if (type.equals (ConversionFunctions.HOUR)) 226 { 227 228 v += (a * 3600000); 229 230 return new Date (v); 231 232 } 233 234 if (type.equals (ConversionFunctions.DAY)) 235 { 236 237 v += (a * 24 * 3600000); 238 239 return new Date (v); 240 241 } 242 243 if (type.equals (ConversionFunctions.WEEK)) 244 { 245 246 v += (a * 7 * 24 * 3600000); 247 248 return new Date (v); 249 250 } 251 252 if (type.equals (ConversionFunctions.MONTH)) 253 { 254 255 GregorianCalendar gc = new GregorianCalendar (); 257 gc.setTime (d); 258 259 gc.add (Calendar.MONTH, 260 a); 261 262 return gc.getTime (); 263 264 } 265 266 if (type.equals (ConversionFunctions.YEAR)) 267 { 268 269 GregorianCalendar gc = new GregorianCalendar (); 271 gc.setTime (d); 272 273 gc.add (Calendar.YEAR, 274 a); 275 276 return gc.getTime (); 277 278 } 279 280 return d; 282 283 } 284 285 public Date toDate (Object value) 286 throws QueryExecutionException 287 { 288 289 if (value == null) 290 { 291 292 return null; 293 294 } 295 296 if (value instanceof Number ) 297 { 298 299 return new Date (((Number ) value).longValue ()); 300 301 } 302 303 if (value instanceof String ) 304 { 305 306 return this.toDate ((String ) value, 307 null); 308 309 } 310 311 if (value instanceof Date ) 312 { 313 314 return (Date ) value; 315 316 } 317 318 throw new QueryExecutionException ("Type: " + value.getClass ().getName () + " is not supported."); 319 320 } 321 322 public Date to_date (Object value) 323 throws QueryExecutionException 324 { 325 326 return this.toDate (value); 327 328 } 329 330 public Date to_date (String value, 331 String spec) 332 throws QueryExecutionException 333 { 334 335 return this.toDate (value, 336 spec); 337 338 } 339 340 public Date toDate (String value, 341 String spec) 342 throws QueryExecutionException 343 { 344 345 if (spec == null) 346 { 347 348 spec = ConversionFunctions.DEFAULT_DATE_FORMAT_SPEC; 349 350 } 351 352 SimpleDateFormat df = (SimpleDateFormat ) ConversionFunctions.sdfs.get (spec); 353 354 if (df == null) 355 { 356 357 df = new SimpleDateFormat (spec); 358 359 ConversionFunctions.sdfs.put (spec, 360 df); 361 362 } 363 364 try 365 { 366 367 return df.parse (value); 368 369 } catch (Exception e) { 370 371 throw new QueryExecutionException ("Unable to parse date value: " + 372 value + 373 " using spec: " + 374 spec, 375 e); 376 377 } 378 379 } 380 381 public Long toMillis (Date d) 382 { 383 384 return new Long (d.getTime ()); 385 386 } 387 388 public Long toDateMillis (String value, 389 String spec) 390 throws QueryExecutionException 391 { 392 393 if (spec == null) 394 { 395 396 spec = ConversionFunctions.DEFAULT_DATE_FORMAT_SPEC; 397 398 } 399 400 SimpleDateFormat df = (SimpleDateFormat ) ConversionFunctions.sdfs.get (spec); 401 402 if (df == null) 403 { 404 405 df = new SimpleDateFormat (spec); 406 407 ConversionFunctions.sdfs.put (spec, 408 df); 409 410 } 411 412 try 413 { 414 415 Date d = df.parse (value); 416 417 return new Long (d.getTime ()); 418 419 } catch (Exception e) { 420 421 throw new QueryExecutionException ("Unable to parse date value: " + 422 value + 423 " using spec: " + 424 spec, 425 e); 426 427 } 428 429 } 430 431 public String upper (Object o) 432 { 433 434 if (o == null) 435 { 436 437 return null; 438 439 } 440 441 return o.toString ().toUpperCase (); 442 443 } 444 445 public String lower (Object o) 446 { 447 448 if (o == null) 449 { 450 451 return null; 452 453 } 454 455 return o.toString ().toLowerCase (); 456 457 } 458 459 public String to_string (Object o) 460 { 461 462 return this.toString (o); 463 464 } 465 466 public String toString (Object o) 467 { 468 469 return o + ""; 470 471 } 472 473 public Number to_number (Object o) 474 { 475 476 return this.toNumber (o); 477 478 } 479 480 public Number toNumber (Object o) 481 { 482 483 if (o == null) 484 { 485 486 return null; 487 488 } 489 490 if (o instanceof String ) 491 { 492 493 try 495 { 496 497 return new Double ((String ) o); 498 499 } catch (Exception e) { 500 501 503 } 504 505 } 506 507 if (o instanceof Date ) 508 { 509 510 return new Double (((Date ) o).getTime ()); 511 512 } 513 514 if (!(o instanceof Number )) 515 { 516 517 return null; 518 519 } 520 521 return (Number ) o; 522 523 } 524 525 } 526 | Popular Tags |