package com.winsea.svc.clouddisk.annotation;


import java.text.SimpleDateFormat;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class DateTimeValidator implements ConstraintValidator<DateTime, String> {
	
	private DateTime dateTime;

	@Override
	public void initialize(DateTime dateTime) {
		this.dateTime = dateTime;
	}

	@Override
	public boolean isValid(String value, ConstraintValidatorContext context) { 
																				
        if (value == null) {
            return true;
        }
		String format = dateTime.format();
		
		if (value.length() != format.length()) {
			return false;
		}
		
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		try {
			simpleDateFormat.setLenient(false);
			simpleDateFormat.parse(value);
		} catch (Exception e) {
			return false;
		}
		return true;
	}
}