org.apache.click.extras.cayenne
Class CayenneTemplate

java.lang.Object
  extended by org.apache.click.extras.cayenne.CayenneTemplate

public class CayenneTemplate
extends Object

Provides base Cayenne data access object or service class to extend, following the Spring DAO template pattern. This class uses thread bound DataContext for all data access operations.

This class is designed to be extended by custom DAO or Service subclasses which provide their own public interface. All methods on CayenneTemplate have protected visibility so they are not publicly visible on the custom subclasses.

CayenneTemplate provides many convenience DataContext methods using the DataContext object bound to the current thread.


Constructor Summary
CayenneTemplate()
           
 
Method Summary
protected  void commitChanges()
          Commit any changes in the thread local DataContext.
protected
<T> T
createAndRegisterNewObject(Class<T> persistentClass)
          Deprecated. since 2.3.0, use newObject(java.lang.Class) instead.
protected  void deleteObject(org.apache.cayenne.DataObject dataObject)
          Schedules an object for deletion on the next commit of this DataContext.
protected
<T> T
findObject(Class<T> persistentClass, String property, Object value)
          Find the persistent object for the specified class, property name and property value, or null if no persistent object was found.
protected  Connection getConnection()
          Return a pooled Cayenne connection for the shared configuration and the first configured DataNode.
protected  org.apache.cayenne.access.DataContext getDataContext()
          Return the thread local DataContext.
protected
<T> T
getObjectForPK(Class<T> persistentClass, Object id)
          Perform a database query returning the data object specified by the class and the primary key.
protected
<T> T
getObjectForPK(Class<T> persistentClass, Object id, boolean refresh)
          Perform a query returning the persistent object specified by the class and the primary key value.
protected  String getPkName(Class persistentClass)
          Return the database primary key column name for the given data object.
protected
<T> T
newObject(Class<T> persistentClass)
          Instantiate and register a new persistent object.
protected  int[] performNonSelectingQuery(org.apache.cayenne.query.Query query)
          Performs a single database query that does not select rows.
protected  int[] performNonSelectingQuery(String queryName)
          Performs a named mapped query that does not select rows.
protected  int[] performNonSelectingQuery(String queryName, Map<String,?> parameters)
          Performs a named mapped non-selecting query using a map of parameters.
protected  List performQuery(Class dataObjectClass, String property, Object value)
          Return a list of data object of the specified class for the given property and value.
protected  List performQuery(org.apache.cayenne.query.Query query)
          Performs a single selecting query.
protected  List performQuery(String queryName, boolean refresh)
          Returns a list of objects or DataRows for a named query stored in one of the DataMaps.
protected  List performQuery(String queryName, Map parameters, boolean refresh)
          Returns a list of objects or DataRows for a named query stored in one of the DataMaps.
protected  void registerNewObject(org.apache.cayenne.DataObject dataObject)
          Registers a transient object with the context, recursively registering all transient DataObjects attached to this object via relationships.
protected  void rollbackChanges()
          Reverts any changes that have occurred to objects registered in the thread local DataContext.
protected  Map toMap(String key, Object value)
          Return a Map containing the given key name and value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CayenneTemplate

public CayenneTemplate()
Method Detail

newObject

protected <T> T newObject(Class<T> persistentClass)
Instantiate and register a new persistent object. Object class must have a default constructor.

Parameters:
persistentClass - the persistent object class to create and register
Returns:
the new registered persistent object

createAndRegisterNewObject

protected <T> T createAndRegisterNewObject(Class<T> persistentClass)
Deprecated. since 2.3.0, use newObject(java.lang.Class) instead.

Instantiates new object and registers it with itself. Object class must have a default constructor.

Parameters:
persistentClass - the persistent object class to create and register
Returns:
the new registered persistent object

commitChanges

protected void commitChanges()
Commit any changes in the thread local DataContext.


deleteObject

protected void deleteObject(org.apache.cayenne.DataObject dataObject)
                     throws org.apache.cayenne.DeleteDenyException
Schedules an object for deletion on the next commit of this DataContext. Object's persistence state is changed to PersistenceState.DELETED; objects related to this object are processed according to delete rules, i.e. relationships can be unset ("nullify" rule), deletion operation is cascaded (cascade rule).

Parameters:
dataObject - a persistent data object that we want to delete
Throws:
org.apache.cayenne.DeleteDenyException - if a DENY delete rule is applicable for object deletion

findObject

protected <T> T findObject(Class<T> persistentClass,
                           String property,
                           Object value)
Find the persistent object for the specified class, property name and property value, or null if no persistent object was found.

