net.xoetrope.optional.data.sql
Class DatabaseTableModel

java.lang.Object
  extended by net.xoetrope.xui.data.XModel
      extended by net.xoetrope.xui.data.XBaseModel
          extended by net.xoetrope.optional.data.sql.DatabaseTableModel
All Implemented Interfaces:
java.lang.Cloneable, DumpWriter, XRowSelector

public class DatabaseTableModel
extends XBaseModel
implements DumpWriter, XRowSelector

A wrapper for a database table allowing it to integrate with the XModel

The table model consists of a set of DatabaseRowModel objects and these in turn consist of DatabaseFieldModel nodes. These objects all act as wrappers for the DatabaseTable object and do not themselves maintain data.

Copyright: Copyright (c) 2003
License: see license.txt

$Revision: 2.12 $ License: see license.txt


Field Summary
protected  java.lang.String modelId
           
 
Fields inherited from class net.xoetrope.xui.data.XBaseModel
addByDefault, appendByDefault, ID_ATTRIBUTE, NUM_FIXED_ATTRIBUTE, VALUE_ATTRIBUTE, values
 
Fields inherited from class net.xoetrope.xui.data.XModel
parentModel, tagName
 
Constructor Summary
DatabaseTableModel()
          Create a new model node for a database table
 
Method Summary
 java.lang.Object append(java.lang.String id)
          This method does not nothing it is provided merely as an implementation of the XModel interface.
 void dump(java.io.Writer w)
          Write teh contents of this model node to a stream
 int executeUpdate(java.lang.String sqlStr)
          Update the underlying database table using the specified SQL statement
 DatabaseRowModel findRow(int fieldIdx, java.lang.String value)
          Find a row with a specified field value
 DatabaseRowModel findRow(java.lang.String where)
          Find a row with a specified field value
 DatabaseTableModel findRows(int fieldIdx, java.lang.String value)
          Find a set of rows with a specified field value
 DatabaseTableModel findRows(java.lang.String where)
          Find a set of rows with a specified field value
 DatabaseTableModel findRows(java.lang.String requestFields, java.lang.String where)
          Find a set of rows with a specified field value.
 void first()
          Moves the table's cursor to the first row
 java.lang.Object get()
          gets the value attribute
 XModel get(int i)
          Get the XModel at element i
 java.lang.Object get(java.lang.String element)
          Get the value of the element located at the path in the element parameter If the attribName parameter is not null we get the value of the attributeValues
 java.lang.String getAttribName(int i)
          Gets the attribute name for field i.
 int getAttribute(java.lang.String attribName)
          returns the index of the attribiteNames array whose value is the same as the attribName
 java.lang.Object getAttribValue(int i)
          Get the value of an attribute (row)
 double getAttribValueAsDouble(int i)
          Get an attribute as a double value
 int getAttribValueAsInt(int i)
          Get an attribute as an int value
 java.lang.String getAttribValueAsString(int i)
          Get an attribute as a String object
 java.lang.String getFieldValue(int colIdx)
          Provides iterative access to the table values.
 java.lang.String getFieldValue(int rowIdx, int colIdx)
          Provides random access to the table values.
 java.lang.String getId()
          Gets the name attribute, by default the table name
 java.sql.ResultSetMetaData getMetaData()
          Get the table's meta data
 int getNumAttributes()
          Get the number of fields in this TableModel
 int getNumChildren()
          Gets the number of rows for this table
 java.lang.String getOriginalSql()
           
 int getSelectedRow()
          Get the row selection index
 CachedDatabaseTable getTable()
           
