com.jenkov.mrpersister.impl.mapping
Class ObjectMapper

java.lang.Object
  extended by com.jenkov.mrpersister.impl.mapping.ObjectMapper
All Implemented Interfaces:
IObjectMapper

public class ObjectMapper
extends java.lang.Object
implements IObjectMapper

Author:
Jakob Jenkov, Jenkov Development

Field Summary
protected  IDbNameDeterminer nameDeterminer
           
protected  IDbNameGuesser nameGuesser
           
protected  IObjectMappingFactory objectMappingFactory
           
protected  IDbPrimaryKeyDeterminer primaryKeyDeterminer
           
 
Constructor Summary
ObjectMapper(IObjectMappingFactory objectMappingFactory)
           
 
Method Summary
protected  IObjectMapping createObjectMapping(java.lang.Class theClass, java.lang.String tableName, java.sql.Connection connection)
           
 IDbNameDeterminer getDbNameDeterminer()
          Returns the database name determiner used by this object mapper.
 IDbNameGuesser getDbNameGuesser()
          Returns the database name guesser used by this object mapper.
 IDbPrimaryKeyDeterminer getDbPrimaryKeyDeterminer()
          Returns the database primary key determiner used by this object mapper.
 IObjectMapping getObjectMapping(java.lang.Object objectMappingKey, IPersistenceConfiguration configuration, java.sql.Connection connection)
          Returns an object mapping from the cache in the given configuration, if one already exists.
 IObjectMapping mapGettersToTable(java.lang.Class objectClass, IObjectMapping objectMapping, java.sql.Connection connection, java.lang.String databaseName, java.lang.String table)
          Maps an object to the table provided as parameter, using method name guessing for the members/fields.
 IObjectMapping mapSettersToSelf(java.lang.Class persistentObjectClass, IObjectMapping objectMapping)
          Maps an object to fields named the same as it's setters and adds the method mappings to the provided object mapping.
 IObjectMapping mapSettersToTable(java.lang.Class objectClass, IObjectMapping objectMapping, java.sql.Connection connection, java.lang.String databaseName, java.lang.String table)
          Maps an object to the table provided as parameter, using method name guessing for the members/fields.
 IObjectMapping mapToTable(java.lang.Class objectClass, IObjectMapping objectMapping, java.sql.Connection connection, java.lang.String databaseName, java.lang.String table)
          Maps an object to the table provided as parameter, using method name guessing for the members/fields.
 void setDbNameDeterminer(IDbNameDeterminer nameDeterminer)
          Sets the database name determiner to be used by this object mapper.
 void setDbNameGuesser(IDbNameGuesser guesser)
          Sets the database name determiner used by this object mapper.
 void setDbPrimaryKeyDeterminer(IDbPrimaryKeyDeterminer primaryKeyDeterminer)
          Sets the database primary key determiner to be used by this object mapper.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

nameGuesser

protected IDbNameGuesser nameGuesser

nameDeterminer

protected IDbNameDeterminer nameDeterminer

primaryKeyDeterminer

protected IDbPrimaryKeyDeterminer primaryKeyDeterminer

objectMappingFactory

protected IObjectMappingFactory objectMappingFactory
Constructor Detail

ObjectMapper

public ObjectMapper(IObjectMappingFactory objectMappingFactory)
Method Detail

getDbPrimaryKeyDeterminer

public IDbPrimaryKeyDeterminer getDbPrimaryKeyDeterminer()
Description copied from interface: IObjectMapper
Returns the database primary key determiner used by this object mapper.

Specified by:
getDbPrimaryKeyDeterminer in interface IObjectMapper
Returns:
The database primary key determiner used by this object mapper.

setDbPrimaryKeyDeterminer

public void setDbPrimaryKeyDeterminer(IDbPrimaryKeyDeterminer primaryKeyDeterminer)
Description copied from interface: IObjectMapper
Sets the database primary key determiner to be used by this object mapper.

Specified by:
setDbPrimaryKeyDeterminer in interface IObjectMapper
Parameters:
primaryKeyDeterminer - The database primary key determiner to be used by this object mapper.

getDbNameGuesser

public IDbNameGuesser getDbNameGuesser()
Description copied from interface: IObjectMapper
Returns the database name guesser used by this object mapper.

Specified by:
getDbNameGuesser in interface IObjectMapper
Returns:
The database name guesser used by this object mapper.

setDbNameGuesser

public void setDbNameGuesser(IDbNameGuesser guesser)
Description copied from interface: IObjectMapper
Sets the database name determiner used by this object mapper.

Specified by:
setDbNameGuesser in interface IObjectMapper
Parameters:
guesser - The databa name guesser to be used by this object mapper.

getDbNameDeterminer

public IDbNameDeterminer getDbNameDeterminer()
Description copied from interface: IObjectMapper
Returns the database name determiner used by this object mapper.

Specified by:
getDbNameDeterminer in interface IObjectMapper
Returns:
The database name determiner used by this object mapper.

setDbNameDeterminer

public void setDbNameDeterminer(IDbNameDeterminer nameDeterminer)
Description copied from interface: IObjectMapper
Sets the database name determiner to be used by this object mapper.

Specified by:
setDbNameDeterminer in interface IObjectMapper
Parameters:
nameDeterminer - The database name determiner to be used by this object mapper.

