Spring Data Document

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:
InitializingBean, ApplicationContextAware, MongoConverter, MongoReader<Object>, MongoWriter<Object>

public class MappingMongoConverter
extends AbstractMongoConverter
implements ApplicationContextAware

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

Author:
Jon Brisbin , Oliver Gierke

Field Summary
protected  ApplicationContext applicationContext
           
static 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  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, Object value, DBObject dbObject)
          Adds custom type information to the given DBObject if necessary.
 ObjectId convertObjectId(Object id)
          Returns the ObjectId instance for the given id.
<T> T
convertObjectId(ObjectId id, Class<T> targetType)
          Converts the given ObjectId to the given target type.
protected  DBRef createDBRef(Object target, DBRef dbref)
           
protected  Class<?> findTypeToBeUsed(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  Object getValueInternal(MongoPersistentProperty prop, DBObject dbo, StandardEvaluationContext ctx, 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(Class<S> clazz, 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, DBObject dbo)
           
 void setApplicationContext(ApplicationContext applicationContext)
           
 void setMongoDbFactory(MongoDbFactory mongoDbFactory)
           
 void setUseFieldAccessOnly(boolean useFieldAccessOnly)
           
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  DBObject writeCollectionInternal(MongoPersistentProperty property, Object obj)
           
protected  void writeInternal(Object obj, DBObject dbo)
          Internal write conversion method which should be used for nested invocations.
protected  void writeInternal(Object obj, DBObject dbo, MongoPersistentEntity<?> entity)
           
protected  void writeMapInternal(Map<Object,Object> obj, DBObject dbo, org.springframework.data.util.TypeInformation<?> propertyType)
           
protected  void writePropertyInternal(MongoPersistentProperty prop, Object obj, 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 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 SpelExpressionParser spelExpressionParser

applicationContext

protected 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(ObjectId id,
                             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(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(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<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,
                     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 MongoWriter<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(Object obj,
                             DBObject dbo)
Internal write conversion method which should be used for nested invocations.

Parameters:
obj -
dbo -

writeInternal

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

setApplicationContext

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

writePropertyInternal

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

writeCollectionInternal

protected DBObject writeCollectionInternal(MongoPersistentProperty property,
                                           Object obj)

writeMapInternal

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

addCustomTypeKeyIfNecessary

public 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 -
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 DBRef createDBRef(Object target,
                            DBRef dbref)

getValueInternal

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

findTypeToBeUsed

protected Class<?> findTypeToBeUsed(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> List<?> unwrapList(BasicDBList dbList,
                                 org.springframework.data.util.TypeInformation<T> targetType)

Spring Data Document

Copyright © 2011. All Rights Reserved.