static DatabaseTableModel getTable(java.lang.String tableName)
          Look up a table by name in the overall model.
 java.lang.String getTagName()
          Gets the model element tag name, e.g.
 java.lang.String getValue()
          Provides iterative access to the table values.
 XModel getValue(int i)
          Get a child value/node (a DatabaseRowModel) at the specified index
 double getValueAsDouble(java.lang.String elementName)
          Gets the value attribute as a double value from the current record
 int getValueAsInt(java.lang.String elementName)
          Gets the value attribute of the specified node as an int.
 int hashCode()
          Get the hashcode for this model node
 boolean hasMoreRows()
          Has the table more records/rows?
 boolean isDistinct()
          Check if the table is set to retrieve distinct rows
 void last()
          Moves the table's cursor to the last row
 boolean next()
          Moves the table's cursor to the next row
 boolean previous()
          Moves the table's cursor to the previous row
 void retrieve()
          Get the table data.
 void set(java.lang.Object s)
          Sets the model value
 void set(java.lang.String attribName, java.lang.Object newObject)
          Set the value of the XModel located at the elementName
 void set(java.lang.String elementName, java.lang.String attribName, java.lang.Object newObject)
          Set the value of the attribute in the XModel located at the elementName
static void setAllowNull(boolean allow)
          Set a flag to determine how null values are treated.
 void setAttribValue(int i, java.lang.Object value)
          Sets the attribute value
 void setDirty(boolean isDirty)
          Ensures that the query is recalled.
 void setDistinct(boolean isDistinct)
          Set the table to retrieve distinct rows
 void setFieldValue(int rowIdx, int colIdx, java.lang.String newValue)
          Set a field value
 void setFieldValue(int colIdx, java.lang.String newValue)
          Set a field value
 void setName(java.lang.String newName)
          Sets the name attribute and constructs a new table of that name
 void setOrderField(java.lang.String fld)
          Set the field(s) used ORDER clause of the SQL query
 void setParams(java.lang.String[] params)
          Used in the case of a PreparedStatement.
 void setReadOnly(boolean b)
          Sets the read only flag indicating whether or not the underlying table is read-only
 void setSelectedRow(int rowIdx)
          Set the row selection index
 void setSqlStatement(java.lang.String sqlStr, java.lang.String connName, boolean writable)
          Specify the sql statement in its entirety.
 void setupTable(java.lang.String tableName)
          List the fields retrieved from the table
 void setupTable(java.lang.String tableName, java.lang.String fields, java.lang.String whereClause)
          List the fields retrieved from the table
 void setupTable(java.lang.String tableName, java.lang.String fields, java.lang.String whereClause, boolean writable)
          List the fields retrieved from the table
 void setupTable(java.lang.String tableName, java.lang.String fields, java.lang.String whereClause, java.lang.String connName, boolean writable)
          List the fields retrieved from the table
protected  void sync()
          Syncs the model with the service response is the xtable has been changed or if the node has timed out.
 
Methods inherited from class net.xoetrope.xui.data.XBaseModel
append, getAddByDefault, getAppendByDefault, getAttribFromPath, getBaseFromPath, getChildExists, getModel, getValueAsString, hasAutoId, hasAutoId, insertChildAt, remove, removeAttributes, removeChild, removeChild, removeChildren, setAddByDefault, setAppendByDefault, setAttribValue, setId, setNumAttributes, setNumChildren
 
Methods inherited from class net.xoetrope.xui.data.XModel
addModelListener, clear, fireModelUpdated, getInstance, getParent, prefixOutputPath, reset, resetAttributes, setParent, setTagName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

modelId

protected java.lang.String modelId
Constructor Detail

DatabaseTableModel

public DatabaseTableModel()
Create a new model node for a database table

Method Detail

setAllowNull

public static void setAllowNull(boolean allow)
Set a flag to determine how null values are treated.

Parameters:
allow - if false then nulls are return as zero or an empty string, if true the null is returned for string types and Double.NaN for doubles and Integer.MIN_VALUE for int fields and so on

setDirty

public void setDirty(boolean isDirty)
Ensures that the query is recalled. This is important where PreparedStatements are used to retrieve the data. The query itself does not change but the parameters do.

Parameters:
isDirty - boolean to set the dirty flag

setName

