1 package org.hibernate.validator; 3 4 import java.text.DateFormat ; 5 import java.text.ParseException ; 6 import java.util.Calendar ; 7 import java.util.Date ; 8 9 import org.hibernate.mapping.Column; 10 import org.hibernate.mapping.Property; 11 12 17 public class PastValidator implements Validator<Past>, PropertyConstraint { 18 19 public void initialize(Past parameters) {} 20 21 public boolean isValid(Object value) { 22 if (value==null) return true; 23 if ( value instanceof String ) { 24 try { 25 Date date = DateFormat.getTimeInstance().parse( (String ) value ); 26 return date.before( new Date () ); 27 } 28 catch (ParseException nfe) { 29 return false; 30 } 31 } 32 else if ( value instanceof Date ) { 33 Date date = (Date ) value; 34 return date.before( new Date () ); 35 } 36 else if ( value instanceof Calendar ) { 37 Calendar cal = (Calendar ) value; 38 return cal.before( Calendar.getInstance() ); 39 } 40 else { 41 return false; 42 } 43 } 44 45 public void apply(Property property) { 46 Column col = (Column) property.getColumnIterator().next(); 47 col.setCheckConstraint( col.getName() + " < current_date" ); 48 } 49 } 50 | Popular Tags |