SPT Core API

com.sptci.echo2.table
Class DatabaseTableModel

java.lang.Object
  extended by nextapp.echo2.app.table.AbstractTableModel
      extended by com.sptci.echo2.table.AbstractTableModel<RowSet>
          extended by com.sptci.echo2.table.RowSetTableModel
              extended by com.sptci.echo2.table.DatabaseTableModel
All Implemented Interfaces:
PageableTableModel, SortableTableModel, Serializable, TableModel
Direct Known Subclasses:
StatementTableModel

public class DatabaseTableModel
extends RowSetTableModel
implements PageableTableModel, SortableTableModel

A custom TableModel used to represent data contained in in a database table.

Note: This class requires quite a lot of configuration prior to initialisation. Hence no convenient constructor's are provided. You must configure the model as appropriate and invoke the init() method. The following shows sample code for initialising this model:

   import com.sptci.echo2.table.DatabaseTableModel;
   import com.sptci.echo2.table.RowSetTable;
   import com.sptci.echo2.table.TableNavigation;

     ...
     List columns = new ArrayList();
     columns.add( "col1" );
     columns.add( "col2" );
     List order = new ArrayList();
     order.add( "col2 desc" );
     DatabaseTableModel model = new DatabaseTableModel();
     model.setDisplayColumns( columns );
     model.setSchema( null );
     model.setTable( "table1" );
     model.setDataSourceName( "java:/comp/env/jdbc/db" );
     model.setPrefix( "table1.headings" );
     model.setPageSize( 10 );
     model.setOrderBy( order );
     try
     {
       model.init();
     }
     catch ( Throwable t )
     {
       // Handle exception
     }
 

© Copyright 2007 Sans Pareil Technologies, Inc.

Version:
$Id: DatabaseTableModel.java 4553 2008-12-24 10:34:16Z rakesh $
Author:
Rakesh Vidyadharan 2007-09-19
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.sptci.echo2.table.SortableTableModel
SortableTableModel.Direction
 
Field Summary
protected  DataSource dataSource
          The DataSource to use to fetch data.
protected  String dataSourceName
          The JNDI name of the DataSource to use to fetch data.
protected  List<String> displayColumns
          The list of columns that are to be displayed in the model.
protected  boolean initialised
          A flag used to indicate whether the model has been initialised.
protected  List<String> orderBy
          The list of columns to order the table data by.
protected  int page
          The current page being displayed in the table.
protected  int pageSize
          The number of rows to display on one page.
protected  List<String> primaryKeys
          The list of primary key columns for the table.
protected  String schema
          The name of database schema in which the table exists.
protected  String table
          The name of the database table to represent in the model.
protected  int totalRows
          The total number of records available in the database.
 
Fields inherited from class com.sptci.echo2.table.RowSetTableModel
columns, prefix, rowCount, rowSet
 
Fields inherited from class com.sptci.echo2.table.AbstractTableModel
sortDirection, sortIndex
 
Constructor Summary
DatabaseTableModel()
          Default constructor.
 
Method Summary
protected  void fetchData()
          Fetch the data from table.
 String getDataSourceName()
          Returns dataSourceName.
 List<String> getDisplayColumns()
          Returns displayColumns.
 List<String> getOrderBy()
          Returns orderBy.
 int getPage()
          Returns page.
 int getPageSize()
          Returns pageSize.
 List<String> getPrimaryKeys()
          Returns primaryKeys list of primary key column names.
 String getSchema()
          Returns schema.
 String getTable()
          Returns table.
 int getTotalPages()
          Return the total number of pages that may be displayed by fetching records from table.
 void init()
          Intialise this table model with data from the table.
protected  void introspect()
          Introspect the table and determine its primary key columns.
protected  void processColumns()
          Process the RowSetMetaData associated with RowSetTableModel.rowSet and populate RowSetTableModel.columns.
 void setDataSourceName(String dataSourceName)
          Sets dataSourceName.
 void setDisplayColumns(List<String> columns)
          Sets displayColumns.
 void setOrderBy(List<String> order)
          Sets orderBy.
 void setPage(int page)
          Sets the value of page.
 void setPageSize(int pageSize)
          Sets the value of pageSize.
 void setSchema(String schema)
          Sets the schema in which the table represented by this model exists.
 void setTable(String table)
          Sets the name of the table to represent in this model.
 void sort(int column)
          Sort the data displayed in the table by the specified column.
 void sort(int column, SortableTableModel.Direction direction)
          Sort the data displayed in the table by the specified column.
 
Methods inherited from class com.sptci.echo2.table.RowSetTableModel
addRow, deleteRow, dispose, getColumnClass, getColumnCount, getColumnName, getObjectAt, getPrefix, getRowCount, getValueAt, setColumn, setPrefix, setRowCount, setRowCount, updateRow
 
Methods inherited from class com.sptci.echo2.table.AbstractTableModel
getSortDirection
 
Methods inherited from class nextapp.echo2.app.table.AbstractTableModel
addTableModelListener, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getEventListenerList, removeTableModelListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.sptci.echo2.table.SortableTableModel
getSortDirection
 

Field Detail

schema

protected String schema
The name of database schema in which the table exists.


table

protected String table
The name of the database table to represent in the model.


dataSourceName

protected String dataSourceName
The JNDI name of the DataSource to use to fetch data.


dataSource

protected DataSource dataSource
The DataSource to use to fetch data.


totalRows

protected int totalRows
The total number of records available in the database.


displayColumns

protected final List<String> displayColumns
The list of columns that are to be displayed in the model. If not specified all columns are displayed.


primaryKeys

protected final List<String> primaryKeys
The list of primary key columns for the table.


orderBy

protected final List<String> orderBy
The list of columns to order the table data by.


page

protected int page
The current page being displayed in the table.