public void setName(java.lang.String newName)
Sets the name attribute and constructs a new table of that name

Parameters:
newName - the new name

hashCode

public int hashCode()
Get the hashcode for this model node

Overrides:
hashCode in class XBaseModel
Returns:
the hashcode

setupTable

public void setupTable(java.lang.String tableName)
List the fields retrieved from the table

Parameters:
tableName - the database table name

setupTable

public void setupTable(java.lang.String tableName,
                       java.lang.String fields,
                       java.lang.String whereClause)
List the fields retrieved from the table

Parameters:
tableName - the database table name
fields - the database field names, or null if all fields are to be retrieved
whereClause - the where clause, or null if all rows are to be selected

setupTable

public void setupTable(java.lang.String tableName,
                       java.lang.String fields,
                       java.lang.String whereClause,
                       java.lang.String connName,
                       boolean writable)
List the fields retrieved from the table

Parameters:
writable - true if the table is to be treated as writable
tableName - the database table name
fields - the database field names, or null if all fields are to be retrieved
whereClause - the where clause, or null if all rows are to be selected
connName - the connection name

setSqlStatement

public void setSqlStatement(java.lang.String sqlStr,
                            java.lang.String connName,
                            boolean writable)
Specify the sql statement in its entirety. In setting the entire SQL statement any other parameters are ignored.

Parameters:
writable - true if the table is to be treated as writeable
sqlStr - the full SQL statement
connName - the connection name

getOriginalSql

public java.lang.String getOriginalSql()

executeUpdate

public int executeUpdate(java.lang.String sqlStr)
                  throws java.lang.Exception
Update the underlying database table using the specified SQL statement

Parameters:
sqlStr - the full UPDATE SQL statement
Returns:
the number of rows affected by the update
Throws:
java.lang.Exception - SqlException(s) thrown while performing the update

setDistinct

public void setDistinct(boolean isDistinct)
Set the table to retrieve distinct rows

Parameters:
isDistinct - true if the DISTINCT keyword is to be used in the query

isDistinct

public boolean isDistinct()
Check if the table is set to retrieve distinct rows

Returns:
the distinct flag

setOrderField

public void setOrderField(java.lang.String fld)
Set the field(s) used ORDER clause of the SQL query

Parameters:
fld - the field name(s)

setupTable

public void setupTable(java.lang.String tableName,
                       java.lang.String fields,
                       java.lang.String whereClause,
                       boolean writable)
List the fields retrieved from the table

Parameters:
tableName - the database table name
fields - the database field names, or null if all fields are to be retrieved
whereClause - the where clause, or null if all rows are to be selected
writable - sets the table as writable, normally this should be false

setReadOnly

public void setReadOnly(boolean b)
Sets the read only flag indicating whether or not the underlying table is read-only

Parameters:
b - true for a read-only table.

sync

protected void sync()
Syncs the model with the service response is the xtable has been changed or if the node has timed out.


get

public java.lang.Object get(java.lang.String element)
Get the value of the element located at the path in the element parameter If the attribName parameter is not null we get the value of the attributeValues

Overrides:
get in class XBaseModel
Parameters:
element - The path to the XModel we require
Returns:
The value of the XModel or the attribute

set

public void set(java.lang.String elementName,
                java.lang.String attribName,
                java.lang.Object newObject)
Set the value of the attribute in the XModel located at the elementName

Parameters:
elementName - The path to the XModel in the format 'base/foo
attribName - The name of the attribute whose value we require
newObject - The new value of the attribute

set

public void set(java.lang.String attribName,
                java.lang.Object newObject)
Set the value of the XModel located at the elementName

Overrides:
set in class XBaseModel
Parameters:
attribName - The path to the XModel in the format 'base/foo
newObject - The new value of the XModel

get

public XModel get(int i)
Get the XModel at element i

Overrides:
get in class XBaseModel
Parameters:
i - The index of the values array
Returns:
The XModel at location i

get

public java.lang.Object get()
gets the value attribute

