All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.primefaces.validate.bean.BeanValidationResolver Maven / Gradle / Ivy

There is a newer version: 14.0.0-RC3
Show newest version
/*
 * Copyright 2009-2013 PrimeTek.
 *
 * 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 org.primefaces.validate.bean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.el.ELContext;
import javax.el.ValueExpression;
import javax.el.ValueReference;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.constraints.AssertFalse;
import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Digits;
import javax.validation.constraints.Future;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import javax.validation.metadata.BeanDescriptor;
import javax.validation.metadata.ConstraintDescriptor;
import javax.validation.metadata.PropertyDescriptor;

import org.primefaces.context.RequestContext;
import org.primefaces.el.ValueExpressionAnalyzer;

public class BeanValidationResolver {

    private static final Logger LOG = Logger.getLogger(BeanValidationResolver.class.getName());

    private static final Map, ClientValidationConstraint> CONSTRAINT_MAPPER = new HashMap, ClientValidationConstraint>();
    
    static {
        CONSTRAINT_MAPPER.put(NotNull.class, new NotNullClientValidationConstraint());
        CONSTRAINT_MAPPER.put(Null.class, new NullClientValidationConstraint());
        CONSTRAINT_MAPPER.put(Size.class, new SizeClientValidationConstraint());
        CONSTRAINT_MAPPER.put(Min.class, new MinClientValidationConstraint());
        CONSTRAINT_MAPPER.put(Max.class, new MaxClientValidationConstraint());
        CONSTRAINT_MAPPER.put(DecimalMin.class, new DecimalMinClientValidationConstraint());
        CONSTRAINT_MAPPER.put(DecimalMax.class, new DecimalMaxClientValidationConstraint());
        CONSTRAINT_MAPPER.put(AssertTrue.class, new AssertTrueClientValidationConstraint());
        CONSTRAINT_MAPPER.put(AssertFalse.class, new AssertFalseClientValidationConstraint());
        CONSTRAINT_MAPPER.put(Digits.class, new DigitsClientValidationConstraint());
        CONSTRAINT_MAPPER.put(Past.class, new PastClientValidationConstraint());
        CONSTRAINT_MAPPER.put(Future.class, new FutureClientValidationConstraint());
        CONSTRAINT_MAPPER.put(Pattern.class, new PatternClientValidationConstraint());
    }
    
    public static BeanValidationMetadata resolveValidationMetadata(FacesContext context, UIComponent component, RequestContext requestContext) throws IOException {
        ValueExpression ve = component.getValueExpression("value");
        ELContext elContext = context.getELContext();
        Map metadata = new HashMap();
        List validatorIds = new ArrayList();
        
        if(ve != null) {
            ValueReference vr = ValueExpressionAnalyzer.getReference(elContext, ve);
            
            if(vr != null) {
                Validator validator = requestContext.getApplicationContext().getValidatorFactory().getValidator();
                Object base = vr.getBase();
                Object property = vr.getProperty();
                
                if(base != null && property != null) {
                    BeanDescriptor beanDescriptor = validator.getConstraintsForClass(base.getClass());
                    
                    if (beanDescriptor != null) {
	                    PropertyDescriptor propertyDescriptor = beanDescriptor.getConstraintsForProperty(property.toString());
	                    
	                    if (propertyDescriptor != null) {
		                    Set> constraints = propertyDescriptor.getConstraintDescriptors();
		                    
		                    if(constraints != null && !constraints.isEmpty()) {
		                        for(ConstraintDescriptor constraintDescriptor : constraints) {
                                    Class annotationType = constraintDescriptor.getAnnotation().annotationType();
		                            ClientValidationConstraint clientValidationConstraint = CONSTRAINT_MAPPER.get(annotationType);
		                            
		                            if(clientValidationConstraint != null) {
		                                String validatorId = clientValidationConstraint.getValidatorId();
		                                Map constraintMetadata = clientValidationConstraint.getMetadata(constraintDescriptor);
		                                
		                                if(constraintMetadata != null)
		                                    metadata.putAll(constraintMetadata);
		                                
		                                if(validatorId != null)
		                                    validatorIds.add(validatorId);
		                            }
                                    else {
                                        ClientConstraint clientConstraint = annotationType.getAnnotation(ClientConstraint.class);
                                        if(clientConstraint != null) {
                                            Class resolvedBy = clientConstraint.resolvedBy();
                                            
                                            if(resolvedBy != null) {
                                                try {
                                                    ClientValidationConstraint customClientValidationConstraint = (ClientValidationConstraint) resolvedBy.newInstance();
                                                    
                                                    String validatorId = customClientValidationConstraint.getValidatorId();
                                                    Map constraintMetadata = customClientValidationConstraint.getMetadata(constraintDescriptor);
		                                
                                                    if(constraintMetadata != null)
                                                        metadata.putAll(constraintMetadata);

                                                    if(validatorId != null)
                                                        validatorIds.add(validatorId);
                                                    
                                                } catch (InstantiationException ex) {
                                                    LOG.log(Level.SEVERE, null, ex);
                                                } catch (IllegalAccessException ex) {
                                                    LOG.log(Level.SEVERE, null, ex);
                                                }
                                            }
                                        }
                                    }
		                        }
		                    }
	                    }
                    }
                }
            }
        }
        
        return new BeanValidationMetadata(metadata, validatorIds);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy