package test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import test.DayTime;
import test.DateTimeNotNull;
public class DayTimeValid implements ConstraintValidator<DateTimeNotNull, DayTime> {
private final static SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
@Override
public void initialize(DayTimeVo obj) {
}
@Override
public boolean isValid(DayTime daytime,
ConstraintValidatorContext constraintValidatorContext) {
constraintValidatorContext.disableDefaultConstraintViolation();
if (daytime.getMorningStartDateTime() == null
&& daytime.getMorningEndDateTime() == null
&& daytime.getAfternoonStartDateTime() == null
&& daytime.getAfternoonEndDateTime() == null) {
constraintValidatorContext.buildConstraintViolationWithTemplate(
"服务时间不能为空!").addConstraintViolation();
return false;
} else if (daytime.getMorningStartDateTime() == null
&& daytime.getMorningEndDateTime() != null) {
constraintValidatorContext.buildConstraintViolationWithTemplate(
"上午开始时间不能为空!").addConstraintViolation();
return false;
} else if (daytime.getMorningStartDateTime() != null
&& daytime.getMorningEndDateTime() == null) {
constraintValidatorContext.buildConstraintViolationWithTemplate(
"上午结束时间不能为空!").addConstraintViolation();
return false;
} else if (daytime.getAfternoonStartDateTime() == null
&& daytime.getAfternoonEndDateTime() != null) {
constraintValidatorContext.buildConstraintViolationWithTemplate(
"下午开始时间不能为空!").addConstraintViolation();
return false;
} else if (daytime.getAfternoonStartDateTime() != null
&& daytime.getAfternoonEndDateTime() == null) {
constraintValidatorContext.buildConstraintViolationWithTemplate(
"下午结束时间不能为空!").addConstraintViolation();
return false;
} else if (daytime.getMorningStartDateTime() != null
&& daytime.getMorningEndDateTime() != null) {
try {
sdf.parse(daytime.getMorningStartDateTime());
} catch (ParseException e) {
constraintValidatorContext
.buildConstraintViolationWithTemplate("上午开始时间格式不正确!")
.addConstraintViolation();
return false;
}
try {
sdf.parse(daytime.getMorningEndDateTime());
} catch (ParseException e) {
constraintValidatorContext
.buildConstraintViolationWithTemplate("上午结束时间格式不正确!")
.addConstraintViolation();
return false;
}
} else if (daytime.getAfternoonStartDateTime() != null
&& daytime.getAfternoonEndDateTime() != null) {
try {
sdf.parse(daytime.getAfternoonStartDateTime());
} catch (ParseException e) {
constraintValidatorContext
.buildConstraintViolationWithTemplate("下午开始时间格式不正确!")
.addConstraintViolation();
return false;
}
try {
sdf.parse(daytime.getAfternoonEndDateTime());
} catch (ParseException e) {
constraintValidatorContext
.buildConstraintViolationWithTemplate("下午结束时间格式不正确!")
.addConstraintViolation();
return false;
}
}
return true;
}
}