org.enhydra.barracuda.core.forms.validators
Class DateRangeValidator

java.lang.Object
  |
  +--org.enhydra.barracuda.core.forms.AbstractFormValidator
        |
        +--org.enhydra.barracuda.core.forms.DefaultFormValidator
              |
              +--org.enhydra.barracuda.core.forms.validators.DateRangeValidator
All Implemented Interfaces:
FormValidator

public class DateRangeValidator
extends DefaultFormValidator

This validator ensures that a value is within a given date range


Field Summary
protected  java.util.Date myEndDate
           
protected  java.util.Date myStartDate
           
 
Fields inherited from class org.enhydra.barracuda.core.forms.DefaultFormValidator
localLogger, validators
 
Constructor Summary
DateRangeValidator()
          Public no-args constructor.
DateRangeValidator(java.util.Date theStartDate, java.util.Date theEndDate)
          Public constructor.
DateRangeValidator(java.util.Date theStartDate, java.util.Date theEndDate, java.lang.String theErrMsg)
          Public constructor.
 
Method Summary
 java.util.Date getEndDate()
          Return the maximum date value
 java.util.Date getStartDate()
          Return the minimum date value
 void validateFormElement(java.lang.Object val, FormElement element, boolean deferExceptions)
          Validate a FormElement to make see if the element equals() a given object
 
Methods inherited from class org.enhydra.barracuda.core.forms.DefaultFormValidator
addValidator, getValidators, removeValidator, validate, validateForm, validateFormElement
 
Methods inherited from class org.enhydra.barracuda.core.forms.AbstractFormValidator
generateException, getErrorMessage, isNull, setErrorMessage
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

myStartDate

protected java.util.Date myStartDate

myEndDate

protected java.util.Date myEndDate
Constructor Detail

DateRangeValidator

public DateRangeValidator()
Public no-args constructor.


DateRangeValidator

public DateRangeValidator(java.util.Date theStartDate,
                          java.util.Date theEndDate)
Public constructor.

Parameters:
theStartDate - the low end of the range
theEndDate - the high end of the range

DateRangeValidator

public DateRangeValidator(java.util.Date theStartDate,
                          java.util.Date theEndDate,
                          java.lang.String theErrMsg)
Public constructor.

Parameters:
theStartDate - the low end of the range
theEndDate - the high end of the range
theErrMsg - the message associated with this error
Method Detail

getStartDate

public java.util.Date getStartDate()
Return the minimum date value

Returns:
The date being compared against

getEndDate

public java.util.Date getEndDate()
Return the maximum date value

Returns:
The date being compared against

validateFormElement

public void validateFormElement(java.lang.Object val,
                                FormElement element,
                                boolean deferExceptions)
                         throws ValidationException
Validate a FormElement to make see if the element equals() a given object

Overrides:
validateFormElement in class DefaultFormValidator
Parameters:
element - the form element to be validated
deferExceptions - do we want to deferValidation exceptions and attempt to validate all elements so that we can process all the exceptions at once
val - the actual value to be validated
Throws:
ValidationException - if the element is not valid public void validateFormElement(Object val, FormElement element, boolean deferExceptions) throws ValidationException { //eliminate the obvious if (myStartDate==null && myEndDate==null) { throw this.generateException(element, deferExceptions, "Invalid range:"+myStartDate+" to "+myEndDate); } // cannot validate a null or empty string value, use NotNullvalidator if you want to disallow nulls if ( val==null || val.toString().length() == 0 ) return; //figure out what type of element we're dealing with if (localLogger.isInfoEnabled()) localLogger.info("validating val="+val+" is in range between "+myStartDate+" and "+myEndDate); FormType formType = element.getType(); //if element is null, or not form type=STRING or DATE, err because this should //not be considered a valid option (note that these exceptions are always //immediate since they typically represent a programming error) if ( element==null ) throw new ValidationException(val, "Object val:"+val+" is associated with a null FormElement"); if ( !(formType.equals(FormType.DATE) || formType.equals(FormType.STRING)) ) { throw new ValidationException(val, "Unsupported validation: "+ val+" is of FormType " + formType.toString() + " and cannot be validated by this validator"); } Date dateVal = null; if ( formType.equals(FormType.DATE) ) { try { dateVal = (Date)val; } catch ( ClassCastException ex ) { throw this.generateException(element, deferExceptions, val.toString() + " is not a valid date class"); } } else { // attempt to convert the string to a date try { dateVal = (Date)(FormType.DATE.parse( val.toString() )); } catch ( ParseException ex ) { throw this.generateException( element, deferExceptions, val.toString() + " is not a valid date string"); } } String defaultErrMsg = null; if (myEndDate==null) { defaultErrMsg = "Value: "+val+ " is not after "+this.myStartDate; } else if (myStartDate==null) { defaultErrMsg = "Value: "+val+ " is not before "+this.myEndDate; } else { defaultErrMsg = "Value: "+val+ " is not in the range between "+this.myStartDate+" and "+this.myEndDate; } if ( null != this.myStartDate && dateVal.before(this.myStartDate) ) { throw this.generateException(element, deferExceptions, defaultErrMsg); } if ( null != this.myEndDate && dateVal.after(this.myEndDate) ) { throw this.generateException(element, deferExceptions, defaultErrMsg); } }


Copyright © 2001 Enhydra.org