Spring Data MongoDB - Core

org.springframework.data.mongodb.core.convert
Class MappingMongoConverter

java.lang.Object
  extended by org.springframework.data.mongodb.core.convert.AbstractMongoConverter
      extended by org.springframework.data.mongodb.core.convert.MappingMongoConverter
All Implemented Interfaces:
Aware, InitializingBean, ApplicationContextAware, EntityConverter<MongoPersistentEntity<?>,MongoPersistentProperty,Object,com.mongodb.DBObject>, EntityReader<Object,com.mongodb.DBObject>, EntityWriter<Object,com.mongodb.DBObject>, MongoConverter, MongoWriter<Object>

public class MappingMongoConverter
extends AbstractMongoConverter
implements ApplicationContextAware

MongoConverter that uses a MappingContext to do sophisticated mapping of domain objects to DBObject.

Author:
Oliver Gierke, Jon Brisbin, Patrik Wasik, Thomas Darimont, Christoph Strobl

Field Summary
protected  ApplicationContext applicationContext
           
protected  DbRefResolver dbRefResolver
           
protected  QueryMapper idMapper
           
protected static org.slf4j.Logger LOGGER
           
protected  String mapKeyDotReplacement
           
protected  MappingContext<? extends MongoPersistentEntity<?>,MongoPersistentProperty> mappingContext
           
protected  SpelExpressionParser spelExpressionParser
           
protected  MongoTypeMapper typeMapper
           
 
Fields inherited from class org.springframework.data.mongodb.core.convert.AbstractMongoConverter
conversions, conversionService, instantiators
 
Constructor Summary
MappingMongoConverter(DbRefResolver dbRefResolver, MappingContext<? extends MongoPersistentEntity<?>,MongoPersistentProperty> mappingContext)
          Creates a new MappingMongoConverter given the new DbRefResolver and MappingContext.
MappingMongoConverter(MongoDbFactory mongoDbFactory, MappingContext<? extends MongoPersistentEntity<?>,MongoPersistentProperty> mappingContext)
          Deprecated. use the constructor taking a DbRefResolver instead.
 
Method Summary
protected  void addCustomTypeKeyIfNecessary(TypeInformation<?> type, Object value, com.mongodb.DBObject dbObject)
          Adds custom type information to the given DBObject if necessary.
 Object convertToMongoType(Object obj, TypeInformation<?> typeInformation)
          Converts the given object into one Mongo will be able to store natively but retains the type information in case the given TypeInformation differs from the given object type.
protected  com.mongodb.DBObject createCollection(Collection<?> collection, MongoPersistentProperty property)
          Writes the given Collection using the given MongoPersistentProperty information.
protected  com.mongodb.DBRef createDBRef(Object target, MongoPersistentProperty property)
           
protected  com.mongodb.DBObject createMap(Map<Object,Object> map, MongoPersistentProperty property)
          Writes the given Map using the given MongoPersistentProperty information.
 MappingContext<? extends MongoPersistentEntity<?>,MongoPersistentProperty> getMappingContext()
           
 MongoTypeMapper getTypeMapper()
          Returns thw TypeMapper being used to write type information into DBObjects created with that converter.
protected  Object getValueInternal(MongoPersistentProperty prop, com.mongodb.DBObject dbo, SpELExpressionEvaluator evaluator, Object parent)
           
 com.mongodb.BasicDBList maybeConvertList(Iterable<?> source, TypeInformation<?> typeInformation)
           
protected  String potentiallyEscapeMapKey(String source)
          Potentially replaces dots in the given map key with the configured map key replacement if configured or aborts conversion if none is configured.
protected  String potentiallyUnescapeMapKey(String source)
          Translates the map key replacements in the given key just read with a dot in case a map key replacement has been configured.
<S> S
read(Class<S> clazz, com.mongodb.DBObject dbo)
           
protected
<S> S
read(TypeInformation<S> type, com.mongodb.DBObject dbo)
           
protected
<S> S
read(TypeInformation<S> type, com.mongodb.DBObject dbo, Object parent)
           
protected  Map<Object,Object> readMap(TypeInformation<?> type, com.mongodb.DBObject dbObject, Object parent)
          Reads the given DBObject into a Map. will recursively resolve nested Maps as well.
 void setApplicationContext(ApplicationContext applicationContext)
           
 void setMapKeyDotReplacement(String mapKeyDotReplacement)
          Configure the characters dots potentially contained in a Map shall be replaced with.
 void setTypeMapper(MongoTypeMapper typeMapper)
          Configures the MongoTypeMapper to be used to add type information to DBObjects created by the converter and how to lookup type information from DBObjects when reading them.
 com.mongodb.DBRef toDBRef(Object object, MongoPersistentProperty referingProperty)
          Creates a DBRef to refer to the given object.
