1 7 package org.jahia.security.license; 8 9 import java.text.ParseException ; 10 import java.text.SimpleDateFormat ; 11 import java.util.Date ; 12 import org.jahia.resourcebundle.ResourceMessage; 13 14 20 public class DateValidator extends AbstractValidator { 21 22 SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd"); 23 Date nowDate = new Date (); 24 25 public DateValidator(String name, String value, License license) { 26 super(name, value, license); 27 } 28 29 public boolean assertEquals(String value) { 30 Date licenseDate; 31 try { 32 licenseDate = dateFormat.parse(value); 33 } catch (ParseException 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 fromValue, String toValue) { 46 Date fromLicenseDate; 47 Date toLicenseDate; 48 try { 49 fromLicenseDate = dateFormat.parse(fromValue); 50 } catch (ParseException 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 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 |