First page Back Continue Last page Graphics


Modifying the Code for the Global Rule (continued)

3. Next you perform the comparison in the validate() method. You first call the method that you just defined to ensure that the validator is defined at the entity level, and then you call validateValue() to compare the dates, raising an exception if the validation fails: public void validate(JboValidatorContext ctx) { if (validatorAttachedAtEntityLevel(ctx)){ EntityImpl eo = (EntityImpl)ctx.getSource(); Date earlierDate = (Date)eo.getAttribute(getEarlierDateAttrName()); Date laterDate = (Date)eo.getAttribute(getLaterDateAttrName()); if (!validateValue(earlierDate,laterDate)) { throw new ValidationException("model.CompareDates validation failed"); } } else { throw new RuntimeException("Rule must be at entity level"); } }

4. Finally, you modify validateValue() to perform the comparison: private boolean validateValue(Date earlierDate, Date laterDate) { return (earlierDate == null) || (laterDate == null) || (earlierDate.compareTo(laterDate) <= 0); }