net.sf.click.extras.cayenne
Class CayenneTemplate

java.lang.Object
  extended bynet.sf.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 convience DataContext methods using the DataContext object bound to the current thread.

Author:
Malcolm Edgar, Andrei Adamchik

Constructor Summary
CayenneTemplate()
           
 
Method Summary
protected  void commitChanges()
          Commit any changes in the thread local DataContext.
protected  org.apache.cayenne.DataObject createAndRegisterNewObject(Class dataObjectClass)
          Instantiates new object and registers it with itself.
protected  void deleteObject(org.apache.cayenne.DataObject dataObject)
          Schedules an object for deletion on the next commit of this DataContext.
protected  org.apache.cayenne.DataObject findObject(Class dataObjectClass, String property, Object value)
          Find the data object for the specified class, property name and property value, or null if no data object was found.
protected  org.apache.cayenne.access.DataContext getDataContext()
          Return the thread local DataContext.
protected  org.apache.cayenne.DataObject getObjectForPK(Class doClass, Object id)
          Perform a database query returning the data object specified by the class and the primary key.
protected  org.apache.cayenne.DataObject getObjectForPK(Class dataObjectClass, Object id, boolean refresh)
          Perform a query returning the data object specified by the class and the primary key value.
protected  String getPkName(Class dataObjectClass)
          Return the database primary key column name for the given data 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 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

createAndRegisterNewObject

protected org.apache.cayenne.DataObject createAndRegisterNewObject(Class dataObjectClass)
Instantiates new object and registers it with itself. Object class must have a default constructor.

Parameters:
dataObjectClass - the data object class to create and register
Returns:
the new registered data 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 org.apache.cayenne.DataObject findObject(Class dataObjectClass,
                                                   String property,
                                                   Object value)
Find the data object for the specified class, property name and property value, or null if no data object was found.

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

getDataContext

protected org.apache.cayenne.access.DataContext getDataContext()
Return the thread local DataContext.

Returns:
the thread local DataContext
Throws:
IllegalStateException - if there is no DataContext bound to the current thread

getObjectForPK

protected org.apache.cayenne.DataObject getObjectForPK(Class doClass,
                                                       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:
doClass - the data object class to retrieve
id - the data object primary key
Returns:
the data object for the given class and id

getObjectForPK

protected org.apache.cayenne.DataObject getObjectForPK(Class dataObjectClass,
                                                       Object id,
                                                       boolean refresh)
Perform a query returning the data 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:
dataObjectClass - the data object class to retrieve
id - the data object primary key
refresh - the refresh the object cache mode
Returns:
the data object for the given class and id

getPkName

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

Parameters:
dataObjectClass - the class of the data 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 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 paramater 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