edu.hussam.database.sql
Class DatabaseRecordHome

java.lang.Object
extended by edu.hussam.database.sql.DatabaseRecordHome
All Implemented Interfaces:
RecordHome

public class DatabaseRecordHome
extends java.lang.Object
implements RecordHome

This resembles a Database Table. you can insert, remove, update and serach for records. You can extend this class to add more functionality. To select or try to find any record, make note that this class does not read null entries when trying to find. dont use the find method if you have null entries.!!

You can benefit from this class easily. Extend this class
 and override the create method to return a subclass of record, that has 
 a your business functionalty. 
 For example you can create a CustomerHome class that resembles a Customer 
 Table in the database. and you can create a Customer class that extends Record
 that can be used to access the data and implement business logic.
 
This object has its own Record Pool. So dont worry, you wont have duplicate Records generated from the same pool.


Constructor Summary
DatabaseRecordHome(ConnectionPool pool, java.lang.String tableName)
           
DatabaseRecordHome(ConnectionPool pool, java.lang.String tableName, java.lang.String primaryKeyName)
           
DatabaseRecordHome(TablesDataSource source, ConnectionPool pool, java.lang.String tableName)
           
DatabaseRecordHome(TablesDataSource source, ConnectionPool pool, java.lang.String tableName, java.lang.String primaryKeyName)
           
 
Method Summary
protected  voidaddRecordToMemory(Record record)
          adds this record to the memory of record pool.
 voidaddRelation(java.lang.String foreignKey, RecordHome home)
          adds a relation between this RecordHome and another recordhome using an entryname in this recordhome.
 Recordbuild(java.sql.ResultSet result)
          Builds one record from this Result set.
 java.util.List<? extends Record>buildAll(java.sql.ResultSet result)
          Returns all records in this result set.
 Recordcreate(java.util.Map<java.lang.String,java.lang.Object> entries)
          This is a method that creates a sepcific Record from map of entries.
 voidcreateTable()
          Creates the table if it was not created.
 java.util.List<? extends Record>find(java.util.Map<java.lang.String,java.lang.Object> entries)
          Finds the records that meets the given conditions.
 java.lang.ObjectgenerateKey()
          Generates an unused key.
 java.util.List<java.lang.String>getColumnNames()
          Returns a List of String representing the column names of the database row.
 java.util.Map<java.lang.String,RecordHome>getForeignKeys()
          Returns a map with String Keys and RecordHome values.
 java.lang.StringgetName()
          Returns the name of this Factory or Home.
static java.lang.StringgetPrimaryKeyForTable(java.sql.Connection c, java.lang.String table)
           
 java.lang.StringgetPrimaryKeyName()
          Returns the name of the primary key.
 java.util.Map<java.lang.String,java.lang.Object>getSearchOptions()
          returns a map representing serach options.
protected  booleanisInMemory(java.lang.Object key)
          Tests if this id is in memory.
 Recordload(java.lang.Object id)
          Loads a spicific Record from the database.
protected  RecordloadFromMemory(java.lang.Object key)
           
 voidremove(Record record)
          Removes this record from the RecordHome.
 voidsave(Record record)
          inserts a spcific Record into the database.
protected  voidtestForNull(Record r)
           
 java.lang.ObjecttoID(java.lang.Object o)
          If the object is a record, the method will return the ID of that record.
 java.lang.ObjecttoRecord(java.lang.String name, java.lang.Object o)
          If this object is a record, it will return the id of the record.
 java.lang.StringtoString()
           
 voidupdate(Record record)
          Updates this record into the database.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DatabaseRecordHome

public DatabaseRecordHome(TablesDataSource source,
ConnectionPool pool,
                          java.lang.String tableName,
                          java.lang.String primaryKeyName)
                   throws java.sql.SQLException
Parameters:
source -
pool -
tableName -
primaryKeyName -
Throws:
java.sql.SQLException

DatabaseRecordHome

public DatabaseRecordHome(TablesDataSource source,
ConnectionPool pool,
                          java.lang.String tableName)
                   throws java.sql.SQLException
Parameters:
source -
pool -
tableName -
Throws:
java.sql.SQLException

DatabaseRecordHome

public DatabaseRecordHome(ConnectionPool pool,
                          java.lang.String tableName,
                          java.lang.String primaryKeyName)
                   throws java.sql.SQLException
Parameters:
pool -
tableName -
primaryKeyName -
Throws:
java.sql.SQLException

DatabaseRecordHome

public DatabaseRecordHome(ConnectionPool pool,
                          java.lang.String tableName)
                   throws java.sql.SQLException
Parameters:
pool -
tableName -
Throws:
java.sql.SQLException
Method Detail

getPrimaryKeyForTable

public static java.lang.String getPrimaryKeyForTable(java.sql.Connection c,
                                                     java.lang.String table)
                                              throws java.sql.SQLException
Parameters:
c -
table -
Returns:
Throws:
java.sql.SQLException

getPrimaryKeyName

public java.lang.String getPrimaryKeyName()
Description copied from interface: RecordHome
Returns the name of the primary key.

