com.revusky.niggle.data.jdbc
Class JDBCBackedRecordSet

java.lang.Object
  |
  +--com.revusky.niggle.data.AbstractDataSource
        |
        +--com.revusky.niggle.data.AbstractMutableDataSource
              |
              +--com.revusky.niggle.data.jdbc.JDBCBackedRecordSet
All Implemented Interfaces:
DataListener, DataSource, java.util.EventListener, MutableDataSource, java.rmi.Remote

public class JDBCBackedRecordSet
extends AbstractMutableDataSource
implements DataListener

A class that is essentially a facade in front of an external relational database table. This is just one of various possible implementations. This implementation allows for an in-memory cache of adjustable size. This container is probably highly efficient for a web app whose profile is that there are a lot of reads and relatively few writes to the external RDBMS. That is probably more the rule than the exception.

Author:
Jonathan Revusky

Constructor Summary
JDBCBackedRecordSet()
           
 
Method Summary
 void delete(Record rec)
          Delete a record given its primary lookup key.
 Record get(java.lang.String type, java.lang.Object key)
           
protected  JDBCConnection getConnection()
           
 void init(java.util.Properties props)
          TODO
 void insert(Record rec)
          Adds a new record to the managed pool.
 java.util.List keys(java.lang.String type)
           
 void recordDeleted(DataEvent event)
          invoked when the MutableDataSource whose events we are listening to has just deleted a record.
 void recordInserted(DataEvent event)
          invoked when the MutableDataSource whose events we are listening to has just inserted a new record.
 void recordUpdated(DataEvent event)
          invoked when the MutableDataSource whose events we are listening to has just updated an existing record.
 java.util.List select(java.lang.String type, RecordFilter filter)
          Fetches a list of records matching filter.
protected  java.lang.Object uniqueKey(Record rec)
           
 void update(Record oldRec, Record newRec)
          Replaces an existing version of a record with a new updated version.
 void wipeCache()
          If this data source is backed by some external mechanism, like an RDBMS, wipes the in-memory cache, if one is being maintained.
 
Methods inherited from class com.revusky.niggle.data.AbstractMutableDataSource
addDataListener, close, fireDataEvent, freeze, getRecords, markStale, normalizeKey, removeDataListener, select
 
Methods inherited from class com.revusky.niggle.data.AbstractDataSource
get, getName, keys
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.revusky.niggle.data.DataSource
get, getName, keys
 

Constructor Detail

JDBCBackedRecordSet

public JDBCBackedRecordSet()
                    throws java.io.IOException
Method Detail

recordDeleted

public void recordDeleted(DataEvent event)
                   throws java.io.IOException
Description copied from interface: DataListener
invoked when the MutableDataSource whose events we are listening to has just deleted a record.
Specified by:
recordDeleted in interface DataListener

recordInserted

public void recordInserted(DataEvent event)
                    throws java.io.IOException
Description copied from interface: DataListener
invoked when the MutableDataSource whose events we are listening to has just inserted a new record.
Specified by:
recordInserted in interface DataListener

recordUpdated

public void recordUpdated(DataEvent event)
                   throws java.io.IOException
Description copied from interface: DataListener
invoked when the MutableDataSource whose events we are listening to has just updated an existing record.
Specified by:
recordUpdated in interface DataListener

init

public void init(java.util.Properties props)
          throws java.io.IOException
Description copied from class: AbstractMutableDataSource
TODO
Overrides:
init in class AbstractMutableDataSource

delete

public void delete(Record rec)
            throws java.io.IOException
Description copied from interface: MutableDataSource
Delete a record given its primary lookup key.
Following copied from interface: com.revusky.niggle.data.MutableDataSource
Parameters:
the - record to delete.

insert

public void insert(Record rec)
            throws java.io.IOException
Description copied from interface: MutableDataSource
Adds a new record to the managed pool.
Following copied from interface: com.revusky.niggle.data.MutableDataSource
Parameters:
rec - the record to add
Throws:
DuplicateRecordException - if another record already exists with same primary key as 'rec'.
java.io.IOException - if the record cannot be initialized (i.e. has missing fields or invalid field values, etc.), or in case of a low-level error.

update

public void update(Record oldRec,
                   Record newRec)
            throws java.io.IOException
Description copied from interface: MutableDataSource
Replaces an existing version of a record with a new updated version. Automatically replaces the previous version if it has not been modified since the object was taken from the data source. A ModifiedRecordException is thrown otherwise.

Not all underlying storage mediums will be able to support these semantics. Check your implementation carefully.

Following copied from interface: com.revusky.niggle.data.MutableDataSource
Parameters:
oldRec - the record to replace.
newRec - the new record.
Throws:
ModifiedRecordException - thrown if the record was modified by another process since rec was requested from a data source.
java.io.IOException - thrown in case of any other database or communication error.

keys

public java.util.List keys(java.lang.String type)
                    throws java.io.IOException
Following copied from interface: com.revusky.niggle.data.DataSource
Parameters:
type - the record type we are interested, under some circumstances, this may be null.
Returns:
a list of all valid lookup keys that correspond to a given record type.

get

public Record get(java.lang.String type,
                  java.lang.Object key)
           throws java.io.IOException
Following copied from interface: com.revusky.niggle.data.DataSource
Parameters:
type - the type of the record, if this is null, then any type will do.
key - the lookup key
Returns:
a Record of the given type, with the given lookup key. If the data source allows multiple records, only returns the first one found. Use getRecords() in that situation. returns null if none is found.

wipeCache

public void wipeCache()
Description copied from interface: MutableDataSource
If this data source is backed by some external mechanism, like an RDBMS, wipes the in-memory cache, if one is being maintained. Otherwise, it does nothing.
Overrides:
wipeCache in class AbstractMutableDataSource

select

public java.util.List select(java.lang.String type,
                             RecordFilter filter)
                      throws java.io.IOException
Description copied from interface: MutableDataSource
Fetches a list of records matching filter. If filter is null, returns all the records of the given type in the container.
Following copied from interface: com.revusky.niggle.data.MutableDataSource
Parameters:
the - type of record we are interested in, possibly null
filter - the record filter, or null.
Returns:
a List of all the records that are an instance of a given class. If no records match the filter, an empty List.

getConnection

protected JDBCConnection getConnection()

uniqueKey

protected java.lang.Object uniqueKey(Record rec)
                              throws java.io.IOException