001: /*
002: * Created by IntelliJ IDEA.
003: * User: sg426575
004: * Date: May 13, 2006
005: * Time: 1:51:27 PM
006: */
007: package com.technoetic.xplanner.upgrade;
008:
009: import java.sql.Connection;
010: import java.sql.SQLException;
011:
012: import net.sf.hibernate.HibernateException;
013: import net.sf.hibernate.Session;
014: import org.springframework.jdbc.core.JdbcTemplate;
015: import org.springframework.jdbc.datasource.SingleConnectionDataSource;
016: import com.tacitknowledge.util.migration.MigrationContext;
017: import com.tacitknowledge.util.migration.MigrationException;
018: import com.tacitknowledge.util.migration.MigrationTaskSupport;
019: import com.tacitknowledge.util.migration.jdbc.DataSourceMigrationContext;
020: import com.tacitknowledge.util.migration.jdbc.JdbcMigrationContext;
021:
022: import com.technoetic.xplanner.db.hibernate.GlobalSessionFactory;
023: import com.technoetic.xplanner.db.hibernate.HibernateHelper;
024: import com.technoetic.xplanner.upgrade.schema.DBSchemaMigrater;
025:
026: public abstract class JdbcMigrationTaskSupport extends
027: MigrationTaskSupport {
028: protected DBSchemaMigrater migrater;
029: protected JdbcTemplate template;
030:
031: protected JdbcMigrationTaskSupport(String name, int level) {
032: setName(name);
033: setLevel(new Integer(level));
034: }
035:
036: private Connection getConnection(MigrationContext context)
037: throws MigrationException {
038: try {
039: return ((JdbcMigrationContext) context).getConnection();
040: } catch (SQLException e) {
041: throw new MigrationException("Could not open connection", e);
042: }
043: }
044:
045: final public void migrate(MigrationContext context)
046: throws MigrationException {
047: init(context);
048:
049: boolean inException = false;
050: try {
051: setUp();
052: migrate();
053: } catch (MigrationException e) {
054: throw e;
055: } catch (Exception e) {
056: inException = true;
057: throw new MigrationException("exception during migration",
058: e);
059: } finally {
060: try {
061: tearDown();
062: } catch (Exception e) {
063: if (!inException) {
064: throw new MigrationException(
065: "exception in tearDown", e);
066: }
067: }
068: }
069: }
070:
071: protected void init(MigrationContext context)
072: throws MigrationException {
073: Connection connection = getConnection(context);
074: migrater = new DBSchemaMigrater(connection);
075: template = new JdbcTemplate(new SingleConnectionDataSource(
076: connection, true));
077: }
078:
079: protected void setUp() throws Exception {
080: }
081:
082: protected void tearDown() throws Exception {
083: }
084:
085: abstract protected void migrate() throws Exception;
086:
087: /**
088: * Support for running a migration task in isolation (for manual testing for example)
089: * Warning: This should be used with caution since it does not update the patch table and therefore will not take care
090: * of preventing this task to run the next time
091: */
092: public void run() throws Exception {
093: Session session = newSession();
094: final Connection conn = session.connection();
095: try {
096: migrate(new DataSourceMigrationContext() {
097: public Connection getConnection() throws SQLException {
098: return conn;
099: }
100: });
101: } finally {
102: conn.commit();
103: session.close();
104: }
105: }
106:
107: private Session newSession() throws HibernateException {
108: HibernateHelper.initializeHibernate();
109: Session session = GlobalSessionFactory.get().openSession();
110: return session;
111: }
112: }
|