pageSize

protected int pageSize
The number of rows to display on one page. Defaults to the same value as TableNavigation.MINIMUM_PAGE_SIZE


initialised

protected boolean initialised
A flag used to indicate whether the model has been initialised.

Constructor Detail

DatabaseTableModel

public DatabaseTableModel()
Default constructor. You must invoke init() method.

Method Detail

init

public void init()
          throws RuntimeException
Intialise this table model with data from the table.

Overrides:
init in class RowSetTableModel
Throws:
RuntimeException - If a NamingException or SQLException are encountered while initialising dataSource or RowSetTableModel.rowSet.
See Also:
introspect(), fetchData(), processColumns(), RowSetTableModel.setRowCount()

introspect

protected void introspect()
                   throws SQLException
Introspect the table and determine its primary key columns.

Throws:
SQLException - If errors are encountered while introspecting the table.
See Also:
DatabaseHelper.getPrimaryKeys(javax.sql.DataSource, java.lang.String, java.lang.String)

fetchData

protected void fetchData()
                  throws SQLException
Fetch the data from table. If RowSetTableModel.columns have been specified, only data from those columns will be retrieved. All columns in primaryKeys will be retrieved regardless of whether they are specified in RowSetTableModel.columns or not.

Throws:
SQLException - If errors are encountered while fetching data from the table.
See Also:
RowSetTableModel.setRowCount(), DatabaseHelper.getData(java.lang.String, javax.sql.DataSource, java.lang.String, java.util.List, int, int), AbstractTableModel.fireTableDataChanged()

processColumns

protected void processColumns()
                       throws SQLException
Process the RowSetMetaData associated with RowSetTableModel.rowSet and populate RowSetTableModel.columns. Over-ridden to process only columns specified in displayColumns if specified.

Overrides:
processColumns in class RowSetTableModel
Throws:
SQLException - If errors are encountered while introspecting the meta data.

sort

public void sort(int column)
          throws RuntimeException
Sort the data displayed in the table by the specified column. Re-fetch the data from the table ordered by the specified column.

Specified by:
sort in interface SortableTableModel
Parameters:
column - The index of the column in RowSetTableModel.columns by which to order the results.
Throws:
RuntimeException - If errors are encountered while fetching the data.
See Also:
SortableTableModel.sort( int ), AbstractTableModel.getSortDirection(int), SortableTableModel.sort( int, Direction )

sort

public void sort(int column,
                 SortableTableModel.Direction direction)
          throws RuntimeException
Sort the data displayed in the table by the specified column. Re-fetch the data from the table ordered by the specified column. The page is set to point to the first page of data since the re-sorted data will usually have no reference to the original data.

Specified by:
sort in interface SortableTableModel
Parameters:
column - The index of the column in RowSetTableModel.columns by which to order the results.
direction - The direction in which to sort the data.
Throws:
RuntimeException - If errors are encountered while fetching the data.
See Also:
SortableTableModel.sort( int, Direction ), fetchData()

getPage

public int getPage()
Returns page.

Specified by:
getPage in interface PageableTableModel
Returns:
The value/reference of/to page.
See Also:
PageableTableModel.getPage()

setPage

public void setPage(int page)
Sets the value of page.

Specified by:
setPage in interface PageableTableModel
Parameters:
page - The current page of data to load in the model.
Throws:
RuntimeException - If SQLException is raised by fetchData().
See Also:
PageableTableModel.setPage(int), fetchData()

getPageSize

public int getPageSize()
Returns pageSize.

Specified by:
getPageSize in interface PageableTableModel
Returns:
The value/reference of/to pageSize.
See Also:
PageableTableModel.getPageSize()

setPageSize

public void setPageSize(int pageSize)
Sets the value of pageSize.

Specified by:
setPageSize in interface PageableTableModel
Parameters:
pageSize - The number of rows to display in the model.
Throws:
RuntimeException - If SQLException is raised by fetchData().
See Also:
PageableTableModel.setPageSize(int), fetchData()

getTotalPages

public int getTotalPages()
Return the total number of pages that may be displayed by fetching records from table.

Specified by:
getTotalPages in interface PageableTableModel
Returns:
The number of pages that may be displayed.

getDisplayColumns

public List<String> getDisplayColumns()
Returns displayColumns.

Returns:
The value/reference of/to displayColumns.

setDisplayColumns

public void setDisplayColumns(List<String> columns)
Sets displayColumns. Note that this cannot be set after the model is initialised through init().

Parameters:
columns - The list of columns to display.

getPrimaryKeys

public List<String> getPrimaryKeys()
Returns primaryKeys list of primary key column names.

Returns:
The value/reference of/to primaryKeys.

getDataSourceName

public String getDataSourceName()
Returns dataSourceName.

Returns:
The value/reference of/to dataSourceName.

setDataSourceName

public void setDataSourceName(String dataSourceName)
Sets dataSourceName.

Parameters:
dataSourceName - The JNDI resource name.

getSchema

public String getSchema()
Returns schema.

Returns:
The value/reference of/to schema.

setSchema

public void setSchema(String schema)
Sets the schema in which the table represented by this model exists.

Parameters:
schema - The name of the schema.

getTable

public String getTable()
Returns table.

Returns:
The value/reference of/to table.

setTable

public void setTable(String table)
Sets the name of the table to represent in this model.

Parameters:
table - The name of the table.

getOrderBy

public List<String> getOrderBy()
Returns orderBy.

Returns:
The value/reference of/to orderBy.

setOrderBy

public void setOrderBy(List<String> order)
                throws SQLException
Sets orderBy. This method may be invoked after initialising the model to resort the results.

Parameters:
order - The list of order by clauses.
Throws:
SQLException - If errors are encountered while refetching the data from the table.

SPT Core API