All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.validator.PastValidator Maven / Gradle / Ivy

There is a newer version: 3.5.6-Final
Show newest version
//$Id: PastValidator.java 9795 2006-04-26 06:41:18Z epbernard $
package org.hibernate.validator;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

import org.hibernate.mapping.Column;
import org.hibernate.mapping.Property;

/**
 * Check that a given date is in the past, and apply the same restriction
 * at the DB level
 *
 * @author Gavin King
 */
public class PastValidator implements Validator, PropertyConstraint, Serializable {

	public void initialize(Past parameters) {
	}

	public boolean isValid(Object value) {
		if ( value == null ) return true;
		if ( value instanceof String ) {
			try {
				Date date = DateFormat.getTimeInstance().parse( (String) value );
				return date.before( new Date() );
			}
			catch (ParseException nfe) {
				return false;
			}
		}
		else if ( value instanceof Date ) {
			Date date = (Date) value;
			return date.before( new Date() );
		}
		else if ( value instanceof Calendar ) {
			Calendar cal = (Calendar) value;
			return cal.before( Calendar.getInstance() );
		}
		else {
			return false;
		}
	}

	public void apply(Property property) {
		Column col = (Column) property.getColumnIterator().next();
		col.setCheckConstraint( col.getName() + " < current_date" );
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy