001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.jpa.entity;
021    
022    import org.apache.commons.lang.builder.ReflectionToStringBuilder;
023    import org.apache.commons.lang.builder.ToStringStyle;
024    
025    import javax.persistence.*;
026    import java.sql.Connection;
027    import java.sql.ResultSet;
028    import java.sql.SQLException;
029    import java.sql.Statement;
030    
031    @Entity
032    @Table(name = SchemaMigration.TABLE_NAME, uniqueConstraints = {@UniqueConstraint(columnNames = {"version"})})
033    public class SchemaMigration {
034    
035      public final static int VERSION_UNKNOWN = -1;
036    
037      public static final int LAST_VERSION = 241;
038      public static final int VERSION_2_13 = 241;
039    
040      public final static String TABLE_NAME = "schema_migrations";
041    
042      @Id
043      @Column(name = "version", updatable = true)
044      private String version;
045    
046      public String getVersion() {
047        return version;
048      }
049    
050      public void setVersion(String s) {
051        this.version = s;
052      }
053    
054      public void setVersion(int i) {
055        this.version = String.valueOf(i);
056      }
057    
058      public static int getCurrentVersion(Connection connection) {
059        Statement stmt = null;
060        ResultSet rs = null;
061        int version = VERSION_UNKNOWN;
062        try {
063          stmt = connection.createStatement();
064          rs = stmt.executeQuery("SELECT version FROM " + SchemaMigration.TABLE_NAME);
065          while (rs.next()) {
066            int i = Integer.parseInt(rs.getString(1));
067            if (i > version) {
068              version = i;
069            }
070          }
071        } catch (SQLException e) {
072          // ignore
073        } finally {
074          close(rs);
075          close(stmt);
076        }
077    
078        return version;
079      }
080    
081      private static void close(ResultSet rs) {
082        if (rs != null) {
083          try {
084            rs.close();
085          } catch (SQLException e) {
086            // why does close() throw a checked-exception ???
087          }
088        }
089      }
090    
091      private static void close(Statement st) {
092        if (st != null) {
093          try {
094            st.close();
095          } catch (SQLException e) {
096            // why does close() throw a checked-exception ???
097          }
098        }
099      }
100    
101      @Override
102      public String toString() {
103        return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString();
104      }
105    }