1 23 24 package org.dbforms.config; 25 26 import org.apache.commons.lang.StringUtils; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 import org.dbforms.util.FileHolder; 31 import org.dbforms.util.TimeUtil; 32 import org.dbforms.util.UniqueIDGenerator; 33 import org.dbforms.util.Util; 34 35 import java.io.Serializable ; 37 import java.math.BigDecimal ; 38 39 import java.sql.Date ; 40 import java.sql.Time ; 41 import java.sql.Timestamp ; 42 43 import java.text.DecimalFormat ; 44 import java.text.SimpleDateFormat ; 45 46 import java.util.Calendar ; 47 import java.util.Locale ; 48 49 50 51 63 public class FieldValue implements Cloneable , Serializable { 64 65 private Field field; 66 67 68 private FileHolder fileHolder; 69 70 71 private Locale locale; 72 73 74 private static Log logCat = LogFactory.getLog(FieldValue.class.getName()); 75 76 77 private String fieldValue; 78 79 80 private String oldValue; 81 82 83 private String pattern; 84 85 86 private boolean logicalOR = false; 87 88 92 private boolean renderHiddenHtmlTag = true; 93 94 98 private int operator; 99 100 101 private int searchAlgorithm; 102 103 104 private int searchMode; 105 106 110 private int sortDirection = Constants.ORDER_NONE; 111 112 118 public FieldValue(Field field, 119 String fieldValue) { 120 this(field); 121 this.fieldValue = fieldValue; 122 } 123 124 125 130 private FieldValue(Field field) { 131 this.field = field; 132 } 133 134 147 public static FieldValue createFieldValueForSearching(Field field, 148 String fieldValue, 149 Locale locale, 150 int operator, 151 int searchMode, 152 int searchAlgorithm, 153 boolean logicalOR) { 154 FieldValue fv = new FieldValue(field, fieldValue); 155 fv.setLocale(locale); 156 fv.setOperator(operator); 157 fv.setSearchMode(searchMode); 158 fv.setSearchAlgorithm(searchAlgorithm); 159 fv.setLogicalOR(logicalOR); 160 161 return fv; 162 } 163 164 165 173 public static FieldValue createFieldValueForSorting(Field field, 174 int sortDirection) { 175 FieldValue fv = new FieldValue(field); 176 fv.setSortDirection(sortDirection); 177 178 return fv; 179 } 180 181 182 187 public void setField(Field field) { 188 this.field = field; 189 } 190 191 192 197 public Field getField() { 198 return field; 199 } 200 201 202 207 public void setFieldValue(String fieldValue) { 208 this.fieldValue = fieldValue; 209 } 210 211 212 217 public String getFieldValue() { 218 return fieldValue; 219 } 220 221 222 229 public Object getFieldValueAsObject() { 230 if (getField() == null) { 231 return null; 232 } 233 234 return getFieldValueAsObject(getFieldValue(), getField().getType()); 235 } 236 237 238 243 public void setFileHolder(FileHolder fileHolder) { 244 this.fileHolder = fileHolder; 245 } 246 247 248 253 public FileHolder getFileHolder() { 254 return fileHolder; 255 } 256 257 258 263 public void setLocale(Locale locale) { 264 this.locale = locale; 265 } 266 267 268 273 public Locale getLocale() { 274 return locale; 275 } 276 277 278 283 public void setLogicalOR(boolean newLogicalOR) { 284 logicalOR = newLogicalOR; 285 } 286 287 288 293 public boolean getLogicalOR() { 294 return logicalOR; 295 } 296 297 298 305 public static final boolean isNull(FieldValue[] arr) { 306 return ((arr == null) || (arr.length == 0)); 307 } 308 309 310 315 public void setOldValue(String string) { 316 oldValue = string; 317 } 318 319 320 325 public String getOldValue() { 326 return oldValue; 327 } 328 329 330 335 public Object getOldValueAsObject() { 336 if (getField() == null) { 337 return null; 338 } 339 340 return getFieldValueAsObject(getOldValue(), getField().getType()); 341 } 342 343 344 349 public void setOperator(int i) { 350 operator = i; 351 } 352 353 354 359 public int getOperator() { 360 return operator; 361 } 362 363 364 369 public void setPattern(String pattern) { 370 this.pattern = pattern; 371 } 372 373 374 379 public String getPattern() { 380 return pattern; 381 } 382 383 384 389 public void setRenderHiddenHtmlTag(boolean renderHiddenHtmlTag) { 390 this.renderHiddenHtmlTag = renderHiddenHtmlTag; 391 } 392 393 394 399 public boolean getRenderHiddenHtmlTag() { 400 return renderHiddenHtmlTag; 401 } 402 403 404 409 public void setSearchAlgorithm(int searchAlgorithm) { 410 this.searchAlgorithm = searchAlgorithm; 411 } 412 413 414 419 public int getSearchAlgorithm() { 420 return searchAlgorithm; 421 } 422 423 424 429 public void setSearchMode(int searchMode) { 430 this.searchMode = searchMode; 431 } 432 433 434 439 public int getSearchMode() { 440 return searchMode; 441 } 442 443 444 449 public void setSortDirection(int sortDirection) { 450 this.sortDirection = sortDirection; 451 } 452 453 454 459 public int getSortDirection() { 460 return sortDirection; 461 } 462 463 464 469 public Object clone() { 470 try { 472 FieldValue fv = (FieldValue) super.clone(); 473 474 return fv; 475 } catch (CloneNotSupportedException e) { 476 logCat.error("::clone - exception", e); 478 } 479 480 return null; 481 } 482 483 484 490 public static void invert(FieldValue[] fv) { 491 for (int i = 0; i < fv.length; i++) { 492 switch (fv[i].getSortDirection()) { 493 case Constants.ORDER_ASCENDING: 494 fv[i].setSortDirection(Constants.ORDER_DESCENDING); 495 496 break; 497 498 case Constants.ORDER_DESCENDING: 499 fv[i].setSortDirection(Constants.ORDER_ASCENDING); 500 501 break; 502 } 503 } 504 } 505 506 507 514 public String toString() { 515 StringBuffer buf = new StringBuffer (); 516 517 String fieldName = getField() 518 .getName(); 519 buf.append(" ") 520 .append("field [") 521 .append(fieldName) 522 .append("] has value, oldvalue [") 523 .append(getFieldValue()) 524 .append(", ") 525 .append(getOldValue()) 526 .append("]\n"); 527 528 return buf.toString(); 529 } 530 531 532 539 public static String toString(FieldValue[] fieldValues) { 540 StringBuffer sb = new StringBuffer (); 541 sb.append("FieldValue array size: ") 542 .append(fieldValues.length) 543 .append("; elements are:\n"); 544 545 for (int i = 0; i < fieldValues.length; i++) { 546 FieldValue fieldValue = fieldValues[i]; 547 sb.append(fieldValue.toString()); 548 } 549 550 return sb.toString(); 551 } 552 553 554 private Object getFieldValueAsObject(String value, 555 int fieldType) { 556 Object res = null; 557 558 switch (fieldType) { 559 case FieldTypes.INTEGER: 560 res = parseINTEGER(value); 561 562 break; 563 564 case FieldTypes.DOUBLE: 565 res = parseDOUBLE(value); 566 567 break; 568 569 case FieldTypes.FLOAT: 570 res = parseFLOAT(value); 571 572 break; 573 574 case FieldTypes.NUMERIC: 575 res = parseNUMERIC(value); 576 577 break; 578 579 case FieldTypes.CHAR: 580 res = parseCHAR(value); 581 582 break; 583 584 case FieldTypes.DATE: 585 res = parseDATE(value); 586 587 break; 588 589 case FieldTypes.TIME: 590 res = parseTIME(value); 591 592 break; 593 594 case FieldTypes.TIMESTAMP: 595 res = parseTIMESTAMP(value); 596 597 break; 598 599 case FieldTypes.BLOB: 600 601 if (getFileHolder() == null) { 603 res = value; 604 } else { 605 res = getFileHolder(); 606 } 607 608 break; 609 610 case FieldTypes.DISKBLOB: 611 res = parseDISKBLOB(); 612 613 break; 614 615 default: 616 break; 617 } 618 619 return res; 620 } 621 622 623 private String parseCHAR(String value) { 624 String res; 625 if ("[NULL]".equals(value)) { 626 res = null; 627 } else { 628 res = value; 629 } 630 return res; 631 } 632 633 634 private Date parseDATE(String value) { 635 String valueStr = value.trim(); 636 637 if (Util.isNull(valueStr)) { 638 return null; 639 } 640 641 if (getLocale() != null) { 642 try { 643 SimpleDateFormat f = (SimpleDateFormat ) getField() 644 .getFormat(getPattern(), 645 getLocale()); 646 Calendar result = TimeUtil.parseDate(f, valueStr); 647 result.set(Calendar.HOUR_OF_DAY, 0); 648 result.set(Calendar.MINUTE, 0); 649 result.set(Calendar.SECOND, 0); 650 651 return new Date (result.getTime().getTime()); 652 } catch (Exception e) { 653 logCat.error(e.getMessage() + " <" + valueStr + "/" + getPattern() 654 + ">"); 655 656 try { 658 SimpleDateFormat f = (SimpleDateFormat ) getField() 659 .getFormat("short", 660 getLocale()); 661 Calendar result = TimeUtil.parseDate(f, valueStr); 662 result.set(Calendar.HOUR_OF_DAY, 0); 663 result.set(Calendar.MINUTE, 0); 664 result.set(Calendar.SECOND, 0); 665 666 return new Date (result.getTime().getTime()); 667 } catch (Exception ex) { 668 logCat.error(ex.getMessage() + " <" + valueStr + "/" 669 + getPattern() + ">"); 670 } 671 } 672 } 673 674 try { 675 String [] vStr = StringUtils.split(valueStr, " "); 678 String str = vStr[0]; 679 680 return Date.valueOf(str); 681 } catch (Exception e) { 682 logCat.error(e.getMessage() + " <" + valueStr + ">"); 683 } 684 685 return null; 686 } 687 688 689 private String parseDISKBLOB() { 690 if (getField() 691 .hasEncodedSet()) { 692 String fileName = getFileHolder() 693 .getFileName(); 694 int dotIndex = fileName.lastIndexOf('.'); 695 String suffix = (dotIndex != -1) ? fileName.substring(dotIndex) 696 : ""; 697 fileHolder.setFileName(UniqueIDGenerator.getUniqueID() + suffix); 698 } 699 700 return fileHolder.getFileName(); 701 } 702 703 704 private Double parseDOUBLE(String value) { 705 String valueStr = value.trim(); 706 707 if (Util.isNull(valueStr)) { 708 return null; 709 } 710 711 if (getLocale() != null) { 712 try { 713 DecimalFormat f = (DecimalFormat ) getField() 714 .getFormat(getPattern(), 715 getLocale()); 716 717 return new Double (f.parse(valueStr).doubleValue()); 718 } catch (Exception e) { 719 logCat.error(e.getMessage() + " <" + valueStr + "/" + getPattern() 720 + ">"); 721 } 722 } 723 724 try { 725 return new Double (valueStr); 726 } catch (Exception e) { 727 logCat.error(e.getMessage() + " <" + valueStr + ">"); 728 } 729 730 return null; 731 } 732 733 734 private Float parseFLOAT(String value) { 735 String valueStr = value.trim(); 736 737 if (Util.isNull(valueStr)) { 738 return null; 739 } 740 741 if (getLocale() != null) { 742 try { 743 DecimalFormat f = (DecimalFormat ) getField() 744 .getFormat(getPattern(), 745 getLocale()); 746 747 return new Float (f.parse(valueStr).floatValue()); 748 } catch (Exception e) { 749 logCat.error(e.getMessage() + " <" + valueStr + "/" + getPattern() 750 + ">"); 751 } 752 } 753 754 try { 755 return new Float (valueStr); 756 } catch (Exception e) { 757 logCat.error(e.getMessage() + " <" + valueStr + ">"); 758 } 759 760 return null; 761 } 762 763 764 private Integer parseINTEGER(String value) { 765 String valueStr = value.trim(); 766 767 if (Util.isNull(valueStr)) { 768 return null; 769 } 770 771 if (getLocale() != null) { 772 try { 773 DecimalFormat f = (DecimalFormat ) getField() 774 .getFormat(getPattern(), 775 getLocale()); 776 777 return new Integer (f.parse(valueStr).intValue()); 778 } catch (Exception e) { 779 logCat.error(e.getMessage() + " <" + valueStr + "/" + getPattern() 780 + ">"); 781 } 782 } 783 784 try { 785 return new Integer (valueStr); 786 } catch (Exception e) { 787 logCat.error(e.getMessage() + " <" + valueStr + ">"); 788 } 789 790 return null; 791 } 792 793 794 private BigDecimal parseNUMERIC(String value) { 795 String valueStr = value.trim(); 796 797 if (Util.isNull(valueStr)) { 798 return null; 799 } 800 801 if (getLocale() != null) { 802 try { 803 DecimalFormat f = (DecimalFormat ) getField() 804 .getFormat(getPattern(), 805 getLocale()); 806 807 return new BigDecimal (f.parse(valueStr).doubleValue()); 808 } catch (Exception e) { 809 logCat.error(e.getMessage() + " <" + valueStr + "/" + getPattern() 810 + ">"); 811 } 812 } 813 814 try { 815 return new BigDecimal (valueStr); 816 } catch (Exception e) { 817 logCat.error(e.getMessage() + " <" + valueStr + ">"); 818 } 819 820 return null; 821 } 822 823 824 private Time parseTIME(String value) { 825 String valueStr = value.trim(); 826 827 if (Util.isNull(valueStr)) { 828 return null; 829 } 830 831 if (getLocale() != null) { 832 try { 833 SimpleDateFormat f = (SimpleDateFormat ) getField() 834 .getFormat(getPattern(), 835 getLocale()); 836 Calendar result = TimeUtil.parseDate(f, valueStr); 837 result.set(Calendar.DAY_OF_MONTH, 0); 838 result.set(Calendar.MONTH, 0); 839 result.set(Calendar.YEAR, 0); 840 841 return new Time (result.getTime().getTime()); 842 } catch (Exception e) { 843 logCat.error(e.getMessage() + " <" + valueStr + "/" + getPattern() 844 + ">"); 845 846 try { 848 SimpleDateFormat f = (SimpleDateFormat ) getField() 849 .getFormat("short", 850 getLocale()); 851 Calendar result = TimeUtil.parseDate(f, valueStr); 852 result.set(Calendar.DAY_OF_MONTH, 0); 853 result.set(Calendar.MONTH, 0); 854 result.set(Calendar.YEAR, 0); 855 856 return new Time (result.getTime().getTime()); 857 } catch (Exception ex) { 858 logCat.error(ex.getMessage() + " <" + valueStr + "/" 859 + getPattern() + ">"); 860 } 861 } 862 } 863 864 try { 865 return Time.valueOf(valueStr); 866 } catch (Exception e) { 867 logCat.error(e.getMessage() + " <" + valueStr + ">"); 868 } 869 870 return null; 871 } 872 873 874 private Timestamp parseTIMESTAMP(String value) { 875 String valueStr = value.trim(); 876 877 if (Util.isNull(valueStr)) { 878 return null; 879 } 880 881 if (getLocale() != null) { 882 try { 883 SimpleDateFormat f = (SimpleDateFormat ) getField() 884 .getFormat(getPattern(), 885 getLocale()); 886 Calendar result = TimeUtil.parseDate(f, valueStr); 887 888 return new Timestamp (result.getTime().getTime()); 889 } catch (Exception e) { 890 logCat.error(e.getMessage() + " <" + valueStr + "/" + getPattern() 891 + ">"); 892 893 try { 895 SimpleDateFormat f = (SimpleDateFormat ) getField() 896 .getFormat("short", 897 getLocale()); 898 Calendar result = TimeUtil.parseDate(f, valueStr); 899 900 return new Timestamp (result.getTime().getTime()); 901 } catch (Exception ex) { 902 logCat.error(ex.getMessage() + " <" + valueStr + "/" 903 + getPattern() + ">"); 904 } 905 } 906 } 907 908 try { 909 return Timestamp.valueOf(valueStr); 910 } catch (Exception e) { 911 logCat.error(e.getMessage() + " <" + valueStr + ">"); 912 } 913 914 return null; 915 } 916 } 917 | Popular Tags |