Specified by:
getPrimaryKeyName in interface RecordHome
Returns:

addRelation

public void addRelation(java.lang.String foreignKey,
RecordHome home)
adds a relation between this RecordHome and another recordhome using an entryname in this recordhome. This is like a foreign key.

Parameters:
foreignKey -
home -

getForeignKeys

public java.util.Map<java.lang.String,RecordHome> getForeignKeys()
Description copied from interface: RecordHome
Returns a map with String Keys and RecordHome values. it is a map. This map represents the link between RecordHome or database tables.

Specified by:
getForeignKeys in interface RecordHome
Returns:

getName

public java.lang.String getName()
Description copied from interface: RecordHome
Returns the name of this Factory or Home.

Specified by:
getName in interface RecordHome
Returns:

createTable

public void createTable()
                 throws DatabaseException
Description copied from interface: RecordHome
Creates the table if it was not created.

Specified by:
createTable in interface RecordHome
Throws:
DatabaseException

getColumnNames

public java.util.List<java.lang.String> getColumnNames()
Description copied from interface: RecordHome
Returns a List of String representing the column names of the database row.

Specified by:
getColumnNames in interface RecordHome
Returns:

generateKey

public java.lang.Object generateKey()
                             throws DatabaseException
Description copied from interface: RecordHome
Generates an unused key.

Specified by:
generateKey in interface RecordHome
Returns:
Throws:
DatabaseException

testForNull

protected void testForNull(Record r)
                    throws DatabaseException
Parameters:
r -
Throws:
DatabaseException

save

public void save(Record record)
          throws DatabaseException
Description copied from interface: RecordHome
inserts a spcific Record into the database. this can be an SQL INSERT statement. This create a new Record.

Specified by:
save in interface RecordHome
Parameters:
record -
Throws:
DatabaseException

toRecord

public java.lang.Object toRecord(java.lang.String name,
                                 java.lang.Object o)
                          throws DatabaseException
If this object is a record, it will return the id of the record. otherwise returns the same object.

Specified by:
toRecord in interface RecordHome
Parameters:
name -
o -
Returns:
Throws:
DatabaseException

toID

public java.lang.Object toID(java.lang.Object o)
                      throws DatabaseException
Description copied from interface: RecordHome
If the object is a record, the method will return the ID of that record.

Specified by:
toID in interface RecordHome
Parameters:
o -
Returns:
Throws:
DatabaseException

load

public Record load(java.lang.Object id)
            throws DatabaseException
Description copied from interface: RecordHome
Loads a spicific Record from the database.

Specified by:
load in interface RecordHome
Parameters:
id -
Returns:
Throws:
DatabaseException

remove

public void remove(Record record)
            throws DatabaseException
Description copied from interface: RecordHome
Removes this record from the RecordHome.

Specified by:
remove in interface RecordHome
Parameters:
record -
Throws:
DatabaseException

build

public Record build(java.sql.ResultSet result)
             throws DatabaseException
Builds one record from this Result set. This function does not call next() of the result. You should have called next() before calling this method. You can use the buildAll method if you wanted to return all records.

Parameters:
result -
Returns:
Throws:
DatabaseException

buildAll

public java.util.List<? extends Record> buildAll(java.sql.ResultSet result)
                                          throws DatabaseException
Returns all records in this result set.

Parameters:
result -
Returns:
Throws:
DatabaseException

addRecordToMemory

protected void addRecordToMemory(Record record)
adds this record to the memory of record pool.

Parameters:
record -

isInMemory

protected boolean isInMemory(java.lang.Object key)
Tests if this id is in memory.

Parameters:
key -
Returns:

loadFromMemory

protected Record loadFromMemory(java.lang.Object key)

update

public void update(Record record)
            throws DatabaseException
Description copied from interface: RecordHome
Updates this record into the database. the record is already in the database, but it will be updated.

Specified by:
update in interface RecordHome
Parameters:
record -
Throws:
DatabaseException

getSearchOptions

public java.util.Map<java.lang.String,java.lang.Object> getSearchOptions()
Description copied from interface: RecordHome
returns a map representing serach options. The returned map is set to have (anything or %) condition. This can be used to add any condition like: map.put("name", "Hussam"); After that you can search for records using this condition: List list=home.find(map); The list will contian all records that have name=Hussam

Specified by:
getSearchOptions in interface RecordHome
Returns:

find

public java.util.List<? extends Record> find(java.util.Map<java.lang.String,java.lang.Object> entries)
                                      throws DatabaseException
Description copied from interface: RecordHome
Finds the records that meets the given conditions.

Specified by:
find in interface RecordHome
Parameters:
entries -
Returns:
Throws:
DatabaseException

create

public Record create(java.util.Map<java.lang.String,java.lang.Object> entries)
              throws DatabaseException
Description copied from interface: RecordHome
This is a method that creates a sepcific Record from map of entries. the creation does not mean it is storred in the database. The user can call the save method to have this record saved.

Specified by:
create in interface RecordHome
Parameters:
entries -
Returns:
Throws:
DatabaseException

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
Returns: