KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > validators > DateRangeValidator


1 package dinamica.validators;
2
3 import java.util.HashMap JavaDoc;
4 import javax.servlet.http.HttpServletRequest JavaDoc;
5 import dinamica.*;
6
7 /**
8  * Generic validator for date ranges (date from - date to).<br>
9  * Will return FALSE if date2 &lt; date1, requires two custom attributes named "date1" and "date2",
10  * representing the field names of the two recordset fields to use
11  * in the validation logic. Returns TRUE if any of the parameters is null.
12  *
13  * <br>
14  * Creation date: 29/10/2003<br>
15  * Last Update: 29/10/2003<br>
16  * (c) 2004 Martin Cordova<br>
17  * This code is released under the LGPL license<br>
18  * @author Martin Cordova (dinamica@martincordova.com)
19  * */

20 public class DateRangeValidator extends AbstractValidator
21 {
22
23     /* (non-Javadoc)
24      * @see dinamica.AbstractValidator#isValid(javax.servlet.http.HttpServletRequest, dinamica.Recordset, java.util.ArrayList)
25      */

26     public boolean isValid(
27         HttpServletRequest JavaDoc req,
28         Recordset inputParams,
29         HashMap JavaDoc attribs)
30         throws Throwable JavaDoc
31     {
32     
33         String JavaDoc date1 = (String JavaDoc)attribs.get("date1");
34         String JavaDoc date2 = (String JavaDoc)attribs.get("date2");
35         
36         if (date1==null || date2==null)
37             throw new Throwable JavaDoc("Invalid attributes 'date1' or 'date2' - cannot be null.");
38         
39         java.util.Date JavaDoc d1 = (java.util.Date JavaDoc)inputParams.getValue(date1);
40         java.util.Date JavaDoc d2 = (java.util.Date JavaDoc)inputParams.getValue(date2);
41         
42         if (d1!=null && d2!=null && d2.compareTo(d1) < 0 )
43             return false;
44         else
45             return true;
46         
47     }
48
49 }
50
Popular Tags