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

java.lang.Object
  extended by org.springframework.data.document.mongodb.convert.MappingMongoConverter
All Implemented Interfaces:
MongoConverter, MongoReader<java.lang.Object>, MongoWriter<java.lang.Object>

public class MappingMongoConverter
extends java.lang.Object
implements MongoConverter

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

Author:
Jon Brisbin , Oliver Gierke

Nested Class Summary
protected  class MappingMongoConverter.PersistentPropertyWrapper
           
 
Field Summary
protected  ApplicationContext applicationContext
           
protected  GenericConversionService conversionService
           
private static java.lang.String CUSTOM_TYPE_KEY
           
protected  java.util.Map<java.lang.Class<?>,java.lang.Class<?>> customTypeMapping
           
protected  java.lang.String defaultDatabase
           
protected static Log log
           
protected  MappingContext mappingContext
           
protected  Mongo mongo
           
private static java.util.List<java.lang.Class<?>> MONGO_TYPES
           
protected  SpelExpressionParser spelExpressionParser
           
protected  boolean useFieldAccessOnly
           
private static java.util.List<java.lang.Class<?>> VALID_ID_TYPES
           
 
Constructor Summary
MappingMongoConverter(MappingContext mappingContext)
          Creates a new MappingMongoConverter with the given MappingContext.
 
Method Summary
 void afterPropertiesSet()
           
 ObjectId convertObjectId(java.lang.Object id)
          Returns the ObjectId instance for the given id.
<T> T
convertObjectId(ObjectId id, java.lang.Class<T> targetType)
          Converts the given ObjectId to the given target type.
protected  DBRef createDBRef(java.lang.Object target, DBRef dbref)
           
protected  java.lang.Class<?> findTypeToBeUsed(DBObject dbObject)
          Returns the type to be used to convert the DBObject given to.
 java.lang.String getDefaultDatabase()
           
 MappingContext getMappingContext()
           
 Mongo getMongo()
           
protected  java.lang.Object getValueInternal(PersistentProperty prop, DBObject dbo, StandardEvaluationContext ctx, Value spelExpr)
           
private  void initializeConverters()
          Registers converters for ObjectId handling, removes plain Object.toString() converter and promotes the configured ConversionService to MappingBeanHelper.
 boolean isUseFieldAccessOnly()
           
private
<S> S
read( entity, DBObject dbo)
          Ready from the native MongoDB DBObject representation to an instance of the class T.
<S> S
read(java.lang.Class<S> clazz, DBObject dbo)
          Ready from the native MongoDB DBObject representation to an instance of the class T.
private  void registerConverter( converter)
          Inspects the given Converter for the types it can convert and registers the pair for custom type conversion in case the target type is a Mongo basic type.
 void setApplicationContext(ApplicationContext applicationContext)
           
 void setConverters(java.util.List<> converters)
          Add custom Converter or ConverterFactory instances to be used that will take presidence over metadata driven conversion between of objects to/from DBObject
 void setDefaultDatabase(java.lang.String defaultDatabase)
           
 void setMongo(Mongo mongo)
           
 void setUseFieldAccessOnly(boolean useFieldAccessOnly)
           
 void write(java.lang.Object obj, DBObject dbo)
          Write the given object of type T to the native MongoDB object representation DBObject.
protected  void write(java.lang.Object obj, DBObject dbo,  entity)
           
protected  void writeMapInternal(java.util.Map<java.lang.Object,java.lang.Object> obj, DBObject dbo)
           
protected  void writePropertyInternal(PersistentProperty prop, java.lang.Object obj, DBObject dbo)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CUSTOM_TYPE_KEY

private static final java.lang.String CUSTOM_TYPE_KEY
See Also:
Constant Field Values

MONGO_TYPES

private static final java.util.List<java.lang.Class<?>> MONGO_TYPES

VALID_ID_TYPES

private static final java.util.List<java.lang.Class<?>> VALID_ID_TYPES

log

protected static final Log log

conversionService

protected final GenericConversionService conversionService

customTypeMapping

protected final java.util.Map<java.lang.Class<?>,java.lang.Class<?>> customTypeMapping

mappingContext

protected final MappingContext mappingContext

spelExpressionParser

protected SpelExpressionParser spelExpressionParser

applicationContext

protected ApplicationContext applicationContext

useFieldAccessOnly

