package kr.co.uplus.ez.common.components;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;

@Component
public class ValidComponents {

	private static final Logger logger = LoggerFactory.getLogger(ValidComponents.class);

	/**
	 * 파라미터 검증.
	 *
	 * @param bindingResult
	 * @return boolean
	 */
	public boolean validParameter(BindingResult bindingResult) {
		if(bindingResult.hasErrors()) {
			StringBuffer sb = new StringBuffer();
			List<ObjectError> objectErrors = bindingResult.getAllErrors();

			objectErrors.forEach(objectError -> {
				FieldError fieldError = (FieldError) objectError;
				String field = fieldError.getField();
				String message = fieldError.getDefaultMessage();
				sb.append("field : " + field + "\n");
				sb.append("message : " + message + "\n");
			});
			logger.debug("adminList parm Error : {}", sb.toString());
			return true;
		}
		return false;
	}
}
