Copyright © 2011 Citra Technologies. All Rights Reserved.

com.citra.treetable.db
Class DefaultDatabaseTreeTableModel

java.lang.Object
  extended by com.citra.treetable.AbstractTreeTableModel
      extended by com.citra.treetable.db.AbstractDatabaseTreeTableModel
          extended by com.citra.treetable.db.DefaultDatabaseTreeTableModel
All Implemented Interfaces:
CacheableTreeTableModel, DatabaseTreeTableModel, TreeTableModel, Serializable, TreeModel

public class DefaultDatabaseTreeTableModel
extends AbstractDatabaseTreeTableModel

DefaultDatabaseTreeTableModel provides grouping for a DatabaseTreeTableModel with the help of a set of column indexes. These indexes refer to the database columns as they are defined in the model.

This class uses a Connection object to connect to the database. The column names and their java types can be specified via a String and a class array respectively. Alternatively, this information can also be provided by a TableModel or a DatabaseTableDefinition.

DefaultDatabaseTableModel uses simple sql select statements to retrieve the database data. A "select count(*) from table" is used to get the row count, while a "limit" keyword is used to retrieve a given row or rows.

The "where" clause is constructed from the FilterTableModel instance, with the help of a SQLFilterConverter, while the "order by" clause from the SortTableModel instance.

DefaultDatabaseTreeTableModel was extensively tested under MYSQL v4.0, 4.1 and 5.0, with complete success. It should work with other databases as well, at least those that support the 'limit' keyword. For databases that do not, you can try turning on cursors, by calling setCursors(true).

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class com.citra.treetable.db.AbstractDatabaseTreeTableModel
AbstractDatabaseTreeTableModel.DefaultTreeTableAggregateRowCache
 
Field Summary
protected  Connection c
          The current connection to the database
protected  SQLFilterConverter converter
          utility class that converts a TableFilter to an sql 'where' clause.
protected  boolean cursors
          boolean indicating if a cursor will be used to fetch data from the database.
protected  DatabaseTableDefinition definition
          the database definition.
protected  TreeTableCache groupCache
          the object that is used to cache the GROUP values of the tree's nodes.
protected  String orderSQL
          a string which is used to hold the order byclause of the sql statement
protected  String table
          string that holds the name of the table to query
protected  String whereSQL
          a string which is used to hold the where clause of the sql statement
 
Fields inherited from class com.citra.treetable.db.AbstractDatabaseTreeTableModel
aggregateRowCache, aggregator, cache, dbFilter, dbSorter
 
Fields inherited from class com.citra.treetable.AbstractTreeTableModel
listenerList, root
 
Fields inherited from interface com.citra.treetable.TreeTableModel
CHANGE_COLUMN_SOURCE
 
Fields inherited from interface com.citra.treetable.TreeTableModel
CHANGE_COLUMN_SOURCE
 
Constructor Summary
DefaultDatabaseTreeTableModel(Connection c, String table, DatabaseTableDefinition def)
          Constructs a DefaultDatabaseTableModel.
DefaultDatabaseTreeTableModel(Connection c, String table, DatabaseTableDefinition def, TreeTableCache groupCache)
          Constructs a DefaultDatabaseTableModel.
DefaultDatabaseTreeTableModel(Connection c, String table, String[] columns)
          Constructs a DefaultDatabaseTableModel.
DefaultDatabaseTreeTableModel(Connection c, String table, String[] columns, Class[] classes)
          Constructs a DefaultDatabaseTableModel.
DefaultDatabaseTreeTableModel(Connection c, String table, TableModel model)
          Constructs a DefaultDatabaseTableModel.
 
Method Summary
protected  DatabaseAggregator createDefaultAggregator()
          Creates and returns the default aggregator.
protected  TreeTableRow createTableRow(TreeTableRow ttr, int index)
          Creates and returns a TreeTableRow for the node parent at its child index identified by index.
 List fetchChildren(Object parentNode, int from, int to)
          This method retrieves the data from the database for the children of parent in the range from - to.
