@FacesValidator("com.liferay.faces.showcase.validator.WeekdayValidator")
public class WeekdayValidator implements Validator {
@Override
public void validate(FacesContext facesContext, UIComponent uiComponent, Object object) throws ValidatorException {
if (object != null) {
Date date;
InputDate inputDate = (InputDate) uiComponent;
String timeZoneString = inputDate.getTimeZone();
TimeZone timeZone = TimeZone.getTimeZone(timeZoneString);
if (object instanceof Date) {
date = (Date) object;
}
else {
String dateString = object.toString();
String datePattern = inputDate.getPattern();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(datePattern);
simpleDateFormat.setTimeZone(timeZone);
try {
date = simpleDateFormat.parse(dateString);
}
catch (ParseException e) {
String message = AlloyValidatorHelper.getMessage(facesContext, inputDate, "invalid-selection");
FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message);
throw new ValidatorException(facesMessage, e);
}
}
Calendar calendar = new GregorianCalendar(timeZone);
calendar.setTime(date);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
if ((dayOfWeek == Calendar.SATURDAY) || (dayOfWeek == Calendar.SUNDAY)) {
String message = AlloyValidatorHelper.getMessage(facesContext, inputDate, "sat-and-sun-are-not-valid");
FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_WARN, message, message);
throw new ValidatorException(facesMessage);
}
}
}
}