repast.simphony.freezedry.datasource
Class JDBCDataSource

java.lang.Object
  extended by repast.simphony.freezedry.AbstractDataSource<ResultSet>
      extended by repast.simphony.freezedry.datasource.JDBCDataSource
All Implemented Interfaces:
FreezeDryedDataSource

public class JDBCDataSource
extends AbstractDataSource<ResultSet>


Field Summary
static String CLASS_NAME_COL
           
protected  Map<String,String> classIdMap
           
static String DEFAULT_CLASS_MAPPING_TABLE
           
protected  boolean storeLogin
           
 
Fields inherited from class repast.simphony.freezedry.AbstractDataSource
ARRAY_MARKER, BYTES_MARKER, CHILD_CLASS_COL, CHILD_ID_COL, CHILD_ID_MARKER, CHILDREN_MARKER, CLASS_MARKER, ID_COL, PARENT_ID_COL, SPECIAL_COL_MARKER
 
Constructor Summary
JDBCDataSource(DBConnectionInfo info)
           
JDBCDataSource(DBConnectionInfo info, boolean storeLogin)
           
JDBCDataSource(String connectionString, String driverClassName, String userName, String password)
           
 
Method Summary
protected  String cleanColName(String name)
           
protected  void closeChildQuery(Class clazz)
           
protected  void closeQuery(Class clazz)
           
protected  void createChildTableIfNeeded(Connection con, FreezeDryedObject fdo)
           
protected  void createTableIfNeeded(Connection con, FreezeDryedObject fdo)
           
protected  void endTypeRead(Class clazz)
          Finishes the reading of the specified class.
protected  void executeCreate(Connection con, String creationSQL)
           
protected  void finishChildrenRead(FreezeDryedObject fdo)
          Ends the reading of the children for the specified FreezeDryedObject.
protected  void finishChildrenWrite(FreezeDryedObject fdo)
          Finishes the writing of the children for the specified FreezeDryedObject.
protected  void finishTypeWrite(FreezeDryedObject fdo)
          Finishes the writing of the specified FreezeDryedObject.
protected  String generateId(Class<?> clazz)
           
protected  String generateId(String className)
           
protected  String getArrayType(String field)
           
protected  Connection getChildConnection(Class clazz, boolean canCreate)
           
protected  Connection getChildConnection(FreezeDryedObject fdo, boolean canCreate)
           
protected  String getChildrenColumnName(FreezeDryedObject fdo)
           
protected  String getChildTableName(FreezeDryedObject fdo)
           
protected  String getClassMapping(String className)
           
protected  String getColumnType(FreezeDryedObject fdo, String colName)
           
protected  Connection getConnection()
           
protected  Connection getConnection(Class clazz, boolean canCreate)
           
protected  Connection getConnection(FreezeDryedObject fdo, boolean canCreate)
           
 String getDBURL()
           
 String getDriverName()
           
protected  List<String> getFields(String type)
          Retrieves the fields for a specified type.
protected  Object getFieldValue(ResultSet row, String field)
          Retrieves the value of the specified field in the specified row of data.
 String getPassword()
           
 boolean getStoreLogin()
           
protected  String getTableName(FreezeDryedObject fdo)
           
protected  String getTableName(String type)
           
protected  String getType(Class clazz)
           
protected  String getType(String classType)
           
 String getUserName()
           
protected  String getValue(String value)
           
protected  String queryDBForType(String classType)
           
protected  byte[] readByteArray(FreezeDryedObject fdo, String field, Object bytesId)
          Reads in a byte[] that corresponds to the specified bytesId and will be loaded into the specified fdo through the specified field.
protected  ResultSet readChildsRow(FreezeDryedObject parent, String id)
          Reads in a row of data about a child of the specified FreezeDryedObject.
protected  ResultSet readRow(Class clazz, String key)
          Reads in a row of data that will be parsed by the use of #getFieldValue(X, String) and AbstractDataSource.getFields(String).

If the data source can find an object with the specified key it should return data for reading that object, otherwise it may return any object.

 void setStoreLogin(boolean storeLogin)
           
