1 23 24 package org.dbforms.taglib; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import org.dbforms.config.Constants; 30 import org.dbforms.config.Field; 31 import org.dbforms.config.FieldTypes; 32 import org.dbforms.config.ResultSetVector; 33 34 import org.dbforms.event.WebEvent; 35 import org.dbforms.event.eventtype.EventType; 36 37 import org.dbforms.util.ICustomFormat; 38 import org.dbforms.util.IEscaper; 39 import org.dbforms.util.MessageResources; 40 import org.dbforms.util.MessageResourcesInternal; 41 import org.dbforms.util.ParseUtil; 42 import org.dbforms.util.ReflectionUtil; 43 import org.dbforms.util.Util; 44 45 import java.text.Format ; 46 47 import java.util.HashMap ; 48 import java.util.Locale ; 49 import java.util.Vector ; 50 51 import javax.servlet.http.HttpServletRequest ; 52 import javax.servlet.http.HttpSession ; 53 import javax.servlet.jsp.JspException ; 54 55 87 public abstract class DbBaseHandlerTag extends TagSupportWithScriptHandler { 88 private static Log logCat = LogFactory.getLog(DbBaseHandlerTag.class 89 .getName()); 90 91 private DbFormTag parentForm; 92 93 private IEscaper escaper = null; 94 95 private Field field; 96 97 private Format formatter; 98 99 private String customFormatter = null; 101 102 private String defaultValue; 103 104 private String escaperClass = null; 105 106 private String fieldName; 107 108 private String maxlength = null; 109 110 private String nullFieldValue; 111 112 private String overrideFormFieldName; 114 115 private String pattern; 116 117 private String readOnly = "false"; 118 119 120 private String readOnlyStyleClass = null; 121 122 127 public void setCustomFormatter(String string) { 128 customFormatter = string; 129 } 130 131 137 public void setDefaultValue(String value) { 138 this.defaultValue = value; 139 } 140 141 147 public String getDefaultValue() { 148 return defaultValue; 149 } 150 151 156 public IEscaper getEscaper() { 157 if (escaper == null) { 158 String s = getEscaperClass(); 159 160 if (!Util.isNull(s)) { 161 try { 162 escaper = (IEscaper) ReflectionUtil.newInstance(s); 163 } catch (Exception e) { 164 logCat 165 .error("cannot create the new escaper [" + s + "]", 166 e); 167 } 168 } 169 170 if ((escaper == null) && (getField() != null)) { 171 escaper = getField().getEscaper(); 172 } 173 174 if ((escaper == null)) { 175 escaper = getConfig().getEscaper(); 176 } 177 } 178 179 return escaper; 180 } 181 182 187 public void setEscaperClass(String string) { 188 escaperClass = string; 189 } 190 191 196 public String getEscaperClass() { 197 return escaperClass; 198 } 199 200 205 public Field getField() { 206 return field; 207 } 208 209 215 public void setFieldName(String fieldName) { 216 this.fieldName = fieldName; 217 218 if (getParentForm().getTable() != null) { 219 setField(getParentForm().getTable().getFieldByName(fieldName)); 220 } else { 221 setField(null); 222 } 223 224 if (getParentForm().isSubForm() && (this.field != null)) { 225 getParentForm().strikeOut(this.field); 228 } 229 } 230 231 237 public void setFormatter(Format formatter) { 238 this.formatter = formatter; 239 } 240 241 246 public Format getFormatter() { 247 Format res = formatter; 248 249 if ((res == null) && (getField() != null)) { 250 res = getField().getFormat(pattern, getLocale()); 251 } 252 253 return res; 254 } 255 256 262 public void setMaxlength(String maxlength) { 263 this.maxlength = maxlength; 264 } 265 266 271 public String getMaxlength() { 272 return maxlength; 273 } 274 275 280 public String getName() { 281 return (getField() != null) ? getField().getName() : fieldName; 282 } 283 284 290 public void setNullFieldValue(String nullFieldValue) { 291 this.nullFieldValue = nullFieldValue; 292 } 293 294 299 public void setOverrideFormFieldName(String string) { 300 overrideFormFieldName = string; 301 } 302 303 309 public void setParent(final javax.servlet.jsp.tagext.Tag parent) { 310 super.setParent(parent); 311 312 parentForm = (DbFormTag) findAncestorWithClass(this, DbFormTag.class); 315 } 316 317 322 public void setPattern(String string) { 323 pattern = string; 324 } 325 326 331 public String getPattern() { 332 Format f = getFormatter(); 333 334 if (f == null) { 335 return null; 336 } 337 338 return Util.getPattern(f); 339 } 340 341 347 public void setReadOnly(String readOnly) { 348 this.readOnly = readOnly; 349 } 350 351 357 public void setReadOnlyStyleClass(String readOnlyStyleClass) { 358 this.readOnlyStyleClass = readOnlyStyleClass; 359 } 360 361 366 public String getReadOnlyStyleClass() { 367 return readOnlyStyleClass; 368 } 369 370 375 public String getStyleClass() { 376 boolean readonly = hasReadOnlySet() || getParentForm().hasReadOnlySet(); 377 378 if (readonly && !Util.isNull(getReadOnlyStyleClass())) { 379 return getReadOnlyStyleClass(); 380 } else { 381 return super.getStyleClass(); 382 } 383 } 384 385 392 protected String customFormat(String s) { 393 if (!Util.isNull(customFormatter)) { 394 HttpSession session = pageContext.getSession(); 395 HashMap hm = (HashMap ) session 396 .getAttribute(SetCustomFormatterTag.sessionKey); 397 if (hm != null) { 398 Object obj = hm.get(customFormatter); 399 if (obj instanceof ICustomFormat) { 400 ICustomFormat cf = (ICustomFormat) obj; 401 Locale locale = MessageResources 402 .getLocale((HttpServletRequest ) pageContext 403 .getRequest()); 404 cf.setLocale(locale); 405 s = cf.sprintf(s); 406 } 407 } 408 } 409 return s; 410 } 411 412 415 public void doFinally() { 416 formatter = null; 417 field = null; 418 defaultValue = null; 419 pattern = null; 420 nullFieldValue = null; 421 maxlength = null; 422 readOnlyStyleClass = null; 423 readOnly = "false"; 424 escaperClass = null; 425 escaper = null; 426 customFormatter = null; 427 overrideFormFieldName = null; 428 super.doFinally(); 429 } 430 431 436 public boolean hasReadOnlySet() { 437 return Util.getTrue(readOnly); 438 } 439 440 445 protected String getCustomFormatter() { 446 return customFormatter; 447 } 448 449 455 protected void setField(Field field) { 456 this.field = field; 457 } 458 459 464 protected Object getFieldObject() { 465 Object fieldValueObj = null; 466 ResultSetVector res = getParentForm().getResultSetVector(); 467 468 if ((res != null) && (getField() != null)) { 469 fieldValueObj = res.getFieldAsObject(getField().getName()); 470 } else { 471 fieldValueObj = ParseUtil.getParameter( 473 (HttpServletRequest ) pageContext.getRequest(), 474 getFormFieldName()); 475 476 if (fieldValueObj == null) { 477 fieldValueObj = getDefaultValue(); 480 } else { 481 fieldValueObj = (getEscaper() == null) ? fieldValueObj 482 : getEscaper().unescapeHTML((String ) fieldValueObj); 483 } 484 } 485 486 return fieldValueObj; 487 } 488 489 495 protected String getFieldValue() { 496 ResultSetVector rsv = getParentForm().getResultSetVector(); 497 String res = null; 498 499 if ((getField() != null) && (rsv != null)) { 500 String [] s = rsv.getCurrentRow(); 501 502 if (s != null) { 503 res = rsv.getCurrentRow()[getField().getId()]; 504 } 505 } 506 507 return res; 508 } 509 510 515 protected String getFormFieldDefaultValue() { 516 if (defaultValue != null) { 517 return defaultValue; 520 } 521 522 return typicalDefaultValue(); 525 } 526 527 532 protected String getFormFieldName() { 533 if (hasOverrideFormFieldNameSet()) { 534 return getOverrideFormFieldName(); 535 } 536 537 StringBuffer buf = new StringBuffer (); 538 539 if ((getParentForm().getTable() != null) && (getField() != null)) { 540 String keyIndex = (getParentForm().isFooterReached()) ? (Constants.FIELDNAME_INSERTPREFIX + getParentForm() 541 .getPositionPathCore()) 542 : getParentForm().getPositionPath(); 543 buf.append(Constants.FIELDNAME_PREFIX); 544 buf.append(getParentForm().getTable().getId()); 545 buf.append("_"); 546 buf.append(keyIndex); 547 buf.append("_"); 548 buf.append(getField().getId()); 549 } else { 550 buf.append(fieldName); 551 } 552 553 return buf.toString(); 554 } 555 556 568 protected String getFormFieldValue() { 569 HttpServletRequest request = (HttpServletRequest ) this.pageContext 570 .getRequest(); 571 Vector errors = (Vector) request.getAttribute("errors"); 572 WebEvent we = getParentForm().getWebEvent(); 573 574 if (!getParentForm().isFooterReached()) { 576 if ((getParentForm().hasRedisplayFieldsOnErrorSet() 579 && (errors != null) && (errors.size() > 0)) 580 || ((we != null) && EventType.EVENT_NAVIGATION_RELOAD 581 .equals(we.getType()))) { 582 String oldValue = ParseUtil.getParameter(request, 584 getFormFieldName()); 585 586 if (oldValue != null) { 587 return oldValue; 588 } 589 } 590 591 return getFormattedFieldValue(); 592 } else { 593 if (((we != null) && (EventType.EVENT_NAVIGATION_COPY.equals(we 595 .getType())))) { 596 String copyValue = ParseUtil.getParameter(request, 597 getFormFieldNameForCopyEvent()); 598 599 if (copyValue != null) { 600 return copyValue; 601 } 602 } 603 604 if (((we != null) && EventType.EVENT_NAVIGATION_RELOAD.equals(we 605 .getType())) 606 || ((errors != null) && (errors.size() > 0))) { 607 String oldValue = ParseUtil.getParameter(request, 608 getFormFieldName()); 609 610 if (oldValue != null) { 611 return oldValue; 612 } 613 614 if (this instanceof DbCheckboxTag) { 619 return typicalDefaultValue() + "_"; 620 } 621 } 622 623 return getFormFieldDefaultValue(); 624 } 625 } 626 627 633 protected String getFormattedFieldValue() { 634 Object fieldValueObj = getFieldObject(); 635 String res; 636 637 if (fieldValueObj == null) { 638 res = getNullFieldValue(); 639 } else { 640 if (fieldValueObj.getClass().isArray() 648 && "byte".equals(fieldValueObj.getClass() 649 .getComponentType().toString())) { 650 res = new String ((byte[]) fieldValueObj); 651 } else if (getField() != null) { 652 switch (getField().getType()) { 653 case FieldTypes.INTEGER: 654 case FieldTypes.DOUBLE: 655 case FieldTypes.FLOAT: 656 case FieldTypes.NUMERIC: 657 case FieldTypes.DATE: 658 case FieldTypes.TIME: 659 case FieldTypes.TIMESTAMP: 660 661 try { 662 res = getFormatter().format(fieldValueObj); 663 } catch (Exception e) { 664 logCat.error("field type: " + getField().getType() 665 + "\n" + "object type: " 666 + fieldValueObj.getClass().getName() + "\n" 667 + "pattern: " + getPattern() + "\n" 668 + e.getMessage()); 669 res = fieldValueObj.toString(); 670 } 671 672 break; 673 674 case FieldTypes.BLOB: 675 case FieldTypes.DISKBLOB: 676 case FieldTypes.CHAR: 677 default: 678 res = fieldValueObj.toString(); 679 680 break; 681 } 682 } else { 683 res = fieldValueObj.toString(); 684 } 685 } 686 res = customFormat(res); 688 return res; 689 } 690 691 696 protected Locale getLocale() { 697 return getParentForm().getLocale(); 698 } 699 700 705 protected String getOverrideFormFieldName() { 706 return overrideFormFieldName; 707 } 708 709 714 protected DbFormTag getParentForm() { 715 return parentForm; 716 } 717 718 726 protected String escapeHTML(String html) { 727 return (getEscaper() == null) ? html : getEscaper().escapeHTML(html); 728 } 729 730 735 protected boolean hasOverrideFormFieldNameSet() { 736 return overrideFormFieldName != null; 737 } 738 739 744 protected String renderOldValueHtmlInputField() { 745 StringBuffer tagBuf = new StringBuffer (); 746 tagBuf.append("<input type=\"hidden\" name=\""); 747 tagBuf.append(Constants.FIELDNAME_OLDVALUETAG + getFormFieldName()); 748 tagBuf.append("\" value=\""); 749 750 if (!getParentForm().isFooterReached()) { 751 tagBuf.append(escapeHTML(getFormattedFieldValue())); 752 } else { 753 tagBuf.append(escapeHTML(getFormFieldDefaultValue())); 754 } 755 756 tagBuf.append("\" />"); 757 758 return tagBuf.toString(); 759 } 760 761 768 protected String renderPatternHtmlInputField() { 769 StringBuffer tagBuf = new StringBuffer (); 770 String ppattern = getPattern(); 771 772 if (!Util.isNull(ppattern)) { 773 tagBuf.append("<input type=\"hidden\" name=\""); 774 tagBuf.append(Constants.FIELDNAME_PATTERNTAG + getFormFieldName()); 775 tagBuf.append("\" value=\""); 776 tagBuf.append(ppattern); 777 tagBuf.append("\" />"); 778 } 779 780 return tagBuf.toString(); 781 } 782 783 788 protected String typicalDefaultValue() { 789 String res = ""; 791 792 if (getField() != null) { 793 switch (field.getType()) { 794 case org.dbforms.config.FieldTypes.INTEGER: 795 case org.dbforms.config.FieldTypes.NUMERIC: 796 case org.dbforms.config.FieldTypes.DOUBLE: 797 case org.dbforms.config.FieldTypes.FLOAT: 798 799 try { 800 res = getFormatter().format(new Double (0)); 801 } catch (Exception e) { 802 res = "0"; 803 } 804 805 } 807 } 808 809 return res; 810 } 811 812 815 protected void writeOutSpecialValues() throws JspException { 816 try { 817 pageContext.getOut().write(renderOldValueHtmlInputField()); 818 } catch (java.io.IOException ioe) { 819 throw new JspException ("IO Error: " + ioe.getMessage()); 820 } 821 } 822 823 829 private String getFormFieldNameForCopyEvent() { 830 boolean footerReached = getParentForm().isFooterReached(); 831 getParentForm().setFooterReached(false); 832 833 String name = getFormFieldName(); 834 getParentForm().setFooterReached(footerReached); 835 836 return name; 837 } 838 839 844 private String getNullFieldValue() { 845 String res = nullFieldValue; 846 847 if (res == null) { 848 res = MessageResourcesInternal.getMessage("dbforms.nodata", 849 getLocale()); 850 } 851 852 if ((getParentForm() != null) 854 && getParentForm().hasCaptionResourceSet()) { 855 res = MessageResources.getMessage(res, getLocale(), res); 856 } 857 858 863 if (res == null) { 864 res = ""; 865 } 866 867 return res; 868 } 869 } 870 | Popular Tags |