KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > security > license > DateValidator


1 /*
2  * Created on Sep 14, 2003
3  *
4  * To change the template for this generated file go to
5  * Window>Preferences>Java>Code Generation>Code and Comments
6  */

7 package org.jahia.security.license;
8
9 import java.text.ParseException JavaDoc;
10 import java.text.SimpleDateFormat JavaDoc;
11 import java.util.Date JavaDoc;
12 import org.jahia.resourcebundle.ResourceMessage;
13
14 /**
15  * @author loom
16  *
17  * To change the template for this generated type comment go to
18  * Window>Preferences>Java>Code Generation>Code and Comments
19  */

20 public class DateValidator extends AbstractValidator {
21
22     SimpleDateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc("yyyy-MM-dd");
23     Date JavaDoc nowDate = new Date JavaDoc();
24
25     public DateValidator(String JavaDoc name, String JavaDoc value, License license) {
26         super(name, value, license);
27     }
28
29     public boolean assertEquals(String JavaDoc value) {
30         Date JavaDoc licenseDate;
31         try {
32             licenseDate = dateFormat.parse(value);
33         } catch (ParseException JavaDoc e) {
34             errorMessage = new ResourceMessage("org.jahia.security.license.DateValidator.invalidLicenseDateValue.label", value);
35             return false;
36         }
37         if (licenseDate.after(nowDate)) {
38             return true;
39         } else {
40             errorMessage = new ResourceMessage("org.jahia.security.license.DateValidator.invalidDate.label", nowDate, licenseDate);
41             return false;
42         }
43     }
44
45     public boolean assertInRange(String JavaDoc fromValue, String JavaDoc toValue) {
46         Date JavaDoc fromLicenseDate;
47         Date JavaDoc toLicenseDate;
48         try {
49             fromLicenseDate = dateFormat.parse(fromValue);
50         } catch (ParseException JavaDoc e) {
51             errorMessage = new ResourceMessage("org.jahia.security.license.DateValidator.invalidLicenseDateValue.label", fromValue);
52             return false;
53         }
54
55         try {
56             toLicenseDate = dateFormat.parse(toValue);
57         } catch (ParseException JavaDoc e) {
58             errorMessage = new ResourceMessage("org.jahia.security.license.DateValidator.invalidLicenseDateValue.label", toValue);
59             return false;
60         }
61
62         if ((fromLicenseDate.compareTo(nowDate) <= 0) &&
63             (toLicenseDate.compareTo(nowDate) >= 0)) {
64             return true;
65         }
66
67         errorMessage = new ResourceMessage("org.jahia.security.license.DateValidator.dateNotInRange.label", nowDate, fromLicenseDate, toLicenseDate);
68         return false;
69     }
70
71 }
72
Popular Tags