protected  String fetchChildrenStatement(Object parentNode, int from, int to)
          Retrieves the sql statement for the fetchChildren method.
 int fetchGroupCount(Object n)
          This method retrieves the aggregate values for the children of parent in the range from - to, and assigns them on the treeModel.
protected  String fetchGroupCountStatement(Object node)
          Retrieves the sql statement for the fetchGroupCount method.
 List fetchGroups(Object parentNode, int from, int to)
          This method retrieves the group values from the database for the children of parent in the range from - to.
 List fetchGroups(Object parentNode, int from, int to, String[] selectExpr)
          A utility method to fetch group values from the database.
protected  String fetchGroupsStatement(Object parentNode, int from, int to, String[] selectExpr)
          Retrieves the sql statement for the fetchGroups method.
 Class getColumnClass(int column)
          Returns the class of all the cells in the column.
 int getColumnCount()
          Returns the number of columns.
 String getColumnName(int column)
          Returns the name of the column at columnIndex as it will be displayed by the table
 DatabaseTableDefinition getDatabaseDefinition()
           
 String getGroupColumns(int level)
          Returns the column or columns that this level is grouped by.
 String getGroupCriteria(TreeTableRow node)
          Returns a clause that identifies the criteria for the supplied node.
 int[] getGroupingColumns()
          Returns the group columns as a list array.
 SQLFilterConverter getSQLFilterConverter()
          Returns the object that will be used to convert a TableFilter to an sql 'where' clause.
 int getTotalGroupCount()
          Returns the total number of groups defined.
 boolean isGroupedBy(TreeTableRow node, int columnIndex)
          Determines whether or not columnIndex at node was used to group the model.
 void orderByClause()
          This method is called whenever sorting is requested.
 void setCursors(boolean useCursors)
          Tells DefaultDatabaseTreeTableModel to use cursors when fetching a range of rows from the database.
 void setGroupCache(TreeTableCache newGroupCache)
          Assigns a new cache for the the group values of aggregate rows.
 void setGroupingColumns(int[] columns)
          Assigns a new set of grouping columns.
 void setSQLFilterConverter(SQLFilterConverter converter)
          Assigns the object that will be used to convert a TableFilter to an sql 'where' clause.
 void whereClause()
          This method is called whenever filtering is requested.
 
Methods inherited from class com.citra.treetable.db.AbstractDatabaseTreeTableModel
getAggregator, getChild, getChildCount, getFilterTableModel, getIndexOfChild, getPathToRoot, getPathToRoot, getSortTableModel, getUncachedChildren, getUncachedGroupCount, getUncachedGroups, getValueAt, isCellEditable, isCountCached, isLeaf, isRangedModel, isValueCached, nodeChanged, nodesChanged, nodeStructureChanged, nodesWereInserted, nodesWereRemoved, retrieveFromDB, retrieveFromDB, setAggregator, setCache, setValueAt, valueForPathChanged
 
Methods inherited from class com.citra.treetable.AbstractTreeTableModel
addTreeModelListener, fireTreeNodesChanged, fireTreeNodesInserted, fireTreeNodesRemoved, fireTreeStructureChanged, fireTreeTableStructureChanged, fireTreeTableStructureChanged, getRoot, isAggregate, isFooter, isHeader, removeTreeModelListener, setRoot
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.citra.treetable.TreeTableModel
isAggregate, isFooter, isHeader
 
Methods inherited from interface javax.swing.tree.TreeModel
addTreeModelListener, getRoot, removeTreeModelListener
 
Methods inherited from interface com.citra.treetable.TreeTableModel
isAggregate, isFooter, isHeader
 
Methods inherited from interface javax.swing.tree.TreeModel
addTreeModelListener, getRoot, removeTreeModelListener
 

