/*
* Copyright 2005-2006 the original author or authors.
*
* 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.strecks.validator.factory;
import java.lang.reflect.Method;
import java.util.List;
import org.strecks.util.ReflectHelper;
import org.strecks.validator.Validator;
import org.strecks.validator.internal.ValidatorWrapper;
import org.strecks.validator.message.DefaultMessageParameterProvider;
import org.strecks.validator.message.MessageParameterProvider;
/**
* Convenience base class of <code>ValidatorFactory</code> implementations
* @author Phil Zoio
*/
public abstract class BaseFactory implements ValidatorFactory
{
/**
* Returns a <code>ValidatorWrapper</code> which encapsulates all the validations for a property
* @param validator
* the <code>Validator</code> instance wrapped by the <code>ValidatorWrapper</code>
* @param key
* the message key used to look up the relevant error message on validation failure
* @param order
* the order in which this validator should be called. Validators are called in ascending order
* @param parameters
* additional parameters supplied to the validator. For example, a date validator will require a pattern
* parameter, and an integer range validator will require minimum and maximum ranges
* @param method
* the setter method for the property being validated
* @return
*/
protected ValidatorWrapper create(Validator validator, String key, int order, List<Object> parameters, Method method)
{
MessageParameterProvider provider = newMessageParameterProvider();
ValidatorWrapper wrapper = newWrapper(validator, key, order, parameters, method, provider);
return wrapper;
}
ValidatorWrapper newWrapper(Validator validator, String key, int order, List<Object> parameters, Method method,
MessageParameterProvider provider)
{
Class<?> parameterizedType = ReflectHelper.getGenericType(validator.getClass(), Validator.class);
boolean useConvertedValue = shouldUseConvertedValue(validator, method);
if (useConvertedValue)
{
return new ValidatorWrapper(key, order, parameters, validator, method, provider, useConvertedValue,
parameterizedType);
}
else
{
return new ValidatorWrapper(key, order, parameters, validator, method, provider);
}
}
boolean shouldUseConvertedValue(Validator validator, Method method)
{
Class<?> returnType = method.getReturnType();
Class<?> parameterizedType = ReflectHelper.getGenericType(validator.getClass(), Validator.class);
boolean useConvertedValue = false;
// if no parametized type, then use the raw value for validation
if (parameterizedType == null)
useConvertedValue = false;
else
{
if (parameterizedType.isAssignableFrom(returnType))
{
// if return type can be assigned to parameterized type, then use the raw value for validation
useConvertedValue = false;
}
else
{
// if return type cannot be assigned to parameterized type, then use converted value for validation
// e.g. return type is String, parameterized type is Integer
useConvertedValue = true;
}
}
return useConvertedValue;
}
MessageParameterProvider newMessageParameterProvider()
{
MessageParameterProvider provider = getMessageParameterProvider();
if (provider == null)
return new DefaultMessageParameterProvider();
else
return provider;
}
protected MessageParameterProvider getMessageParameterProvider()
{
return null;
}
}