1 15 package org.josql.functions; 16 17 import java.util.List ; 18 import java.util.ArrayList ; 19 import java.util.Date ; 20 21 import java.text.SimpleDateFormat ; 22 import java.text.DecimalFormat ; 23 24 import com.gentlyweb.utils.TimeDuration; 25 import com.gentlyweb.utils.Timing; 26 import com.gentlyweb.utils.Getter; 27 28 import org.josql.Query; 29 import org.josql.QueryExecutionException; 30 31 public class FormattingFunctions extends AbstractFunctionHandler 32 { 33 34 public static final String HANDLER_ID = "_internal_formatting"; 35 36 public static String DEFAULT_DATE_FORMAT_SPEC = "dd/MMM/yyyy"; 37 public static String DEFAULT_DATE_TIME_FORMAT_SPEC = FormattingFunctions.DEFAULT_DATE_FORMAT_SPEC + 38 ", hh:mm:ss"; 39 public static String DEFAULT_DECIMAL_FORMAT_SPEC = "###,###,###.##"; 40 41 private SimpleDateFormat defSDF = new SimpleDateFormat (FormattingFunctions.DEFAULT_DATE_FORMAT_SPEC); 42 private SimpleDateFormat defSDTF = new SimpleDateFormat (FormattingFunctions.DEFAULT_DATE_TIME_FORMAT_SPEC); 43 44 public String formatTimeDuration (Object o) 45 throws QueryExecutionException 46 { 47 48 if (o instanceof Number ) 49 { 50 51 return TimeDuration.getInstance (((Number ) o).longValue ()).format (); 52 53 } 54 55 if (o instanceof Date ) 56 { 57 58 return TimeDuration.getInstance ((Date ) o).format (); 59 60 } 61 62 if (o instanceof TimeDuration) 63 { 64 65 return TimeDuration.getInstance ((TimeDuration) o).format (); 66 67 } 68 69 if (o instanceof Timing) 70 { 71 72 return TimeDuration.getInstance ((Timing) o).format (); 73 74 } 75 76 throw new QueryExecutionException ("Type: " + 77 o.getClass ().getName () + 78 " not supported."); 79 80 } 81 82 public void setDefaultDateFormatSpec (String spec) 83 { 84 85 this.defSDF = new SimpleDateFormat (spec); 86 87 } 88 89 public String formatDate (Object o) 90 throws QueryExecutionException 91 { 92 93 if (o == null) 94 { 95 96 throw new QueryExecutionException ("Cannot format a null date."); 97 98 } 99 100 Date d = null; 101 102 if (o instanceof Date ) 103 { 104 105 d = (Date ) o; 106 107 } 108 109 if (o instanceof Number ) 110 { 111 112 d = new Date (((Number ) o).longValue ()); 113 114 } 115 116 if (o instanceof String ) 119 { 120 121 d = ((ConversionFunctions) this.q.getFunctionHandler (ConversionFunctions.HANDLER_ID)).toDate ((String ) o); 122 123 } 124 125 if (d == null) 126 { 127 128 throw new QueryExecutionException ("Type: " + 129 o.getClass ().getName () + 130 " not supported."); 131 132 } 133 134 return this.defSDF.format (d); 135 136 } 137 138 public String formatDateTime (Object o) 139 throws QueryExecutionException 140 { 141 142 if (o == null) 143 { 144 145 throw new QueryExecutionException ("Cannot format a null date."); 146 147 } 148 149 Date d = null; 150 151 if (o instanceof Date ) 152 { 153 154 d = (Date ) o; 155 156 } 157 158 if (o instanceof Number ) 159 { 160 161 d = new Date (((Number ) o).longValue ()); 162 163 } 164 165 if (o instanceof String ) 168 { 169 170 d = ((ConversionFunctions) this.q.getFunctionHandler (ConversionFunctions.HANDLER_ID)).toDate ((String ) o); 171 172 } 173 174 if (d == null) 175 { 176 177 throw new QueryExecutionException ("Type: " + 178 o.getClass ().getName () + 179 " not supported."); 180 181 } 182 183 return this.defSDTF.format (d); 184 185 } 186 187 public String formatDate (Query q, 188 Object o, 189 Getter g, 190 String spec, 191 String saveValueName) 192 throws QueryExecutionException 193 { 194 195 if (g != null) 196 { 197 198 try 199 { 200 201 o = g.getValue (o); 202 203 } catch (Exception e) { 204 205 throw new QueryExecutionException ("Unable to get value from accessor: " + 206 g, 207 e); 208 209 } 210 211 } 212 213 if (o == null) 214 { 215 216 return null + ""; 217 218 } 219 220 Date d = null; 221 222 if (o instanceof Date ) 223 { 224 225 d = (Date ) o; 226 227 } 228 229 if (o instanceof Long ) 230 { 231 232 d = new Date (((Long ) o).longValue ()); 233 234 } 235 236 Object so = null; 237 238 if (saveValueName != null) 239 { 240 241 so = q.getSaveValue (saveValueName); 242 243 } 244 245 SimpleDateFormat df = null; 246 247 if (so != null) 248 { 249 250 df = (SimpleDateFormat ) so; 251 252 } else { 253 254 if (spec == null) 255 { 256 257 spec = FormattingFunctions.DEFAULT_DATE_FORMAT_SPEC; 258 259 } 260 261 df = new SimpleDateFormat (spec); 262 263 } 264 265 return df.format (d); 266 267 } 268 269 public String formatNumber (Object n) 270 throws QueryExecutionException 271 { 272 273 return this.formatNumber (this.q, 274 n, 275 null, 276 null); 277 278 } 279 280 public String formatNumber (Query q, 281 Object o, 282 String spec, 283 String saveValueName) 284 throws QueryExecutionException 285 { 286 287 if (!(o instanceof Number )) 288 { 289 290 if (o == null) 291 { 292 293 return "NaN (null)"; 294 295 } 296 297 return "NaN (" + o.getClass ().getName () + ")"; 298 299 } 300 301 if (o == null) 302 { 303 304 return "0"; 305 306 } 307 308 Object so = null; 309 310 if (saveValueName != null) 311 { 312 313 so = q.getSaveValue (saveValueName); 314 315 } 316 317 Number n = (Number ) o; 318 319 DecimalFormat df = null; 320 321 if (so != null) 322 { 323 324 if (!(so instanceof DecimalFormat )) 325 { 326 327 throw new QueryExecutionException ("Expected save value: \"" + 328 saveValueName + 329 "\" object to be of type: " + 330 DecimalFormat .class.getName () + 331 ", is: " + 332 so.getClass ().getName ()); 333 334 } 335 336 df = (DecimalFormat ) so; 337 338 } else { 339 340 if (spec == null) 341 { 342 343 spec = FormattingFunctions.DEFAULT_DECIMAL_FORMAT_SPEC; 344 345 } 346 347 df = new DecimalFormat (spec); 348 349 } 350 351 return df.format (n.doubleValue ()); 352 353 } 354 355 public String formatNumber (Query q, 356 Object o, 357 Getter g, 358 String spec, 359 String saveValueName) 360 throws QueryExecutionException 361 { 362 363 try 364 { 365 366 o = g.getValue (o); 367 368 } catch (Exception e) { 369 370 throw new QueryExecutionException ("Unable to get value from accessor: " + 371 g, 372 e); 373 374 } 375 376 return this.formatNumber (q, 377 o, 378 spec, 379 saveValueName); 380 381 } 382 383 } 384 | Popular Tags |