Spring Data Document

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, org.springframework.data.convert.EntityConverter<MongoPersistentEntity<?>,MongoPersistentProperty,Object,DBObject>, org.springframework.data.convert.EntityReader<Object,DBObject>, org.springframework.data.convert.EntityWriter<Object,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

Field Summary
protected  ApplicationContext applicationContext
           
protected  QueryMapper idMapper
           
protected static org.apache.commons.logging.Log log
           
protected  String mapKeyDotReplacement
           
protected  org.springframework.data.mapping.context.MappingContext<? extends MongoPersistentEntity<?>,MongoPersistentProperty> mappingContext
           
protected  MongoDbFactory mongoDbFactory
           
protected  SpelExpressionParser spelExpressionParser
           
protected  MongoTypeMapper typeMapper
           
protected  boolean useFieldAccessOnly
           
 
Fields inherited from class org.springframework.data.mongodb.core.convert.AbstractMongoConverter
conversions, conversionService
 
Constructor Summary
MappingMongoConverter(MongoDbFactory mongoDbFactory, org.springframework.data.mapping.context.MappingContext<? extends MongoPersistentEntity<?>,MongoPersistentProperty> mappingContext)
          Creates a new MappingMongoConverter given the new MongoDbFactory and MappingContext.
 
Method Summary
protected  void addCustomTypeKeyIfNecessary(org.springframework.data.util.TypeInformation<?> type, Object value, DBObject dbObject)
          Adds custom type information to the given DBObject if necessary.
 Object convertToMongoType(Object obj)
          Converts the given object into one Mongo will be able to store natively.
protected  DBObject createCollection(Collection<?> collection, MongoPersistentProperty property)
          Writes the given Collection using the given MongoPersistentProperty information.
protected  DBRef createDBRef(Object target, DBRef dbref)
           
 org.springframework.data.mapping.context.MappingContext<? extends MongoPersistentEntity<?>,MongoPersistentProperty> getMappingContext()
           
protected  Object getValueInternal(MongoPersistentProperty prop, DBObject dbo, StandardEvaluationContext ctx, String spelExpr)
           
 BasicDBList maybeConvertList(Iterable<?> source)
           
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, DBObject dbo)
           
protected
<S> S
read(org.springframework.data.util.TypeInformation<S> type, DBObject dbo)
           
protected  Map<Object,Object> readMap(org.springframework.data.util.TypeInformation<?> type, DBObject dbObject)
          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.
 void setUseFieldAccessOnly(boolean useFieldAccessOnly)
          Configures whether to use field access only for entity mapping.
protected
<T> List<?>
unwrapList(BasicDBList dbList, org.springframework.data.util.TypeInformation<T> targetType)
           
 void write(Object obj, DBObject dbo)
          Root entry method into write conversion.
protected  void writeInternal(Object obj, DBObject dbo, MongoPersistentEntity<?> entity)
           
protected  void writeInternal(Object obj, DBObject dbo, org.springframework.data.util.TypeInformation<?> typeHint)
          Internal write conversion method which should be used for nested invocations.
protected  DBObject writeMapInternal(Map<Object,Object> obj, DBObject dbo, org.springframework.data.util.TypeInformation<?> propertyType)
          Writes the given Map to the given DBObject considering the given TypeInformation.
protected  void writePropertyInternal(Object obj, DBObject dbo, MongoPersistentProperty prop)
           
 
Methods inherited from class org.springframework.data.mongodb.core.convert.AbstractMongoConverter
afterPropertiesSet, getConversionService, setCustomConversions
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static final org.apache.commons.logging.Log log

mappingContext

protected final org.springframework.data.mapping.context.MappingContext<? extends MongoPersistentEntity<?>,MongoPersistentProperty> mappingContext

spelExpressionParser

protected final SpelExpressionParser spelExpressionParser

mongoDbFactory

protected final MongoDbFactory mongoDbFactory

idMapper

protected final QueryMapper idMapper

applicationContext

protected ApplicationContext applicationContext

useFieldAccessOnly

protected boolean useFieldAccessOnly

typeMapper

protected MongoTypeMapper typeMapper

mapKeyDotReplacement

protected String mapKeyDotReplacement
Constructor Detail

MappingMongoConverter

public MappingMongoConverter(MongoDbFactory mongoDbFactory,
                             org.springframework.data.mapping.context.MappingContext<? extends MongoPersistentEntity<?>,MongoPersistentProperty> mappingContext)
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

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 org.springframework.data.mapping.context.MappingContext<? extends MongoPersistentEntity<?>,MongoPersistentProperty> getMappingContext()
Specified by:
getMappingContext in interface org.springframework.data.convert.EntityConverter<MongoPersistentEntity<?>,MongoPersistentProperty,Object,DBObject>

setUseFieldAccessOnly

public void setUseFieldAccessOnly(boolean useFieldAccessOnly)
Configures whether to use field access only for entity mapping. Setting this to true will force the MongoConverter to not go through getters or setters even if they are present for getting and setting property values.

Parameters:
useFieldAccessOnly -

setApplicationContext

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

read

public <S> S read(Class<S> clazz,
                  DBObject dbo)
Specified by:
read in interface org.springframework.data.convert.EntityReader<Object,DBObject>

read

protected <S> S read(org.springframework.data.util.TypeInformation<S> type,
                     DBObject dbo)

write

public void write(Object obj,
                  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 org.springframework.data.convert.EntityWriter<Object,DBObject>
See Also:
org.springframework.data.mongodb.core.core.convert.MongoWriter#write(java.lang.Object, com.mongodb.DBObject)

writeInternal

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

Parameters:
obj -
dbo -

writeInternal

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

writePropertyInternal

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

createCollection

protected 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:

writeMapInternal

protected DBObject writeMapInternal(Map<Object,Object> obj,
                                    DBObject dbo,
                                    org.springframework.data.util.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(org.springframework.data.util.TypeInformation<?> type,
                                           Object value,
                                           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 DBRef createDBRef(Object target,
                            DBRef dbref)

getValueInternal

protected Object getValueInternal(MongoPersistentProperty prop,
                                  DBObject dbo,
                                  StandardEvaluationContext ctx,
                                  String spelExpr)

readMap

protected Map<Object,Object> readMap(org.springframework.data.util.TypeInformation<?> type,
                                     DBObject dbObject)
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(BasicDBList dbList,
                                 org.springframework.data.util.TypeInformation<T> targetType)

convertToMongoType

public Object convertToMongoType(Object obj)
Description copied from interface: MongoWriter
Converts the given object into one Mongo will be able to store natively. If the given object can already be stored as is, no conversion will happen.

Specified by:
convertToMongoType in interface MongoWriter<Object>
Returns:

maybeConvertList

public BasicDBList maybeConvertList(Iterable<?> source)

Spring Data Document

Copyright © 2012. All Rights Reserved.