protected
<T> List<?>
unwrapList(com.mongodb.BasicDBList dbList, TypeInformation<T> targetType)
           
 void write(Object obj, com.mongodb.DBObject dbo)
          Root entry method into write conversion.
protected  void writeInternal(Object obj, com.mongodb.DBObject dbo, MongoPersistentEntity<?> entity)
           
protected  void writeInternal(Object obj, com.mongodb.DBObject dbo, TypeInformation<?> typeHint)
          Internal write conversion method which should be used for nested invocations.
protected  com.mongodb.DBObject writeMapInternal(Map<Object,Object> obj, com.mongodb.DBObject dbo, TypeInformation<?> propertyType)
          Writes the given Map to the given DBObject considering the given TypeInformation.
protected  void writePropertyInternal(Object obj, com.mongodb.DBObject dbo, MongoPersistentProperty prop)
           
 
Methods inherited from class org.springframework.data.mongodb.core.convert.AbstractMongoConverter
afterPropertiesSet, convertToMongoType, getConversionService, setCustomConversions, setInstantiators
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOGGER

protected static final org.slf4j.Logger LOGGER

mappingContext

protected final MappingContext<? extends MongoPersistentEntity<?>,MongoPersistentProperty> mappingContext

spelExpressionParser

protected final SpelExpressionParser spelExpressionParser

idMapper

protected final QueryMapper idMapper

dbRefResolver

protected final DbRefResolver dbRefResolver

applicationContext

protected ApplicationContext applicationContext

typeMapper

protected MongoTypeMapper typeMapper

mapKeyDotReplacement

protected String mapKeyDotReplacement
Constructor Detail

MappingMongoConverter

public MappingMongoConverter(DbRefResolver dbRefResolver,
                             MappingContext<? extends MongoPersistentEntity<?>,MongoPersistentProperty> mappingContext)
Creates a new MappingMongoConverter given the new DbRefResolver and MappingContext.

Parameters:
mongoDbFactory - must not be null.
mappingContext - must not be null.

MappingMongoConverter

@Deprecated
public MappingMongoConverter(MongoDbFactory mongoDbFactory,
                                        MappingContext<? extends MongoPersistentEntity<?>,MongoPersistentProperty> mappingContext)
Deprecated. use the constructor taking a DbRefResolver instead.

Creates a new MappingMongoConverter given the new MongoDbFactory and MappingContext.

Parameters:
mongoDbFactory - must not be null.
mappingContext - must not be null.
Method Detail

setTypeMapper

public void setTypeMapper(MongoTypeMapper typeMapper)
Configures the MongoTypeMapper to be used to add type information to DBObjects created by the converter and how to lookup type information from DBObjects when reading them. Uses a DefaultMongoTypeMapper by default. Setting this to null will reset the TypeMapper to the default one.

Parameters:
typeMapper - the typeMapper to set

getTypeMapper

public MongoTypeMapper getTypeMapper()
Description copied from interface: MongoConverter
Returns thw TypeMapper being used to write type information into DBObjects created with that converter.

Specified by:
getTypeMapper in interface MongoConverter
Returns:
will never be null.

setMapKeyDotReplacement

public void setMapKeyDotReplacement(String mapKeyDotReplacement)
Configure the characters dots potentially contained in a Map shall be replaced with. By default we don't do any translation but rather reject a Map with keys containing dots causing the conversion for the entire object to fail. If further customization of the translation is needed, have a look at potentiallyEscapeMapKey(String) as well as potentiallyUnescapeMapKey(String).

Parameters:
mapKeyDotReplacement - the mapKeyDotReplacement to set

getMappingContext

public MappingContext<? extends MongoPersistentEntity<?>,MongoPersistentProperty> getMappingContext()
Specified by:
getMappingContext in interface EntityConverter<MongoPersistentEntity<?>,MongoPersistentProperty,Object,com.mongodb.DBObject>

setApplicationContext

public void setApplicationContext(ApplicationContext applicationContext)
                           throws BeansException
Specified by:
setApplicationContext in interface ApplicationContextAware
Throws:
BeansException

read

public <S> S read(Class<S> clazz,
                  com.mongodb.DBObject dbo)
Specified by:
read in interface EntityReader<Object,com.mongodb.DBObject>

read

protected <S> S read(TypeInformation<S> type,
                     com.mongodb.DBObject dbo)

read

protected <S> S read(TypeInformation<S> type,
                     com.mongodb.DBObject dbo,
                     Object parent)

toDBRef

