01: package dinamica.validators;
02:
03: import java.util.HashMap;
04: import javax.servlet.http.HttpServletRequest;
05: import dinamica.*;
06:
07: /**
08: * Generic validator for date ranges (date from - date to).<br>
09: * Will return FALSE if date2 < 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: /* (non-Javadoc)
23: * @see dinamica.AbstractValidator#isValid(javax.servlet.http.HttpServletRequest, dinamica.Recordset, java.util.ArrayList)
24: */
25: public boolean isValid(HttpServletRequest req,
26: Recordset inputParams, HashMap<String, String> attribs)
27: throws Throwable {
28:
29: String date1 = (String) attribs.get("date1");
30: String date2 = (String) attribs.get("date2");
31:
32: if (date1 == null || date2 == null)
33: throw new Throwable(
34: "Invalid attributes 'date1' or 'date2' - cannot be null.");
35:
36: java.util.Date d1 = (java.util.Date) inputParams
37: .getValue(date1);
38: java.util.Date d2 = (java.util.Date) inputParams
39: .getValue(date2);
40:
41: if (d1 != null && d2 != null && d2.compareTo(d1) < 0)
42: return false;
43: else
44: return true;
45:
46: }
47:
48: }
|