fi.paatti.paattidatabaseutils.dbservice.querydelegates
Class PaattiQueryDelegate

java.lang.Object
  extended by fi.paatti.paattidatabaseutils.dbservice.querydelegates.PaattiQueryDelegate
All Implemented Interfaces:
com.vaadin.data.util.sqlcontainer.query.FreeformQueryDelegate, com.vaadin.data.util.sqlcontainer.query.FreeformStatementDelegate, Serializable
Direct Known Subclasses:
BelongsQueryDelegate, ChoiceQueryDelegate, EventsInScheduleDelegate, TaskQueryDelegate, UserGroupRolesAndResearchDelegate, UserUnfinishedEventsDelegate, VoluntaryEventsDelegate

public abstract class PaattiQueryDelegate
extends Object
implements com.vaadin.data.util.sqlcontainer.query.FreeformStatementDelegate, Serializable

A base class for all other delegates. Has all the methods, fields etc. that are common in all other delegates.

This delegate does not implement RowIdChange-events, though it has its own implementation for informing about the acquired new row ID (see NewRowIdEvent).

Since:
0.0.3
Author:
Lauri Satokangas, lauri.n.satokangas@student.jyu.fi
See Also:
Serialized Form
 

Field Summary
static String ROWSTATUS_ACTIVE
          The status for active row.
static String ROWSTATUS_DELETED
          The status for deleted row.
 
Constructor Summary
PaattiQueryDelegate(String tableName, String idColumn, String[] columnNames)
          Constructor.
 
Method Summary
 void addNewiRowIdListener(NewRowIDListener listener)
          Adds a NewRowIDListener to the list of listeners.
protected  void fireNewRowIdEvent(Object newRowId)
          Fires an event after acquiring the new row ID from a succesful insert to the database.
 String getArrayAsStringWithAddedString(String[] array, String fieldToBeLeftOut, String stringToBeAdded, boolean removeLastStringToBeAdded)
          Creates a string from array adding the given string between arrays fields.
 String getArrayAsStringWithCommas(String[] allColumnNames)
          Creates a String with all the column names from the given array.
 String getArrayAsStringWithCommasAndWithoutIdColumn(String[] allColumnNames, String idColumn)
          Creates a String with all the column names (except the id column) from the given array.
 String[] getColumnNames()
          Returns the column names that are used in this delegate.
 com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper getContainsRowQueryStatement(Object... keys)
          Inherited from FreeformStatementDelegate.
 String getContainsRowQueryString(Object... keys)
          Inherited from FreeformStatementDelegate.
 String getCountQuery()
          Inherited from FreeformStatementDelegate.
 com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper getCountStatement()
          Returns the row count from the delegate's table.
 List<com.vaadin.data.Container.Filter> getFilters()
          Returns the container filter list.
 String getFilterStringWithoutWhere(com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper sh, boolean startWithAnd)
          Creates a string from the list of filters, without the word 'WHERE'.
 String getIdColumn()
          Returns the column name that is used as the main table's id column.
 String getInsertQueryString()
          Uses the set tablename, id column and column names to create an insert string.
 String getInsertQueryString(String tableName, String idColumn, String[] allColumnNames)
          Creates an insert query from the given parameters.
 List<com.vaadin.data.util.sqlcontainer.query.OrderBy> getOrderBys()
          Returns the sorting rules (order by) list.
 String getQueryString(int offset, int limit)
          Inherited from FreeformStatementDelegate.
 String getTableName()
          Returns the table name that is used as the delegate's "main" table.
 String getUpdateQueryString()
          Uses the set tablename, id column and column names to create an update string.
 String getUpdateQueryString(String tableName, String idColumn, String[] allColumnNames)
          Create an update String.
 String getValuesString(int numberOfValues)
          Creates a string for inserting values.
 void removeNewRowIdListener(NewRowIDListener listener)
          Removes a NewRowIDListener from the list of listeners.
 boolean removeRow(Connection conn, com.vaadin.data.util.sqlcontainer.RowItem row)
          Removes a row by setting its rowStatus to 'DELETED'.
 void setFilters(List<com.vaadin.data.Container.Filter> filters)
          Sets the delegate's container filters.
 void setOrderBy(List<com.vaadin.data.util.sqlcontainer.query.OrderBy> orderBys)
          Sets the delegate's OrderBys.
protected abstract  void setRowValues(PreparedStatement statement, com.vaadin.data.util.sqlcontainer.RowItem row)
          Sets the values for the row.
abstract  int storeRow(Connection conn, com.vaadin.data.util.sqlcontainer.RowItem row)
          Stores a row in the database.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.vaadin.data.util.sqlcontainer.query.FreeformStatementDelegate
