Java tutorial
package com.webbfontaine.valuewebb.model.validators.tt; import com.webbfontaine.valuewebb.model.constants.Constants; import com.webbfontaine.valuewebb.model.util.Utils; import com.webbfontaine.valuewebb.validation.ErrorHandling; import com.webbfontaine.valuewebb.validation.MessageHandling; import org.apache.commons.lang3.time.DateUtils; import org.hibernate.validator.Validator; import org.jboss.seam.annotations.In; import org.jboss.seam.annotations.Name; import javax.faces.application.FacesMessage; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.validator.ValidatorException; import java.util.Calendar; import java.util.Date; /** * Copyrights 2002-2013 Webb Fontaine * This software is the proprietary information of Webb Fontaine. * Its use is subject to License terms. * Developer: nigiyan * Date: 07/10/2013 */ @Name("ttDateValidator") public class TTDateValidator implements Validator<TTDate> { @In ErrorHandling errorHandling; @Override public boolean isValid(Object value) { int range = Utils.getAppCountry().equals(Constants.GH) ? -3 : -2; if (Utils.currentPage().contains("TtGenList")) { return true; } boolean notFuture; if (value != null && value instanceof Date) { Date dateValue = (Date) value; Calendar cal = Calendar.getInstance(); cal.add(Calendar.YEAR, range); notFuture = !DateUtils.truncate(dateValue, Calendar.DAY_OF_MONTH) .after(DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH)); // value must not be in future return notFuture && dateValue.after(cal.getTime()); } return true; } @Override public void initialize(TTDate parameters) { } public void validate(FacesContext facesContext, UIComponent uiComponent, Object o) throws ValidatorException { int range = Utils.getAppCountry().equals(Constants.GH) ? 36 : 24; String errorMessage = "{Invalid Date! Date must either not be in the future or older than " + range + " months}"; if (isValid(o)) { errorHandling.removeErrorByFieldIdAndErrorMessage( uiComponent.getClientId(FacesContext.getCurrentInstance()), errorMessage); } else { FacesMessage facesMessage = MessageHandling.createFacesErrorMessage(errorMessage); throw new ValidatorException(facesMessage); } } }