From a93880a60e472daa6e06478f28c173e13b2048f8 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 17 Mar 2011 09:27:33 +0100 Subject: [PATCH] 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. --- .../data/mapping/BasicMappingContext.java | 51 +++++++++++++++++-- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/BasicMappingContext.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/BasicMappingContext.java index d738072b6..3da952c24 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/BasicMappingContext.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/BasicMappingContext.java @@ -66,6 +66,7 @@ public class BasicMappingContext implements MappingContext, InitializingBean, Ap protected ConcurrentMap> persistentEntities = new ConcurrentHashMap>(); protected ConcurrentMap, List> validators = new ConcurrentHashMap, List>(); protected GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService(); + private List> customSimpleTypes = new ArrayList>(); 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> 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 PersistentEntity addPersistentEntity(Class type) { return addPersistentEntity(new ClassTypeInformation(type));