| 
						
						
						
					 | 
				
				 | 
				
					@ -1,25 +1,26 @@ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					/** | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					 * Copyright (c) 2018 人人开源 All rights reserved. | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					 * | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					 * <p> | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					 * https://www.renren.io
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					 * | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					 * <p> | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					 * 版权所有,侵权必究! | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					 */ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					package com.epmet.commons.tools.validator; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					import com.epmet.commons.tools.exception.RenException; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					import com.epmet.commons.tools.exception.EpmetErrorCode; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					import com.epmet.commons.tools.exception.ValidateException; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					import com.epmet.commons.tools.validator.group.CustomerClientShowGroup; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					import org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					import org.springframework.context.i18n.LocaleContextHolder; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					import org.springframework.context.support.ResourceBundleMessageSource; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					import org.springframework.util.CollectionUtils; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					import org.springframework.validation.beanvalidation.MessageSourceResourceBundleLocator; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					import javax.validation.ConstraintViolation; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					import javax.validation.Validation; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					import javax.validation.Validator; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					import java.util.Locale; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					import java.util.Set; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					import java.util.*; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					/** | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					 * hibernate-validator校验工具类 | 
				
			
			
		
	
	
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
				
				 | 
				
					@ -39,20 +40,65 @@ public class ValidatorUtils { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    /** | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					     * 校验对象 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					     * | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					     * @param object 待校验对象 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					     * @param groups 待校验的组 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					     * @throws ValidateException 校验不通过,则报RenException异常 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					     */ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    public static void validateEntity(Object object, Class<?>... groups) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            throws ValidateException { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        List<Class<?>> customerShowGroups = new ArrayList<>(); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        List<Class<?>> internalGroups = new ArrayList<>(); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        Arrays.asList(groups).forEach(g -> { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            if (CustomerClientShowGroup.class.isAssignableFrom(g)) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                //如果派生自客户端显示分组,那么会优先校验,并且将错误提示给客户端,返回客户端可见的错误码
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                customerShowGroups.add(g); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            } else { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                internalGroups.add(g); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        }); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        if (!CollectionUtils.isEmpty(customerShowGroups)) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            // 1.校验客户端显示分组
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            validate(EpmetErrorCode.CUSTOMER_VALIDATE_ERROR ,object, customerShowGroups.toArray(new Class<?>[customerShowGroups.size()])); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        } else { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            // 2.内部校验分组
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            validate(EpmetErrorCode.INTERNAL_VALIDATE_ERROR ,object, internalGroups.toArray(new Class<?>[customerShowGroups.size()])); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    private static Validator getValidator() { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        Locale.setDefault(LocaleContextHolder.getLocale()); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        Validator validator = Validation.byDefaultProvider().configure().messageInterpolator( | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                new ResourceBundleMessageInterpolator(new MessageSourceResourceBundleLocator(getMessageSource()))) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                .buildValidatorFactory().getValidator(); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        return Validation | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                .byDefaultProvider() | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                .configure() | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                .messageInterpolator(new ResourceBundleMessageInterpolator(new MessageSourceResourceBundleLocator(getMessageSource()))) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                .buildValidatorFactory() | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                .getValidator(); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    /** | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					     * 真正的校验方法 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					     * @param object | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					     * @param errorCode | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					     * @param groups | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					     */ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    private static void validate(EpmetErrorCode errorCode, Object object, Class<?>... groups) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        Validator validator = getValidator(); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        Set<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        if (!constraintViolations.isEmpty()) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        	ConstraintViolation<Object> constraint = constraintViolations.iterator().next(); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            throw new ValidateException(constraint.getMessage()); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            StringBuilder sb = new StringBuilder(); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            Iterator<ConstraintViolation<Object>> it = constraintViolations.iterator(); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            while (it.hasNext()) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                ConstraintViolation<Object> constraint = it.next(); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					                sb.append(constraint.getMessage()).append(","); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            int lastDouhaoIndex = sb.lastIndexOf(","); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            sb.replace(lastDouhaoIndex, lastDouhaoIndex + 1, ""); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					            throw new ValidateException(errorCode.getCode(),sb.toString()); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					} | 
				
			
			
		
	
	
		
			
				
					| 
						
						
						
					 | 
				
				 | 
				
					
  |