org.opensubsystems.patterns.mappeddata.persist.db
Class MappingDatabaseSchema

java.lang.Object
  extended by org.opensubsystems.core.persist.db.DatabaseSchemaImpl
      extended by org.opensubsystems.core.persist.db.ModifiableDatabaseSchemaImpl
          extended by org.opensubsystems.patterns.mappeddata.persist.db.MappingDatabaseSchema
All Implemented Interfaces:
org.opensubsystems.core.persist.db.DatabaseOperations, org.opensubsystems.core.persist.db.DatabaseSchema, org.opensubsystems.core.persist.db.ModifiableDatabaseSchema
Direct Known Subclasses:
DB2MappingDatabaseSchema, HsqlDBMappingDatabaseSchema, MSSQLMappingDatabaseSchema, MySQLMappingDatabaseSchema, OracleMappingDatabaseSchema, PostgreSQLMappingDatabaseSchema, SapDBMappingDatabaseSchema, SybaseMappingDatabaseSchema

public abstract class MappingDatabaseSchema
extends org.opensubsystems.core.persist.db.ModifiableDatabaseSchemaImpl

Database specific operations related to persistence of mapping tables. Multiple records from TABLE1 can be mapped to records in TABLE2. The total relationship between these two tables is is N:M. The same two records can be also mapped using different mapping types. Subsystems that want to create mappings should derive database schema class from this class. The only thing which is required is to implement constructor that provides attributes, that are use to construct database tables where the mappings will be stored. - 1st important rule is, that name of the table should contains part of: a.) name of the table the 1 related record is comming from b.) name of the table the M related records are comming from For example: a.) TABLE1 ... we want to map 1 record from this table b.) TABLE2 ... we want to map M records from this table mapped table name =

_TABLE1_TABLE2 - 2nd important rule is, that we will map 1 related item within the MAPPING_SCHEMA_COLUMN1 and we will map M related items within the MAPPING_SCHEMA_COLUMN2. This is important because within the method geSelectMappingRecord(int) we specify that we want to select all IDs stored in MAPPING_SCHEMA_COLUMN2 if we know ID from column MAPPING_SCHEMA_COLUMN1. This class also differs from other database schema clases, because it doesn't rely on inheritance to create database schema instance for specific database. This would require subsystem that uses this class to derive new class from each one of the database specific schemas. Instead this database schema creates directly instances of database schemas for specific databases and then executes their operation. This makes implementation of subsystems that use this schema much easier (only 1 schema class is required rather then N, one for each supported database) but when new database support is added, this class must change to hardcode support for that database.

Version:
$Id: MappingDatabaseSchema.java,v 1.14 2007/01/10 05:16:59 bastafidli Exp $
Author:
Julian Legeny
Code reviewer:
Miro Halas
Code reviewed:
1.9 2006/09/25 23:18:33 jlegeny

Field Summary
protected  java.lang.Class m_schema1
          Schema entry from which we are mapping.
protected  java.lang.Class m_schema2
          Schema entry from which we are mapping.
protected  java.lang.String m_strColumnName1
          Name of the column used for constructing of mapping table.
protected  java.lang.String m_strColumnName2
          Name of the column used for constructing of mapping table.
protected  java.lang.String m_strConstraintBody
          Constructed body of the constraint.
protected  java.lang.String m_strMapTableName
          Name of the mapping table that will be created.
protected  java.lang.String m_strTableName1
          Name of the table used for constructing of mapping table.
protected  java.lang.String m_strTableName2
          Name of the table used for constructing of mapping table.
static int MAPPING_CUSTOM_DATA_MAXLENGTH
          Maximal length of custom data.
static java.lang.String MAPPING_SCHEMA_COLUMN1
          Name identifies new column of the mapping table.
static java.lang.String MAPPING_SCHEMA_COLUMN2
          Name identifies new column of the mapping table.
static java.lang.String MAPPING_SCHEMA_NAME
          Name identifies this schema in the database.
static int MAPPING_SCHEMA_VERSION
          Version of this schema in the database.
 
Fields inherited from class org.opensubsystems.core.persist.db.ModifiableDatabaseSchemaImpl
m_mapModifiableTableNames
 