protected  void startChildrenRead(FreezeDryedObject fdo)
          Initializes the reading of the children of a specified FreezeDryedObject.
protected  void startChildrenWrite(FreezeDryedObject fdo)
          Initiates the writing of the children for the specified FreezeDryedObject.
protected  void startTypeRead(Class clazz)
          Initiates the reading of the specified class.
protected  void startTypeWrite(FreezeDryedObject fdo)
          This is called at the start of a call to write.
protected  void storeClassMapping(String clazz, String id)
           
protected  boolean validateConnectionSettings()
           
protected  Object writeByteArray(FreezeDryedObject object, String fieldName, byte[] bs)
          Writes a byte array retrieved from the specified FreezeDryedObject with the specified fieldName.
protected  void writeChildData(FreezeDryedObject fdo, HashMap<String,Object> child)
          Writes the data for a child of the specified FreezeDryedObject.
protected  void writeRow(FreezeDryedObject fdo, Map<String,Object> row)
          This writes a row of data out which was generated by the specified FreezeDryedObject.
 
Methods inherited from class repast.simphony.freezedry.AbstractDataSource
close, getArrayValue, getColumnKeysetPairs, getColumns, getTrueFieldName, getType, getValue, getValue, getValueArray, pair, parseChildRow, parseRow, read, read, readChildren, readFreezeDryedParentChild, reset, write, writeChildren
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CLASS_MAPPING_TABLE

public static final String DEFAULT_CLASS_MAPPING_TABLE
See Also:
Constant Field Values

CLASS_NAME_COL

public static final String CLASS_NAME_COL
See Also:
Constant Field Values

classIdMap

protected transient Map<String,String> classIdMap

storeLogin

protected boolean storeLogin
Constructor Detail

JDBCDataSource

public JDBCDataSource(String connectionString,
                      String driverClassName,
                      String userName,
                      String password)

JDBCDataSource

public JDBCDataSource(DBConnectionInfo info)

JDBCDataSource

public JDBCDataSource(DBConnectionInfo info,
                      boolean storeLogin)
Method Detail

getValue

protected String getValue(String value)
Overrides:
getValue in class AbstractDataSource<ResultSet>

getConnection

protected Connection getConnection()
                            throws SQLException
Throws:
SQLException

getConnection

protected Connection getConnection(FreezeDryedObject fdo,
                                   boolean canCreate)
                            throws SQLException
Throws:
SQLException

getConnection

protected Connection getConnection(Class clazz,
                                   boolean canCreate)
                            throws SQLException
Throws:
SQLException

getChildConnection

protected Connection getChildConnection(FreezeDryedObject fdo,
                                        boolean canCreate)
                                 throws SQLException
Throws:
SQLException

getChildConnection

protected Connection getChildConnection(Class clazz,
                                        boolean canCreate)
                                 throws SQLException
Throws:
SQLException

startTypeWrite

protected void startTypeWrite(FreezeDryedObject fdo)
                       throws FreezeDryingException
Description copied from class: AbstractDataSource
This is called at the start of a call to write. This should cause any needed preparation for reading this type to occur. It will be followed by calls to AbstractDataSource.writeRow(FreezeDryedObject, Map) and a call to AbstractDataSource.finishTypeWrite(FreezeDryedObject).

Specified by:
startTypeWrite in class AbstractDataSource<ResultSet>
Parameters:
fdo - the FreezeDryedObject who will begin to be written
Throws:
FreezeDryingException - when there's an error

finishTypeWrite

protected void finishTypeWrite(FreezeDryedObject fdo)
                        throws FreezeDryingException
Description copied from class: AbstractDataSource
Finishes the writing of the specified FreezeDryedObject. This will be preceded by a #startTypeWrite(FreezeDryedObject) call and calls to {@link #writeRow(FreezeDryedObject, Map)} (along with the other child and byte methods).

Specified by:
finishTypeWrite in class AbstractDataSource<ResultSet>
Throws:
FreezeDryingException - when there's an error

writeRow

protected void writeRow(FreezeDryedObject fdo,
                        Map<String,Object> row)
                 throws FreezeDryingException
