package com.xintong.visualinspection.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import com.xintong.visualinspection.err.BusinessException;
@Aspect
@Component
public class ControllerValidatorInterceptor {
	 @Around("execution(* com.xintong.visualinspection.controller..*.*(..)) && args(..,bindingResult)")
     public Object doAround(ProceedingJoinPoint pjp, BindingResult bindingResult) throws Throwable {
         Object retVal;
         if (bindingResult.hasErrors()) {
        	 
        	 StringBuffer sb = new StringBuffer();
        	 for(ObjectError err:bindingResult.getAllErrors()){
        		 sb.append(err.getDefaultMessage()+";");
        	 }
        	 String s = sb.toString();
        	 if(s.length()>0) s = sb.substring(0, s.length()-1);
             throw new BusinessException(20001,s);
         } else {
             retVal = pjp.proceed();
         }
         return retVal;
     }
}
