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:
MongoConverter, MongoReader<java.lang.Object>, MongoWriter<java.lang.Object>

public class MappingMongoConverter
extends AbstractMongoConverter

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 java.lang.String CUSTOM_TYPE_KEY
           
protected static Log log
           
protected   mappingContext
           
protected  MongoDbFactory mongoDbFactory
           
protected  SpelExpressionParser spelExpressionParser
           
protected  boolean useFieldAccessOnly
           
private static java.util.List<java.lang.Class<?>> VALID_ID_TYPES
           
 
Fields inherited from class org.springframework.data.document.mongodb.convert.AbstractMongoConverter
conversionService
 
Constructor Summary
MappingMongoConverter(MongoDbFactory mongoDbFactory,  mappingContext)
          Creates a new MappingMongoConverter given the new MongoDbFactory and MappingContext.
 
Method Summary
 void addCustomTypeKeyIfNecessary( type, java.lang.Object value, DBObject dbObject)
          Adds custom type information to the given DBObject if necessary.
 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.
  getMappingContext()
           
private
<S>
getMoreConcreteTargetType(DBObject dbObject,  basicType)
          Inspects the a custom class definition stored inside the given DBObject and returns that in case it's a subtype of the given basic one.
protected  java.lang.Object getValueInternal(MongoPersistentProperty prop, DBObject dbo, StandardEvaluationContext ctx, java.lang.String spelExpr)
           
  getValueType( type)
          Returns the type type information of the actual value to be stored.
 boolean isUseFieldAccessOnly()
           
protected
<S> S
read( type, 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
<S> S
read(MongoPersistentEntity<S> entity, DBObject dbo)
           
 void setApplicationContext(ApplicationContext applicationContext)
           
 void setMongoDbFactory(MongoDbFactory mongoDbFactory)
           
 void setUseFieldAccessOnly(boolean useFieldAccessOnly)
           
protected
<T> java.util.List<?>
unwrapList(BasicDBList dbList,  targetType)
           
 void write(java.lang.Object obj, DBObject dbo)
          Root entry method into write conversion.
protected  DBObject writeCollectionInternal(MongoPersistentProperty property, java.lang.Object obj)
           
protected  void writeInternal(java.lang.Object obj, DBObject dbo)
          Internal write conversion method which should be used for nested invocations.
protected  void writeInternal(java.lang.Object obj, DBObject dbo, MongoPersistentEntity<?> entity)
           
protected  void writeMapInternal(java.util.Map<java.lang.Object,java.lang.Object> obj, DBObject dbo,  propertyType)
           
protected  void writePropertyInternal(MongoPersistentProperty prop, java.lang.Object obj, DBObject dbo)
           
private  void writeSimpleInternal(java.lang.String key, java.lang.Object value, DBObject dbObject)
          Writes the given simple value to the given DBObject.
 
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

VALID_ID_TYPES

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

log

protected static final Log log

mappingContext

protected final  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,
                              mappingContext)
Creates a new MappingMongoConverter given the new MongoDbFactory and MappingContext.

Parameters:
mongoDbFactory -
mappingContext -
Method Detail

getMappingContext

public  getMappingContext()

setMongoDbFactory

public void setMongoDbFactory(MongoDbFactory mongoDbFactory)

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.

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.

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.

Parameters:
clazz - the type of the return value
dbo - theDBObject
Returns:
the converted object

read

protected <S> S read( type,
                     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.

Parameters:
type - the type of the return value
dbo - theDBObject
Returns:
the converted object

read

private <S> S read(MongoPersistentEntity<S> entity,
                   DBObject dbo)

write

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

Parameters:
obj - The object to convert to a DBObject
dbo - The DBObject to use for writing.
See Also:
org.springframework.data.document.mongodb.MongoWriter#write(java.lang.Object, com.mongodb.DBObject)

writeInternal

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

Parameters:
obj -
dbo -

writeInternal

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

setApplicationContext

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

writePropertyInternal

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

writeCollectionInternal

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

writeMapInternal

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

addCustomTypeKeyIfNecessary

public void addCustomTypeKeyIfNecessary( type,
                                        java.lang.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  getValueType( 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:

writeSimpleInternal

private void writeSimpleInternal(java.lang.String key,
                                 java.lang.Object value,
                                 DBObject dbObject)
Writes the given simple value to the given DBObject. Will store enum names for enum values.

Parameters:
key -
value -
dbObject -

createDBRef

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

getValueInternal

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

findTypeToBeUsed

protected java.lang.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.

getMoreConcreteTargetType

private <S>  getMoreConcreteTargetType(DBObject dbObject,
                                             basicType)
Inspects the a custom class definition stored inside the given DBObject and returns that in case it's a subtype of the given basic one.

Parameters:
dbObject -
basicType -
Returns:

unwrapList

protected <T> java.util.List<?> unwrapList(BasicDBList dbList,
                                            targetType)