SPT Core API

com.sptci.jdo
Class Factory<T>

java.lang.Object
  extended by com.sptci.jdo.Factory<T>
Type Parameters:
T - The class name of the object which the repository class manages.
Direct Known Subclasses:
CategoryFactory, GroupFactory, StartLetterFactory, UserFactory

public abstract class Factory<T>
extends Object

The base class for all repository classes. Provides common methods. This class is not meant from direct use by clients. Always use the appropriate sub-class for full features.

© Copyright 2006 Sans Pareil Technologies, Inc.

Version:
$Id: Factory.java 4853 2009-08-10 21:10:14Z rakesh $
Author:
Rakesh Vidyadharan 2006-05-11

Field Summary
protected static Logger logger
          The logger to use for all repository classes.
 
Constructor Summary
Factory()
           
 
Method Summary
protected  void addAlphabeticalIndex(Object object, javax.jdo.PersistenceManager pm)
          Check for the AlphabeticalIndex annotation and populate JDOObject.startLetter as appropriate.
 void begin(javax.jdo.PersistenceManager pm)
          Start a transaction in the specified persistence manager.
 void close(javax.jdo.PersistenceManager pm)
          Close the specified PersistenceManager.
 void close(javax.jdo.Query query)
          Close the specified query if it is not null.
 void commit(javax.jdo.PersistenceManager pm)
          Commit the active transaction in the specified persistence manager.
protected  long count(Class cls, javax.jdo.PersistenceManager pm)
          Return the total number of objects persisted in the data store.
 void delete(T object, javax.jdo.PersistenceManager pm)
          Delete the specified persistence capable object from the data store.
protected  Collection<T> fetch(Class cls, long start, long end, javax.jdo.PersistenceManager pm)
          Return a collection that contains all instances in the data store of the specified class within the specified range.
protected  Collection<T> fetch(Class cls, long start, long end, String sortClause, javax.jdo.PersistenceManager pm)
          Return a collection that contains all instances in the data store of the specified class within the specified range.
 T fetch(Object oid, javax.jdo.PersistenceManager pm)
          Fetch the object identified by the ObjectId specified.
protected  Collection<T> fetchAll(Class cls, javax.jdo.PersistenceManager pm)
          Return a collection that contains all instances in the data store of the specified class.
 javax.jdo.PersistenceManager getPersistenceManager()
          Return a PersistenceManager from the PersistenceManagerFactory
protected  void removeStartLetter(Object object, StartLetter startLetter, javax.jdo.PersistenceManager pm)
          Remove start letters that are not in use by instances of the type of object specified.
 void rollback(javax.jdo.PersistenceManager pm)
          Roll back any active transaction associated wih the specified persistence manager.
 T save(T object, javax.jdo.PersistenceManager pm)
          Save the specified persistence capable object to the data store.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected static final Logger logger
The logger to use for all repository classes.

Constructor Detail

Factory

public Factory()
Method Detail

getPersistenceManager

public javax.jdo.PersistenceManager getPersistenceManager()
Return a PersistenceManager from the PersistenceManagerFactory

Returns:
PersistenceManager The appropriate instance.
See Also:
PersistenceManagerFactory.getPersistenceManager()

begin

public void begin(javax.jdo.PersistenceManager pm)
Start a transaction in the specified persistence manager. Checks for an active transaction before attempting to start a new transaction. A convenience method to safely start a transaction.

Parameters:
pm - The persistence manager for which a transaction is to be started if necessary.

commit

public void commit(javax.jdo.PersistenceManager pm)
Commit the active transaction in the specified persistence manager. This method is fail-safe since it checks for an active transaction before committing. Attempts to commit non-active transactions will be logged.

Parameters:
pm - The persistence manager for which a transaction is to be committed.

rollback

public void rollback(javax.jdo.PersistenceManager pm)
Roll back any active transaction associated wih the specified persistence manager.

Parameters:
pm - The persistence manager whose associated transaction is to be rolled back.

close

public void close(javax.jdo.PersistenceManager pm)
Close the specified PersistenceManager. Rolls back any uncommitted Transactions prior to closing the manager.

Parameters:
pm - The PersistenceManager that is to be closed.
See Also:
rollback(javax.jdo.PersistenceManager)

close

public void close(javax.jdo.Query query)
Close the specified query if it is not null.

Parameters:
query - The query to close.

save

public T save(T object,
              javax.jdo.PersistenceManager pm)
       throws javax.jdo.JDOException
Save the specified persistence capable object to the data store.

Parameters:
object - The instance to persist to the data store.
pm - The persistence manager to use to interact with the data store. It is assumed that an active transaction is in operation.
Returns:
Returns the potentially modified instance.
Throws:
javax.jdo.JDOException - If errors are encountered while persisting the object.
See Also:
addAlphabeticalIndex(java.lang.Object, javax.jdo.PersistenceManager)

delete

public void delete(T object,
                   javax.jdo.PersistenceManager pm)
            throws javax.jdo.JDOException
Delete the specified persistence capable object from the data store.

Parameters:
object - The instance to delete from the data store.
pm - The persistence manager to use to interact with the data store. It is assumed that an active transaction is in operation.
Throws:
javax.jdo.JDOException - If errors are encountered while deleting the persistent object.

fetchAll

protected Collection<T> fetchAll(Class cls,
                                 javax.jdo.PersistenceManager pm)
                          throws javax.jdo.JDOException
Return a collection that contains all instances in the data store of the specified class. Note that all sub-classes of the specified type are also returned.

Parameters:
cls - The class whose instances are to be retrieved. This should be the same as the class of parametrised type T. This parameter is required since we cannot infer the type of the parametriced handle T.
pm - The persistence manager to use. It is assumed that an active transaction exists (if non-transaction read is not enabled).
Returns:
The collection of objects. Returns an empty collection if no objects exist in the data store.
Throws:
javax.jdo.JDOException - If errors are encountered while fetching the objects.

fetch

protected Collection<T> fetch(Class cls,
                              long start,
                              long end,
                              javax.jdo.PersistenceManager pm)
                       throws javax.jdo.JDOException
Return a collection that contains all instances in the data store of the specified class within the specified range. This method is typically used to implement paginated view of the data in the data store. Use the count(java.lang.Class, javax.jdo.PersistenceManager) method to fetch the total number of persistent instances in the data store.

Parameters:
cls - The class whose instances are to be retrieved. This should be the same as the class of parametrised type T. This parameter is required since we cannot infer the type of the parametriced handle T.
start - The index (inclusive) from which objects are to be retrieved from the data store.
end - The index (exclusive) till which objects are to be retrieved from the data store.
pm - The persistence manager to use. It is assumed that an active transaction exists (if non-transaction read is not enabled).
Returns:
The collection of objects. Returns an empty collection if no objects exist in the data store.
Throws:
javax.jdo.JDOException - If errors are encountered while fetching the objects.

fetch

protected Collection<T> fetch(Class cls,
                              long start,
                              long end,
                              String sortClause,
                              javax.jdo.PersistenceManager pm)
                       throws javax.jdo.JDOException
Return a collection that contains all instances in the data store of the specified class within the specified range. This method is typically used to implement paginated view of the data in the data store. Use the count(java.lang.Class, javax.jdo.PersistenceManager) method to fetch the total number of persistent instances in the data store.

Parameters:
cls - The class whose instances are to be retrieved. This should be the same as the class of parametrised type T. This parameter is required since we cannot infer the type of the parametriced handle T.
start - The index (inclusive) from which objects are to be retrieved from the data store.
end - The index (exclusive) till which objects are to be retrieved from the data store.
sortClause - The sort clause to use to order the query results. See Query.setOrdering(java.lang.String) for details on how to specify this parameter.
pm - The persistence manager to use. It is assumed that an active transaction exists (if non-transaction read is not enabled).
Returns:
The collection of objects. Returns an empty collection if no objects exist in the data store.
Throws:
javax.jdo.JDOException - If errors are encountered while fetching the objects.

fetch

public T fetch(Object oid,
               javax.jdo.PersistenceManager pm)
        throws javax.jdo.JDOException
Fetch the object identified by the ObjectId specified.

Parameters:
oid - The object id using which the persistent object is to be retrieved.
pm - The persistence manager to use to interact with the data store. It is assumed that an active transaction is in operation.
Returns:
The appropriate object instance. Returns null if no corresponding object could be found.
Throws:
javax.jdo.JDOException - If errors are encountered while fetching the object.

count

protected final long count(Class cls,
                           javax.jdo.PersistenceManager pm)
Return the total number of objects persisted in the data store.

Parameters:
cls - The class whose instance count is to be retrieved.
pm - The persistence manager to use to interact with the data store. It is assumed that an active transaction is in operation.
Returns:
The total number of instances or 0 if no instances exist.

addAlphabeticalIndex

protected void addAlphabeticalIndex(Object object,
                                    javax.jdo.PersistenceManager pm)
Check for the AlphabeticalIndex annotation and populate JDOObject.startLetter as appropriate.

Parameters:
object - The object that is to be indexed if required.
pm - The persistence manager to use.

removeStartLetter

protected void removeStartLetter(Object object,
                                 StartLetter startLetter,
                                 javax.jdo.PersistenceManager pm)
Remove start letters that are not in use by instances of the type of object specified. Use after deleting a persistent instance of the specified class to ensure that alphabetical indices are accurate.

Parameters:
object - The object for which associated indices are to be cleaned.
startLetter - The object that is to be deleted if required.
pm - The persistence manager to use.

SPT Core API