Field Detail

groupCache

protected TreeTableCache groupCache
the object that is used to cache the GROUP values of the tree's nodes.


converter

protected SQLFilterConverter converter
utility class that converts a TableFilter to an sql 'where' clause.


definition

protected DatabaseTableDefinition definition
the database definition.


cursors

protected boolean cursors
boolean indicating if a cursor will be used to fetch data from the database.


c

protected Connection c
The current connection to the database


table

protected String table
string that holds the name of the table to query


whereSQL

protected String whereSQL
a string which is used to hold the where clause of the sql statement


orderSQL

protected String orderSQL
a string which is used to hold the order byclause of the sql statement

Constructor Detail

DefaultDatabaseTreeTableModel

public DefaultDatabaseTreeTableModel(Connection c,
                                     String table,
                                     String[] columns)
Constructs a DefaultDatabaseTableModel.


DefaultDatabaseTreeTableModel

public DefaultDatabaseTreeTableModel(Connection c,
                                     String table,
                                     String[] columns,
                                     Class[] classes)
Constructs a DefaultDatabaseTableModel.


DefaultDatabaseTreeTableModel

public DefaultDatabaseTreeTableModel(Connection c,
                                     String table,
                                     DatabaseTableDefinition def)
Constructs a DefaultDatabaseTableModel.


DefaultDatabaseTreeTableModel

public DefaultDatabaseTreeTableModel(Connection c,
                                     String table,
                                     DatabaseTableDefinition def,
                                     TreeTableCache groupCache)
Constructs a DefaultDatabaseTableModel.


DefaultDatabaseTreeTableModel

public DefaultDatabaseTreeTableModel(Connection c,
                                     String table,
                                     TableModel model)
Constructs a DefaultDatabaseTableModel.

Method Detail

createDefaultAggregator

protected DatabaseAggregator createDefaultAggregator()
Creates and returns the default aggregator. The aggregator will be used to retrieve values for the aggregate (DatabaseHeaderRows) rows of the model.

Specified by:
createDefaultAggregator in class AbstractDatabaseTreeTableModel
Returns:
the default database aggregator.

createTableRow

protected TreeTableRow createTableRow(TreeTableRow ttr,
                                      int index)
Creates and returns a TreeTableRow for the node parent at its child index identified by index.

Specified by:
createTableRow in class AbstractDatabaseTreeTableModel
Parameters:
ttr - the parent node
index - the child index of parent that we want returned
Returns:
the child of parent at index

fetchChildren

public List fetchChildren(Object parentNode,
                          int from,
                          int to)
This method retrieves the data from the database for the children of parent in the range from - to. The following condition must be true: from >= to. The List returned must contain the row elements starting at from. However, the List may contain less elements than those specified in the range.

Parameters:
parentNode - the parent node
from - the index of the first child
to - the index of the last child
Returns:
the retrieved group values as a List.

fetchChildrenStatement

protected String fetchChildrenStatement(Object parentNode,
                                        int from,
                                        int to)
Retrieves the sql statement for the fetchChildren method.

Parameters:
parentNode - the parent node
from - the index of the first child
to - the index of the last child
Returns:
the sql statement as a string

fetchGroupCount

public int fetchGroupCount(Object n)
This method retrieves the aggregate values for the children of parent in the range from - to, and assigns them on the treeModel.

Parameters:
n - the node to consider
Returns:
the group count for node

fetchGroupCountStatement

protected String fetchGroupCountStatement(Object node)
Retrieves the sql statement for the fetchGroupCount method.

Parameters:
node - the node to consider
Returns:
the sql statement as a string

fetchGroups

public List fetchGroups(Object parentNode,
                        int from,
                        int to)
This method retrieves the group values from the database for the children of parent in the range from - to. The following condition must be true: from >= to. The List returned must contain the row elements starting at from. However, the List may contain less elements than those specified in the range.

