package org.gwtoolbox.bean.client.validation;
import javax.validation.ConstraintViolation;
import javax.validation.Path;
import java.util.HashSet;
import java.util.Set;
/**
* @author Uri Boness
*/
public class ViolationRegistry<T> {
private Set<ConstraintViolation> violations = new HashSet<ConstraintViolation>();
private PathImpl contextPath;
private final T rootBean;
private final Class<T> rootBeanClass;
public ViolationRegistry(T rootBean) {
this(rootBean, (Class<T>) rootBean.getClass());
}
public ViolationRegistry(T rootBean, Class<T> rootBeanClass) {
this.rootBean = rootBean;
this.rootBeanClass = rootBeanClass;
contextPath = new PathImpl();
}
public void register(Object bean, String property, String message, String messageTemplate, Object invalidValue) {
Path path = property != null ? contextPath.push(new PathImpl.NodeImpl(property)) : contextPath;
ConstraintViolation<T> violation = new ConstraintViolationImpl<T>(
message, messageTemplate, rootBean, rootBeanClass, bean, path, invalidValue);
violations.add(violation);
}
public void push(Path.Node node) {
contextPath = contextPath.push(node);
}
public void pop() {
contextPath = contextPath.pop();
}
public boolean hasViolations() {
return !violations.isEmpty();
}
public Set<ConstraintViolation> getViolations() {
return violations;
}
}