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

java.lang.Object
  extended by org.springframework.data.document.mongodb.convert.AbstractMongoConverter
      extended by org.springframework.data.document.mongodb.convert.MappingMongoConverter
All Implemented Interfaces:
org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationContextAware, MongoConverter, MongoReader<java.lang.Object>, MongoWriter<java.lang.Object>

public class MappingMongoConverter
extends AbstractMongoConverter
implements org.springframework.context.ApplicationContextAware

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

Author:
Jon Brisbin , Oliver Gierke

Field Summary
protected  org.springframework.context.ApplicationContext applicationContext
           
static java.lang.String CUSTOM_TYPE_KEY
           
protected static org.apache.commons.logging.Log log
           
protected  org.springframework.data.mapping.model.MappingContext<? extends MongoPersistentEntity<?>,MongoPersistentProperty> mappingContext
           
protected  MongoDbFactory mongoDbFactory
           
protected  org.springframework.expression.spel.standard.SpelExpressionParser spelExpressionParser
           
protected  boolean useFieldAccessOnly
           
 
Fields inherited from class org.springframework.data.document.mongodb.convert.AbstractMongoConverter
conversionService
 
Constructor Summary
MappingMongoConverter(MongoDbFactory mongoDbFactory, org.springframework.data.mapping.model.MappingContext<? extends MongoPersistentEntity<?>,MongoPersistentProperty> mappingContext)
          Creates a new MappingMongoConverter given the new MongoDbFactory and MappingContext.
 
Method Summary
 void addCustomTypeKeyIfNecessary(org.springframework.data.util.TypeInformation<?> type, java.lang.Object value, com.mongodb.DBObject dbObject)
          Adds custom type information to the given DBObject if necessary.
 org.bson.types.ObjectId convertObjectId(java.lang.Object id)
          Returns the ObjectId instance for the given id.
<T> T
convertObjectId(org.bson.types.ObjectId id, java.lang.Class<T> targetType)
          Converts the given ObjectId to the given target type.
protected  com.mongodb.DBRef createDBRef(java.lang.Object target, DBRef dbref)
           
protected  java.lang.Class<?> findTypeToBeUsed(com.mongodb.DBObject dbObject)
          Returns the type to be used to convert the DBObject given to.
 org.springframework.data.mapping.model.MappingContext<? extends MongoPersistentEntity<?>,MongoPersistentProperty> getMappingContext()
           
protected  java.lang.Object getValueInternal(MongoPersistentProperty prop, com.mongodb.DBObject dbo, org.springframework.expression.spel.support.StandardEvaluationContext ctx, java.lang.String spelExpr)
           
 org.springframework.data.util.TypeInformation<?> getValueType(org.springframework.data.util.TypeInformation<?> type)
          Returns the type type information of the actual value to be stored.
 boolean isUseFieldAccessOnly()
           
<S> S
read(java.lang.Class<S> clazz, com.mongodb.DBObject dbo)
          Ready from the native MongoDB DBObject representation to an instance of the class T.
protected
<S> S
read(org.springframework.data.util.TypeInformation<S> type, com.mongodb.DBObject dbo)
           
 void setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
           
 void setMongoDbFactory(MongoDbFactory mongoDbFactory)
           
 void setUseFieldAccessOnly(boolean useFieldAccessOnly)
           
protected
<T> java.util.List<?>
unwrapList(com.mongodb.BasicDBList dbList, org.springframework.data.util.TypeInformation<T> targetType)
           
 void write(java.lang.Object obj, com.mongodb.DBObject dbo)
          Root entry method into write conversion.
protected  com.mongodb.DBObject writeCollectionInternal(MongoPersistentProperty property, java.lang.Object obj)
           
protected  void writeInternal(java.lang.Object obj, com.mongodb.DBObject dbo)
          Internal write conversion method which should be used for nested invocations.
protected  void writeInternal(java.lang.Object obj, com.mongodb.DBObject dbo, MongoPersistentEntity<?> entity)
           
protected  void writeMapInternal(java.util.Map<java.lang.Object,java.lang.Object> obj, com.mongodb.DBObject dbo, org.springframework.data.util.TypeInformation<?> propertyType)
           
