protected ValueModel createConvertingValueModel(String formProperty, Class targetClass) {
if (logger.isDebugEnabled()) {
logger.debug("Creating converting value model for form property '" + formProperty
+ "' converting to type '" + targetClass + "'.");
}
final ValueModel sourceValueModel = getValueModel(formProperty);
Assert.notNull(sourceValueModel, "Form does not have a property called '" + formProperty + "'.");
final Class sourceClass = ClassUtils
.convertPrimitiveToWrapper(getFieldMetadata(formProperty).getPropertyType());
// sourceClass can be null when using eg Map, assume that given
// targetClass is the correct one
if ((sourceClass == null) || (sourceClass == targetClass)) {
return sourceValueModel;
}
final ConversionService conversionService = getConversionService();
ConversionExecutor convertTo = null;
ConversionExecutor convertFrom = null;
// Check for locally registered property converters
if (propertyConversionServices.containsKey(formProperty)) {
// TODO - extract ConfigurableConversionService interface...
final GenericConversionService propertyConversionService = (GenericConversionService) propertyConversionServices
.get(formProperty);
if (propertyConversionService != null) {
convertTo = propertyConversionService.getConversionExecutor(sourceClass, targetClass);
convertFrom = propertyConversionService.getConversionExecutor(targetClass, sourceClass);
}
}
// If we have nothing from the property level, then try the conversion
// service
if (convertTo == null) {
convertTo = conversionService.getConversionExecutor(sourceClass, targetClass);
}
Assert.notNull(convertTo, "conversionService returned null ConversionExecutor");
if (convertFrom == null) {
convertFrom = conversionService.getConversionExecutor(targetClass, sourceClass);
}
Assert.notNull(convertFrom, "conversionService returned null ConversionExecutor");
ValueModel convertingValueModel = preProcessNewConvertingValueModel(formProperty, targetClass,
new TypeConverter(sourceValueModel, convertTo, convertFrom));
preProcessNewConvertingValueModel(formProperty, targetClass, convertingValueModel);
return convertingValueModel;
}