KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > persist > db > ModifiableDatabaseSchemaImpl


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: ModifiableDatabaseSchemaImpl.java,v 1.1 2007/01/10 05:09:38 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.persist.db;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.opensubsystems.core.data.DataConstant;
30 import org.opensubsystems.core.data.ModifiableDataObject;
31 import org.opensubsystems.core.error.OSSDataCreateException;
32 import org.opensubsystems.core.error.OSSDataDeleteException;
33 import org.opensubsystems.core.error.OSSDataSaveException;
34 import org.opensubsystems.core.error.OSSException;
35
36 /**
37  * Base class implementation for database schemas that provide queries or
38  * operations that allow to modify data in tables managed by this schema.
39  *
40  * @version $Id: ModifiableDatabaseSchemaImpl.java,v 1.1 2007/01/10 05:09:38 bastafidli Exp $
41  * @author Miro Halas
42  * @code.reviewer Miro Halas
43  * @code.reviewed Initial revision.
44  */

45 public abstract class ModifiableDatabaseSchemaImpl extends DatabaseSchemaImpl
46                                                    implements ModifiableDatabaseSchema
47 {
48    // Attributes ///////////////////////////////////////////////////////////////
49

50    /**
51     * Map of all tables belonging to this schema that can be modified by the
52     * operations provided by the schema (e.g. schema allows insert, update or
53     * delete on this table). Key is the data type, value is the string with the
54     * table name.
55     */

56    protected Map JavaDoc m_mapModifiableTableNames;
57
58    // Constructors /////////////////////////////////////////////////////////////
59

60    /**
61     * Constructor allowing to specify attributes for database schema that
62     * support multiple data object types and therefore have multiple sets of
63     * tables that can be modified and columns sets.
64     *
65     * @param arrDependentSchemas - array of dependent schemas
66     * @param strSchemaName - name of the schema
67     * @param iSchemaVersion - version of the schema
68     * @param bIsInDomain - flag signaling if object is in domain
69     * @param mapModifiableTableNames - map of all tables belonging to this schema
70     * that can be modified by the schema (e.g.
71     * schema allows insert, update or delete on
72     * this table)
73     * @throws OSSException - an error has occured
74     */

75    public ModifiableDatabaseSchemaImpl(
76       DatabaseSchema[] arrDependentSchemas,
77       String JavaDoc strSchemaName,
78       int iSchemaVersion,
79       boolean bIsInDomain,
80       Map JavaDoc mapModifiableTableNames
81    ) throws OSSException
82    {
83       super(arrDependentSchemas, strSchemaName, iSchemaVersion, bIsInDomain);
84       
85       m_mapModifiableTableNames = mapModifiableTableNames;
86    }
87
88    /**
89     * Constructor allowing to specify attributes for database schema that
90     * support only single data object type and therefore have most likely only
91     * one table name that can be modified and single columns sets.
92     *
93     * @param arrDependentSchemas - array of dependent schemas
94     * @param strSchemaName - name of the schema
95     * @param iSchemaVersion - version of the schema
96     * @param iModifiableDataType - data type that can be modified using
97     * operations in this schema
98     * @param strModifiableTableName - table belonging to this schema that can be
99     * modified by the schema (e.g. schema allows
100     * insert, update or delete on this table)
101     * @param bIsInDomain - flag signaling if object is in domain
102     * @throws OSSException - an error has occured
103     */

104    public ModifiableDatabaseSchemaImpl(
105       DatabaseSchema[] arrDependentSchemas,
106       String JavaDoc strSchemaName,
107       int iSchemaVersion,
108       boolean bIsInDomain,
109       Integer JavaDoc iModifiableDataType,
110       String JavaDoc strModifiableTableName
111    ) throws OSSException
112    {
113       super(arrDependentSchemas, strSchemaName, iSchemaVersion, bIsInDomain);
114       
115       if ((strModifiableTableName != null) && (strModifiableTableName.length() > 0))
116       {
117          m_mapModifiableTableNames = new HashMap JavaDoc(1);
118          m_mapModifiableTableNames.put(iModifiableDataType, strModifiableTableName);
119       }
120    }
121
122    // Logic ////////////////////////////////////////////////////////////////////
123

124    /**
125     * Method deletes related child data when main data object is being deleted.
126     * This method has to be overriden for databases that don't support cascade
127     * delete (eg. Sybase ASE).
128     *
129     * @param cntDBConnection - valid connection to database
130     * @param iDataType - data type identifying what to delete
131     * @param iId - ID of the record that has to be deleted
132     * @throws OSSException - problem deleting related data
133     * @throws SQLException - problem deleting related data
134     * @return int - number of deleted records
135     */

136    public int deleteRelatedData(
137       Connection JavaDoc cntDBConnection,
138       int iDataType,
139       int iId
140    ) throws OSSException,
141             SQLException JavaDoc
142    {
143       // Default implementation does nothing
144
return 0;
145    }
146
147    // Helper methods ///////////////////////////////////////////////////////////
148

149    /**
150     * {@inheritDoc}
151     */

152    public Map JavaDoc getModifiableTableNames(
153    )
154    {
155       return m_mapModifiableTableNames;
156    }
157
158    /**
159     * {@inheritDoc}
160     */

161    public void handleSQLException(
162       SQLException JavaDoc exc,
163       Connection JavaDoc dbConnection,
164       int iOperationType,
165       int iDataType,
166       Object JavaDoc data
167    ) throws OSSException
168    {
169       switch (iOperationType)
170       {
171          case (DBOP_INSERT) :
172          {
173             throw new OSSDataCreateException(
174                           "Failed to create data in the database.", exc);
175          }
176          case (DBOP_UPDATE) :
177          {
178             // This will detects conflict when two users tries to modify the
179
// same data and one of them saves the changes and then other tries
180
// to overwrite them without knowing that the data were modified
181
if ((exc.getMessage().indexOf("[100]") > -1)
182                && (data instanceof ModifiableDataObject))
183             {
184                Integer JavaDoc iDataTypeCode = new Integer JavaDoc(iDataType);
185                
186                DatabaseDataUtils.checkUpdateError(
187                   dbConnection,
188                   DataConstant.getDataTypeName(iDataTypeCode),
189                   (String JavaDoc)getModifiableTableNames().get(iDataTypeCode),
190                   ((ModifiableDataObject)data).getId(),
191                   ((ModifiableDataObject)data).getModificationTimestamp());
192             }
193             // If this wasn't concurrent modification, throw the generic error
194
throw new OSSDataSaveException(
195                          "Failed to update data in the database.", exc);
196          }
197          case (DBOP_DELETE) :
198          {
199             throw new OSSDataDeleteException(
200                          "Failed to delete data from the database.", exc);
201          }
202          default:
203          {
204             super.handleSQLException(exc, dbConnection, iOperationType,
205                                      iDataType, data);
206          }
207       }
208    }
209 }
210
Popular Tags