getQueryStatement
 

Field Detail

ROWSTATUS_DELETED

public static final String ROWSTATUS_DELETED
The status for deleted row.

See Also:
Constant Field Values

ROWSTATUS_ACTIVE

public static final String ROWSTATUS_ACTIVE
The status for active row.

See Also:
Constant Field Values
Constructor Detail

PaattiQueryDelegate

public PaattiQueryDelegate(String tableName,
                           String idColumn,
                           String[] columnNames)
Constructor.

Parameters:
tableName - The name of the database table.
idColumn - The name of the id column in the database table.
columnNames - The column names.
Method Detail

fireNewRowIdEvent

protected void fireNewRowIdEvent(Object newRowId)
Fires an event after acquiring the new row ID from a succesful insert to the database.

Parameters:
newRowId - The new row ID.

addNewiRowIdListener

public void addNewiRowIdListener(NewRowIDListener listener)
Adds a NewRowIDListener to the list of listeners.

Parameters:
listener - The listener to be added.

removeNewRowIdListener

public void removeNewRowIdListener(NewRowIDListener listener)
Removes a NewRowIDListener from the list of listeners.

Parameters:
listener - The listener to be removed.

getCountStatement

public com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper getCountStatement()
Returns the row count from the delegate's table.

SELECT COUNT (*) FROM [table]

If the filters are set they are used in the query.

Specified by:
getCountStatement in interface com.vaadin.data.util.sqlcontainer.query.FreeformStatementDelegate
Returns:
@throws UnsupportedOperationException.

getInsertQueryString

public String getInsertQueryString(String tableName,
                                   String idColumn,
                                   String[] allColumnNames)
Creates an insert query from the given parameters.

Example: INSERT INTO TASK (TASK_description, TASK_content, TASK_url, TASK_sequence, TASK_EVENT_eventID, TASK_TASKTYPE_taskTypeID) VALUES (?, ?, ?, ?, ?, ?)

Parameters:
tableName - The name of the table.
idColumn - The name of the table id column.
allColumnNames - The column names.
Returns:
The INSERT query string.

getInsertQueryString

public String getInsertQueryString()
Uses the set tablename, id column and column names to create an insert string.

Returns:
The INSERT query string.
See Also:
getInsertQueryString()

getUpdateQueryString

public String getUpdateQueryString(String tableName,
                                   String idColumn,
                                   String[] allColumnNames)
Create an update String.

Example: UPDATE TASK SET TASK_description = ?, TASK_content = ?, TASK_url = ?, TASK_sequence = ?, TASK_EVENT_eventID = ?, TASK_TASKTYPE_taskTypeID = ? WHERE TASK_taskId = ?

Parameters:
tableName - The name of the table.
idColumn - The name of the id column.
allColumnNames - The array holding all the column names.
Returns:
The string to be used in the update query.

getUpdateQueryString

public String getUpdateQueryString()
Uses the set tablename, id column and column names to create an update string.

Returns:
The combined update query string.
See Also:
getUpdateQueryString()

storeRow

public abstract int storeRow(Connection conn,
                             com.vaadin.data.util.sqlcontainer.RowItem row)
                      throws SQLException
Stores a row in the database.

Specified by:
storeRow in interface com.vaadin.data.util.sqlcontainer.query.FreeformQueryDelegate
Parameters:
conn - The database connection.
row - The row where the values will be set.
Returns:
The number of affected rows in the database.
Throws:
SQLException

setRowValues

protected abstract void setRowValues(PreparedStatement statement,
                                     com.vaadin.data.util.sqlcontainer.RowItem row)
                              throws SQLException
Sets the values for the row.

Parameters:
statement - The prepared statement where the values will be set.
row - The row where the values will be taken from.
Throws:
SQLException

removeRow

public boolean removeRow(Connection conn,
                         com.vaadin.data.util.sqlcontainer.RowItem row)
                  throws SQLException
Removes a row by setting its rowStatus to 'DELETED'.

Specified by:
removeRow in interface com.vaadin.data.util.sqlcontainer.query.FreeformQueryDelegate
Parameters:
conn - The database connection
row - The row where the values will be set.
Returns:
True if the count of affected rows is one, otherwise false.
Throws:
SQLException

setFilters

public void setFilters(List<com.vaadin.data.Container.Filter> filters)
Sets the delegate's container filters.

Specified by:
setFilters in interface com.vaadin.data.util.sqlcontainer.query.FreeformQueryDelegate
Parameters:
filters - The list of container filters.

setOrderBy

public void setOrderBy(List<com.vaadin.data.util.sqlcontainer.query.OrderBy> orderBys)
Sets the delegate's OrderBys.

