com.sun.jsfcl.data
Class RowSetDataModel

java.lang.Object
  extended byjavax.faces.model.DataModel
      extended bycom.sun.jsfcl.data.RowSetDataModel

public class RowSetDataModel
extends javax.faces.model.DataModel

Runtime implementation of javax.faces.model.DataModel that caches retrieved database data in memory (in a session scope attribute of type DataCache), even when the underlying rowset is closed.

Author:
craigmcc

Constructor Summary
RowSetDataModel()
          Create a new RowSetDataModel instance not connected to any underlying RowSet.
RowSetDataModel(javax.sql.RowSet rowSet)
          Create a new RowSetDataModel instance wrapping the specified RowSet.
 
Method Summary
 void clear()
          Clear any cached row-specific data.
 void commit()
          Push any deleted or updated cached rows to the specified table, then call commit() on the JDBC Connection underlying our current RowSet, as well as our associated DataCache.
 void execute()
          Clear any cached data, then re-execute the query for the rowset we are connected to.
 DataCache getDataCache()
          Return the DataCache containing our cached row and column data, creating one if necessary.
 java.lang.String getDataCacheKey()
          Return the session attribute key under which our DataCache instance will be stored.
 int getRowCount()
          Return -1 to indicate that the number of rows available is unknown.
 java.lang.Object getRowData()
          Return a Map representing the column values for the row specified by the current rowIndex.
 int getRowIndex()
          Return the zero-relative index of the currently positioned row, or -1 if we are not positioned on a row.
 javax.sql.RowSet getRowSet()
          Return the RowSet we are connected with, if any; otherwise, return null.
 java.lang.String getSchemaName()
          Return the name of the database schema containing the table we will update when commit() is called.
 java.lang.String getTableName()
          Return the name of the database table we will update when commit() is called.
 java.lang.Object getWrappedData()
          Return the RowSet we are currently wrapping, if any.
 boolean isRowAvailable()
          Return true if there is a cache entry for the current rowIndex value.
 void reset()
          Reset any deleted or updated values in the cache, so that any cached rows no longer appear to have been modified.
 void rollback()
          Reset any deleted or updated values in the cache (as is done by the reset() method), then call rollback() on the JDBC Connectino underlying our current RowSet.
 void setDataCacheKey(java.lang.String dataCacheKey)
          Set the session attribute key under which our DataCache instance will be stored.
 void setObject(int index, java.lang.Object value)
          Set the designated parameter on the RowSet to which we are connected.
 void setObject(int index, java.lang.Object value, int type)
          Set the designated parameter on the RowSet to which we are connected.
 void setObject(int index, java.lang.Object value, int type, int scale)
          Set the designated parameter on the RowSet to which we are connected.
 void setRowIndex(int rowIndex)
          Set the zero relative index for the newly positioned row, or set to -1 for no currently selected row.
 void setRowSet(javax.sql.RowSet rowSet)
          Set the RowSet we are connected with, or pass null to disconnect.
 void setSchemaName(java.lang.String schemaName)
          Set the name of the database schema containing the table we will update when commit() is called.
 void setTableName(java.lang.String tableName)
          Set the name of the database table we will update when commit() is called.
 void setWrappedData(java.lang.Object rowSet)
          Set the RowSet wrapped by this RowSetDataModel, or null to disconnect from the previously connected RowSet.
 
Methods inherited from class javax.faces.model.DataModel
addDataModelListener, getDataModelListeners, removeDataModelListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RowSetDataModel

public RowSetDataModel()

Create a new RowSetDataModel instance not connected to any underlying RowSet.


RowSetDataModel

public RowSetDataModel(javax.sql.RowSet rowSet)

Create a new RowSetDataModel instance wrapping the specified RowSet.

Parameters:
rowSet - RowSet to be mapped
Method Detail

getDataCache

public DataCache getDataCache()

Return the DataCache containing our cached row and column data, creating one if necessary.


getDataCacheKey

public java.lang.String getDataCacheKey()

Return the session attribute key under which our DataCache instance will be stored.


setDataCacheKey

public void setDataCacheKey(java.lang.String dataCacheKey)

Set the session attribute key under which our DataCache instance will be stored.

Parameters:
dataCacheKey - The new key

getRowSet

public javax.sql.RowSet getRowSet()

Return the RowSet we are connected with, if any; otherwise, return null. This is a type=safe alias for getWrappedData().


setRowSet

public void setRowSet(javax.sql.RowSet rowSet)

Set the RowSet we are connected with, or pass null to disconnect. This is a type-safe alias for setWrappedData().

Parameters:
rowSet - The RowSet we are connected to, or null to disconnect

getSchemaName

public java.lang.String getSchemaName()

Return the name of the database schema containing the table we will update when commit() is called.


setSchemaName

public void setSchemaName(java.lang.String schemaName)

Set the name of the database schema containing the table we will update when commit() is called.

Parameters:
schemaName - The schema name to be updated

getTableName

public java.lang.String getTableName()

Return the name of the database table we will update when commit() is called.


setTableName

public void setTableName(java.lang.String tableName)