protected  void writePropertyInternal(MongoPersistentProperty prop, java.lang.Object obj, com.mongodb.DBObject dbo)
           
 
Methods inherited from class org.springframework.data.document.mongodb.convert.AbstractMongoConverter
afterPropertiesSet, getConversionService, getCustomTarget, maybeConvertArray, maybeConvertList, maybeConvertObject, setCustomConverters
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CUSTOM_TYPE_KEY

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

log

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

mappingContext

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

spelExpressionParser

protected final org.springframework.expression.spel.standard.SpelExpressionParser spelExpressionParser

applicationContext

protected org.springframework.context.ApplicationContext applicationContext

useFieldAccessOnly

protected boolean useFieldAccessOnly

mongoDbFactory

protected MongoDbFactory mongoDbFactory
Constructor Detail

MappingMongoConverter

public MappingMongoConverter(MongoDbFactory mongoDbFactory,
                             org.springframework.data.mapping.model.MappingContext<? extends MongoPersistentEntity<?>,MongoPersistentProperty> mappingContext)
Creates a new MappingMongoConverter given the new MongoDbFactory and MappingContext.

Parameters:
mongoDbFactory -
mappingContext -
Method Detail

getMappingContext

public org.springframework.data.mapping.model.MappingContext<? extends MongoPersistentEntity<?>,MongoPersistentProperty> getMappingContext()
Specified by:
getMappingContext in interface MongoConverter

setMongoDbFactory

public void setMongoDbFactory(MongoDbFactory mongoDbFactory)

isUseFieldAccessOnly

public boolean isUseFieldAccessOnly()

setUseFieldAccessOnly

public void setUseFieldAccessOnly(boolean useFieldAccessOnly)

convertObjectId

public <T> T convertObjectId(org.bson.types.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 org.bson.types.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,
                  com.mongodb.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

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

write

public void write(java.lang.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 MongoWriter<java.lang.Object>
Parameters:
obj - The object to convert to a DBObject
dbo - The DBObject to use for writing.
See Also:
MongoWriter.write(java.lang.Object, com.mongodb.DBObject)

writeInternal

protected void writeInternal(java.lang.Object obj,
                             com.mongodb.DBObject dbo)
Internal write conversion method which should be used for nested invocations.

Parameters:
obj -
dbo -

writeInternal

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

setApplicationContext

public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
                           throws org.springframework.beans.BeansException
Specified by:
setApplicationContext in interface org.springframework.context.ApplicationContextAware
Throws:
org.springframework.beans.BeansException

writePropertyInternal

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

writeCollectionInternal

protected com.mongodb.DBObject writeCollectionInternal(MongoPersistentProperty property,
                                                       java.lang.Object obj)

writeMapInternal

protected void writeMapInternal(java.util.Map<java.lang.Object,java.lang.Object> obj,
                                com.mongodb.DBObject dbo,
                                org.springframework.data.util.TypeInformation<?> propertyType)

addCustomTypeKeyIfNecessary

public void addCustomTypeKeyIfNecessary(org.springframework.data.util.TypeInformation<?> type,
                                        java.lang.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 -
dbObject -

getValueType

public org.springframework.data.util.TypeInformation<?> getValueType(org.springframework.data.util.TypeInformation<?> type)
Returns the type type information of the actual value to be stored. That is, for maps it will return the map value type, for collections it will return the component type as well as the given type if it is a non-collection or non-map one.

Parameters:
type -
Returns:

createDBRef

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

getValueInternal

protected java.lang.Object getValueInternal(MongoPersistentProperty prop,
                                            com.mongodb.DBObject dbo,
                                            org.springframework.expression.spel.support.StandardEvaluationContext ctx,
                                            java.lang.String spelExpr)

findTypeToBeUsed

protected java.lang.Class<?> findTypeToBeUsed(com.mongodb.DBObject dbObject)
Returns the type to be used to convert the DBObject given to. Will return null if there's not type hint found in the DBObject or the type hint found can't be converted into a Class as the type might not be available.

Parameters:
dbObject -
Returns:
the type to be used for converting the given DBObject into or null if there's no type found.

unwrapList

protected <T> java.util.List<?> unwrapList(com.mongodb.BasicDBList dbList,
                                           org.springframework.data.util.TypeInformation<T> targetType)


Copyright © 2011. All Rights Reserved.