getObjectMapping

public IObjectMapping getObjectMapping(java.lang.Object objectMappingKey,
                                       IPersistenceConfiguration configuration,
                                       java.sql.Connection connection)
                                throws PersistenceException
Description copied from interface: IObjectMapper
Returns an object mapping from the cache in the given configuration, if one already exists. If not a new object mapping is tried generated, cached for later use, and returned.

Specified by:
getObjectMapping in interface IObjectMapper
Parameters:
objectMappingKey - The key under which to retrieve and/or store the object mapping.
configuration - The configuration containing the object mapping cache.
connection - A database connection used in case the mapper
Returns:
Throws:
PersistenceException

createObjectMapping

protected IObjectMapping createObjectMapping(java.lang.Class theClass,
                                             java.lang.String tableName,
                                             java.sql.Connection connection)
                                      throws PersistenceException
Throws:
PersistenceException

mapToTable

public IObjectMapping mapToTable(java.lang.Class objectClass,
                                 IObjectMapping objectMapping,
                                 java.sql.Connection connection,
                                 java.lang.String databaseName,
                                 java.lang.String table)
                          throws PersistenceException
Description copied from interface: IObjectMapper
Maps an object to the table provided as parameter, using method name guessing for the members/fields.

Specified by:
mapToTable in interface IObjectMapper
Parameters:
objectClass - The class of the object to map.
objectMapping - An optional object mapping to add the method mappings to. If null is provided the object mapper will create an empty object mapping, add the method mappings to it and return it.
connection - A connection to the database containing the table the objects should be mapped to.
databaseName - The name of the database the table is residing in. In most cases this parameter can be null, since the connection is normally pointing to a certain database.
table - The name of the database table to map the objects to. Optional. If null is given the object mapper will try to guess the table name from the objects class name.
Returns:
An object mapping with guessed method names from the given table. If a method name for an object mapping (getter/setter) could not be guessed there will be no method method for that method.
Throws:
PersistenceException - If anything goes wrong during the method.

mapGettersToTable

public IObjectMapping mapGettersToTable(java.lang.Class objectClass,
                                        IObjectMapping objectMapping,
                                        java.sql.Connection connection,
                                        java.lang.String databaseName,
                                        java.lang.String table)
                                 throws PersistenceException
Description copied from interface: IObjectMapper
Maps an object to the table provided as parameter, using method name guessing for the members/fields.

Specified by:
mapGettersToTable in interface IObjectMapper
Parameters:
objectClass - The class of the object to map.
objectMapping - An optional object mapping to add the method mappings to. If null is provided the object mapper will create an empty object mapping, add the method mappings to it and return it.
connection - A connection to the database containing the table the objects should be mapped to.
databaseName - The name of the database the table is residing in. In most cases this parameter can be null, since the connection is normally pointing to a certain database.
table - The name of the database table to map the objects to. Optional. If null is given the object mapper will try to guess the table name from the objects class name.
Returns:
An object mapping with guessed method names from the given table. If a method name for an object mapping (getter/setter) could not be guessed there will be no method method for that method.
Throws:
PersistenceException - If anything goes wrong during the method.

mapSettersToTable

public IObjectMapping mapSettersToTable(java.lang.Class objectClass,
                                        IObjectMapping objectMapping,
                                        java.sql.Connection connection,
                                        java.lang.String databaseName,
                                        java.lang.String table)
                                 throws PersistenceException
Description copied from interface: IObjectMapper
Maps an object to the table provided as parameter, using method name guessing for the members/fields.

Specified by:
mapSettersToTable in interface IObjectMapper
Parameters:
objectClass - The class of the object to map.
objectMapping - An optional object mapping to add the method mappings to. If null is provided the object mapper will create an empty object mapping, add the method mappings to it and return it.
connection - A connection to the database containing the table the objects should be mapped to.
databaseName - The name of the database the table is residing in. In most cases this parameter can be null, since the connection is normally pointing to a certain database.
table - The name of the database table to map the objects to. Optional. If null is given the object mapper will try to guess the table name from the objects class name.
Returns:
An object mapping with guessed method names from the given table. If a method name for an object mapping (getter/setter) could not be guessed there will be no method method for that method.
Throws:
PersistenceException - If anything goes wrong during the method.

mapSettersToSelf

public IObjectMapping mapSettersToSelf(java.lang.Class persistentObjectClass,
                                       IObjectMapping objectMapping)
                                throws PersistenceException
Description copied from interface: IObjectMapper
Maps an object to fields named the same as it's setters and adds the method mappings to the provided object mapping. If null is provided as object mapping an empty object method instance is created and used. An object containing a setHomeAddress will have a method method with the method name "setHomeAddress". This method is very useful when method objects to SQL queries instead of tables. With a self-mapped object you can read values into it using an sql like: select homeAddr as setHomeAddress from users. The method in the result set will be called setHomeAddres. The object mapping will then translate that name to the setHomeAddress(...) method (because that method is mapped to a method called "setHomeAddress" in the object mapping).

Specified by:
mapSettersToSelf in interface IObjectMapper
Parameters:
persistentObjectClass - The class to map to itself.
Returns:
An object mapping with all setters mapped to fields with the same name as the coresponding setter method.
Throws:
PersistenceException