KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > validator > PastValidator


1 //$Id: PastValidator.java,v 1.1 2005/05/27 08:58:54 epbernard Exp $
2
package org.hibernate.validator;
3
4 import java.text.DateFormat JavaDoc;
5 import java.text.ParseException JavaDoc;
6 import java.util.Calendar JavaDoc;
7 import java.util.Date JavaDoc;
8
9 import org.hibernate.mapping.Column;
10 import org.hibernate.mapping.Property;
11
12 /**
13  * Check that a given date is in the pas, and apply the same restriction
14  * at the DB level
15  * @author Gavin King
16  */

17 public class PastValidator implements Validator<Past>, PropertyConstraint {
18     
19     public void initialize(Past parameters) {}
20
21     public boolean isValid(Object JavaDoc value) {
22         if (value==null) return true;
23         if ( value instanceof String JavaDoc ) {
24             try {
25                 Date JavaDoc date = DateFormat.getTimeInstance().parse( (String JavaDoc) value );
26                 return date.before( new Date JavaDoc() );
27             }
28             catch (ParseException JavaDoc nfe) {
29                 return false;
30             }
31         }
32         else if ( value instanceof Date JavaDoc ) {
33             Date JavaDoc date = (Date JavaDoc) value;
34             return date.before( new Date JavaDoc() );
35         }
36         else if ( value instanceof Calendar JavaDoc ) {
37             Calendar JavaDoc cal = (Calendar JavaDoc) 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