Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.richfaces.validator.BeanValidatorServiceImpl Maven / Gradle / Ivy
package org.richfaces.validator;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.el.ELException;
import javax.el.ValueExpression;
import javax.faces.FacesException;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import javax.validation.metadata.ConstraintDescriptor;
import javax.validation.metadata.ElementDescriptor.ConstraintFinder;
import javax.validation.metadata.PropertyDescriptor;
import org.richfaces.component.util.Strings;
import org.richfaces.el.ValueDescriptor;
import org.richfaces.el.ValueExpressionAnalayser;
import com.google.common.collect.ImmutableSet;
public class BeanValidatorServiceImpl implements BeanValidatorService {
private static final Collection HIDDEN_PARAMS = ImmutableSet.of("message" , "payload" , "groups" );
private static final String FACES_CONTEXT_IS_NULL = "Faces context is null" ;
private static final String INPUT_PARAMETERS_IS_NOT_CORRECT = "Input parameters is not correct." ;
private static final Class[] DEFAULT_GROUP = {};
private final ValueExpressionAnalayser analayser;
private final BeanValidatorFactory validatorFactory;
public BeanValidatorServiceImpl (ValueExpressionAnalayser analayser, BeanValidatorFactory validatorFactory) {
this .analayser = analayser;
this .validatorFactory = validatorFactory;
}
public Collection getConstrains (FacesContext context, ValueExpression expression, String message,
Class... groups) {
try {
ValueDescriptor propertyDescriptor = analayser.getPropertyDescriptor(context, expression);
if (propertyDescriptor == null ) {
return Collections.emptySet();
}
return processBeanAttribute(context, propertyDescriptor, message, groups);
} catch (ELException e) {
return Collections.emptySet();
}
}
private Validator getValidator (FacesContext context) {
return validatorFactory.getValidator(context);
}
Collection processBeanAttribute (FacesContext context, ValueDescriptor descriptor, String msg,
Class... groups) {
PropertyDescriptor constraintsForProperty = getValidator(context).getConstraintsForClass(descriptor.getBeanType())
.getConstraintsForProperty(descriptor.getName());
if (null != constraintsForProperty) {
ConstraintFinder propertyConstraints = constraintsForProperty.findConstraints();
if (null != groups && groups.length > 0 ) {
propertyConstraints = propertyConstraints.unorderedAndMatchingGroups(groups);
}
Set> constraints = propertyConstraints
.getConstraintDescriptors();
FacesMessage message = Strings.isEmpty(msg) ? null : new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
return processConstraints(context, constraints, message);
} else {
return Collections.emptySet();
}
}
Collection processConstraints (FacesContext context, Set > constraints,
FacesMessage msg) {
Set descriptors = new HashSet(constraints.size());
for (ConstraintDescriptor cd : constraints) {
Annotation a = cd.getAnnotation();
Map parameters = cd.getAttributes();
FacesMessage message = null == msg ? validatorFactory.interpolateMessage(context, cd) : msg;
Class validatorClass = findAnnotationClass(a);
BeanValidatorDescriptor beanValidatorDescriptor = new BeanValidatorDescriptor(validatorClass, message);
for (Map.Entry entry : parameters.entrySet()) {
String key = entry.getKey();
if (!HIDDEN_PARAMS.contains(key)) {
Object value = entry.getValue();
try {
Method method = validatorClass.getDeclaredMethod(key);
Object defaultValue = method.getDefaultValue();
if (!value.equals(defaultValue)) {
beanValidatorDescriptor.addParameter(key, value);
}
} catch (SecurityException e) {
beanValidatorDescriptor.addParameter(key, value);
} catch (NoSuchMethodException e) {
beanValidatorDescriptor.addParameter(key, value);
}
}
}
beanValidatorDescriptor.makeImmutable();
descriptors.add(beanValidatorDescriptor);
descriptors.addAll(processConstraints(context, cd.getComposingConstraints(), msg));
}
return descriptors;
}
private Class findAnnotationClass (Annotation a) {
Class annotationClass = a.getClass();
if (!annotationClass.isAnnotation()) {
Class[] interfaces = annotationClass.getInterfaces();
for (Class implemented : interfaces) {
if (implemented.isAnnotation()) {
annotationClass = (Class) implemented;
}
}
}
return annotationClass;
}
public Collection validateExpression (FacesContext context, ValueExpression expression, Object newValue,
Class... groups) {
if (null == context) {
throw new FacesException(INPUT_PARAMETERS_IS_NOT_CORRECT);
}
Collection validationMessages = null ;
if (null != expression) {
ValueDescriptor valueDescriptor;
try {
valueDescriptor = analayser.updateValueAndGetPropertyDescriptor(context, expression, newValue);
} catch (ELException e) {
throw new FacesException(e);
}
if (valueDescriptor != null ) {
validationMessages = validate(context, valueDescriptor.getBeanType(), valueDescriptor.getName(), newValue,
groups);
}
}
if (validationMessages == null ) {
validationMessages = Collections.emptySet();
}
return validationMessages;
}
protected static class ValidatorKey {
private final Class validatableClass;
private final Locale locale;
public ValidatorKey (Class validatableClass, Locale locale) {
this .validatableClass = validatableClass;
this .locale = locale;
}
@Override
public int hashCode () {
final int prime = 31 ;
int result = 1 ;
result = prime * result + ((this .locale == null ) ? 0 : this .locale.hashCode());
result = prime * result + ((this .validatableClass == null ) ? 0 : this .validatableClass.hashCode());
return result;
}
@Override
public boolean equals (Object obj) {
if (this == obj) {
return true ;
}
if (obj == null ) {
return false ;
}
if (getClass() != obj.getClass()) {
return false ;
}
ValidatorKey other = (ValidatorKey) obj;
if (this .locale == null ) {
if (other.locale != null ) {
return false ;
}
} else if (!this .locale.equals(other.locale)) {
return false ;
}
if (this .validatableClass == null ) {
if (other.validatableClass != null ) {
return false ;
}
} else if (!this .validatableClass.equals(other.validatableClass)) {
return false ;
}
return true ;
}
}
protected Collection validate (FacesContext facesContext, Class beanType, String property, Object value,
Class[] groups) {
@SuppressWarnings ("unchecked" )
Set> constrains = getValidator(facesContext).validateValue((Class) beanType,
property, value, getGroups(groups));
return extractMessages(constrains);
}
private Class[] getGroups(Class[] groups) {
return null == groups ? DEFAULT_GROUP : groups;
}
public Collection validateObject (FacesContext context, Object value, Class... groups) {
Set> violations = getValidator(context).validate(value, getGroups(groups));
Collection messages = extractMessages(violations);
return messages;
}
private Collection extractMessages (Set > violations) {
Collection messages;
if (null != violations && violations.size() > 0 ) {
messages = new ArrayList(violations.size());
for (ConstraintViolation constraintViolation : violations) {
messages.add(constraintViolation.getMessage());
}
} else {
messages = Collections.emptySet();
}
return messages;
}
}