1 23 24 package org.dbforms.config; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import org.dbforms.util.IEscaper; 30 import org.dbforms.util.MessageResourcesInternal; 31 import org.dbforms.util.ReflectionUtil; 32 import org.dbforms.util.StringUtil; 33 import org.dbforms.util.Util; 34 35 import java.text.DateFormat ; 36 import java.text.Format ; 37 38 import java.util.Locale ; 39 import java.util.Vector ; 40 41 import java.io.Serializable ; 42 43 48 public class Field implements Serializable { 49 private static Log logCat = LogFactory.getLog(Field.class.getName()); 50 51 private IEscaper escaper = null; 52 53 57 private String directory = null; 58 59 private String escaperClass = null; 60 61 private String expression = null; 62 63 private String fieldType = null; 64 65 66 private String key = "false"; 67 68 69 private String name; 70 71 private Table table = null; 72 73 78 private boolean encoded = false; 79 80 81 private boolean isAutoInc = false; 82 83 84 private boolean isSortable = false; 85 86 87 private int id; 88 89 90 private int size = -1; 91 92 93 private int type; 94 95 101 public void setAutoInc(String autoInc) { 102 this.isAutoInc = Util.getTrue(autoInc); 103 } 104 105 111 public void setDirectory(String directory) { 112 this.directory = directory; 113 } 114 115 120 public String getDirectory() { 121 return directory; 122 } 123 124 130 public void setEncoding(String encoding) { 131 this.encoded = Util.getTrue(encoding); 132 } 133 134 139 public IEscaper getEscaper() { 140 if (escaper == null) { 141 String s = getEscaperClass(); 142 143 if (!Util.isNull(s)) { 144 try { 145 escaper = (IEscaper) ReflectionUtil.newInstance(s); 146 } catch (Exception e) { 147 logCat 148 .error("cannot create the new escaper [" + s + "]", 149 e); 150 } 151 } 152 153 if (escaper == null) { 154 if (getTable() == null) { 155 try { 156 escaper = DbFormsConfigRegistry.instance().lookup() 157 .getEscaper(); 158 } catch (Exception e) { 159 logCat 160 .error("cannot create the new default escaper", 161 e); 162 } 163 } else { 164 escaper = getTable().getEscaper(); 165 } 166 } 167 } 168 169 return escaper; 170 } 171 172 177 public void setEscaperClass(String string) { 178 escaperClass = string; 179 } 180 181 187 public void setExpression(String expression) { 188 this.expression = expression; 189 } 190 191 196 public String getExpression() { 197 return expression; 198 } 199 200 208 public String getFieldName(String core) { 209 StringBuffer sb = new StringBuffer (core); 210 sb.append(getTable().getId()); 211 sb.append("_"); 212 sb.append(getId()); 213 214 return sb.toString(); 215 } 216 217 226 public void setFieldType(String aFieldType) { 227 this.fieldType = aFieldType.toLowerCase(); 228 229 if (fieldType.startsWith("char") || fieldType.startsWith("varchar") 230 || fieldType.startsWith("nvarchar") 231 || fieldType.startsWith("longchar") 232 || fieldType.startsWith("long varchar") 233 || fieldType.startsWith("text")) { 234 type = FieldTypes.CHAR; 235 } else if (fieldType.startsWith("int") 236 || fieldType.startsWith("smallint") 237 || fieldType.startsWith("long") 238 || fieldType.startsWith("tinyint")) { 239 type = FieldTypes.INTEGER; 240 } else if (fieldType.startsWith("numeric") 241 || fieldType.startsWith("number") 242 || fieldType.startsWith("decimal")) { 243 type = FieldTypes.NUMERIC; 244 } else if (fieldType.startsWith("date")) { 245 type = FieldTypes.DATE; 246 } else if (fieldType.startsWith("timestamp")) { 247 type = FieldTypes.TIMESTAMP; 248 } else if (fieldType.startsWith("time")) { 249 type = FieldTypes.TIME; 250 } else if (fieldType.startsWith("double") 251 || fieldType.startsWith("float")) { 252 type = FieldTypes.DOUBLE; 253 } else if (fieldType.startsWith("real")) { 254 type = FieldTypes.FLOAT; 255 } else if (fieldType.startsWith("blob") 256 || fieldType.startsWith("image")) { 257 type = FieldTypes.BLOB; 258 } else if (fieldType.startsWith("diskblob")) { 259 type = FieldTypes.DISKBLOB; 260 } else if (fieldType.startsWith("bool")) { 261 type = FieldTypes.BOOLEAN; 262 } 263 } 264 265 270 public String getFieldType() { 271 return fieldType; 272 } 273 274 284 public Format getFormat(String pattern, Locale locale) { 285 switch (getType()) { 286 case FieldTypes.INTEGER: 287 case FieldTypes.NUMERIC: 288 case FieldTypes.DOUBLE: 289 case FieldTypes.FLOAT: 290 case FieldTypes.DATE: 291 case FieldTypes.TIME: 292 case FieldTypes.TIMESTAMP: 293 break; 294 295 default: 296 return null; 297 } 298 299 Format res = null; 300 int dateStyle = Constants.DATE_STYLE_DEFAULT; 301 int timeStyle = Constants.TIME_STYLE_DEFAULT; 302 303 if (Util.isNull(pattern)) { 304 pattern = MessageResourcesInternal.getMessage("dbforms.pattern." 305 + getFieldType(), locale); 306 } 307 308 if (!Util.isNull(pattern)) { 309 if ("short".startsWith(pattern.toLowerCase())) { 310 dateStyle = DateFormat.SHORT; 311 pattern = MessageResourcesInternal.getMessage( 312 "dbforms.pattern." + getFieldType() + "." + pattern, 313 locale); 314 } else if ("medium".startsWith(pattern.toLowerCase())) { 315 dateStyle = DateFormat.MEDIUM; 316 pattern = MessageResourcesInternal.getMessage( 317 "dbforms.pattern." + getFieldType() + "." + pattern, 318 locale); 319 } else if ("long".startsWith(pattern.toLowerCase())) { 320 dateStyle = DateFormat.LONG; 321 pattern = MessageResourcesInternal.getMessage( 322 "dbforms.pattern." + getFieldType() + "." + pattern, 323 locale); 324 } else if ("full".startsWith(pattern.toLowerCase())) { 325 dateStyle = DateFormat.FULL; 326 pattern = MessageResourcesInternal.getMessage( 327 "dbforms.pattern." + getFieldType() + "." + pattern, 328 locale); 329 } 330 } 331 332 switch (getType()) { 333 case FieldTypes.INTEGER: 334 335 try { 336 res = java.text.NumberFormat.getIntegerInstance(locale); 337 } catch (NoSuchMethodError nsme) { 338 res = java.text.NumberFormat.getNumberInstance(locale); 339 } 340 341 ((java.text.DecimalFormat ) res).setParseIntegerOnly(true); 342 343 if (!Util.isNull(pattern)) { 344 try { 345 ((java.text.DecimalFormat ) res).applyPattern(pattern); 346 } catch (Exception e) { 347 logCat.error(pattern, e); 348 } 349 } 350 351 break; 352 353 case FieldTypes.NUMERIC: 354 case FieldTypes.DOUBLE: 355 case FieldTypes.FLOAT: 356 res = java.text.NumberFormat.getNumberInstance(locale); 357 if (!Util.isNull(pattern)) { 358 try { 359 ((java.text.DecimalFormat ) res).applyPattern(pattern); 360 } catch (Exception e) { 361 logCat.error(pattern, e); 362 } 363 } 364 break; 365 366 case FieldTypes.DATE: 367 res = java.text.DateFormat.getDateInstance(dateStyle, locale); 368 if (!Util.isNull(pattern)) { 369 try { 370 ((java.text.SimpleDateFormat ) res).applyPattern(pattern); 371 } catch (Exception e) { 372 logCat.error(pattern, e); 373 } 374 } 375 376 break; 377 378 case FieldTypes.TIME: 379 res = java.text.DateFormat.getTimeInstance(timeStyle, locale); 380 381 if (!Util.isNull(pattern)) { 382 try { 383 ((java.text.SimpleDateFormat ) res).applyPattern(pattern); 384 } catch (Exception e) { 385 logCat.error(pattern, e); 386 } 387 } 388 389 break; 390 391 case FieldTypes.TIMESTAMP: 392 res = java.text.DateFormat.getDateTimeInstance(dateStyle, 393 timeStyle, locale); 394 395 if (!Util.isNull(pattern)) { 396 try { 397 ((java.text.SimpleDateFormat ) res).applyPattern(pattern); 398 } catch (Exception e) { 399 logCat.error(pattern, e); 400 } 401 } 402 403 break; 404 405 default: 406 break; 407 } 408 409 return res; 410 } 411 412 419 public void setId(int id) { 420 this.id = id; 421 } 422 423 428 public int getId() { 429 return id; 430 } 431 432 438 public void setIsKey(String value) { 439 this.key = value; 440 } 441 442 448 public void setName(String name) { 449 this.name = name; 450 } 451 452 457 public String getName() { 458 return name; 459 } 460 461 467 public void setSortable(String sortable) { 468 this.isSortable = Util.getTrue(sortable); 469 } 470 471 478 public int getType() { 479 return type; 480 } 481 482 488 public void setTypeByObject(Object obj) { 489 if (obj == null) { 490 return; 491 } 492 493 Class clazz = obj.getClass(); 494 Vector v = StringUtil.splitString(clazz.getName().toLowerCase(), "."); 495 fieldType = (String ) v.lastElement(); 496 497 if (clazz.isAssignableFrom(java.lang.Integer .class)) { 498 type = FieldTypes.INTEGER; 499 } else if (clazz.isAssignableFrom(java.lang.Long .class)) { 500 type = FieldTypes.INTEGER; 501 } else if (clazz.isAssignableFrom(java.lang.String .class)) { 502 type = FieldTypes.CHAR; 503 } else if (clazz.isAssignableFrom(java.math.BigDecimal .class)) { 504 type = FieldTypes.NUMERIC; 505 } else if (clazz.isAssignableFrom(java.sql.Date .class)) { 506 type = FieldTypes.DATE; 507 } else if (clazz.isAssignableFrom(java.sql.Time .class)) { 508 type = FieldTypes.TIME; 509 } else if (clazz.isAssignableFrom(java.sql.Timestamp .class)) { 510 type = FieldTypes.TIMESTAMP; 511 } else if (clazz.isAssignableFrom(java.lang.Double .class)) { 512 type = FieldTypes.DOUBLE; 513 } else if (clazz.isAssignableFrom(java.lang.Float .class)) { 514 type = FieldTypes.FLOAT; 515 } else if (clazz.isAssignableFrom(java.lang.Boolean .class)) { 516 type = FieldTypes.BOOLEAN; 517 } 518 } 519 520 529 public static final String dumpFieldValueArray(FieldValue[] fv) { 530 String s = null; 531 532 if (fv != null) { 533 StringBuffer sb = new StringBuffer (); 534 535 for (int i = 0; i < fv.length; i++) { 536 FieldValue f = fv[i]; 537 sb.append(" fv[").append(i).append("] = {").append( 538 f.toString()).append("}\n"); 539 } 540 541 s = sb.toString(); 542 } 543 544 return s; 545 } 546 547 552 public String getEscaperClass() { 553 return escaperClass; 554 } 555 556 561 public String getSearchAlgoName() { 562 return getFieldName(Constants.FIELDNAME_SEARCHALGO); 563 } 564 565 570 public String getSearchFieldName() { 571 return getFieldName(Constants.FIELDNAME_SEARCH); 572 } 573 574 579 public String getSearchModeName() { 580 return getFieldName(Constants.FIELDNAME_SEARCHMODE); 581 } 582 583 589 public void setSize(int size) { 590 this.size = size; 591 } 592 593 598 public int getSize() { 599 return size; 600 } 601 602 607 public String getSortFieldName() { 608 return getFieldName(Constants.FIELDNAME_SORT); 609 } 610 611 616 public void setTable(Table table) { 617 this.table = table; 618 } 619 620 625 public Table getTable() { 626 return table; 627 } 628 629 634 public boolean hasAutoIncSet() { 635 return isAutoInc; 636 } 637 638 643 public boolean hasEncodedSet() { 644 return encoded; 645 } 646 647 652 public boolean hasIsKeySet() { 653 return Util.getTrue(key); 654 } 655 656 661 public boolean hasSortableSet() { 662 return isSortable; 663 } 664 665 670 public String toString() { 671 StringBuffer buf = new StringBuffer (); 672 673 buf.append("name="); 674 buf.append(name); 675 buf.append(" type="); 676 buf.append(type); 677 buf.append(" key="); 678 buf.append(key); 679 buf.append(" isAutoinc="); 680 buf.append(isAutoInc); 681 buf.append(" issortable="); 682 buf.append(isSortable); 683 buf.append(" directory="); 684 buf.append(directory); 685 buf.append(" expression="); 686 buf.append(expression); 687 688 return buf.toString(); 689 } 690 } 691 | Popular Tags |