KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > forms > validators > DateRangeValidator


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: DateRangeValidator.java,v 1.9 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.forms.validators;
21
22 import java.util.*;
23
24 import org.enhydra.barracuda.core.forms.*;
25 import org.enhydra.barracuda.plankton.*;
26
27 /**
28  * This validator ensures that a value is within a given date range
29  */

30 public class DateRangeValidator extends DefaultFormValidator {
31
32     protected Date myStartDate = null;
33     protected Date myEndDate = null;
34
35     /**
36      * Public no-args constructor.
37      */

38     public DateRangeValidator() {
39         this(null, null, null);
40     }
41
42     /**
43      * Public constructor.
44      *
45      * @param theStartDate the low end of the range
46      * @param theEndDate the high end of the range
47      */

48     public DateRangeValidator(Date theStartDate, Date theEndDate) {
49         this(theStartDate, theEndDate, null);
50     }
51
52     /**
53      * Public constructor.
54      *
55      * @param theStartDate the low end of the range
56      * @param theEndDate the high end of the range
57      * @param theErrMsg the message associated with this error
58      */

59     public DateRangeValidator(Date theStartDate, Date theEndDate, String JavaDoc theErrMsg) {
60         super(theErrMsg);
61         myStartDate = theStartDate;
62         myEndDate = theEndDate;
63     }
64
65     //bw_102501.1 - added
66
/**
67      * Return the minimum date value
68      *
69      * @return The date being compared against
70      */

71     public Date getStartDate() {
72        return myStartDate;
73     }
74
75     //bw_102501.1 - added
76
/**
77      * Return the maximum date value
78      *
79      * @return The date being compared against
80      */

81     public Date getEndDate() {
82        return myEndDate;
83     }
84
85     /*
86      * Validate a FormElement to make see if the element equals() a
87      * given object
88      *
89      * @param element the form element to be validated
90      * @param element the form element that contains the val
91      * to validate elements by comparing them with other elements)
92      * @param deferExceptions do we want to deferValidation exceptions
93      * and attempt to validate all elements so that we can process
94      * all the exceptions at once
95      * @throws ValidationException if the element is not valid
96      *
97     public void validateFormElement(Object val, FormElement element, boolean deferExceptions) throws ValidationException {
98
99         //eliminate the obvious
100         if (myStartDate==null && myEndDate==null) {
101             throw this.generateException(element,
102                                          deferExceptions,
103                                          "Invalid range:"+myStartDate+" to "+myEndDate);
104         }
105
106         // cannot validate a null or empty string value, use NotNullvalidator if you want to disallow nulls
107         if (val==null || val.toString().length()==0) return;
108
109
110
111         //figure out what type of element we're dealing with
112         if (localLogger.isInfoEnabled()) localLogger.info("validating val="+val+" is in range between "+myStartDate+" and "+myEndDate);
113         FormType formType = element.getType();
114
115         //if element is null, or not form type=STRING or DATE, err because this should
116         //not be considered a valid option (note that these exceptions are always
117         //immediate since they typically represent a programming error)
118         if (element==null) throw new ValidationException(val, "Object val:"+val+" is associated with a null FormElement");
119         if (!(formType.equals(FormType.DATE) || formType.equals(FormType.STRING))) {
120             throw new ValidationException(val, "Unsupported validation: "+
121                                                                         val+" is of FormType " +
122                                                                         formType.toString() +
123                                                                         " and cannot be validated by this validator");
124         }
125
126         Date dateVal = null;
127
128
129         if (formType.equals(FormType.DATE)) {
130             try {
131                 dateVal = (Date)val;
132             } catch (ClassCastException ex) {
133                 throw this.generateException(element,
134                                                                          deferExceptions,
135                                                                          val.toString() + " is not a valid date class");
136             }
137         } else {
138             // attempt to convert the string to a date
139             try {
140                 dateVal = (Date)(FormType.DATE.parse(val.toString()));
141
142             } catch (ParseException ex) {
143                 throw this.generateException(
144                     element,
145           deferExceptions,
146           val.toString() + " is not a valid date string");
147             }
148         }
149
150         String defaultErrMsg = null;
151         if (myEndDate==null) {
152             defaultErrMsg = "Value: "+val+ " is not after "+this.myStartDate;
153         } else if (myStartDate==null) {
154             defaultErrMsg = "Value: "+val+ " is not before "+this.myEndDate;
155         } else {
156             defaultErrMsg = "Value: "+val+ " is not in the range between "+this.myStartDate+" and "+this.myEndDate;
157         }
158
159         if (null!=this.myStartDate && dateVal.before(this.myStartDate)) {
160             throw this.generateException(element, deferExceptions, defaultErrMsg);
161         }
162
163         if (null!=this.myEndDate && dateVal.after(this.myEndDate)) {
164             throw this.generateException(element, deferExceptions, defaultErrMsg);
165         }
166
167     } */

168
169     /**
170      * Validate a FormElement to make see if the element equals() a
171      * given object
172      *
173      * @param val the value to compare the element value to
174      * @param element the form element that contains the val
175      * to validate elements by comparing them with other elements)
176      * @param deferExceptions do we want to deferValidation exceptions
177      * and attempt to validate all elements so that we can process
178      * all the exceptions at once
179      * @throws ValidationException if the element is not valid
180      */

181     public void validateFormElement(Object JavaDoc val, FormElement element, boolean deferExceptions) throws ValidationException {
182         // ilc_022102.1_start
183
// Make no judgements on null values, use NotNullValidator
184
if (this.isNull(val, element))
185           return;
186         // ilc_022102.1_end
187

188         //eliminate the obvious
189
if (myStartDate==null && myEndDate==null) {
190             throw this.generateException(element,
191                                          deferExceptions,
192                                          "Invalid range:"+myStartDate+" to "+myEndDate);
193         }
194
195         // ilc_022102.2_start
196
// remove following ann replaced it with a call to isNull
197
// cannot validate a null or empty string value, use NotNullvalidator if you want to disallow nulls
198
// if (val==null || val.toString().length()==0) return;
199
// ilc_022102.2_end
200

201
202
203
204
205         //figure out what type of element we're dealing with
206
if (localLogger.isInfoEnabled()) localLogger.info("validating val="+val+" is in range between "+myStartDate+" and "+myEndDate);
207         // ilc_022102.3_start
208
// move element.getType after the test of element for null
209
// FormType formType = element.getType();
210
// ilc_022102.3_end
211

212         //if element is null, or not form type=STRING or DATE, err because this should
213
//not be considered a valid option (note that these exceptions are always
214
//immediate since they typically represent a programming error)
215
if (element==null) throw new ValidationException(val, "Object val:"+val+" is associated with a null FormElement");
216
217         // ilc_022102.4_start
218
// move element.getType after the test of element for null
219
FormType formType = element.getType();
220         // ilc_022102.4_end
221

222         if (!(formType.equals(FormType.DATE) || formType.equals(FormType.STRING))) {
223             throw new ValidationException(val, "Unsupported validation: "+
224                                                                         val+" is of FormType " +
225                                                                         formType.toString() +
226                                                                         " and cannot be validated by this validator");
227         }
228
229         Date dateVal = null;
230
231
232         // ilc_022202.1_start
233
// added the following
234
// technically we're suppose to be validating origVal and it
235
// is always suppose to be a string or ArrayList. I check the
236
// type of val because the test cases test against a Date type
237
if (val instanceof Date) {
238           dateVal = (Date)val;
239         }
240         else {
241           if (formType.equals(FormType.DATE)) {
242             if (element.getParseException()==null)
243               dateVal = (Date)element.getVal();
244             else
245               throw this.generateException(element, deferExceptions, val.toString() + " is not a valid date string");
246           }
247           else {
248
249             try {
250
251               if (element.getParseException()==null)
252
253               dateVal = (Date)(FormType.DATE.parse(val.toString()));
254             } catch (ParseException ex) {
255               throw this.generateException(element, deferExceptions, val.toString() + " is not a valid date string");
256             }
257           }
258         }
259         // ilc_022202.1_end
260

261
262         // ilc_022202.2_start
263
/* removed this and replaced with the above
264                 if (formType.equals(FormType.DATE)) {
265             try {
266                 dateVal = (Date)val;
267             } catch (ClassCastException ex) {
268                 throw this.generateException(element,
269                                                                          deferExceptions,
270                                                                          val.toString() + " is not a valid date class");
271             }
272         } else {
273             // attempt to convert the string to a date
274             try {
275                 dateVal = (Date)(FormType.DATE.parse(val.toString()));
276
277             } catch (ParseException ex) {
278                 throw this.generateException(
279                     element,
280           deferExceptions,
281           val.toString() + " is not a valid date string");
282             }
283         }
284         */

285         // ilc_022202.2_start
286

287
288
289         String JavaDoc defaultErrMsg = null;
290
291         if (myEndDate==null) {
292             defaultErrMsg = "Value: "+val+ " is not after "+this.myStartDate;
293         } else if (myStartDate==null) {
294             defaultErrMsg = "Value: "+val+ " is not before "+this.myEndDate;
295         } else {
296             defaultErrMsg = "Value: "+val+ " is not in the range between "+this.myStartDate+" and "+this.myEndDate;
297         }
298
299         if (null!=this.myStartDate && dateVal.before(this.myStartDate)) {
300             throw this.generateException(element, deferExceptions, defaultErrMsg);
301         }
302
303         if (null!=this.myEndDate && dateVal.after(this.myEndDate)) {
304             throw this.generateException(element, deferExceptions, defaultErrMsg);
305         }
306
307     }
308
309
310 }
311
Popular Tags