1 package org.apache.fulcrum.intake.model; 2 3 56 57 import java.lang.reflect.Method ; 58 import java.util.Locale ; 59 60 import org.apache.fulcrum.ServiceException; 61 import org.apache.fulcrum.TurbineServices; 62 import org.apache.fulcrum.intake.Retrievable; 63 import org.apache.fulcrum.intake.TurbineIntake; 64 import org.apache.fulcrum.intake.validator.InitableByConstraintMap; 65 import org.apache.fulcrum.intake.validator.ValidationException; 66 import org.apache.fulcrum.intake.validator.Validator; 67 import org.apache.fulcrum.intake.xmlmodel.Rule; 68 import org.apache.fulcrum.intake.xmlmodel.XmlField; 69 import org.apache.fulcrum.localization.LocalizationService; 70 import org.apache.fulcrum.parser.ParameterParser; 71 import org.apache.fulcrum.parser.ValueParser; 72 import org.apache.log4j.Category; 73 import org.apache.turbine.services.yaaficomponent.YaafiComponentService; 74 75 83 public abstract class Field 84 { 85 private static final String EMPTY = ""; 86 private static final String VALUE_IF_ABSENT_KEY = "_vifa_"; 87 88 protected final String name; 90 protected final String key; 91 protected String displayName; 92 protected final String mapToObject; 93 protected Validator validator; 94 protected final Method getter; 95 protected final Method setter; 96 protected final String ifRequiredMessage; 97 protected final boolean isMultiValued; 98 protected final Group group; 99 protected boolean alwaysRequired; 100 protected Object onError; 101 protected Object defaultValue; 102 103 protected boolean set_flag; 105 protected boolean valid_flag; 106 protected boolean required; 107 protected boolean initialized; 108 protected String message; 109 protected Retrievable retrievable; 110 111 private Locale locale; 112 private String stringValue; 113 private String [] stringValues; 114 private Object validValue; 115 private Object testValue; 116 private Object [] valArray; 118 122 protected ValueParser pp; 123 124 127 Category category = Category.getInstance(getClass().getName()); 128 129 138 public Field(XmlField field, Group group) 139 throws Exception 140 { 141 this.group = group; 142 key = field.getKey(); 143 name = field.getName(); 144 displayName = field.getDisplayName(); 145 isMultiValued = field.isMultiValued(); 146 setDefaultValue(field.getDefaultValue()); 147 String className = field.getValidator(); 148 if ( className == null && field.getRules().size() > 0 ) 149 { 150 className = getDefaultValidator(); 151 } 152 else if ( className != null && className.indexOf('.') == -1 ) 153 { 154 className = "org.apache.fulcrum.intake.validator." 155 + className; 156 } 157 158 if ( className != null ) 159 { 160 validator = (Validator)Class.forName(className).newInstance(); 161 if ( validator instanceof InitableByConstraintMap ) 164 { 165 ((InitableByConstraintMap)validator).init(field.getRuleMap()); 166 } 167 168 } 169 170 Rule reqRule = (Rule)field.getRuleMap().get("required"); 172 if ( reqRule != null ) 173 { 174 alwaysRequired = new Boolean (reqRule.getValue()).booleanValue(); 175 } 176 177 mapToObject = field.getMapToObject(); 178 String propName = field.getMapToProperty(); 179 Method tmpGetter = null; 180 Method tmpSetter = null; 181 if ( mapToObject != null && mapToObject.length() != 0) 182 { 183 tmpGetter = TurbineIntake.getFieldGetter(mapToObject, propName); 184 tmpSetter = TurbineIntake.getFieldSetter(mapToObject, propName); 185 } 186 getter = tmpGetter; 187 setter = tmpSetter; 188 ifRequiredMessage = field.getIfRequiredMessage(); 189 190 valArray = new Object [1]; 191 } 192 193 194 204 public Field init(ValueParser pp) 205 throws ServiceException 206 { 207 this.pp = pp; 208 valid_flag = true; 209 210 try { 213 LocalizationService localizationService = getLocalizationService(); 214 if (pp instanceof ParameterParser) 218 { 219 this.locale = localizationService.getLocale 220 ( ((ParameterParser)pp).getRequest() ); 221 } 222 else 223 { 224 this.locale = localizationService.getLocale((String )null); 225 } 226 } 228 catch (Exception e){ 229 System.out.println("localization service not registered"); 230 } 231 232 233 if ( pp.containsKey(getKey()) && pp.getString(getKey()) != null ) 234 { 235 set_flag = true; 236 if (validate(pp)) 237 { 238 } 240 } 241 else if ( pp.containsKey(getValueIfAbsent()) && 242 pp.getString(getValueIfAbsent()) != null ) 243 { 244 pp.add(getKey(), pp.getString(getValueIfAbsent())); 245 set_flag = true; 246 validate(pp); 247 } 248 249 initialized = true; 250 return this; 251 } 252 253 262 public Field init(Retrievable obj) 263 { 264 if ( !initialized ) 265 { 266 valid_flag = true; 267 } 268 retrievable = obj; 269 return this; 270 } 271 272 278 protected Locale getLocale() 279 { 280 return locale; 281 } 282 283 286 protected String getDefaultValidator() 287 { 288 return "org.apache.fulcrum.intake.validator.DefaultValidator"; 289 } 290 291 public Validator getValidator() 292 { 293 return validator; 294 } 295 296 300 public boolean isRequired() 301 { 302 return alwaysRequired || required; 303 } 304 305 309 public void setRequired(boolean v) 310 { 311 setRequired(v, ifRequiredMessage); 312 } 313 314 320 public void setRequired(boolean v, String message) 321 { 322 this.required = v; 323 if (v && !set_flag) 324 { 325 valid_flag=false; 326 this.message = message; 327 } 328 } 329 330 334 public void removeFromRequest() 335 { 336 pp.remove(getKey()); 337 } 338 339 340 345 public void dispose() 346 { 347 pp = null; 348 initialized = false; 349 set_flag = false; 350 valid_flag = false; 351 required = false; 352 message = null; 353 retrievable = null; 354 355 locale = null; 356 stringValue = null; 357 stringValues = null; 358 validValue = null; 359 testValue = null; 360 valArray[0] = null; 361 } 362 363 367 public String getKey() 368 { 369 if ( group == null ) 370 { 371 return key; 372 } 373 else 374 { 375 return group.getObjectKey() + key; 376 } 377 } 378 379 384 public String getValueIfAbsent() 385 { 386 return getKey() + VALUE_IF_ABSENT_KEY; 387 } 388 389 396 public boolean isValid() 397 { 398 return valid_flag; 399 } 400 401 407 public boolean isSet() 408 { 409 return set_flag; 410 } 411 412 419 public String getDisplayName() 420 { 421 return (displayName == null) ? name : displayName; 422 } 423 424 430 public void setDisplayName(String newDisplayName) 431 { 432 displayName = newDisplayName; 433 } 434 435 440 public String getMessage() 441 { 442 if ( message == null ) 443 { 444 return EMPTY; 445 } 446 return message; 447 } 448 449 452 public void setMessage(String message) 453 { 454 this.message = message; 455 valid_flag = false; 456 } 457 458 461 protected boolean validate(ValueParser pp) 462 { 463 return validate(); 464 } 465 466 469 protected boolean validate() 470 { 472 if ( isMultiValued ) 473 { 474 stringValues = pp.getStrings(getKey()); 475 if ( stringValues.length == 0 || 478 (stringValues.length == 1 && stringValues[0] == null) ) 479 { 480 set_flag = false; 481 } 482 483 if ( validator != null ) 484 { 485 setTestValue(pp.getStrings(getKey())); 488 for (int i=0; i<stringValues.length; i++) 489 { 490 try 491 { 492 validator.assertValidity(stringValues[i]); 493 } 494 catch (ValidationException ve) 495 { 496 setMessage(ve.getMessage()); 497 } 498 } 499 } 500 501 if ( set_flag && valid_flag ) 502 { 503 doSetValue(pp); 504 } 505 } 506 else 507 { 508 stringValue = pp.getString(getKey()); 509 if ( validator != null ) 510 { 511 setTestValue(pp.getString(getKey())); 514 515 try 516 { 517 validator.assertValidity(stringValue); 518 519 if ( set_flag ) 520 { 521 doSetValue(pp); 522 } 523 } 524 catch (ValidationException ve) 525 { 526 setMessage(ve.getMessage()); 527 } 528 } 529 else if ( set_flag ) 530 { 531 doSetValue(pp); 532 } 533 } 534 535 return valid_flag; 536 } 537 538 541 protected abstract void setDefaultValue(String prop); 542 543 546 protected void doSetValue(ValueParser pp) 547 { 548 doSetValue(); 549 } 550 551 555 protected abstract void doSetValue(); 556 557 563 void setInitialValue(Object obj) 564 { 565 validValue = obj; 566 } 567 568 577 public Object getInitialValue() 578 throws Exception 579 { 580 if ( validValue == null) 581 { 582 if ( retrievable != null ) 583 { 584 getProperty(retrievable); 585 } 586 else 587 { 588 getDefault(); 589 } 590 } 591 return validValue; 592 } 593 594 599 void setTestValue(Object obj) 600 { 601 testValue = obj; 602 } 603 604 609 public Object getTestValue() 610 { 611 return testValue; 612 } 613 614 624 public Object getValue() 625 { 626 Object val = null; 627 try 628 { 629 val = getInitialValue(); 630 } 631 catch (Exception e) 632 { 633 category.error(e); 634 } 635 636 if ( getTestValue() != null ) 637 { 638 val = getTestValue(); 639 } 640 641 if ( val == null ) 642 { 643 val = onError; 644 } 645 return val; 646 } 647 648 654 public String toString() 655 { 656 if ( stringValue != null ) 657 { 658 return stringValue; 659 } 660 else if ( getValue() != null ) 661 { 662 return getValue().toString(); 663 } 664 else 665 { 666 return EMPTY; 667 } 668 } 669 670 673 public void getProperty(Object obj) 674 throws Exception 675 { 676 validValue = getter.invoke(obj, null); 677 } 678 679 682 683 public void getDefault() 684 { 685 validValue = getDefaultValue(); 686 } 687 688 693 public void setProperty(Object obj) 694 throws ServiceException 696 { 697 if (!isValid()) 698 { 699 throw new ServiceException( 700 "Attempted to assign an invalid input."); 701 } 702 if (isSet() && getTestValue() != null) 703 { 704 try 705 { 706 valArray[0] = getTestValue(); 707 setter.invoke(obj, valArray); 708 } 709 catch ( Exception e) 710 { 711 throw new ServiceException("An exception prevented the" + 712 " setting property "+name+" of " + obj + " to " + 713 valArray[0], e); 714 } 715 } 716 } 717 718 721 public Object getDefaultValue() 722 { 723 return defaultValue; 724 } 725 726 732 protected static LocalizationService getLocalizationService() throws Exception { 733 734 YaafiComponentService yaafi = (YaafiComponentService) TurbineServices.getInstance().getService( 735 YaafiComponentService.SERVICE_NAME); 736 return (LocalizationService) yaafi.lookup(LocalizationService.class.getName()); 737 738 } 739 } 740 | Popular Tags |