protected boolean useFieldAccessOnly

mongo

protected Mongo mongo

defaultDatabase

protected java.lang.String defaultDatabase
Constructor Detail

MappingMongoConverter

public MappingMongoConverter(MappingContext mappingContext)
Creates a new MappingMongoConverter with the given MappingContext.

Parameters:
mappingContext -
Method Detail

setConverters

public void setConverters(java.util.List<> converters)
Add custom Converter or ConverterFactory instances to be used that will take presidence over metadata driven conversion between of objects to/from DBObject

Parameters:
converters -

registerConverter

private void registerConverter( converter)
Inspects the given Converter for the types it can convert and registers the pair for custom type conversion in case the target type is a Mongo basic type.

Parameters:
converter -

getMappingContext

public MappingContext getMappingContext()

getMongo

public Mongo getMongo()

setMongo

public void setMongo(Mongo mongo)

getDefaultDatabase

public java.lang.String getDefaultDatabase()

setDefaultDatabase

public void setDefaultDatabase(java.lang.String defaultDatabase)

isUseFieldAccessOnly

public boolean isUseFieldAccessOnly()

setUseFieldAccessOnly

public void setUseFieldAccessOnly(boolean useFieldAccessOnly)

convertObjectId

public <T> T convertObjectId(ObjectId id,
                             java.lang.Class<T> targetType)
Description copied from interface: MongoConverter
Converts the given ObjectId to the given target type.

Specified by:
convertObjectId in interface MongoConverter
Type Parameters:
T - the actual type to create
Parameters:
id - the source ObjectId
targetType - the target type to convert the ObjectId to
Returns:

convertObjectId

public ObjectId convertObjectId(java.lang.Object id)
Description copied from interface: MongoConverter
Returns the ObjectId instance for the given id.

Specified by:
convertObjectId in interface MongoConverter
Returns:

read

public <S> S read(java.lang.Class<S> clazz,
                  DBObject dbo)
Description copied from interface: MongoReader
Ready from the native MongoDB DBObject representation to an instance of the class T. The given type has to be the starting point for marshalling the DBObject into it. So in case there's no real valid data inside DBObject for the given type, just return an empty instance of the given type.

Specified by:
read in interface MongoReader<java.lang.Object>
Parameters:
clazz - the type of the return value
dbo - theDBObject
Returns:
the converted object

read

private <S> S read( entity,
                   DBObject dbo)
Description copied from interface: MongoReader
Ready from the native MongoDB DBObject representation to an instance of the class T. The given type has to be the starting point for marshalling the DBObject into it. So in case there's no real valid data inside DBObject for the given type, just return an empty instance of the given type.

Specified by:
read in interface MongoReader<java.lang.Object>
Parameters:
entity - the type of the return value
dbo - theDBObject
Returns:
the converted object

write

public void write(java.lang.Object obj,
                  DBObject dbo)
Description copied from interface: MongoWriter
Write the given object of type T to the native MongoDB object representation DBObject.

Specified by:
write in interface MongoWriter<java.lang.Object>
Parameters:
obj - The object to convert to a DBObject
dbo - The DBObject to use for writing.

write

protected void write(java.lang.Object obj,
                     DBObject dbo,
                      entity)

setApplicationContext

public void setApplicationContext(ApplicationContext applicationContext)
                           throws BeansException
Throws:
BeansException

initializeConverters

private void initializeConverters()
Registers converters for ObjectId handling, removes plain Object.toString() converter and promotes the configured ConversionService to MappingBeanHelper.


writePropertyInternal

protected void writePropertyInternal(PersistentProperty prop,
                                     java.lang.Object obj,
                                     DBObject dbo)

writeMapInternal

protected void writeMapInternal(java.util.Map<java.lang.Object,java.lang.Object> obj,
                                DBObject dbo)

createDBRef

protected DBRef createDBRef(java.lang.Object target,
                            DBRef dbref)

getValueInternal

protected java.lang.Object getValueInternal(PersistentProperty prop,
                                            DBObject dbo,
                                            StandardEvaluationContext ctx,
                                            Value spelExpr)

findTypeToBeUsed

protected java.lang.Class<?> findTypeToBeUsed(DBObject dbObject)
Returns the type to be used to convert the DBObject given to.

Parameters:
dbObject -
Returns:

afterPropertiesSet

public void afterPropertiesSet()