Overrides:
get in class XBaseModel
Returns:
the value of the model

append

public java.lang.Object append(java.lang.String id)
This method does not nothing it is provided merely as an implementation of the XModel interface. A child or attribute can be logically appended to a table node.

Overrides:
append in class XBaseModel
Parameters:
id - the node id
Returns:
null as nothing is appended

set

public void set(java.lang.Object s)
Sets the model value

Overrides:
set in class XBaseModel
Parameters:
s - the new value

getAttribValue

public java.lang.Object getAttribValue(int i)
Get the value of an attribute (row)

Overrides:
getAttribValue in class XBaseModel
Parameters:
i - The index of the attributeValues array whose value we want
Returns:
The string value of the attributeValues array at position i

getAttribValueAsString

public java.lang.String getAttribValueAsString(int i)
Get an attribute as a String object

Overrides:
getAttribValueAsString in class XBaseModel
Parameters:
i - The index of the attributeValues array whose value we want
Returns:
The string value of the attributeValues array at position i

getAttribValueAsDouble

public double getAttribValueAsDouble(int i)
Get an attribute as a double value

Overrides:
getAttribValueAsDouble in class XBaseModel
Parameters:
i - The index of the attributeValues array whose value we want
Returns:
The string value of the attributeValues array at position i

getAttribValueAsInt

public int getAttribValueAsInt(int i)
Get an attribute as an int value

Overrides:
getAttribValueAsInt in class XBaseModel
Parameters:
i - The index of the attributeValues array whose value we want
Returns:
The string value of the attributeValues array at position i

getAttribute

public int getAttribute(java.lang.String attribName)
returns the index of the attribiteNames array whose value is the same as the attribName

Overrides:
getAttribute in class XBaseModel
Parameters:
attribName - The name of the attribute we are trying to locate
Returns:
The index of the attributeNames array containg the name

retrieve

public void retrieve()
Get the table data.


first

public void first()
Moves the table's cursor to the first row


last

public void last()
Moves the table's cursor to the last row


next

public boolean next()
Moves the table's cursor to the next row

Returns:
true if the operation succeeded

hasMoreRows

public boolean hasMoreRows()
Has the table more records/rows?

Returns:
true if the current row is not on the last row in the table

previous

public boolean previous()
Moves the table's cursor to the previous row

Returns:
true if the operation succeeded

getValue

public java.lang.String getValue()
Provides iterative access to the table values. The current row in the table is used to index the data. This method does not implicitly retrieve the data so you must call the retrieve method explicitly.

Returns:
the data

getValue

public XModel getValue(int i)
Get a child value/node (a DatabaseRowModel) at the specified index

Parameters:
i - The index into the values array
Returns:
The XModel at position i of the values array.

getFieldValue

public java.lang.String getFieldValue(int colIdx)
Provides iterative access to the table values. The current row in the table is used to index the data. This method does not implicitly retrieve the data so you must call the retrieve method explicitly.

Parameters:
colIdx - the column of field index
Returns:
the data

getFieldValue

public java.lang.String getFieldValue(int rowIdx,
                                      int colIdx)
Provides random access to the table values. This method does not implicitly retrieve the data so you must call the retrieve method explicitly.

Parameters:
rowIdx - the row index
colIdx - the column of field index
Returns:
the data

setFieldValue

public void setFieldValue(int colIdx,
                          java.lang.String newValue)
Set a field value

Parameters:
colIdx - the field index, zero based
newValue - the new field value

setFieldValue

public void setFieldValue(int rowIdx,
                          int colIdx,
                          java.lang.String newValue)
Set a field value

Parameters:
rowIdx - the row index, zero based
colIdx - the field index, zero based
newValue - the new field value

getValueAsDouble

public double getValueAsDouble(java.lang.String elementName)
Gets the value attribute as a double value from the current record

Overrides:
getValueAsDouble in class XBaseModel
Parameters:
elementName - the field/column name
Returns:
the field value

