KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > persist > db > postgresql > PostgreSQLVersionedDatabaseSchema


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: PostgreSQLVersionedDatabaseSchema.java,v 1.7 2007/01/07 06:14:43 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.postgresql;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.sql.Statement JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30
31 import org.opensubsystems.core.error.OSSDatabaseAccessException;
32 import org.opensubsystems.core.error.OSSException;
33 import org.opensubsystems.core.persist.db.DatabaseTransactionFactoryImpl;
34 import org.opensubsystems.core.persist.db.VersionedDatabaseSchema;
35 import org.opensubsystems.core.util.DatabaseUtils;
36 import org.opensubsystems.core.util.Log;
37
38 /**
39  * PostgreSQL versioned database schema provide functionality of creating
40  * and upgrading of Postgre SQL database schemas in the database based on
41  * their versions.
42  * This class keeps track of existing and current versiones of PostgreSQL
43  * database schemas and upgrades them as necesary
44  *
45  * @version $Id: PostgreSQLVersionedDatabaseSchema.java,v 1.7 2007/01/07 06:14:43 bastafidli Exp $
46  * @author Julo Legeny
47  * @code.reviewer Miro Halas
48  * @code.reviewed 1.3 2004/12/18 06:18:37 bastafidli
49  */

50 public class PostgreSQLVersionedDatabaseSchema extends VersionedDatabaseSchema
51 {
52
53    // Cached values ////////////////////////////////////////////////////////////
54

55    /**
56     * Logger for this class
57     */

58    private static Logger JavaDoc s_logger = Log.getInstance(PostgreSQLVersionedDatabaseSchema.class);
59
60    // Constructors /////////////////////////////////////////////////////////////
61

62    /**
63     * @throws OSSException - database cannot be started.
64     */

65    public PostgreSQLVersionedDatabaseSchema(
66    ) throws OSSException
67    {
68       super();
69    }
70    
71    /**
72     * {@inheritDoc}
73     */

74    public void loadExistingSchemas(
75       Connection JavaDoc cntDBConnection,
76       Map JavaDoc mpSchemasToAdd,
77       Map JavaDoc mpSchemasToUpgrade
78    ) throws OSSException
79    {
80       super.loadExistingSchemas(cntDBConnection, mpSchemasToAdd, mpSchemasToUpgrade);
81
82       try
83       {
84          DatabaseTransactionFactoryImpl.getInstance().commitTransaction(cntDBConnection);
85       }
86       catch (Throwable JavaDoc thr)
87       {
88          s_logger.log(Level.SEVERE, "Failed to initialize database.",
89                              thr);
90          if (cntDBConnection != null)
91          {
92             try
93             {
94                // At this point we don't know if this is just a single operation
95
// and we need to commit or if it is a part of bigger transaction
96
// and the commit is not desired until all operations proceed.
97
// Therefore let the DatabaseTransactionFactory resolve it
98
DatabaseTransactionFactoryImpl.getInstance().rollbackTransaction(
99                                                        cntDBConnection);
100             }
101             catch (SQLException JavaDoc sqleExc)
102             {
103                // Ignore this
104
s_logger.log(Level.WARNING,
105                                    "Failed to rollback changes for creation of database.",
106                                    sqleExc);
107             }
108          }
109          throw new OSSDatabaseAccessException("Failed to initialize database.",
110                                              thr);
111       }
112    }
113
114    /**
115     * {@inheritDoc}
116     */

117    public void create(
118       Connection JavaDoc cntDBConnection,
119       String JavaDoc strUserName
120    ) throws SQLException JavaDoc,
121             OSSException
122    {
123       s_logger.entering(this.getClass().getName(), "create");
124
125       try
126       {
127          // for postgres database there is neccessary to created
128
// user defined type
129
createUserDefinedType(cntDBConnection, strUserName);
130       }
131       finally
132       {
133          s_logger.exiting(this.getClass().getName(), "create");
134       }
135
136       super.create(cntDBConnection, strUserName);
137    }
138
139    /**
140     * {@inheritDoc}
141     */

142    public void upgrade(
143       Connection JavaDoc cntDBConnection,
144       String JavaDoc strUserName,
145       int iOriginalVersion
146    ) throws SQLException JavaDoc
147    {
148       s_logger.entering(this.getClass().getName(), "upgrade");
149
150       try
151       {
152          if (iOriginalVersion == 1)
153          {
154             // if we are upgrating from version 1 we need to create
155
// user defined type for postgres database
156
createUserDefinedType(cntDBConnection, strUserName);
157          }
158       }
159       finally
160       {
161          s_logger.exiting(this.getClass().getName(), "upgrade");
162       }
163       
164       super.upgrade(cntDBConnection, strUserName, iOriginalVersion);
165    }
166    
167    // Helper methods
168

169    /**
170     * Create user defined type that stores return values (generated id and timestamp)
171     * from stored procedures.
172     *
173     * @param cntDBConnection - database connection
174     * @param strUserName - user name
175     * @throws SQLException - exception occured during creating user defined type
176     */

177    protected void createUserDefinedType(
178       Connection JavaDoc cntDBConnection,
179       String JavaDoc strUserName
180    ) throws SQLException JavaDoc
181    {
182       s_logger.entering(this.getClass().getName(), "createUserDefinedType");
183
184       try
185       {
186          Statement JavaDoc stmQuery = null;
187          try
188          {
189
190             stmQuery = cntDBConnection.createStatement();
191             
192             if (stmQuery.execute("CREATE TYPE type_int_timestamp AS (intgr INTEGER, " +
193                                  "tmstp TIMESTAMP WITH TIME ZONE)"))
194             {
195             // Close any results
196
stmQuery.getMoreResults(Statement.CLOSE_ALL_RESULTS);
197             }
198             s_logger.log(Level.FINEST, "Type 'type_int_timestamp' created.");
199          }
200          catch (SQLException JavaDoc sqleExc)
201          {
202             // Catch this just so we can log the message
203
s_logger.log(Level.WARNING, "Failed to create int - timestamp type.",
204                                 sqleExc);
205             throw sqleExc;
206          }
207          finally
208          {
209             DatabaseUtils.closeStatement(stmQuery);
210          }
211       }
212       finally
213       {
214          s_logger.exiting(this.getClass().getName(), "createUserDefinedType");
215       }
216    }
217 }
218
Popular Tags