1 20 package org.enhydra.barracuda.core.forms; 21 22 import java.util.*; 23 import java.text.*; 24 import javax.servlet.*; 25 import javax.servlet.http.*; 26 27 import org.apache.log4j.*; 28 29 import org.enhydra.barracuda.plankton.data.*; 30 31 55 public class DefaultFormMap implements FormMap { 57 58 protected static final Logger localLogger = Logger.getLogger(DefaultFormMap.class.getName()); 60 61 private static final String PARSE_EXCEPTION = "ParseException"; 62 63 protected Map elements = new HashMap(10); 65 protected List validators = new ArrayList(5); 67 protected StateMap statemap = new DefaultStateMap(); 68 protected static final Locale defaultLoc = Locale.getDefault(); 70 78 public void defineElement(FormElement element) { 79 defineElement(element.getKey(), element); 80 } 81 82 89 public void defineElement(String key, FormElement element) { 90 if (localLogger.isDebugEnabled()) localLogger.debug("Defining FormElement: Key=["+key+"] Element=["+element+"]"); 91 elements.put(key, element); 92 } 93 94 103 public void defineValidator(FormValidator validator) { 104 if (!validators.contains(validator)) { 105 if (localLogger.isDebugEnabled()) localLogger.debug("Defining validator: Validator=["+validator+"]"); 106 validators.add(validator); 107 } 108 } 109 110 124 public FormMap map(ServletRequest req) { 125 return map(req, null, null); 126 } 127 128 144 public FormMap map(ServletRequest req, Locale loc) { 145 return map(req, null, loc); 146 } 147 148 164 public FormMap map(ServletRequest req, String prefix) { 165 return map(req, prefix, null); 166 } 167 168 189 public FormMap map(ServletRequest req, String prefix, Locale loc) { 191 prefix = prefix!=null ? prefix : ""; 194 if (loc==null) loc = defaultLoc; 197 if (localLogger.isInfoEnabled()) localLogger.info("Mapping ServletRequest to FormMap"); 198 if (localLogger.isDebugEnabled()) { 199 localLogger.debug("ServletRequest parameters"); 200 Map map = new ServletRequestParameterStateMap(req).getStateValues(); 201 CollectionsUtil.printStackTrace(map, 0, localLogger, null); 202 localLogger.debug("Map elements"); 203 CollectionsUtil.printStackTrace(elements, 0, localLogger, null); 204 } 205 206 Iterator it = elements.values().iterator(); 207 while (it.hasNext()) { 208 FormElement element = (FormElement) it.next(); 210 if (localLogger.isDebugEnabled()) localLogger.debug("Next FormElement: "+element); 211 219 String [] origVals = req.getParameterValues(prefix + element.getKey()); 225 226 Object origVal = null; 229 Object val = null; 230 if (origVals!=null) { 231 int max = origVals.length; 232 if (max==1) { 233 origVal = origVals[0]; 235 val = mapElement(element, origVal, loc); 237 } else { 238 origVal = new ArrayList(); 240 val = new ArrayList(); 241 List lorigVal = (List) origVal; 242 List lval = (List) val; 243 for (int i=0; i<max; i++) { 244 lorigVal.add(origVals[i]); 245 lval.add(mapElement(element, origVals[i], loc)); 247 } 248 } 249 } else { 250 val = mapElement(element, null, loc); 252 } 253 254 element.setOrigVal(origVal); 256 element.setVal(val); 257 } 258 return this; 259 } 260 261 273 public FormMap map(StateMap map) { 274 return map(map, null, null); 275 } 276 277 291 public FormMap map(StateMap map, String prefix) { 292 return map(map, prefix, null); 293 } 294 295 309 public FormMap map(StateMap map, Locale loc) { 310 return map(map, null, loc); 311 } 312 313 329 public FormMap map(StateMap map, String prefix, Locale loc) { 331 prefix = prefix!=null ? prefix : ""; 334 if (loc==null) loc = defaultLoc; 337 if (localLogger.isInfoEnabled()) localLogger.info("Mapping StateMap to FormMap"); 338 Iterator it = elements.values().iterator(); 339 while (it.hasNext()) { 340 FormElement element = (FormElement) it.next(); 342 Object origVal = map.getState(prefix + element.getKey()); 344 345 Object val = mapElement(element, origVal, loc); 348 349 element.setOrigVal(origVal); 351 element.setVal(val); 352 } 353 return this; 354 } 355 356 365 public FormElement mapElement(String key, Object origVal) { 366 return mapElement(key, origVal, null); 367 } 368 369 378 public FormElement mapElement(String key, Object origVal, Locale loc) { 379 if (loc==null) loc = defaultLoc; 381 382 FormElement element = getElement(key); 384 if (element!=null) { 385 Object val = mapElement(element, origVal, loc); 387 388 element.setOrigVal(origVal); 390 element.setVal(val); 391 } 392 return element; 393 } 394 396 407 private Object mapElement(FormElement element, Object origVal, Locale loc) { 409 if (localLogger.isInfoEnabled()) localLogger.info("Mapping Element: "+element.getKey()+"="+origVal); 416 Object val = null; 417 FormType type = element.getType(); 418 419 if (!isNull(origVal)) { 424 try { 427 Class typeclass = type.getFormClass(); 431 if (typeclass.isInstance(origVal)) 432 val = origVal; 433 else 434 val = type.parse(origVal.toString(), loc); 436 } catch (ParseException e) { 437 if (localLogger.isDebugEnabled()) localLogger.debug("ParseException:", e); 438 element.setParseException(e); 439 } 440 } 441 442 if (val==null) { 443 if (localLogger.isDebugEnabled()) localLogger.debug("Using default val"); 444 val = element.getDefaultVal(); 445 } 446 447 if (localLogger.isDebugEnabled()) localLogger.debug("Result: "+val); 448 return val; 449 } 450 451 464 public FormMap validate(boolean deferExceptions) throws ValidationException { 465 if (localLogger.isInfoEnabled()) localLogger.info("Validating FormMap (form & elements)"); 466 ValidationException ve = null; 467 468 try { 469 try { 471 validateElements(deferExceptions); 472 } catch (DeferredValidationException dve) { 473 if (ve==null) ve = new DeferredValidationException(dve.getSource(), "Validation err:"+dve); 474 ve.addSubException(dve); 475 } 476 477 try { 479 validateForm(deferExceptions); 480 } catch (DeferredValidationException dve) { 481 if (ve==null) ve = new DeferredValidationException(dve.getSource(), "Validation err:"+dve); 482 ve.addSubException(dve); 483 } 484 } catch (ValidationException e) { 485 if (localLogger.isDebugEnabled()) localLogger.debug("Validation err! (immediate): ", e); 486 throw e; 487 } 488 489 if (ve!=null) throw ve; 492 return this; 493 } 494 495 506 public FormMap validateElements(boolean deferExceptions) throws ValidationException { 507 if (localLogger.isInfoEnabled()) localLogger.info("Validating FormMap (elements)"); 508 ValidationException ve = null; 509 510 try { 511 if (localLogger.isDebugEnabled()) localLogger.debug("Validating individual elements..."); 513 Iterator it = elements.values().iterator(); 514 FormElement element = null; 515 while (it.hasNext()) { 516 try { 517 element = (FormElement) it.next(); 518 if (localLogger.isDebugEnabled()) localLogger.debug("Next FormElement: "+element); 519 FormValidator fv = element.getValidator(); 520 if (fv!=null) fv.validate(element, this, deferExceptions); 521 if (localLogger.isDebugEnabled()) localLogger.debug("Element Valid!"); 522 } catch (DeferredValidationException dve) { 523 if (localLogger.isDebugEnabled()) localLogger.debug("Element Invalid! (deferred): "+dve.getMessage()); 524 if (ve==null) ve = new DeferredValidationException(element, "Validation err:"+dve); 525 ve.addSubException(dve); 526 } 527 } 528 } catch (ValidationException e) { 529 if (localLogger.isDebugEnabled()) localLogger.debug("Validation err! (immediate): ", e); 530 throw e; 531 } 532 533 if (ve!=null) throw ve; 536 return this; 537 } 538 539 550 public FormMap validateForm(boolean deferExceptions) throws ValidationException { 551 if (localLogger.isInfoEnabled()) localLogger.info("Validating FormMap (form)"); 552 ValidationException ve = null; 553 554 try { 555 if (localLogger.isDebugEnabled()) localLogger.debug("Validating entire form..."); 557 try { 558 Iterator it = validators.iterator(); 559 while (it.hasNext()) { 560 Object obj = it.next(); 561 if (localLogger.isDebugEnabled()) localLogger.debug("Next obj:"+obj); 562 FormValidator fv = (FormValidator) obj; 563 if (localLogger.isDebugEnabled()) localLogger.debug("Next FormValidator: "+fv); 564 fv.validate(null, this, deferExceptions); 565 if (localLogger.isDebugEnabled()) localLogger.debug("Form Valid!"); 566 } 567 } catch (DeferredValidationException dve) { 568 if (localLogger.isDebugEnabled()) localLogger.debug("Form Invalid! (deferred): ", dve); 569 if (ve==null) ve = new DeferredValidationException(this, "Validation err:"+dve); 570 ve.addSubException(dve); 571 } 572 } catch (ValidationException e) { 573 if (localLogger.isDebugEnabled()) localLogger.debug("Validation err! (immediate): ", e); 574 throw e; 575 } 576 577 if (ve!=null) throw ve; 580 return this; 581 } 582 583 584 585 592 public void putState(Object key, Object val) { 593 statemap.putState(key,val); 594 } 595 596 602 public Object getState(Object key) { 603 return statemap.getState(key); 604 } 605 606 612 public Object removeState(Object key) { 613 return statemap.removeState(key); 614 } 615 616 621 public List getStateKeys() { 622 return statemap.getStateKeys(); 623 } 624 625 630 public Map getStateValues() { 631 return statemap.getStateValues(); 632 } 633 634 638 public void clearState() { 639 statemap.clearState(); 640 } 641 642 649 public boolean exists(String key) { 650 FormElement fel = (FormElement) elements.get(key); 651 if (fel==null) return false; 652 return (fel.getVal()!=null); 653 } 654 655 661 public FormElement getElement(String key) { 662 return (FormElement) elements.get(key); 663 } 664 665 670 public Map getElements() { 671 return new HashMap(elements); 672 } 674 675 681 public Map getElementVals() { 682 Map valmap = new HashMap(); 683 Iterator it = elements.keySet().iterator(); 685 FormElement el = null; 686 while (it.hasNext()) { 687 String key = (String ) it.next(); 688 el = (FormElement) elements.get(key); 689 if (el==null) valmap.put(key, el); 690 else valmap.put(key, el.getVal()); 691 } 692 return valmap; 693 } 694 695 702 public void setVal(String key, Object val) { 703 FormElement el = (FormElement) elements.get(key); 704 if (el==null) { 705 el = new DefaultFormElement(key); 706 defineElement(key, el); 707 } 708 el.setVal(val); 709 } 710 711 722 public Object getVal(String key) { 723 FormElement el = (FormElement) elements.get(key); 724 if (el==null) return null; 725 else return el.getVal(); 726 } 727 728 737 public Object [] getVals(String key) { 738 FormElement el = (FormElement) elements.get(key); 739 if (el==null) return null; 740 else return el.getVals(); 741 } 742 743 751 public String getStringVal(String key) { 752 FormElement el = (FormElement) elements.get(key); 753 if (el==null) return null; 754 else return (String ) el.getVal(); 755 } 756 757 765 public String getStringVal(String key, String dflt) { 766 String val = getStringVal(key); 767 return (val!=null ? val : dflt); 768 } 769 770 778 public Boolean getBooleanVal(String key) { 779 FormElement el = (FormElement) elements.get(key); 780 if (el==null) return null; 781 else return (Boolean ) el.getVal(); 782 } 783 784 792 public Boolean getBooleanVal(String key, Boolean dflt) { 793 Boolean val = getBooleanVal(key); 794 return (val!=null ? val : dflt); 795 } 796 797 805 public Integer getIntegerVal(String key) { 806 FormElement el = (FormElement) elements.get(key); 807 if (el==null) return null; 808 else return (Integer ) el.getVal(); 809 } 810 811 819 public Integer getIntegerVal(String key, Integer dflt) { 820 Integer val = getIntegerVal(key); 821 return (val!=null ? val : dflt); 822 } 823 824 832 public Date getDateVal(String key) { 833 FormElement el = (FormElement) elements.get(key); 834 if (el==null) return null; 835 else return (Date) el.getVal(); 836 } 837 838 846 public Date getDateVal(String key, Date dflt) { 847 Date val = getDateVal(key); 848 return (val!=null ? val : dflt); 849 } 850 851 859 public Long getLongVal(String key) { 860 FormElement el = (FormElement) elements.get(key); 861 if (el==null) return null; 862 else return (Long ) el.getVal(); 863 } 864 865 873 public Long getLongVal(String key, Long dflt) { 874 Long val = getLongVal(key); 875 return (val!=null ? val : dflt); 876 } 877 878 886 public Short getShortVal(String key) { 887 FormElement el = (FormElement) elements.get(key); 888 if (el==null) return null; 889 else return (Short ) el.getVal(); 890 } 891 892 900 public Short getShortVal(String key, Short dflt) { 901 Short val = getShortVal(key); 902 return (val!=null ? val : dflt); 903 } 904 905 913 public Double getDoubleVal(String key) { 914 FormElement el = (FormElement) elements.get(key); 915 if (el==null) return null; 916 else return (Double ) el.getVal(); 917 } 918 919 927 public Double getDoubleVal(String key, Double dflt) { 928 Double val = getDoubleVal(key); 929 return (val!=null ? val : dflt); 930 } 931 932 940 public Float getFloatVal(String key) { 941 FormElement el = (FormElement) elements.get(key); 942 if (el==null) return null; 943 else return (Float ) el.getVal(); 944 } 945 946 954 public Float getFloatVal(String key, Float dflt) { 955 Float val = getFloatVal(key); 956 return (val!=null ? val : dflt); 957 } 958 959 960 966 public boolean isNull(Object val){ 967 return ((val==null) || (val.toString().trim().length() < 1)); 968 } 969 970 971 972 973 974 975 976 978 986 993 1001 1008 1016 1023 1024 1032 1038 1039 1047 1054 1055 1063 1069 1070 1078 1085 1086 1094 1100 1101 1109 1116 1117 1125 1131 1132 1140 1147 1148 1156 1162 1163 1171 1178 1179 1187 1193 1194 1202 1209 1210 1218 1224 1225 1233 1240 1241 1249 1255 1256} 1257 | Popular Tags |