Package es.internna.spring.mvc.api.validation

Source Code of es.internna.spring.mvc.api.validation.SystemValidator

package es.internna.spring.mvc.api.validation;

import es.internna.spring.annotations.Bean;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.StringUtils;
import org.springframework.validation.DirectFieldBindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.Validator;

public class SystemValidator extends AbstractValidatorHolder
{
    private Log log = LogFactory.getLog(SystemValidator.class);
   
    private Class<?> getClassFromName(String className)
    {
        Class<?> clazz = null;
        try
        {
            clazz = Class.forName(className);
        }
        catch (ClassNotFoundException ex)
        {
            log.error("No class found: " + className, ex);
        }
        return clazz;
    }
   
    protected Validator getValidator(String classSimpleName)
    {
        return getValidator(this.getClassFromName(classSimpleName));
    }
   
    protected Validator getValidator(Class clazz)
    {
        Validator validValidator = null;
        if (clazz != null)
        {
            for (Validator validator : this.validators)
                if ((validValidator == null) && (validator.supports(clazz)))
                    validValidator = validator;
        }
        return validValidator;
    }
   
    private Object getValueObject(Class clazz, String fieldValue)
    {
        String type = clazz.getSimpleName();
        if (("byte".equals(type)) | ("Byte".equals(type))) return new Byte(fieldValue);
        if (("int".equals(type)) | ("Integer".equals(type))) return new Integer(fieldValue);
        if (("float".equals(type)) | ("Float".equals(type))) return new Float(fieldValue);
        if (("double".equals(type)) | ("Double".equals(type))) return new Double(fieldValue);
        return fieldValue;
    }
   
    public String validateField(String fieldName, String fieldValue) throws Exception
    {
        String message = null;
        if ((fieldName == null) || (fieldName.indexOf(".") < 0)) throw new DWRBindException(fieldName);
        String className = fieldName.substring(0, fieldName.lastIndexOf("."));
        Class<?> clazz = this.getClassFromName(className);
        Validator validator = this.getValidator(clazz);
        if (validator != null)
        {
            String field = fieldName.substring(fieldName.lastIndexOf(".") + 1);
            String capitalizedField = StringUtils.capitalize(field);
            Object formBackingObject = clazz.newInstance();
            Field f = clazz.getDeclaredField(field);
            Object value = this.getValueObject(f.getType(), fieldValue);
            f.setAccessible(true);
            f.set(formBackingObject, value);
            Errors errors = validateObject(formBackingObject);
            FieldError error = errors.getFieldError(field);
            if (error != null) message = error.getCode();
        }
        return message;
    }

    public Errors validateObject(Object object) throws Exception {
        Errors errors = null;
        if (object != null)
        {
            Validator validator = this.getValidator(object.getClass());
            if (validator != null)
            {
                errors = new DirectFieldBindingResult(object, "command");
                Class[] validationArgs = new Class[] { Object.class, Errors.class };
                String methodName = "validate";
                Method method = validator.getClass().getMethod(methodName, validationArgs);
                if (method != null)
                {
                    if (log.isDebugEnabled()) log.debug("Invoking  " + methodName + " on " + validator);
                    method.invoke(validator, new Object[] { object, errors });
                }
                else
                    if (log.isDebugEnabled()) log.debug("No method validate found in validator " + validator.getClass());
            }
            else
                if (log.isDebugEnabled()) log.debug("No validator found for " + object.getClass());
        }
        return errors;
    }
   
}
TOP

Related Classes of es.internna.spring.mvc.api.validation.SystemValidator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.