KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: DatabaseSchemaImpl.java,v 1.25 2007/01/10 05:11:33 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.sql.Statement JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31
32 import org.opensubsystems.core.error.OSSDatabaseAccessException;
33 import org.opensubsystems.core.error.OSSException;
34 import org.opensubsystems.core.util.Config;
35 import org.opensubsystems.core.util.DatabaseUtils;
36 import org.opensubsystems.core.util.GlobalConstants;
37 import org.opensubsystems.core.util.Log;
38
39 /**
40  * Base class implementation for database schemas that provides the common
41  * functionality needed by all schemas that allow to read data from the tables
42  * managed by this schema.
43  *
44  * @version $Id: DatabaseSchemaImpl.java,v 1.25 2007/01/10 05:11:33 bastafidli Exp $
45  * @author Miro Halas
46  * @code.reviewer Miro Halas
47  * @code.reviewed 1.16 2006/07/21 22:47:52 jlegeny
48  */

49 public abstract class DatabaseSchemaImpl implements DatabaseSchema,
50                                                     DatabaseOperations
51 {
52    // Configuration settings ///////////////////////////////////////////////////
53

54    /**
55     * Name of the property that can specify default prefix to use for database
56     * objects.
57     */

58    public static final String JavaDoc DATABASE_SCHEMA_PREFIX = "oss.dbschema.prefix";
59
60    // Constants ////////////////////////////////////////////////////////////////
61

62    /**
63     * Default prefix to use for database objects.
64     */

65    public static final String JavaDoc DATABASE_SCHEMA_PREFIX_DEFAULT = "BF_";
66    
67    /**
68     * New line constant which should be used when creating any table or stored
69     * procedure in the database. We can reverse engineer them from database e.g.
70     * using CaseStudio but if they do not contain new lines then they appear as
71     * single long string and it is impossible to find out what has changed.
72     */

73    public static final String JavaDoc NL = "\n";
74    
75    // Attributes ///////////////////////////////////////////////////////////////
76

77    /**
78     * Prefix to use for database objects.
79     */

80    private static String JavaDoc s_strSchemaPrefix;
81
82    /**
83     * Array of dependent schemas.
84     */

85    protected DatabaseSchema[] m_arrDependentSchemas;
86
87    /**
88     * Name of the schema.
89     */

90    protected String JavaDoc m_strSchemaName;
91
92    /**
93     * Version of the schema.
94     */

95    protected int m_iSchemaVersion;
96
97    /**
98     * Flag signaling if object is in domain.
99     */

100    protected boolean m_bIsInDomain;
101
102    // Cached values ////////////////////////////////////////////////////////////
103

104    /**
105     * Logger for this class
106     */

107    private static Logger JavaDoc s_logger = Log.getInstance(DatabaseSchemaImpl.class);
108
109    // Constructors /////////////////////////////////////////////////////////////
110

111    /**
112     * Static initializer
113     */

114    static
115    {
116       Properties JavaDoc prpSettings;
117       
118       prpSettings = Config.getInstance().getPropertiesSafely();
119       s_strSchemaPrefix = Config.getStringProperty(prpSettings,
120                                                    DATABASE_SCHEMA_PREFIX,
121                                                    DATABASE_SCHEMA_PREFIX_DEFAULT,
122                                                    "Defaut database table prefix",
123                                                    false);
124       if (s_strSchemaPrefix.length() > DATABASE_SCHEMA_PREFIX_DEFAULT.length())
125       {
126          s_logger.config("Length of " + DATABASE_SCHEMA_PREFIX + " value is" +
127                          " longer than the default value " +
128                          DATABASE_SCHEMA_PREFIX_DEFAULT + ". This may cause" +
129                          " issues for some databases.");
130       }
131    }
132
133    /**
134     * Constructor allowing to specify attributes for database schema that
135     * doesn't provide any operations that would modify any of the tables it is
136     * accessing.
137     *
138     * @param arrDependentSchemas - array of dependent schemas
139     * @param strSchemaName - name of the schema
140     * @param iSchemaVersion - version of the schema
141     * @param bIsInDomain - flag signaling if object is in domain
142     * @throws OSSException - an error has occured
143     */

144    public DatabaseSchemaImpl(
145       DatabaseSchema[] arrDependentSchemas,
146       String JavaDoc strSchemaName,
147       int iSchemaVersion,
148       boolean bIsInDomain
149    ) throws OSSException
150    {
151       super();
152       
153       m_arrDependentSchemas = arrDependentSchemas;
154       m_strSchemaName = strSchemaName;
155       m_iSchemaVersion = iSchemaVersion;
156       m_bIsInDomain = bIsInDomain;
157    }
158
159    // Logic ////////////////////////////////////////////////////////////////////
160

161    /**
162     * {@inheritDoc}
163     */

164    public void upgrade(
165       Connection JavaDoc cntDBConnection,
166       String JavaDoc strUserName,
167       int iOriginalVersion
168    ) throws SQLException JavaDoc
169    {
170       // Most schemas do not provide upgrade method unless they were shipped
171
// to customer and then they were changed. Therefore this empty method
172
// shields the derived classes from the need to implement this method
173
}
174    
175    /**
176     * Create SQL query fragments, which can be used to create database join
177     * between multiple database tables.
178     *
179     * This is convenience implementation since most data objects do not cross
180     * multiple tables.
181     *
182     * @param conditions - list of DataConditions, which may represent search
183     * criterias in multiple tables which may need to be joined
184     * @param columns - columns we are trying to retrieve
185     * @return String[] - index 0 - comma separated tables to add to from
186     * - index 1 - join conditions to add to where using and
187     * - or null if no extra joins are necessary
188     */

189    public String JavaDoc[] getJoinFromWhere(
190       List JavaDoc conditions,
191       int[] columns
192    )
193    {
194       return null;
195    }
196
197    /**
198     * Check if index with the specified name exists within this schema. If this
199     * index exists it can be used for constructing querie to retrieve records.
200     *
201     * This is convenience implementation since most data objects do not need
202     * special indexes for their queries.
203     *
204     * @param strIndexName - name of the index existency of which we want to check
205     * @return boolean - true if the index with that name exists in this schema
206     * and can be used to speed up queries
207     */

208    public boolean isExistingIndex(
209       String JavaDoc strIndexName
210    )
211    {
212       return false;
213    }
214
215    /**
216     * Get prefix which should be used to construct database objects.
217     *
218     * @return String - prefix to use for database objects.
219     */

220    public static String JavaDoc getSchemaPrefix(
221    )
222    {
223       return s_strSchemaPrefix;
224    }
225
226    // Helper methods ///////////////////////////////////////////////////////////
227

228    /**
229     * Upgrade the view by dropping it and recreating it again. Developer has to
230     * make sure that no view depends on it since the depending view will be
231     * dropped too. This method works if the schema contain only a single view.
232     *
233     * @param cntDBConnection - valid connection to database
234     * @param strUserName - name of user who will be accessing this table
235     * @param iOriginalVersion - original version from which to upgrade
236     * @param strViewName - view to upgrade
237     * @throws SQLException - problem creating the database schema
238     * @throws OSSException - problem creating the database schema
239     */

240    protected void upgradeView(
241       Connection JavaDoc cntDBConnection,
242       String JavaDoc strUserName,
243       int iOriginalVersion,
244       String JavaDoc strViewName
245    ) throws SQLException JavaDoc, OSSException
246    {
247       // The faster way to upgrade a view is to drop it and recreate it
248
Statement JavaDoc stmQuery = null;
249       try
250       {
251          int iVersion = iOriginalVersion;
252          
253          stmQuery = cntDBConnection.createStatement();
254          
255          if (iVersion < getVersion())
256          {
257             if (stmQuery.execute("drop view " + strViewName))
258             {
259                // Close any results
260
stmQuery.getMoreResults(Statement.CLOSE_ALL_RESULTS);
261             }
262             s_logger.log(Level.FINEST, "View " + strViewName + " deleted.");
263
264             // And now recreate it again
265
create(cntDBConnection, strUserName);
266          }
267       }
268       catch (SQLException JavaDoc sqleExc)
269       {
270          s_logger.log(Level.WARNING, "Failed to upgrade schema" + getName() +
271                " from version " + iOriginalVersion + " to version " + getVersion(),
272                sqleExc);
273          throw sqleExc;
274       }
275       finally
276       {
277          DatabaseUtils.closeStatement(stmQuery);
278       }
279    }
280
281    /**
282     * This method will return array of names of all columns which represents
283     * owner column. These columns will be checked for Current User Id in case
284     * of OWNED security data condition.
285     *
286     * @return String[] - array of Column Name which represents owner
287     * @deprecated
288     */

289    // TODO: For Miro: Remove this after you are done with removing all overriden methods
290
protected final String JavaDoc[] getOwnerColumnNames()
291    {
292       return null;
293    }
294
295    /**
296     * {@inheritDoc}
297     */

298    public boolean isInDomain()
299    {
300       return m_bIsInDomain;
301    }
302
303    /**
304     * {@inheritDoc}
305     */

306    public DatabaseSchema[] getDependentSchemas(
307    ) throws OSSException
308    {
309       return m_arrDependentSchemas;
310    }
311
312    /**
313     * {@inheritDoc}
314     */

315    public String JavaDoc getName(
316    )
317    {
318       return m_strSchemaName;
319    }
320    
321    /**
322     * {@inheritDoc}
323     */

324    public int getVersion(
325    )
326    {
327       return m_iSchemaVersion;
328    }
329    
330    /**
331     * {@inheritDoc}
332     */

333    public void handleSQLException(
334       SQLException JavaDoc exc,
335       Connection JavaDoc dbConnection,
336       int iOperationType,
337       int iDataType,
338       Object JavaDoc data
339    ) throws OSSException
340    {
341       switch (iOperationType)
342       {
343          case (DBOP_SELECT) :
344          {
345             throw new OSSDatabaseAccessException(
346                          "Failed to read data from the database.", exc);
347          }
348          default:
349          {
350             if (GlobalConstants.ERROR_CHECKING)
351             {
352                assert false : "Unknown update operation type " + iOperationType;
353             }
354          }
355       }
356    }
357 }
358
Popular Tags