Set the name of the database table we will update when commit() is called.

Parameters:
tableName - The table name to be updated

clear

public void clear()

Clear any cached row-specific data. This method may be called by application logic when it is known that the next page to be processed will not involve the RowSet we are connected to, or when the application wants to refresh the cached data to reflect any changes on the underlying database contents.

NOTE - Calling execute() on this RowSetDataModel will implicitly call clear() for you.


commit

public void commit()
            throws java.sql.SQLException

Push any deleted or updated cached rows to the specified table, then call commit() on the JDBC Connection underlying our current RowSet, as well as our associated DataCache. If any SQLException occurs, call rollback() on the Connection() and reset() on the DataCache.

Throws:
java.lang.IllegalArgumentException - if the tableName property has not yet been set
java.lang.IllegalStateException - if this method is called when not connected to an underlying rowset
java.sql.SQLException - if an error occurs while committing

execute

public void execute()
             throws java.sql.SQLException

Clear any cached data, then re-execute the query for the rowset we are connected to. WARNING - this method should ONLY be called when you have changed the query parameters for your query, or your application has performed other database transaction(s) on a different page that should be reflected in the query results.

Throws:
java.lang.IllegalStateException - if this method is called when not connected to an underlying rowset
java.sql.SQLException - if an error occurs executing the rowset

reset

public void reset()

Reset any deleted or updated values in the cache, so that any cached rows no longer appear to have been modified. This method has no effect on the connected rowset (if any).


rollback

public void rollback()
              throws java.sql.SQLException

Reset any deleted or updated values in the cache (as is done by the reset() method), then call rollback() on the JDBC Connectino underlying our current RowSet.

Throws:
java.lang.IllegalStateException - if this method is called when not connected to an underlying rowset
java.sql.SQLException - if an error occurs while committing

setObject

public void setObject(int index,
                      java.lang.Object value)
               throws java.sql.SQLException

Set the designated parameter on the RowSet to which we are connected. This parameter will have no effect on any currently selected rows; it takes effect the next time you call execute().

Parameters:
index - One-relative parameter index
value - Value to be set
Throws:
java.lang.IllegalStateException - if this method is called when not connected to an underlying rowset
java.sql.SQLException - if an error occurs setting the parameter value

setObject

public void setObject(int index,
                      java.lang.Object value,
                      int type)
               throws java.sql.SQLException

Set the designated parameter on the RowSet to which we are connected. This parameter will have no effect on any currently selected rows; it takes effect the next time you call execute().

Parameters:
index - One-relative parameter index
value - Value to be set
type - Destination SQL type (as defined in java.sql.Types)
Throws:
java.lang.IllegalStateException - if this method is called when not connected to an underlying rowset
java.sql.SQLException - if an error occurs setting the parameter value

setObject

public void setObject(int index,
                      java.lang.Object value,
                      int type,
                      int scale)
               throws java.sql.SQLException

Set the designated parameter on the RowSet to which we are connected. This parameter will have no effect on any currently selected rows; it takes effect the next time you call execute().

Parameters:
index - One-relative parameter index
value - Value to be set
type - Destination SQL type (as defined in java.sql.Types)
scale - For java.sql.Types.DECIMAL or java.sql.Types.NUMERIC types, the number of digits after the decimal point (ignored for all other types)
Throws:
java.lang.IllegalStateException - if this method is called when not connected to an underlying rowset
java.sql.SQLException - if an error occurs setting the parameter value

getRowCount

public int getRowCount()

Return -1 to indicate that the number of rows available is unknown.


getRowData

public java.lang.Object getRowData()

Return a Map representing the column values for the row specified by the current rowIndex. The returned Map supports case-insensitive matching on column names, and records any updates to the column values for later transfer to the database when commit() is called. It does not allow column names (and corresponding values) to be added or removed.

Throws:
java.lang.IllegalArgumentException - if there is no cached data for the current rowIndex

getRowIndex

public int getRowIndex()

Return the zero-relative index of the currently positioned row, or -1 if we are not positioned on a row.


getWrappedData

public java.lang.Object getWrappedData()

Return the RowSet we are currently wrapping, if any.


isRowAvailable

public boolean isRowAvailable()

Return true if there is a cache entry for the current rowIndex value.


setRowIndex

public void setRowIndex(int rowIndex)

Set the zero relative index for the newly positioned row, or set to -1 for no currently selected row. If there is no current cache entry for this row, but we are currently connected, position the underlying RowSet to the corresponding one-relative row number, and create a new cache entry. In addition, fire a DataModelEvent if needed, per the Javadocs for this method on javax.faces.model.DataModel.

Parameters:
rowIndex - The row index, or -1 for no selected row
Throws:
javax.faces.FacesException - if an error occurs setting the row index
java.lang.IllegalArgumentException - if rowIndex is less than -1

setWrappedData

public void setWrappedData(java.lang.Object rowSet)

Set the RowSet wrapped by this RowSetDataModel, or null to disconnect from the previously connected RowSet.

Parameters:
rowSet - RowSet to be wrapped, or null to disconnect
Throws:
java.lang.ClassCastException - if this object is not of the correct type