Fields inherited from class org.opensubsystems.core.persist.db.DatabaseSchemaImpl
DATABASE_SCHEMA_PREFIX, DATABASE_SCHEMA_PREFIX_DEFAULT, m_arrDependentSchemas, m_bIsInDomain, m_iSchemaVersion, m_strSchemaName, NL
 
Fields inherited from interface org.opensubsystems.core.persist.db.DatabaseOperations
DBOP_DELETE, DBOP_INSERT, DBOP_SELECT, DBOP_UPDATE
 
Constructor Summary
MappingDatabaseSchema(java.lang.String strMapTableName, java.lang.Class schema1, java.lang.String strTableName1, java.lang.String strColumnName1, java.lang.Class schema2, java.lang.String strTableName2, java.lang.String strColumnName2)
          Full constructor.
 
Method Summary
protected  java.lang.String constructConstraintName()
          Method constructs name of the constraint.
protected  java.lang.String constructSQL(java.lang.String strIDDefinition, java.lang.String strColDataType, java.lang.String strTextDataType, java.lang.String strTmstpDataType, java.lang.String strConstraintBody, boolean bCascade, boolean bPKConstraint)
          Method constructs sql for creating mapping table
 void create(java.sql.Connection cntDBConnection, java.lang.String strUserName)
          
 java.lang.String geSelectMappingMultipleRecords(java.lang.String strColumnIDs)
          Method returns simple select mapping multiple record query.
 java.lang.String geSelectMappingRecords()
          Method returns simple select mapping record query.
 java.lang.StringBuffer getColumns(boolean specific, int[] columns, java.lang.Object[] prefixes, java.lang.Object[] postfixes, java.lang.StringBuffer buffer)
          Convert any attribute regardless if it is in the main table or in join table into database column name.
protected  MappingDatabaseSchema getDatabaseSpecificInstance()
          Create instance of database specific schema.
 java.lang.String getDeleteMappedDataById()
          Get query to delete mapped data by id.
 java.lang.String getDeleteMappingRecord()
          Method returns simple delete mapping record query.
 java.lang.String getInsertMappingRecord()
          Method returns simple insert mapping record query.
 java.lang.String getSelectMappedDataById(int[] columns)
          Get query to select mapped data by id.
protected  void handleInsertMappedDataException(java.sql.SQLException exc)
          Common exception handler for insert of mapped data.
protected  void handleUpdateMappedDataException(java.sql.SQLException exc, java.sql.Connection dbConnection, MappedData data)
          Common exception handler for update of mapped data.
 MappedData insertMappedData(java.sql.Connection cntDBConnection, MappedData data)
          Insert MappedData to the database and return the generated values.
 MappedData updateMappedData(java.sql.Connection cntDBConnection, MappedData data)
          Update MappedData in the database and return the generated values.
 
Methods inherited from class org.opensubsystems.core.persist.db.ModifiableDatabaseSchemaImpl
deleteRelatedData, getModifiableTableNames, handleSQLException
 
Methods inherited from class org.opensubsystems.core.persist.db.DatabaseSchemaImpl
getDependentSchemas, getJoinFromWhere, getName, getOwnerColumnNames, getSchemaPrefix, getVersion, isExistingIndex, isInDomain, upgrade, upgradeView
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.opensubsystems.core.persist.db.DatabaseSchema
getDependentSchemas, getName, getVersion, isInDomain, upgrade
 

Field Detail

MAPPING_SCHEMA_NAME

public static final java.lang.String MAPPING_SCHEMA_NAME
Name identifies this schema in the database.

See Also:
Constant Field Values

MAPPING_SCHEMA_VERSION

public static final int MAPPING_SCHEMA_VERSION
Version of this schema in the database.

See Also:
Constant Field Values

MAPPING_SCHEMA_COLUMN1

public static final java.lang.String MAPPING_SCHEMA_COLUMN1
Name identifies new column of the mapping table. This column is referenced from table name 1 and column name 1.

See Also:
Constant Field Values

MAPPING_SCHEMA_COLUMN2

public static final java.lang.String MAPPING_SCHEMA_COLUMN2
Name identifies new column of the mapping table. This column is referenced from table name 1 and column name 1.

See Also:
Constant Field Values

MAPPING_CUSTOM_DATA_MAXLENGTH

public static final int MAPPING_CUSTOM_DATA_MAXLENGTH
Maximal length of custom data.

See Also:
Constant Field Values

m_strMapTableName

protected java.lang.String m_strMapTableName
Name of the mapping table that will be created. Name doesn't contain database schema prefix. Maximal length of the table name has to be 18 characters becouse of restriction for IBM DB2 database.


m_schema1

protected java.lang.Class m_schema1
Schema entry from which we are mapping.


m_strTableName1

protected java.lang.String m_strTableName1
Name of the table used for constructing of mapping table.


m_strColumnName1

protected java.lang.String m_strColumnName1
Name of the column used for constructing of mapping table.


m_schema2

protected java.lang.Class m_schema2
Schema entry from which we are mapping.


m_strTableName2

protected java.lang.String m_strTableName2
Name of the table used for constructing of mapping table.


m_strColumnName2

protected java.lang.String m_strColumnName2
Name of the column used for constructing of mapping table.


m_strConstraintBody

protected java.lang.String m_strConstraintBody
Constructed body of the constraint.

Constructor Detail

MappingDatabaseSchema

public MappingDatabaseSchema(java.lang.String strMapTableName,
                             java.lang.Class schema1,
                             java.lang.String strTableName1,
                             java.lang.String strColumnName1,
                             java.lang.Class schema2,
                             java.lang.String strTableName2,
                             java.lang.String strColumnName2)
                      throws org.opensubsystems.core.error.OSSException
Full constructor.

Parameters:
strMapTableName - - table name for mapping table
schema1 - - schema name the table 1 was defined in
strTableName1 - - name of the table 1
strColumnName1 - - name of the column 1
schema2 - - schema name the table 2 was defined in
strTableName2 - - name of the table 2
strColumnName2 - - name of the column 2
Throws:
org.opensubsystems.core.error.OSSException - - an error has occured
Method Detail

create

public void create(java.sql.Connection cntDBConnection,
                   java.lang.String strUserName)
            throws java.sql.SQLException,
                   org.opensubsystems.core.error.OSSException

Throws:
java.sql.SQLException
org.opensubsystems.core.error.OSSException

insertMappedData

public MappedData insertMappedData(java.sql.Connection cntDBConnection,
                                   MappedData data)
                            throws org.opensubsystems.core.error.OSSException
Insert MappedData to the database and return the generated values.

Parameters:
cntDBConnection - - database connection
data - - MappedData to insert
Returns:
MappedData - the same instance updated with generated values
Throws:
org.opensubsystems.core.error.OSSException - - an error has occured

updateMappedData

public MappedData updateMappedData(java.sql.Connection cntDBConnection,
                                   MappedData data)
                            throws org.opensubsystems.core.error.OSSException
Update MappedData in the database and return the generated values.

Parameters:
cntDBConnection - - database connection
data - - MappedData to update
Returns:
MappedData - the same instance updated with generated values
Throws:
org.opensubsystems.core.error.OSSException - - an error has occured

getInsertMappingRecord

public java.lang.String getInsertMappingRecord()
                                        throws org.opensubsystems.core.error.OSSException
Method returns simple insert mapping record query. This method is common for all databases.

Returns:
String - simple insert mapping record query
Throws:
org.opensubsystems.core.error.OSSException - - exception during getting query

getDeleteMappingRecord

public java.lang.String getDeleteMappingRecord()
                                        throws org.opensubsystems.core.error.OSSException
Method returns simple delete mapping record query. This method is common for all databases.

Returns:
String - simple delete mapping record query
Throws:
org.opensubsystems.core.error.OSSException - - exception during getting query

geSelectMappingRecords

public java.lang.String geSelectMappingRecords()
                                        throws org.opensubsystems.core.error.OSSException
Method returns simple select mapping record query. This method is common for all databases.

Returns:
String - simple select mapping record query
Throws:
org.opensubsystems.core.error.OSSException - - exception during getting query

geSelectMappingMultipleRecords

public java.lang.String geSelectMappingMultipleRecords(java.lang.String strColumnIDs)
                                                throws org.opensubsystems.core.error.OSSException
Method returns simple select mapping multiple record query. This method is common for all databases.

Parameters:
strColumnIDs - - string representation of column IDs separated by ','
Returns:
String - simple select mapping record query
Throws:
org.opensubsystems.core.error.OSSException - - exception during getting query