Specified by:
setOrderBy in interface com.vaadin.data.util.sqlcontainer.query.FreeformQueryDelegate
Parameters:
orderBys - The list of sorting rules.

getArrayAsStringWithCommas

public String getArrayAsStringWithCommas(String[] allColumnNames)
Creates a String with all the column names from the given array. Commas are added between each column name.

Example from TASK-table: " TASK_taskId, TASK_description, TASK_content, TASK_url, TASK_sequence, TASK_EVENT_eventID, TASK_TASKTYPE_taskTypeID"

Parameters:
allColumnNames - The array of column names.
Returns:
A string containing column names with a comma (and space) between each name.

getArrayAsStringWithCommasAndWithoutIdColumn

public String getArrayAsStringWithCommasAndWithoutIdColumn(String[] allColumnNames,
                                                           String idColumn)
Creates a String with all the column names (except the id column) from the given array. Commas are added between each column name.

Example from TASK-table: " TASK_description, TASK_content, TASK_url, TASK_sequence, TASK_EVENT_eventID, TASK_TASKTYPE_taskTypeID"

Parameters:
allColumnNames - The array of column names.
idColumn - The name of the id column.
Returns:
A string containing column names with a comma (and space) between each name, not containing the id column.

getValuesString

public String getValuesString(int numberOfValues)
Creates a string for inserting values.

Example: " VALUES (?, ?, ?, ?)"

Parameters:
numberOfValues - The values count.
Returns:
The VALUES-string

getArrayAsStringWithAddedString

public String getArrayAsStringWithAddedString(String[] array,
                                              String fieldToBeLeftOut,
                                              String stringToBeAdded,
                                              boolean removeLastStringToBeAdded)
Creates a string from array adding the given string between arrays fields.

Example: getArrayAsStringWithAddedString([TASK_description, TASK_url], " = ?," ) --> " TASK_description = ?, TASK_url = ?, "

Parameters:
array - The array.
fieldToBeLeftOut - The field that will be left out. Set to null if all the fields should be included.
stringToBeAdded - The suffix for all the fields of the array.
removeLastStringToBeAdded - Should the last added string be removed or not.
Returns:
The array as string with the string added between each value of the array.

getFilterStringWithoutWhere

public String getFilterStringWithoutWhere(com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper sh,
                                          boolean startWithAnd)
Creates a string from the list of filters, without the word 'WHERE'.

Parameters:
sh - StatementHelper
startWithAnd - Whether the string should start with 'AND' or not.
Returns:
The filter string.

getTableName

public String getTableName()
Returns the table name that is used as the delegate's "main" table.

Returns:
the name of the delegate's table.

getIdColumn

public String getIdColumn()
Returns the column name that is used as the main table's id column.

Returns:
the name of the idColumn.

getColumnNames

public String[] getColumnNames()
Returns the column names that are used in this delegate.

Returns:
the columnNames array.

getFilters

public List<com.vaadin.data.Container.Filter> getFilters()
Returns the container filter list.

Returns:
The list of container filters.

getOrderBys

public List<com.vaadin.data.util.sqlcontainer.query.OrderBy> getOrderBys()
Returns the sorting rules (order by) list.

Returns:
The list of sorting rules.

getContainsRowQueryStatement

public com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper getContainsRowQueryStatement(Object... keys)
                                                                                               throws UnsupportedOperationException
Inherited from FreeformStatementDelegate. Not in use.

Specified by:
getContainsRowQueryStatement in interface com.vaadin.data.util.sqlcontainer.query.FreeformStatementDelegate
Throws:
UnsupportedOperationException

getCountQuery

public String getCountQuery()
                     throws UnsupportedOperationException
Inherited from FreeformStatementDelegate. Not in use.

Specified by:
getCountQuery in interface com.vaadin.data.util.sqlcontainer.query.FreeformQueryDelegate
Throws:
UnsupportedOperationException

getContainsRowQueryString

public String getContainsRowQueryString(Object... keys)
                                 throws UnsupportedOperationException
Inherited from FreeformStatementDelegate. Not in use.

Specified by:
getContainsRowQueryString in interface com.vaadin.data.util.sqlcontainer.query.FreeformQueryDelegate
Throws:
UnsupportedOperationException

getQueryString

public String getQueryString(int offset,
                             int limit)
                      throws UnsupportedOperationException
Inherited from FreeformStatementDelegate. Not in use.

Specified by:
getQueryString in interface com.vaadin.data.util.sqlcontainer.query.FreeformQueryDelegate
Throws:
UnsupportedOperationException


Copyright © 2012. All Rights Reserved.