public com.mongodb.DBRef toDBRef(Object object,
                                 MongoPersistentProperty referingProperty)
Description copied from interface: MongoWriter
Creates a DBRef to refer to the given object.

Specified by:
toDBRef in interface MongoWriter<Object>
Parameters:
object - the object to create a DBRef to link to. The object's type has to carry an id attribute.
referingProperty - the client-side property referring to the object which might carry additional metadata for the DBRef object to create. Can be null.
Returns:
will never be null.

write

public void write(Object obj,
                  com.mongodb.DBObject dbo)
Root entry method into write conversion. Adds a type discriminator to the DBObject. Shouldn't be called for nested conversions.

Specified by:
write in interface EntityWriter<Object,com.mongodb.DBObject>
See Also:
org.springframework.data.mongodb.core.core.convert.MongoWriter#write(java.lang.Object, com.mongodb.DBObject)

writeInternal

protected void writeInternal(Object obj,
                             com.mongodb.DBObject dbo,
                             TypeInformation<?> typeHint)
Internal write conversion method which should be used for nested invocations.

Parameters:
obj -
dbo -

writeInternal

protected void writeInternal(Object obj,
                             com.mongodb.DBObject dbo,
                             MongoPersistentEntity<?> entity)

writePropertyInternal

protected void writePropertyInternal(Object obj,
                                     com.mongodb.DBObject dbo,
                                     MongoPersistentProperty prop)

createCollection

protected com.mongodb.DBObject createCollection(Collection<?> collection,
                                                MongoPersistentProperty property)
Writes the given Collection using the given MongoPersistentProperty information.

Parameters:
collection - must not be null.
property - must not be null.
Returns:

createMap

protected com.mongodb.DBObject createMap(Map<Object,Object> map,
                                         MongoPersistentProperty property)
Writes the given Map using the given MongoPersistentProperty information.

Parameters:
map - must not null.
property - must not be null.
Returns:

writeMapInternal

protected com.mongodb.DBObject writeMapInternal(Map<Object,Object> obj,
                                                com.mongodb.DBObject dbo,
                                                TypeInformation<?> propertyType)
Writes the given Map to the given DBObject considering the given TypeInformation.

Parameters:
obj - must not be null.
dbo - must not be null.
propertyType - must not be null.
Returns:

potentiallyEscapeMapKey

protected String potentiallyEscapeMapKey(String source)
Potentially replaces dots in the given map key with the configured map key replacement if configured or aborts conversion if none is configured.

Parameters:
source -
Returns:
See Also:
setMapKeyDotReplacement(String)

potentiallyUnescapeMapKey

protected String potentiallyUnescapeMapKey(String source)
Translates the map key replacements in the given key just read with a dot in case a map key replacement has been configured.

Parameters:
source -
Returns:

addCustomTypeKeyIfNecessary

protected void addCustomTypeKeyIfNecessary(TypeInformation<?> type,
                                           Object value,
                                           com.mongodb.DBObject dbObject)
Adds custom type information to the given DBObject if necessary. That is if the value is not the same as the one given. This is usually the case if you store a subtype of the actual declared type of the property.

Parameters:
type -
value - must not be null.
dbObject - must not be null.

createDBRef

protected com.mongodb.DBRef createDBRef(Object target,
                                        MongoPersistentProperty property)

getValueInternal

protected Object getValueInternal(MongoPersistentProperty prop,
                                  com.mongodb.DBObject dbo,
                                  SpELExpressionEvaluator evaluator,
                                  Object parent)

readMap

protected Map<Object,Object> readMap(TypeInformation<?> type,
                                     com.mongodb.DBObject dbObject,
                                     Object parent)
Reads the given DBObject into a Map. will recursively resolve nested Maps as well.

Parameters:
type - the Map TypeInformation to be used to unmarshall this DBObject.
dbObject -
Returns:

unwrapList

protected <T> List<?> unwrapList(com.mongodb.BasicDBList dbList,
                                 TypeInformation<T> targetType)

convertToMongoType

public Object convertToMongoType(Object obj,
                                 TypeInformation<?> typeInformation)
Description copied from interface: MongoWriter
Converts the given object into one Mongo will be able to store natively but retains the type information in case the given TypeInformation differs from the given object type.

Specified by:
convertToMongoType in interface MongoWriter<Object>
Parameters:
obj - can be null.
typeInformation - can be null.
Returns:

maybeConvertList

public com.mongodb.BasicDBList maybeConvertList(Iterable<?> source,
                                                TypeInformation<?> typeInformation)

Spring Data MongoDB - Core

Copyright © 2011-2014–2014 Pivotal Software, Inc.. All rights reserved.