/**
 * 
 */
package org.springblade.etl.util;

import java.util.List;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;

import org.springblade.core.tool.exception.ServiceException;


/**
 * @author wookvn
 *
 */
public final class ValidateUtil {

	private static final Validator validator =
            Validation.buildDefaultValidatorFactory().getValidator();

    /**
     * 校验实体类
     */
    public static <T> void validate(T t) {
        Set<ConstraintViolation<T>> constraintViolations = validator.validate(t);
        if (constraintViolations.size() > 0) {
            String validateError = null;
            for (ConstraintViolation<T> constraintViolation : constraintViolations) {
            	validateError = constraintViolation.getMessage();
            	break;
            }
            throw new ServiceException(validateError.toString());
        }
    }
    
    public static <T> void validate(List<T> list) {
    	for(T t : list) {
    		validate(t);
    	}
    }
}