Parameters:
parentNode - the parent node
from - the index of the first child
to - the index of the last child
Returns:
the retrieved childrend of parent as a List.

fetchGroups

public List fetchGroups(Object parentNode,
                        int from,
                        int to,
                        String[] selectExpr)
A utility method to fetch group values from the database.

Parameters:
parentNode - the parent node
from - the index of the first child
to - the index of the last child
selectExpr - the columns (or functions of columns) to select.
Returns:
the retrieved childrend of parent as a List.

fetchGroupsStatement

protected String fetchGroupsStatement(Object parentNode,
                                      int from,
                                      int to,
                                      String[] selectExpr)
Retrieves the sql statement for the fetchGroups method.

Parameters:
parentNode - the parent node
from - the index of the first child
to - the index of the last child
selectExpr - the columns (or functions of columns) to select.
Returns:
the sql statement as a string

getColumnClass

public Class getColumnClass(int column)
Returns the class of all the cells in the column.

Parameters:
column - the index of the column
Returns:
the class of all the cells in the column.

getColumnCount

public int getColumnCount()
Returns the number of columns.

Returns:
the number of columns.

getColumnName

public String getColumnName(int column)
Returns the name of the column at columnIndex as it will be displayed by the table

Parameters:
column - the index of the column
Returns:
the name of the column

getDatabaseDefinition

public DatabaseTableDefinition getDatabaseDefinition()

getGroupColumns

public String getGroupColumns(int level)
Returns the column or columns that this level is grouped by.

Parameters:
level - the current level in the tree
Returns:
the column(s) string this tree level is grouped by.

getGroupCriteria

public String getGroupCriteria(TreeTableRow node)
Returns a clause that identifies the criteria for the supplied node.

Parameters:
node - the node to consider
Returns:
the criteria for node

getGroupingColumns

public int[] getGroupingColumns()
Returns the group columns as a list array.

Returns:
an int array identifying the order in which the model is grouped.

getSQLFilterConverter

public SQLFilterConverter getSQLFilterConverter()
Returns the object that will be used to convert a TableFilter to an sql 'where' clause.

Returns:
the assigned sql filter converter.

getTotalGroupCount

public int getTotalGroupCount()
Returns the total number of groups defined.

Returns:
the total number of groups defined.

isGroupedBy

public boolean isGroupedBy(TreeTableRow node,
                           int columnIndex)
Determines whether or not columnIndex at node was used to group the model.

Parameters:
node - the node to consider
columnIndex - the column that was used in grouping
Returns:
true if columnIndex was used in grouping, false otherwise

orderByClause

public void orderByClause()
This method is called whenever sorting is requested. Preferrably, this method should reconstruct the sql select query according to the sorting columns of the internal SortTableModel instance.

Specified by:
orderByClause in class AbstractDatabaseTreeTableModel

setCursors

public void setCursors(boolean useCursors)
Tells DefaultDatabaseTreeTableModel to use cursors when fetching a range of rows from the database.

Parameters:
useCursors - true if cursors are to be used.

setGroupCache

public void setGroupCache(TreeTableCache newGroupCache)
Assigns a new cache for the the group values of aggregate rows.

Parameters:
newGroupCache - the new cache to assign.

setGroupingColumns

public void setGroupingColumns(int[] columns)
Assigns a new set of grouping columns.

Parameters:
columns - the new column indexes to group by

setSQLFilterConverter

public void setSQLFilterConverter(SQLFilterConverter converter)
Assigns the object that will be used to convert a TableFilter to an sql 'where' clause.

Parameters:
converter - the sql filter converter to assign.

whereClause

public void whereClause()
This method is called whenever filtering is requested. Preferrably, this method should reconstruct the sql select query according to the table filter of the internal FilterTableModel instance.

Specified by:
whereClause in class AbstractDatabaseTreeTableModel

Copyright © 2011 Citra Technologies. All Rights Reserved.