1 20 package org.enhydra.barracuda.core.forms.validators; 21 22 import java.util.*; 23 24 import org.enhydra.barracuda.core.forms.*; 25 import org.enhydra.barracuda.plankton.*; 26 27 30 public class DateRangeValidator extends DefaultFormValidator { 31 32 protected Date myStartDate = null; 33 protected Date myEndDate = null; 34 35 38 public DateRangeValidator() { 39 this(null, null, null); 40 } 41 42 48 public DateRangeValidator(Date theStartDate, Date theEndDate) { 49 this(theStartDate, theEndDate, null); 50 } 51 52 59 public DateRangeValidator(Date theStartDate, Date theEndDate, String theErrMsg) { 60 super(theErrMsg); 61 myStartDate = theStartDate; 62 myEndDate = theEndDate; 63 } 64 65 71 public Date getStartDate() { 72 return myStartDate; 73 } 74 75 81 public Date getEndDate() { 82 return myEndDate; 83 } 84 85 168 169 181 public void validateFormElement(Object val, FormElement element, boolean deferExceptions) throws ValidationException { 182 if (this.isNull(val, element)) 185 return; 186 188 if (myStartDate==null && myEndDate==null) { 190 throw this.generateException(element, 191 deferExceptions, 192 "Invalid range:"+myStartDate+" to "+myEndDate); 193 } 194 195 201 202 203 204 205 if (localLogger.isInfoEnabled()) localLogger.info("validating val="+val+" is in range between "+myStartDate+" and "+myEndDate); 207 212 if (element==null) throw new ValidationException(val, "Object val:"+val+" is associated with a null FormElement"); 216 217 FormType formType = element.getType(); 220 222 if (!(formType.equals(FormType.DATE) || formType.equals(FormType.STRING))) { 223 throw new ValidationException(val, "Unsupported validation: "+ 224 val+" is of FormType " + 225 formType.toString() + 226 " and cannot be validated by this validator"); 227 } 228 229 Date dateVal = null; 230 231 232 if (val instanceof Date) { 238 dateVal = (Date)val; 239 } 240 else { 241 if (formType.equals(FormType.DATE)) { 242 if (element.getParseException()==null) 243 dateVal = (Date)element.getVal(); 244 else 245 throw this.generateException(element, deferExceptions, val.toString() + " is not a valid date string"); 246 } 247 else { 248 249 try { 250 251 if (element.getParseException()==null) 252 253 dateVal = (Date)(FormType.DATE.parse(val.toString())); 254 } catch (ParseException ex) { 255 throw this.generateException(element, deferExceptions, val.toString() + " is not a valid date string"); 256 } 257 } 258 } 259 261 262 285 287 288 289 String defaultErrMsg = null; 290 291 if (myEndDate==null) { 292 defaultErrMsg = "Value: "+val+ " is not after "+this.myStartDate; 293 } else if (myStartDate==null) { 294 defaultErrMsg = "Value: "+val+ " is not before "+this.myEndDate; 295 } else { 296 defaultErrMsg = "Value: "+val+ " is not in the range between "+this.myStartDate+" and "+this.myEndDate; 297 } 298 299 if (null!=this.myStartDate && dateVal.before(this.myStartDate)) { 300 throw this.generateException(element, deferExceptions, defaultErrMsg); 301 } 302 303 if (null!=this.myEndDate && dateVal.after(this.myEndDate)) { 304 throw this.generateException(element, deferExceptions, defaultErrMsg); 305 } 306 307 } 308 309 310 } 311 | Popular Tags |