com.arch.util.BeanValidationUtils Maven / Gradle / Ivy
package com.arch.util;
import com.arch.util.LogUtils;
import com.arch.util.MessagePropertiesUtils;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Set;
public final class BeanValidationUtils {
private BeanValidationUtils() {
//
}
public static void validation(T objeto, Class>... classes) {
if (classes == null || classes.length == 0) {
return;
}
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set> constraintViolations = validator.validate(objeto, classes);
if (!constraintViolations.isEmpty()) {
throw new ConstraintViolationException(constraintViolations);
}
// ValidationException validacaoException = generateValidationException(constraintViolations);
//
// if (validacaoException.temExcecao()) {
// throw validacaoException;
// }
}
public static void validationProperty(T objeto, String property, Class>... classes) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set> constraintViolations = validator.validateProperty(objeto, property, classes);
if (!constraintViolations.isEmpty()) {
throw new ConstraintViolationException(constraintViolations);
}
// ValidationException validacaoException = generateValidationException(constraintViolations);
//
// if (validacaoException.temExcecao()) {
// throw validacaoException;
// }
}
public static void validationValue(Class classe, String property, Object value, Class>... classes) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set> constraintViolations = validator.validateValue(classe, property, value, classes);
if (!constraintViolations.isEmpty()) {
throw new ConstraintViolationException(constraintViolations);
}
// ValidationException validacaoException = generateValidationException(constraintViolations);
//
// if (validacaoException.temExcecao()) {
// throw validacaoException;
// }
}
public static boolean existsConstraintsValidation(T objeto, Class>... classes) {
try {
validation(objeto, classes);
return false;
} catch (ConstraintViolationException ve) {
LogUtils.generate(ve);
return true;
}
}
public static boolean existsConstraintsValidationProperty(T objeto, String property, Class>... classes) {
try {
validationProperty(objeto, property, classes);
return false;
} catch (ConstraintViolationException ve) {
LogUtils.generate(ve);
return true;
}
}
public static boolean existsConstraintsValidationValue(Class classe, String property, Object value, Class>... classes) {
try {
validationValue(classe, property, value, classes);
return false;
} catch (ConstraintViolationException ve) {
LogUtils.generate(ve);
return true;
}
}
public static String messageBundle(String key) {
return MessagePropertiesUtils.messageBundle("ValidationMessages", key);
}
public static String messageBundleParam(String key, String parametro) {
return MessagePropertiesUtils.messageBundleParam("ValidationMessages", key, parametro);
}
public static String messageBundle(String key, Object... parameters) {
return MessagePropertiesUtils.messageBundle("ValidationMessages", key, parameters);
}
// private static ValidationException generateValidationException(Set> constraintViolations) {
// ValidationException validacaoException = new ValidationException();
//
// constraintViolations
// .stream()
// .forEach(error -> {
// String message = error.getMessage();
// if (message.startsWith("label.") || message.startsWith("message.") || message.startsWith("required")) {
// message = BundleUtils.messageBundle(message);
// } else if (message.equals(BundleUtils.messageBundle("required.campoObrigatorio"))) {
// message = BundleUtils.messageBundle("label." + ReflectionUtils.getValueByName(error, "propertyPath").toString()) + " " + message;
// }
//
// validacaoException.add(message);
// });
//
// return validacaoException;
// }
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy