com.heckmansoft.surjey.model.datastore
Class Datastore

java.lang.Object
  extended bycom.heckmansoft.surjey.model.datastore.Datastore
Direct Known Subclasses:
QuestionDatastore, QuestionTypeDatastore, ResponseAnswerDatastore, ResponseDatastore, SectionDatastore, SurveyDatastore

public class Datastore
extends java.lang.Object

Manages object-relational mapping for ValueObject objects.


Constructor Summary
Datastore(java.lang.String tableName, java.lang.String addTableFields)
          Constructor.
 
Method Summary
protected  boolean executeDelete(java.lang.String id)
          Populate and execute the prepared delete statement.
protected  boolean executeInsert(ValueObject vo)
          Populate and execute the prepared insert statement.
protected  boolean executeUpdate(ValueObject vo)
          Populate and execute the prepared update statement.
protected  ValueObject extract(java.sql.ResultSet results)
          Extract an object from the given result set.
protected  ValueObject extract(java.sql.ResultSet results, ValueObject vo)
          Populate a given object using the given result set.
protected  ValueObjectList extractList(java.sql.ResultSet results)
          Extract a list of objects from a given result set.
protected  ValueObject extractSingle(java.sql.ResultSet results)
          Extract an object from the given result set.
 ValueObjectList findAll()
          Finds all objects managed in this datastore.
protected  java.lang.String generateDeleteSQL(java.lang.String whereClause)
          Generate an SQL delete statement.
protected  java.lang.String generateInsertSQL()
          Generate an SQL insert statement.
protected  java.lang.String generateSelectSQL(java.lang.String whereClause)
          Generate an SQL select statement.
protected  java.lang.String generateUpdateSQL(java.lang.String whereClause)
          Generate an SQL update statement.
protected  java.util.List getArgs(ValueObject vo)
          From the given object, get a list of arguments to use for constructing a query or update.
 java.sql.Connection getConnection()
          Get the current connection.
 Transaction getTransaction()
          Get the current transaction.
 boolean insert(ValueObject vo)
          Store an object.
 boolean insert(ValueObjectList vos)
          Store a list of objects.
protected  void populateStatement(java.util.List args)
          Populate the current prepared statement, using given args.
 boolean remove(java.lang.String id)
          Remove a stored object with given id.
 boolean remove(ValueObject vo)
          Remove a stored object.
 boolean remove(ValueObjectList vos)
          Remove a list of stored objects.
protected  java.sql.ResultSet selectAll()
          Finds all objects for current table.
protected  java.sql.ResultSet selectById(java.lang.String id)
          Finds a particular object by its id in the current table.
protected  java.sql.ResultSet selectWhere(java.lang.String whereClause, java.util.List args)
          Finds all objects in the current table matching a given set of criteria.
 boolean update(ValueObject vo)
          Updates a given object that has been stored previously.
 boolean update(ValueObjectList vos)
          Updates a list of objects that have been stored previously.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Datastore

public Datastore(java.lang.String tableName,
                 java.lang.String addTableFields)
Constructor. Note that this datastore assumes that fields "id" and "name" exist in the table. You can specify additional fields using the second argument to this constructor.

Parameters:
tableName - The table being serviced by this datastore.
addTableFields - A comma delimited string of additional fields.
Method Detail

getConnection

public java.sql.Connection getConnection()
                                  throws DatastoreException
Get the current connection. Obtains a new one from the DAO if there is no current connection or the current connection is closed.

Returns:
The current connection.
Throws:
DatastoreException

getTransaction

public Transaction getTransaction()
                           throws DatastoreException
Get the current transaction. If there is no current transaction, a new one is created using the current connection. If there is a current transaction, the number of users of that transaction is incremented.

Returns:
The current transaction.
Throws:
DatastoreException

findAll

public ValueObjectList findAll()
                        throws DatastoreException
Finds all objects managed in this datastore.

Returns:
A List containing the objects.
Throws:
DatastoreException

insert

public boolean insert(ValueObject vo)
               throws DatastoreException
Store an object. This should only be used for objects that have not been stored before.

Parameters:
vo - The object to be stored.
Returns:
true if insert was successful.
Throws:
DatastoreException

insert

public boolean insert(ValueObjectList vos)
               throws DatastoreException
Store a list of objects. This should only be used for objects that have not been stored before. This method is more efficient than calling insert() multiple times.

Parameters:
vos - A list of objects.
Returns:
true if objects were inserted successfully
Throws:
DatastoreException

update

public boolean update(ValueObject vo)
               throws DatastoreException
Updates a given object that has been stored previously.

Parameters:
vo - The object that has been updated.
Returns:
true if object is found and updated.
Throws:
DatastoreException

update

public boolean update(ValueObjectList vos)
               throws DatastoreException
Updates a list of objects that have been stored previously. More efficient than calling update() multiple times.

Parameters:
vos - A list of updated value objects.
Returns:
true if update was successful.
Throws:
DatastoreException

remove

public boolean remove(ValueObject vo)
               throws DatastoreException
Remove a stored object.

Parameters:
vo - Object to be removed.
Returns:
true if a matching object is found and removed
Throws:
DatastoreException

remove

public boolean remove(java.lang.String id)
               throws DatastoreException
Remove a stored object with given id.

Parameters:
id - Id of object to be removed.
Returns:
true if a matching object is found and removed
Throws:
DatastoreException

remove

public boolean remove(ValueObjectList vos)
               throws DatastoreException
Remove a list of stored objects. More efficient than calling remove() multiple times.

Parameters:
vos - A list of objects to be removed.
Returns:
true if remove was successful.
Throws:
DatastoreException

selectAll

protected java.sql.ResultSet selectAll()
                                throws DatastoreException
Finds all objects for current table.

Returns:
A result set containing the results of the query.
Throws:
DatastoreException

selectById

protected java.sql.ResultSet selectById(java.lang.String id)
                                 throws DatastoreException
Finds a particular object by its id in the current table.

Parameters:
id - the id of the object to find.
Returns:
A result set containing the results of the query.
Throws:
DatastoreException

selectWhere

protected java.sql.ResultSet selectWhere(java.lang.String whereClause,
                                         java.util.List args)
                                  throws DatastoreException
Finds all objects in the current table matching a given set of criteria.

Parameters:
whereClause - The SQL where clause to use.
args - Array of Integers or Strings, with which to populate the where clause.
Returns:
A result set containing the results of the query.
Throws:
DatastoreException

executeInsert

protected boolean executeInsert(ValueObject vo)
                         throws DatastoreException
Populate and execute the prepared insert statement.

Parameters:
vo - The value object to use to populate the insert statement.
Returns:
true if insert was successful.
Throws:
java.sql.SQLException
DatastoreException

executeUpdate

protected boolean executeUpdate(ValueObject vo)
                         throws DatastoreException
Populate and execute the prepared update statement.

Parameters:
vo - The value object to use to populate the update statement.
Returns:
true if update was successful.
Throws:
java.sql.SQLException
DatastoreException

executeDelete

protected boolean executeDelete(java.lang.String id)
                         throws DatastoreException
Populate and execute the prepared delete statement.

Parameters:
id - The id to use in populating the statement.
Returns:
true if update was successful.
Throws:
java.sql.SQLException
DatastoreException

generateSelectSQL

protected java.lang.String generateSelectSQL(java.lang.String whereClause)
Generate an SQL select statement.

Parameters:
whereClause - Optional where clause to add to statement.
Returns:
The SQL statement.

generateInsertSQL

protected java.lang.String generateInsertSQL()
Generate an SQL insert statement.

Returns:
The SQL statement.

generateUpdateSQL

protected java.lang.String generateUpdateSQL(java.lang.String whereClause)
Generate an SQL update statement.

Parameters:
whereClause - Optional where clause to add to statement.
Returns:
The SQL statement.

generateDeleteSQL

protected java.lang.String generateDeleteSQL(java.lang.String whereClause)
Generate an SQL delete statement.

Parameters:
whereClause - Optional where clause to add to statement.
Returns:
The SQL statement.

populateStatement

protected void populateStatement(java.util.List args)
                          throws DatastoreException
Populate the current prepared statement, using given args.

Parameters:
args - An array of Integers and Strings
Throws:
java.sql.SQLException
DatastoreException

extractList

protected ValueObjectList extractList(java.sql.ResultSet results)
                               throws DatastoreException
Extract a list of objects from a given result set.

Parameters:
results - The result set.
Throws:
DatastoreException

extractSingle

protected ValueObject extractSingle(java.sql.ResultSet results)
                             throws DatastoreException
Extract an object from the given result set.

Parameters:
results - The result set.
Returns:
The newly extracted object.
Throws:
DatastoreException

extract

protected ValueObject extract(java.sql.ResultSet results,
                              ValueObject vo)
                       throws DatastoreException
Populate a given object using the given result set.

Parameters:
results - The result set.
vo - An object to populate.
Returns:
The populated object.
Throws:
DatastoreException

getArgs

protected java.util.List getArgs(ValueObject vo)
From the given object, get a list of arguments to use for constructing a query or update. When subclassing, this method should be overridden and called via super.getArgs(), and the resulting list should be added to.

Parameters:
vo - Object containing data to use.
Returns:
A list of arguments.

extract

protected ValueObject extract(java.sql.ResultSet results)
                       throws DatastoreException
Extract an object from the given result set. When subclassing, this method should be overridden and called via super.extract(results,foo), where foo is a new object of the type being served by the subclass.

Parameters:
results - The result set.
Returns:
The newly extracted object.
Throws:
DatastoreException