getValueAsInt

public int getValueAsInt(java.lang.String elementName)
Gets the value attribute of the specified node as an int.

Overrides:
getValueAsInt in class XBaseModel
Parameters:
elementName - the field name
Returns:
the field value

getAttribName

public java.lang.String getAttribName(int i)
Gets the attribute name for field i. For this node the table field name is returned.

Overrides:
getAttribName in class XBaseModel
Parameters:
i - The index of the attributeNames array whose value we want
Returns:
The string value of the attributeNames array at position i

getId

public java.lang.String getId()
Gets the name attribute, by default the table name

Overrides:
getId in class XBaseModel
Returns:
the model node's ID

setAttribValue

public void setAttribValue(int i,
                           java.lang.Object value)
Sets the attribute value

Overrides:
setAttribValue in class XBaseModel
Parameters:
i - The index of the attributeValues array whose value we want
value - the value object

getNumChildren

public int getNumChildren()
Gets the number of rows for this table

Overrides:
getNumChildren in class XBaseModel
Returns:
the number of rows

getNumAttributes

public int getNumAttributes()
Get the number of fields in this TableModel

Overrides:
getNumAttributes in class XBaseModel
Returns:
the number of fields

getTagName

public java.lang.String getTagName()
Gets the model element tag name, e.g. 'Component' from the XML fragment
Overrides:
getTagName in class XModel
Returns:
the model element name

findRows

public DatabaseTableModel findRows(int fieldIdx,
                                   java.lang.String value)
Find a set of rows with a specified field value

Parameters:
fieldIdx - the index of the search field
value - the search value
Returns:
the table model containing the selected rows. null is returned if this table has uncommitted updates

findRows

public DatabaseTableModel findRows(java.lang.String where)
Find a set of rows with a specified field value

Parameters:
where - the search criteria
Returns:
the table model containing the selected rows. null is returned if this table has uncommitted updates

setParams

public void setParams(java.lang.String[] params)
Used in the case of a PreparedStatement. Passed straight through to the xtable

Parameters:
params - The String array which will popuate the PreparedStatement.

findRows

public DatabaseTableModel findRows(java.lang.String requestFields,
                                   java.lang.String where)
Find a set of rows with a specified field value. The query differs depending on whether or not the table has been defind using the setupTable method. If not the where clause is appended to the 'GROUP BY', 'HAVING' or 'ORDER BY' clause - whichever is last.

Parameters:
requestFields - the fields to return
where - the search criteria
Returns:
the table model containing the selected rows. null is returned if this table has uncommitted updates

findRow

public DatabaseRowModel findRow(int fieldIdx,
                                java.lang.String value)
Find a row with a specified field value

Parameters:
fieldIdx - the index of the search field
value - the search value
Returns:
the table row model containing the selected row. null is returned if this table has uncommitted updates

findRow

public DatabaseRowModel findRow(java.lang.String where)
Find a row with a specified field value

Parameters:
where - the search criteria
Returns:
the table row model containing the selected row. null is returned if this table has uncommitted updates

getTable

public static DatabaseTableModel getTable(java.lang.String tableName)
Look up a table by name in the overall model.

Parameters:
tableName - the table name
Returns:
the table node

dump

public void dump(java.io.Writer w)
Write teh contents of this model node to a stream

Specified by:
dump in interface DumpWriter
Parameters:
w - the write/stream

setSelectedRow

public void setSelectedRow(int rowIdx)
Set the row selection index

Specified by:
setSelectedRow in interface XRowSelector
Parameters:
rowIdx - the new row selection index (zero based)

getSelectedRow

public int getSelectedRow()
Get the row selection index

Specified by:
getSelectedRow in interface XRowSelector
Returns:
the current row selection index (zero based)

getMetaData

public java.sql.ResultSetMetaData getMetaData()
Get the table's meta data

Returns:
the meta data from the JDBC result set

getTable

public CachedDatabaseTable getTable()