org.opensubsystems.core.persist.db
Class DatabaseUpdateOperation

java.lang.Object
  extended by org.opensubsystems.core.persist.db.DatabaseOperation
      extended by org.opensubsystems.core.persist.db.DatabaseUpdateOperation
All Implemented Interfaces:
DatabaseOperations
Direct Known Subclasses:
DatabaseCreateMultipleDataObjectsOperation, DatabaseCreateSingleDataObjectOperation, DatabaseDeleteSingleDataObjectOperation, DatabaseUpdateMultipleDataObjectsOperation, DatabaseUpdateSingleDataObjectOperation

public abstract class DatabaseUpdateOperation
extends DatabaseOperation
implements DatabaseOperations

Adapter to simplify writing of database updates, which takes care of requesting and returning connections, transaction management and exception handling. To use this adapter you just need to define anonymous class and override method performOperation to provide the actual database update. Optionally you may want to override one of the handleXXX methods to provide custom error handling. Example how to perform the update using query retrieved from schema public ModifiableDataObject save( final ModifiableDataObject data ) throws OSSException { DatabaseUpdateOperation dbop = new DatabaseUpdateOperation( this, m_schema.getQueryToUpdateMyData(data), DatabaseUpdateOperation.DBOP_UPDATE, m_schema, DataConstants.MY_DATA, data ) { protected void updateDatabase( DatabaseFactoryImpl dbfactory, Connection cntConnection, PreparedStatement pstmQuery ) throws OSSException, SQLException { ModifiableDataObject objData = data; int iIndex = ((ModifiableDatabaseFactory)dbfactory).setValuesForUpdate( pstmQuery, objData); DatabaseImpl.getInstance().updatedAndFetchGeneratedValues( m_strDataObjectName, cntConnection, pstmQuery, m_dbschema.isInDomain(), m_dbschema.getTableNames().get( new Integer(m_iDataType)).toString(), iIndex, objData); setReturnData(objData); } }; dbop.executeUpdate(); return (ModifiableDataObject)dbop.getReturnData(); } Example of method in factory which saves data using its schema public ModifiableDataObject save( final ModifiableDataObject data ) throws OSSException { DatabaseUpdateOperation dbop = new DatabaseUpdateOperation( this, DatabaseUpdateOperation.DBOP_UPDATE) { protected void updateDatabase( DatabaseFactoryImpl dbfactory, Connection cntConnection, PreparedStatement pstmQuery ) throws OSSException, SQLException { setReturnData(m_schema.updateData(cntConnection, (MyData)data)); } }; dbop.executeUpdate(); return (ModifiableDataObject)dbop.getReturnData(); }

Version:
$Id: DatabaseUpdateOperation.java,v 1.19 2007/01/28 06:54:42 bastafidli Exp $
Author:
Miro Halas
Code reviewer:
Miro Halas
Code reviewed:
1.10 2006/07/26 23:45:36 jlegeny

Field Summary
 
Fields inherited from class org.opensubsystems.core.persist.db.DatabaseOperation
m_data, m_dbschema, m_factory, m_iDataType, m_returnData, m_strQuery
 
Fields inherited from interface org.opensubsystems.core.persist.db.DatabaseOperations
DBOP_DELETE, DBOP_INSERT, DBOP_SELECT, DBOP_UPDATE
 
Constructor Summary
DatabaseUpdateOperation(DatabaseFactoryImpl factory, int iUpdateType)
          Constructor to use when database update doesn't require any prepared statement.
DatabaseUpdateOperation(DatabaseFactoryImpl factory, java.lang.String strQueryToPrepare, ModifiableDatabaseSchema schema, int iUpdateType, java.lang.Object data)
          Constructor to use when database update requires prepared statement.
 
Method Summary
 void executeUpdate()
          Method to execute database update invoking the user defined code in performOperation.
protected  void handleKnownError(OSSException exc, java.sql.Connection cntConnection, int iOperationType, int iDataType, java.lang.Object data)
          Override this method to provide any custom error handling for expected error, which were most likely produced by lower layer.
protected  void handleSQLException(java.sql.SQLException sqleExc, java.sql.Connection cntConnection, int iOperationType, int iDataType, java.lang.Object data)
          Provide custom handling of SQL Exceptions to usually detect constraint violation.
protected  void handleUnknownError(java.lang.Throwable thr, java.sql.Connection cntConnection, int iOperationType, int iDataType, java.lang.Object data)
          Override this method to provide any custom error handling for unexpected error, which weren't handled by lower layer.
protected  void performOperation(DatabaseFactoryImpl dbfactory, java.sql.Connection cntConnection, java.sql.PreparedStatement pstmStatement)
          Define content of this method to perform the database operation using the provided connection and optional prepared statement.
protected  void prepareData(DataObject data)
          Define content of this method to perform the prepare data (update dataobject attribute).
protected  int setValuesForInsert(java.sql.PreparedStatement insertStatement, DataObject data, int initialIndex)
          Method sets values to the prepared statement for insert of data object.
protected  int setValuesForUpdate(java.sql.PreparedStatement updateStatement, DataObject data, int initialIndex)
          Method sets values to the prepared statement for update of data object.
 
Methods inherited from class org.opensubsystems.core.persist.db.DatabaseOperation
getReturnData, setReturnData
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DatabaseUpdateOperation

public DatabaseUpdateOperation(DatabaseFactoryImpl factory,
                               int iUpdateType)
