Menu

XavaDateValidator

2008-01-17
2012-11-26
  • Janesh Kodikara

    Janesh Kodikara - 2008-01-17

    Hi Javi
    Following date validation class is used within qaManager.
    I feel this can be included in OpneXava (may be with modifications)

    package org.openxava.validators;

    import org.apache.commons.validator.routines.DateValidator;
    import org.openxava.util.Messages;
    import java.util.Date;

    /**
    * @author Janesh Kodikara
    */

    public class XavaDateValidator {

        DateValidator validator;

        public XavaDateValidator() {
            validator = new DateValidator();
        }

        public boolean isSameDate(Date startDate, Date endDate) {
            boolean isSame = false;

            if (validator.compareDates(startDate, endDate, null) == 0) {
                isSame = true;
            }
            return isSame;
        }

        public void isSameDate(Messages errors, String startFieldName, Date startDate, String endFieldName, Date endDate) {

            if (startDate == null || endDate == null) return;

            if (validator.compareDates(startDate, endDate, null) == 0) {
                errors.add("same_dates_error", endFieldName, startFieldName);
            }
        }

        public boolean isFutureDate(Date value) {
            boolean isSame = false;
            Date today = new Date();
            if (validator.compareDates(value, today, null) > 0) {
                isSame = true;
            }
            return isSame;

        }

        public void isFutureDate(Messages errors, String fieldName, Date value) {
            Date today = new Date();

            if (value == null) return;

            if (validator.compareDates(value, today, null) > 0) {
                errors.add("date_in_future_error", fieldName);
            }

        }

        public boolean isPastDate(Date value) {
            boolean isSame = false;
            Date today = new Date();
            if (validator.compareDates(value, today, null) < 0) {
                isSame = true;
            }
            return isSame;

        }
        public void isPastDate(Messages errors, String fieldName, Date value) {
            Date today = new Date();

            if (value == null) return;

            if (validator.compareDates(value, today, null) > 0) {
                errors.add("past_date_error", fieldName);
            }

        }

        public void isStartDateAfter(Messages errors, String startFieldName, Date startDate, String endFieldName, Date endDate) {
             if (startDate == null || endDate == null) return;

            if (validator.compareDates(startDate, endDate, null) > 0) {
                errors.add("start_date_greater_error", endFieldName, startFieldName);
            }
        }

    }

    Please do the necessary changes .

    Thanking you
    Janesh

     
    • Javier Paniza

      Javier Paniza - 2008-01-18

      Hi Janesh,

      I find problems about nomenclature.
      Nomenclature is important in OpenXava and in any
      no trivial system, we need not only intuitive names,
      but coherence in the way for naming all things.

      First, the name of your name has the suffix Validator,
      and it is not a validator, because it does not implement
      IPropertyValidator or IValidator. It is not a validator
      from a OpenXava developer perspective, so the name is
      confused.

      Second, it seems an utility class, but utility classes are in
      org.openxava.util package. An utility class must be in
      plural and its methods have to be static.
      Already exists an utility class for dates calles Dates.

      Third, a method that starts with 'is' cannot have void as result.

      The logic is your class is good but you have to adapt its shape
      in order to fit nicely in OpenXava, in this way:
      1. Move the methods with Message to its own IPropertyValidator, if it applies.
      2. Move other methods to the already existing org.openxava.util.Dates. These
      method must be static, with not Message arguments and its logic cannot be
      duplicated.

      Have a look to these issues and give me your opinion.

      Cheers
      Javi

       
    • Janesh Kodikara

      Janesh Kodikara - 2008-01-21

      Hi Javi
      Thanks and appreciate the recommendations.

      Thanking You
      Janesh

       

Log in to post a comment.