getSelectMappedDataById

public java.lang.String getSelectMappedDataById(int[] columns)
                                         throws org.opensubsystems.core.error.OSSException
Get query to select mapped data by id.

Parameters:
columns - - list of columns to retrieve
Returns:
String - query
Throws:
org.opensubsystems.core.error.OSSException - - an error has occured

getDeleteMappedDataById

public java.lang.String getDeleteMappedDataById()
Get query to delete mapped data by id.

Returns:
String - query

getColumns

public java.lang.StringBuffer getColumns(boolean specific,
                                         int[] columns,
                                         java.lang.Object[] prefixes,
                                         java.lang.Object[] postfixes,
                                         java.lang.StringBuffer buffer)
                                  throws org.opensubsystems.core.error.OSSException
Convert any attribute regardless if it is in the main table or in join table into database column name.

Parameters:
specific - - if true - table_name.table_column, if false only table_column
columns - - specific array of columns codes
prefixes - - specific array of prefixes to prepend before each column, each prefix will be mapped with column and the same index they will be directly prepended or appended to the column name and therefore they need to contain any extra space if necessary, specify null, if no prefixes should be prepended
postfixes - - specific array of postfixes to prepend before each column, each prefix will be mapped with column and the same index they will be directly prepended or appended to the column name and therefore they need to contain any extra space if necessary, specify null, if no prefixes should be prepended
buffer - - buffer to use for column codes construction, it may already contain some value, if null, new one will be allocated
Returns:
StringBuffer - specific columns of table divided by comma with specified prefixes and postfixes
Throws:
org.opensubsystems.core.error.OSSException - - error in columns find

handleInsertMappedDataException

protected void handleInsertMappedDataException(java.sql.SQLException exc)
                                        throws org.opensubsystems.core.error.OSSException
Common exception handler for insert of mapped data.

Parameters:
exc - - exception which has occured
Throws:
org.opensubsystems.core.error.OSSException - - more descriptive exception

handleUpdateMappedDataException

protected void handleUpdateMappedDataException(java.sql.SQLException exc,
                                               java.sql.Connection dbConnection,
                                               MappedData data)
                                        throws org.opensubsystems.core.error.OSSException
Common exception handler for update of mapped data.

Parameters:
exc - - exception which has occured
dbConnection - - database connection
data - - data being updated
Throws:
org.opensubsystems.core.error.OSSException - - more descriptive exception

constructSQL

protected java.lang.String constructSQL(java.lang.String strIDDefinition,
                                        java.lang.String strColDataType,
                                        java.lang.String strTextDataType,
                                        java.lang.String strTmstpDataType,
                                        java.lang.String strConstraintBody,
                                        boolean bCascade,
                                        boolean bPKConstraint)
Method constructs sql for creating mapping table

Parameters:
strIDDefinition - - definition for ID column
strColDataType - - datatype of the new column
strTextDataType - - datatype of the text column (custom data)
strTmstpDataType - - datatype of the timestamp columns (creation and modification date)
strConstraintBody - - body of the constraint name
bCascade - - flag signaling if there will be added ON DELETE CASCADE to foreign key constraint definition
bPKConstraint - - flag signaling if there will be defined primary key constraint
Returns:
String - constructed command for creating mapping table

constructConstraintName

protected java.lang.String constructConstraintName()
Method constructs name of the constraint. This index name should be common for all DB systems. But there is restriction for IBM DB2 database, the length of the constraint name shold be max. 18 characters. We will construct this name from column name 1 and column name 2. Template is: where and have length 7 characters and are constructed from table names. Prefix BF_ from the table names will be removed first. Also all others occurences of the character '_' will be removed. There are used max 7. characters from the table name: 3 characters from the begining of the table name and 4 characters from the end of the table name string. For example: we have 2 tables: BF_TAB_NAME_1 and BF_ANOTHER_TABLE constructed constraint name is TABAME1ANOABLE

Returns:
String - constructed name of the constraint

getDatabaseSpecificInstance

protected MappingDatabaseSchema getDatabaseSpecificInstance()
                                                     throws org.opensubsystems.core.error.OSSException
Create instance of database specific schema.

Returns:
MappingDatabaseSchema - new instacne of specific database schema
Throws:
org.opensubsystems.core.error.OSSException - - an error has occured


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