Description copied from class: AbstractDataSource
This writes a row of data out which was generated by the specified FreezeDryedObject. This will be preceded by a #startTypeWrite(FreezeDryedObject) call and will be followed by calls to {@link #writeRow(FreezeDryedObject, Map)} and a call to {@link #finishTypeWrite(FreezeDryedObject)}.

Specified by:
writeRow in class AbstractDataSource<ResultSet>
row - the data for the specified FreezeDryedObject
Throws:
FreezeDryingException - when there's an error

getTableName

protected String getTableName(String type)

getTableName

protected String getTableName(FreezeDryedObject fdo)

getChildTableName

protected String getChildTableName(FreezeDryedObject fdo)

cleanColName

protected String cleanColName(String name)

createTableIfNeeded

protected void createTableIfNeeded(Connection con,
                                   FreezeDryedObject fdo)
                            throws SQLException
Throws:
SQLException

createChildTableIfNeeded

protected void createChildTableIfNeeded(Connection con,
                                        FreezeDryedObject fdo)
                                 throws SQLException
Throws:
SQLException

getColumnType

protected String getColumnType(FreezeDryedObject fdo,
                               String colName)

executeCreate

protected void executeCreate(Connection con,
                             String creationSQL)
                      throws SQLException
Throws:
SQLException

startChildrenWrite

protected void startChildrenWrite(FreezeDryedObject fdo)
                           throws FreezeDryingException
Description copied from class: AbstractDataSource
Initiates the writing of the children for the specified FreezeDryedObject.

