Spring Data Graph

org.springframework.data.document.mongodb
Class SimpleMongoConverter

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

public class SimpleMongoConverter
extends Object
implements MongoConverter

Basic MongoConverter implementation to convert between domain classes and DBObjects.

Author:
Mark Pollack, Thomas Risberg, Oliver Gierke

Nested Class Summary
static class SimpleMongoConverter.BigIntegerToIdConverter
          Simple singleton to convert BigIntegers to their ObjectId representation.
static class SimpleMongoConverter.ObjectIdToBigIntegerConverter
          Simple singleton to convert ObjectIds to their BigInteger representation.
static class SimpleMongoConverter.ObjectIdToStringConverter
          Simple singleton to convert ObjectIds to their String representation.
static class SimpleMongoConverter.StringToObjectIdConverter
          Simple singleton to convert Strings to their ObjectId representation.
 
Constructor Summary
SimpleMongoConverter()
          Creates a SimpleMongoConverter.
SimpleMongoConverter(GenericConversionService conversionService)
          Creates a new SimpleMongoConverter for the given ConversionService.
 
Method Summary
 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  Map<String,Object> createMap()
          Create a Map instance.
protected  org.springframework.data.document.mongodb.MongoBeanWrapper createWrapper(Object target, boolean fieldAccess)
          Callback to allow customizing creation of a MongoBeanWrapper.
protected  void initializeConverters()
          Initializes additional converters that handle ObjectId conversion.
protected static boolean isSimpleType(Class<?> propertyType)
           
<S> S
read(Class<S> clazz, DBObject source)
          Ready from the native MongoDB DBObject representation to an instance of the class T.
protected  Map<?,?> readMap(MongoPropertyDescriptors.MongoPropertyDescriptor pd, DBObject dbo, Class<?> targetType)
          Reads every key/value pair from the DBObject into a Map instance.
 void write(Object obj, DBObject dbo)
          Write the given object of type T to the native MongoDB object representation DBObject.
protected  void writeArray(DBObject dbo, String keyToUse, Object[] array)
          Writes the given array to the given DBObject.
protected  void writeMap(DBObject dbo, String mapKey, Map<String,Object> map)
          Writes the given Map to the given DBObject.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleMongoConverter

public SimpleMongoConverter()
Creates a SimpleMongoConverter.


SimpleMongoConverter

public SimpleMongoConverter(GenericConversionService conversionService)
Creates a new SimpleMongoConverter for the given ConversionService.

Parameters:
conversionService -
Method Detail

initializeConverters

protected void initializeConverters()
Initializes additional converters that handle ObjectId conversion. Will register converters for supported id types if none are registered for those conversion already. GenericConversionService is configured.


write

public void write(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<Object>
Parameters:
obj - The object to convert to a DBObject
dbo - The DBObject to use for writing.

writeMap

protected void writeMap(DBObject dbo,
                        String mapKey,
                        Map<String,Object> map)
Writes the given Map to the given DBObject.

Parameters:
dbo -
mapKey -
map -

writeArray

protected void writeArray(DBObject dbo,
                          String keyToUse,
                          Object[] array)
Writes the given array to the given DBObject.

Parameters:
dbo -
keyToUse -
array -

read

public <S> S read(Class<S> clazz,
                  DBObject source)
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
source - theDBObject
Returns:
the converted object

createMap

protected Map<String,Object> createMap()
Create a Map instance. Will return a HashMap by default. Subclasses might want to override this method to use a custom Map implementation.

Returns:

readMap

protected Map<?,?> readMap(MongoPropertyDescriptors.MongoPropertyDescriptor pd,
                           DBObject dbo,
                           Class<?> targetType)
Reads every key/value pair from the DBObject into a Map instance.

Parameters:
pd -
dbo -
targetType -
Returns:

isSimpleType

protected static boolean isSimpleType(Class<?> propertyType)

createWrapper

protected org.springframework.data.document.mongodb.MongoBeanWrapper createWrapper(Object target,
                                                                                   boolean fieldAccess)
Callback to allow customizing creation of a MongoBeanWrapper.

Parameters:
target - the target object to wrap
fieldAccess - whether to use field access or property access
Returns:

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:

Spring Data Graph

Copyright © 2011. All Rights Reserved.