Constructor to use when database update doesn't require any prepared statement.

Parameters:
factory - - factory which is executing this operation
iUpdateType - - type of update, one of the constants defined above

DatabaseUpdateOperation

public DatabaseUpdateOperation(DatabaseFactoryImpl factory,
                               java.lang.String strQueryToPrepare,
                               ModifiableDatabaseSchema schema,
                               int iUpdateType,
                               java.lang.Object data)
Constructor to use when database update requires prepared statement.

Parameters:
factory - - factory which is executing this operation
strQueryToPrepare - - query which should be used to construct prepared statement which will be passed in to executeUpdate
schema - - database schema used with this operation
iUpdateType - - type of update, one of the constants defined above
data - - data used for operation
Method Detail

executeUpdate

public void executeUpdate()
                   throws OSSException
Method to execute database update invoking the user defined code in performOperation.

Throws:
OSSException - - an error has occured

performOperation

protected void performOperation(DatabaseFactoryImpl dbfactory,
                                java.sql.Connection cntConnection,
                                java.sql.PreparedStatement pstmStatement)
                         throws OSSException,
                                java.sql.SQLException
Define content of this method to perform the database operation using the provided connection and optional prepared statement.

Parameters:
dbfactory - - database factory used for this operation
cntConnection - - ready to use connection to perform the database operation. No need to return this connection.
pstmStatement - - prepared statement for query passed in as a parameter to the constructor. No need to close this statement. If no query was passed into constructor, this will be null.
Throws:
OSSException - - an error has occured
java.sql.SQLException - - an error has occured

prepareData

protected void prepareData(DataObject data)
Define content of this method to perform the prepare data (update dataobject attribute).

Parameters:
data - - data object the attributes will be updated for

setValuesForInsert

protected int setValuesForInsert(java.sql.PreparedStatement insertStatement,
                                 DataObject data,
                                 int initialIndex)
                          throws OSSException,
                                 java.sql.SQLException
Method sets values to the prepared statement for insert of data object.

Parameters:
insertStatement - - prepared statement the values will be set up for
data - - data object to insert, based on the type of the data object it can be determined what data are we inserting
initialIndex - - initial index for values to be set up into statement
Returns:
int - index of the last parameter in prepared statement (can be used for later processing outside of this method)
Throws:
OSSException - - exception during setting values
java.sql.SQLException - - exception during setting values

setValuesForUpdate

protected int setValuesForUpdate(java.sql.PreparedStatement updateStatement,
                                 DataObject data,
                                 int initialIndex)
                          throws OSSException,
                                 java.sql.SQLException
Method sets values to the prepared statement for update of data object.

Parameters:
updateStatement - - prepared statement the values will be set up for
data - - data object to update, based on the type of the data object it can be determined what data are we updating
initialIndex - - initial index for values to be set up into statement
Returns:
int - index of the last parameter in prepared statement (can be used for later processing outside of this method)
Throws:
OSSException - - exception during setting values
java.sql.SQLException - - exception during setting values

handleSQLException

protected void handleSQLException(java.sql.SQLException sqleExc,
                                  java.sql.Connection cntConnection,
                                  int iOperationType,
                                  int iDataType,
                                  java.lang.Object data)
                           throws OSSException
Provide custom handling of SQL Exceptions to usually detect constraint violation. By default just handle it as unknown error.

Parameters:
sqleExc - - SQLException to handle
cntConnection - - ready to use connection to perform the database operation. No need to return this connection.
iOperationType - - type of the operation that caused the exception, see DatabaseOperations for possible values
iDataType - - data type the data object represents (e.g if this is type user and data is Integer, that means it is id of user object). This is one of the DataConstant constants.
data - - data object the exception is handled for
Throws:
OSSException - - properly handled exception

handleKnownError

protected void handleKnownError(OSSException exc,
                                java.sql.Connection cntConnection,
                                int iOperationType,
                                int iDataType,
                                java.lang.Object data)
                         throws OSSException
Override this method to provide any custom error handling for expected error, which were most likely produced by lower layer. The default implementation just propagates this error.

Parameters:
exc - - known error which must be handled.
cntConnection - - ready to use connection to perform the database operation. No need to return this connection.
iOperationType - - type of the operation that caused the exception, see DatabaseOperations for possible values
iDataType - - data type the data object represents (e.g if this is type user and data is Integer, that means it is id of user object). This is one of the DataConstant constants.
data - - data object the exception is handled for
Throws:
OSSException - - properly handled exception

handleUnknownError

protected void handleUnknownError(java.lang.Throwable thr,
                                  java.sql.Connection cntConnection,
                                  int iOperationType,
                                  int iDataType,
                                  java.lang.Object data)
                           throws OSSException
Override this method to provide any custom error handling for unexpected error, which weren't handled by lower layer. The default implementation wraps it in proper error based on the type of operation.

Parameters:
thr - - throwable causing this error. This is not OSSException or a derived class.
cntConnection - - ready to use connection to perform the database operation. No need to return this connection.
iOperationType - - type of the operation that caused the exception, see DatabaseOperations for possible values
iDataType - - data type the data object represents (e.g if this is type user and data is Integer, that means it is id of user object). This is one of the DataConstant constants.
data - - data object the exception is handled for
Throws:
OSSException - - properly handled exception


Copyright © 2003 - 2006 OpenSubsystems s.r.o.