Specified by:
startChildrenWrite in class AbstractDataSource<ResultSet>
Parameters:
fdo - the {@link FreezeDryedObject whose children will be written
Throws:
FreezeDryingException

finishChildrenWrite

protected void finishChildrenWrite(FreezeDryedObject fdo)
                            throws FreezeDryingException
Description copied from class: AbstractDataSource
Finishes the writing of the children for the specified FreezeDryedObject.

Specified by:
finishChildrenWrite in class AbstractDataSource<ResultSet>
Throws:
FreezeDryingException

getType

protected String getType(Class clazz)
Overrides:
getType in class AbstractDataSource<ResultSet>

queryDBForType

protected String queryDBForType(String classType)
                         throws SQLException
Throws:
SQLException

getType

protected String getType(String classType)

generateId

protected String generateId(Class<?> clazz)

generateId

protected String generateId(String className)

getClassMapping

protected String getClassMapping(String className)
                          throws SQLException
Throws:
SQLException

storeClassMapping

protected void storeClassMapping(String clazz,
                                 String id)
                          throws SQLException
Throws:
SQLException

writeChildData

protected void writeChildData(FreezeDryedObject fdo,
                              HashMap<String,Object> child)
                       throws FreezeDryingException
Description copied from class: AbstractDataSource
Writes the data for a child of the specified FreezeDryedObject.

Specified by:
writeChildData in class AbstractDataSource<ResultSet>
Parameters:
fdo - the parent
child - the data from the child
Throws:
FreezeDryingException

startTypeRead

protected void startTypeRead(Class clazz)
                      throws FreezeDryingException
Description copied from class: AbstractDataSource
Initiates the reading of the specified class. This class represents is of a type that was at some point written out through a FreezeDryedObject.

Specified by:
startTypeRead in class AbstractDataSource<ResultSet>
Parameters:
clazz - the class that was the type returned by FreezeDryedObject.getType()
Throws:
FreezeDryingException

validateConnectionSettings

protected boolean validateConnectionSettings()

endTypeRead

protected void endTypeRead(Class clazz)
Description copied from class: AbstractDataSource
Finishes the reading of the specified class.

Specified by:
endTypeRead in class AbstractDataSource<ResultSet>
Parameters:
clazz - the class whose reading has completed

closeQuery

protected void closeQuery(Class clazz)

closeChildQuery

protected void closeChildQuery(Class clazz)

readRow

protected ResultSet readRow(Class clazz,
                            String key)
                     throws FreezeDryingException
Description copied from class: AbstractDataSource
Reads in a row of data that will be parsed by the use of #getFieldValue(X, String) and AbstractDataSource.getFields(String).

If the data source can find an object with the specified key it should return data for reading that object, otherwise it may return any object.

Specified by:
readRow in class AbstractDataSource<ResultSet>
Parameters:
clazz - the class the data will be used for
key - the key of the object we're looking for, if null it should be ignored
Returns:
a row of data
Throws:
FreezeDryingException

readByteArray

protected byte[] readByteArray(FreezeDryedObject fdo,
                               String field,
                               Object bytesId)
                        throws FreezeDryingException
Description copied from class: AbstractDataSource
Reads in a byte[] that corresponds to the specified bytesId and will be loaded into the specified fdo through the specified field.

Specified by:
readByteArray in class AbstractDataSource<ResultSet>
Parameters:
fdo - the object that the bytes are going to be stored in
field - the field the bytes[] are for
Returns:
the read in byte[]
Throws:
FreezeDryingException

writeByteArray

protected Object writeByteArray(FreezeDryedObject object,
                                String fieldName,
                                byte[] bs)
                         throws FreezeDryingException
Description copied from class: AbstractDataSource
Writes a byte array retrieved from the specified FreezeDryedObject with the specified fieldName.

Specified by:
writeByteArray in class AbstractDataSource<ResultSet>
Parameters:
object - the source of the data
fieldName - the field this data is assosciated with
bs - the data itself
Returns:
the value to be written to the subclass for the fieldName (this could potentially just be an id, or it could just be the bytes themselves)
Throws:
FreezeDryingException

getArrayType

protected String getArrayType(String field)
Overrides:
getArrayType in class AbstractDataSource<ResultSet>

finishChildrenRead

protected void finishChildrenRead(FreezeDryedObject fdo)
Description copied from class: AbstractDataSource
Ends the reading of the children for the specified FreezeDryedObject.

Specified by:
finishChildrenRead in class AbstractDataSource<ResultSet>
Parameters:
fdo - the FreezeDryedObject whose children have been read

readChildsRow

protected ResultSet readChildsRow(FreezeDryedObject parent,
                                  String id)
                           throws FreezeDryingException
Description copied from class: AbstractDataSource
Reads in a row of data about a child of the specified FreezeDryedObject.

Specified by:
readChildsRow in class AbstractDataSource<ResultSet>
Parameters:
parent - the parent of the child that will be read
id - the id of the child we're concerned with
Returns:
a row of data on the child
Throws:
FreezeDryingException

startChildrenRead

protected void startChildrenRead(FreezeDryedObject fdo)
                          throws FreezeDryingException
Description copied from class: AbstractDataSource
Initializes the reading of the children of a specified FreezeDryedObject.

Specified by:
startChildrenRead in class AbstractDataSource<ResultSet>
Parameters:
fdo - the FreezeDryedObject whose children will be read
Throws:
FreezeDryingException

getFields

protected List<String> getFields(String type)
                          throws FreezeDryingException
Description copied from class: AbstractDataSource
Retrieves the fields for a specified type. Generally these should be read in and stored during the AbstractDataSource.startTypeRead(Class) call.

Specified by:
getFields in class AbstractDataSource<ResultSet>
Parameters:
type - the type of the FreezeDryedObject (from AbstractDataSource.getType(Class)).
Returns:
the list of fields of that type
Throws:
FreezeDryingException

getFieldValue

protected Object getFieldValue(ResultSet row,
                               String field)
                        throws FreezeDryingException
Description copied from class: AbstractDataSource
Retrieves the value of the specified field in the specified row of data.

Specified by:
getFieldValue in class AbstractDataSource<ResultSet>
Parameters:
row - the row (returned by #readRow(Class)).
field - the field we're reading
Returns:
the value of that field
Throws:
FreezeDryingException

getChildrenColumnName

protected String getChildrenColumnName(FreezeDryedObject fdo)
Overrides:
getChildrenColumnName in class AbstractDataSource<ResultSet>

getUserName

public String getUserName()

getPassword

public String getPassword()

getDBURL

public String getDBURL()

getDriverName

public String getDriverName()

getStoreLogin

public boolean getStoreLogin()

setStoreLogin

public void setStoreLogin(boolean storeLogin)