package com.gr.project.util;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import javax.validation.Validator;
import javax.ws.rs.core.Response;
@ApplicationScoped
public class EntityValidator {
@Inject
private Logger log;
@Inject
private Validator validator;
/**
* <p>
* Validates the given Member variable and throws validation exceptions based on the type of error. If the error is standard
* bean validation errors then it will throw a ConstraintValidationException with the set of the constraints violated.
* </p>
* <p>
* If the error is caused because an existing member with the same email is registered it throws a regular validation
* exception so that it can be interpreted separately.
* </p>
*
* @param member Member to be validated
* @throws ConstraintViolationException If Bean Validation errors exist
* @throws ValidationException If member with the same email already exists
*/
public <T> void validateEntity(final T entity) throws ConstraintViolationException, ValidationException {
// Create a bean validator and check for issues.
final Set<ConstraintViolation<T>> violations = validator.validate(entity);
if (!violations.isEmpty()) {
throw new ConstraintViolationException(new HashSet<ConstraintViolation<?>>(violations));
}
}
/**
* Creates a JAX-RS "Bad Request" response including a map of all violation fields, and their message. This can then be used
* by clients to show violations.
*
* @param violations A set of violations that needs to be reported
* @return JAX-RS response containing all violations
*/
public Response.ResponseBuilder createViolationResponse(Set<ConstraintViolation<?>> violations) {
log.fine("Validation completed. violations found: " + violations.size());
Map<String, String> responseObj = new HashMap<String, String>();
for (ConstraintViolation<?> violation : violations) {
responseObj.put(violation.getPropertyPath().toString(), violation.getMessage());
}
return Response.status(Response.Status.BAD_REQUEST).entity(responseObj);
}
}