Added customSimpleTypes to BasicMappingContext.

The added custom simple types will be considered in the decision whether to recursively add PersistentEntities. This does not feel quite right for now as for a complete setup we now have to register converters in the MappingMongoConverter *and* add the custom type to the list here. Open for discussion.
This commit is contained in:
Oliver Gierke
2011-03-17 09:27:33 +01:00
parent ab59c57577
commit a93880a60e

View File

@@ -66,6 +66,7 @@ public class BasicMappingContext implements MappingContext, InitializingBean, Ap
protected ConcurrentMap<TypeInformation, PersistentEntity<?>> persistentEntities = new ConcurrentHashMap<TypeInformation, PersistentEntity<?>>();
protected ConcurrentMap<PersistentEntity<?>, List<Validator>> validators = new ConcurrentHashMap<PersistentEntity<?>, List<Validator>>();
protected GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
private List<Class<?>> customSimpleTypes = new ArrayList<Class<?>>();
public BasicMappingContext() {
builder = new BasicMappingConfigurationBuilder();
@@ -79,6 +80,13 @@ public class BasicMappingContext implements MappingContext, InitializingBean, Ap
this.builder = builder;
this.conversionService = conversionService;
}
/**
* @param customSimpleTypes the customSimpleTypes to set
*/
public void setCustomSimpleTypes(List<Class<?>> customSimpleTypes) {
this.customSimpleTypes = customSimpleTypes;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
@@ -143,9 +151,10 @@ public class BasicMappingContext implements MappingContext, InitializingBean, Ap
if (property.isIdProperty()) {
entity.setIdProperty(property);
}
if (property.isComplexType() && !property.isTransient()) {
addPersistentEntity(property.getTypeInformation());
TypeInformation nestedType = getNestedTypeToAdd(property);
if (nestedType != null) {
addPersistentEntity(nestedType);
}
}
} catch (MappingConfigurationException e) {
@@ -172,6 +181,42 @@ public class BasicMappingContext implements MappingContext, InitializingBean, Ap
return null;
}
/**
* Returns a potential nested type tha needs to be added when adding the given property in the course of adding a
* {@link PersistentEntity}. Will return the property's {@link TypeInformation} directly if it is a potential entity,
* a collections component type if it's a collection as well as the value type of a {@link Map} if it's a map
* property.
*
* @param property
* @return the TypeInformation to be added as {@link PersistentEntity} or {@literal
*/
private TypeInformation getNestedTypeToAdd(PersistentProperty property) {
TypeInformation typeInformation = property.getTypeInformation();
if (customSimpleTypes.contains(typeInformation.getType())) {
return null;
}
if (property.isEntity()) {
return typeInformation;
}
if (property.isCollection()) {
return getTypeInformationIfNotSimpleType(typeInformation.getComponentType());
}
if (property.isMap()) {
return getTypeInformationIfNotSimpleType(typeInformation.getMapValueType());
}
return null;
}
private TypeInformation getTypeInformationIfNotSimpleType(TypeInformation information) {
return information == null || MappingBeanHelper.isSimpleType(information.getType()) ? null : information;
}
@Override
public <T> PersistentEntity<T> addPersistentEntity(Class<T> type) {
return addPersistentEntity(new ClassTypeInformation(type));