Parameters:
persistentClass - the persistent object class to find
property - the name of the property
value - the value of the property
Returns:
the persistent object for the specified class, property name and property value
Throws:
RuntimeException - if more than one persistent object was identified for the given property name and value

getConnection

protected Connection getConnection()
                            throws SQLException
Return a pooled Cayenne connection for the shared configuration and the first configured DataNode.

Returns:
a pooled SQL connection
Throws:
SQLException - if a database connection could not be obtained

getDataContext

protected org.apache.cayenne.access.DataContext getDataContext()
Return the thread local DataContext. If a DataContext not not bound to the current thread, this method will create a new DataContext and bind it to the thread.

Returns:
the thread local DataContext

getObjectForPK

protected <T> T getObjectForPK(Class<T> persistentClass,
                               Object id)
Perform a database query returning the data object specified by the class and the primary key. This method will perform a database query and refresh the object cache.

Parameters:
persistentClass - the persistent object class to retrieve
id - the data object primary key
Returns:
the data object for the given class and id

getObjectForPK

protected <T> T getObjectForPK(Class<T> persistentClass,
                               Object id,
                               boolean refresh)
Perform a query returning the persistent object specified by the class and the primary key value. If the refresh parameter is true a database query will be performed, otherwise the a query against the object cache will be performed first.

Parameters:
persistentClass - the persistent object class to retrieve
id - the persistent object primary key
refresh - the refresh the object cache mode
Returns:
the persistent object for the given class and id

getPkName

protected String getPkName(Class persistentClass)
Return the database primary key column name for the given data object.

Parameters:
persistentClass - the class of the persistent object
Returns:
the primary key column name

performQuery

protected List performQuery(org.apache.cayenne.query.Query query)
Performs a single selecting query. Various query setting control the behavior of this method and the results returned:

Parameters:
query - the query to perform
Returns:
a list of DataObjects or a DataRows for the query

performQuery

protected List performQuery(String queryName,
                            boolean refresh)
Returns a list of objects or DataRows for a named query stored in one of the DataMaps. Internally Cayenne uses a caching policy defined in the named query. If refresh flag is true, a refresh is forced no matter what the caching policy is.

Parameters:
queryName - a name of a GenericSelectQuery defined in one of the DataMaps. If no such query is defined, this method will throw a CayenneRuntimeException
refresh - A flag that determines whether refresh of cached lists is required in case a query uses caching.
Returns:
the list of data object or DataRows for the named query

performQuery

protected List performQuery(String queryName,
                            Map parameters,
                            boolean refresh)
Returns a list of objects or DataRows for a named query stored in one of the DataMaps. Internally Cayenne uses a caching policy defined in the named query. If refresh flag is true, a refresh is forced no matter what the caching policy is.

Parameters:
queryName - a name of a GenericSelectQuery defined in one of the DataMaps. If no such query is defined, this method will throw a CayenneRuntimeException
parameters - A map of parameters to use with stored query
refresh - A flag that determines whether refresh of cached lists is required in case a query uses caching.
Returns:
the list of data object or DataRows for the named query

performQuery

protected List performQuery(Class dataObjectClass,
                            String property,
                            Object value)
Return a list of data object of the specified class for the given property and value.

Parameters:
dataObjectClass - the data object class to return
property - the name of the property to select
value - the property value to select
Returns:
a list of data objects for the given class and property name and value

performNonSelectingQuery

protected int[] performNonSelectingQuery(org.apache.cayenne.query.Query query)
Performs a single database query that does not select rows. Returns an array of update counts.

Parameters:
query - the query to perform
Returns:
the array of update counts

performNonSelectingQuery

protected int[] performNonSelectingQuery(String queryName)
Performs a named mapped query that does not select rows. Returns an array of update counts.

Parameters:
queryName - the name of the query to perform
Returns:
the array of update counts

performNonSelectingQuery

protected int[] performNonSelectingQuery(String queryName,
                                         Map<String,?> parameters)
Performs a named mapped non-selecting query using a map of parameters. Returns an array of update counts.

Parameters:
queryName - the name of the query to perform
parameters - the Map of query parameter names and values
Returns:
the array of update counts

registerNewObject

protected void registerNewObject(org.apache.cayenne.DataObject dataObject)
Registers a transient object with the context, recursively registering all transient DataObjects attached to this object via relationships.

Parameters:
dataObject - new object that needs to be made persistent

rollbackChanges

protected void rollbackChanges()
Reverts any changes that have occurred to objects registered in the thread local DataContext.


toMap

protected Map toMap(String key,
                    Object value)
Return a Map containing the given key name and value.

Parameters:
key - the map key name
value - the map key value
Returns:
a Map containing the given key name and value