001/*
002 $Id: SchemaVersion.java 3207 2009-04-09 06:48:11Z gregory $
003
004 Copyright (C) 2007 Gregory Vincic, Olle Mansson
005
006 This file is part of Proteios.
007 Available at http://www.proteios.org/
008
009 Proteios is free software; you can redistribute it and/or modify it
010 under the terms of the GNU General Public License as published by
011 the Free Software Foundation; either version 2 of the License, or
012 (at your option) any later version.
013
014 Proteios is distributed in the hope that it will be useful, but
015 WITHOUT ANY WARRANTY; without even the implied warranty of
016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
017 General Public License for more details.
018
019 You should have received a copy of the GNU General Public License
020 along with this program; if not, write to the Free Software
021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
022 02111-1307, USA.
023 */
024package org.proteios.core;
025
026import org.proteios.core.data.SchemaVersionData;
027
028/**
029 * This class contains methods to get and set the schema version data in the
030 * database.
031 * 
032 * @author Olle
033 * @version 2.0
034 * @proteios.modified $Date: 2007-02-06 10:14:33Z $
035 */
036public class SchemaVersion
037{
038        /**
039         * Log core events.
040         */
041        private static final org.apache.log4j.Logger log = org.apache.log4j.LogManager
042                .getLogger("org.proteios.core");
043
044
045        /**
046         * Get the current application build number as it is stored in the database.
047         * 
048         * @param session org.hibernate.Session a Hibernate session to use for
049         *        queries.
050         * @return int The application build number, or 0 if no application build is
051         *         found
052         */
053        public static int getApplicationBuild(org.hibernate.Session session)
054                        throws BaseException
055        {
056                org.hibernate.Transaction tx = null;
057                int buildNumber = 0;
058                try
059                {
060                        tx = HibernateUtil.newTransaction(session);
061                        org.hibernate.Query query = HibernateUtil.getPredefinedQuery(
062                                session, "GET_SCHEMA_VERSION");
063                        SchemaVersionData data = HibernateUtil.loadData(
064                                SchemaVersionData.class, query);
065                        buildNumber = data == null ? 0 : data.getBuild();
066                        HibernateUtil.commit(tx);
067                }
068                catch (BaseException ex)
069                {
070                        if (tx != null)
071                        {
072                                HibernateUtil.rollback(tx);
073                        }
074                        log.error("SchemaVersion::getApplicationBuild(): FAILED", ex);
075                        throw ex;
076                }
077                return buildNumber;
078        }
079
080
081        /**
082         * Get the current schema version number as it is stored in the database.
083         * 
084         * @param session org.hibernate.Session a Hibernate session to use for
085         *        queries.
086         * @return int The schema version number, or 0 if no schema version is found
087         */
088        public static int getSchemaVersion(org.hibernate.Session session)
089                        throws BaseException
090        {
091                org.hibernate.Transaction tx = null;
092                int schemaVersion = 0;
093                try
094                {
095                        tx = HibernateUtil.newTransaction(session);
096                        org.hibernate.Query query = HibernateUtil.getPredefinedQuery(
097                                session, "GET_SCHEMA_VERSION");
098                        SchemaVersionData data = HibernateUtil.loadData(
099                                SchemaVersionData.class, query);
100                        schemaVersion = data == null ? 0 : data.getSchemaVersion();
101                        HibernateUtil.commit(tx);
102                }
103                catch (BaseException ex)
104                {
105                        if (tx != null)
106                        {
107                                HibernateUtil.rollback(tx);
108                        }
109                        log.error("SchemaVersion::getSchemaVersion(): FAILED", ex);
110                        throw ex;
111                }
112                return schemaVersion;
113        }
114
115
116        public static int getSchemaVersion()
117                        throws BaseException
118        {
119                org.hibernate.Session session = HibernateUtil.newSession();
120                int version = SchemaVersion.getSchemaVersion(session);
121                session.close();
122                return version;
123        }
124
125
126        /**
127         * Update the database with a new schema version number. This method should
128         * be called from an open transaction.
129         * 
130         * @param session org.hibernate.Session a Hibernate session to use for
131         *        queries.
132         * @param schemaVersion int The new schema version number
133         */
134        public static void setSchemaVersion(org.hibernate.Session session,
135                        int schemaVersion)
136                        throws BaseException
137        {
138                org.hibernate.Query query = HibernateUtil.getPredefinedQuery(session,
139                        "SET_SCHEMA_VERSION");
140                query.setInteger("schemaVersion", schemaVersion);
141                query.setInteger("build", Application.getBuild());
142                HibernateUtil.executeUpdate(query);
143        }
144}