br.com.anteros.bean.validation.ConstraintValidatorContextImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Anteros-Bean-Validation Show documentation
Show all versions of Anteros-Bean-Validation Show documentation
Anteros Bean Validation for Java.
/*******************************************************************************
* Copyright 2012 Anteros Tecnologia
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package br.com.anteros.bean.validation;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import br.com.anteros.bean.validation.model.ValidationListener;
import br.com.anteros.bean.validation.util.NodeBuilderDefinedContextImpl;
import br.com.anteros.bean.validation.util.NodeImpl;
import br.com.anteros.bean.validation.util.PathImpl;
import br.com.anteros.validation.api.ConstraintValidator;
import br.com.anteros.validation.api.ConstraintValidatorContext;
import br.com.anteros.validation.api.Path;
import br.com.anteros.validation.api.ValidationException;
/**
* Description: Short-lived {@link ConstraintValidatorContext} implementation passed by
* a {@link ConstraintValidation} to its adapted {@link ConstraintValidator}.
*/
public class ConstraintValidatorContextImpl implements ConstraintValidatorContext {
private final List errorMessages =
new LinkedList();
private final ConstraintValidation> constraintDescriptor;
private final GroupValidationContext> validationContext;
private boolean defaultDisabled;
/**
* Create a new ConstraintValidatorContextImpl instance.
* @param validationContext
* @param aConstraintValidation
*/
public ConstraintValidatorContextImpl(GroupValidationContext> validationContext,
ConstraintValidation> aConstraintValidation) {
this.validationContext = validationContext;
this.constraintDescriptor = aConstraintValidation;
}
/**
* {@inheritDoc}
*/
public void disableDefaultConstraintViolation() {
defaultDisabled = true;
}
/**
* {@inheritDoc}
*/
public String getDefaultConstraintMessageTemplate() {
return constraintDescriptor.getMessageTemplate();
}
/**
* {@inheritDoc}
*/
public ConstraintViolationBuilder buildConstraintViolationWithTemplate(
String messageTemplate) {
return new ConstraintViolationBuilderImpl(this, messageTemplate,
validationContext.getPropertyPath());
}
private static final class ConstraintViolationBuilderImpl
implements ConstraintValidatorContext.ConstraintViolationBuilder {
private final ConstraintValidatorContextImpl parent;
private final String messageTemplate;
private final PathImpl propertyPath;
/**
* Create a new ConstraintViolationBuilderImpl instance.
* @param contextImpl
* @param template
* @param path
*/
ConstraintViolationBuilderImpl(ConstraintValidatorContextImpl contextImpl,
String template, PathImpl path) {
parent = contextImpl;
messageTemplate = template;
propertyPath = path;
}
/**
* {@inheritDoc}
*/
public NodeBuilderDefinedContext addNode(String name) {
PathImpl path;
if (propertyPath.isRootPath()) {
path = PathImpl.create(name);
} else {
path = PathImpl.copy(propertyPath);
path.addNode(new NodeImpl(name));
}
return new NodeBuilderDefinedContextImpl(parent, messageTemplate, path);
}
/**
* {@inheritDoc}
*/
public ConstraintValidatorContext addConstraintViolation() {
parent.addError(messageTemplate, propertyPath);
return parent;
}
}
/**
* Get the queued error messages.
* @return List
*/
public List getErrorMessages() {
if (defaultDisabled && errorMessages.isEmpty()) {
throw new ValidationException(
"At least one custom message must be created if the default error message gets disabled.");
}
List returnedErrorMessages =
new ArrayList(errorMessages);
if (!defaultDisabled) {
returnedErrorMessages.add(new ValidationListener.Error(
getDefaultConstraintMessageTemplate(), validationContext.getPropertyPath(),
null));
}
return returnedErrorMessages;
}
/**
* Get this {@link ConstraintValidatorContext}'s {@link GroupValidationContext}.
* @return {@link GroupValidationContext}
*/
public GroupValidationContext> getValidationContext() {
return validationContext;
}
/**
* Add an error message to this {@link ConstraintValidatorContext}.
* @param messageTemplate
* @param propertyPath
*/
public void addError(String messageTemplate, Path propertyPath) {
errorMessages.add(new ValidationListener.Error(messageTemplate, propertyPath, null));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy