| 1 |  |  package org.kuali.db.jdbc; | 
  | 2 |  |   | 
  | 3 |  |  import java.sql.Connection; | 
  | 4 |  |  import java.sql.SQLException; | 
  | 5 |  |   | 
  | 6 |  |  import org.apache.commons.logging.Log; | 
  | 7 |  |  import org.apache.commons.logging.LogFactory; | 
  | 8 |  |   | 
  | 9 | 0 |  public class DatabaseManager { | 
  | 10 | 0 |      private static final Log log = LogFactory.getLog(DatabaseManager.class); | 
  | 11 |  |      Credentials credentials; | 
  | 12 |  |      Credentials dbaCredentials; | 
  | 13 |  |      DatabaseManagerMode mode; | 
  | 14 |  |      String url; | 
  | 15 |  |      String dbaUrl; | 
  | 16 |  |      JDBCUtils jdbcUtils; | 
  | 17 |  |   | 
  | 18 |  |      public void execute() throws SQLException { | 
  | 19 | 0 |          if (mode.equals(DatabaseManagerMode.DROP_CREATE)) { | 
  | 20 | 0 |              dropDatabase(); | 
  | 21 | 0 |              createDatabase(); | 
  | 22 |  |          } else { | 
  | 23 |  |   | 
  | 24 |  |          } | 
  | 25 | 0 |      } | 
  | 26 |  |   | 
  | 27 |  |      protected void executeDbaSql(final JDBCConfiguration config, final String sql) throws SQLException { | 
  | 28 | 0 |          ConnectionHandler connectionHandler = new ConnectionHandler(); | 
  | 29 | 0 |          connectionHandler.setCredentials(getDbaCredentials()); | 
  | 30 | 0 |          connectionHandler.setUrl(getUrl()); | 
  | 31 | 0 |          connectionHandler.setDriver(config.getDriver()); | 
  | 32 | 0 |          Connection conn = null; | 
  | 33 |  |          try { | 
  | 34 | 0 |              conn = connectionHandler.getConnection(); | 
  | 35 | 0 |              SQLExecutor executor = new SQLExecutor(); | 
  | 36 | 0 |              executor.setConn(conn); | 
  | 37 | 0 |              executor.executeSql(sql); | 
  | 38 |  |          } finally { | 
  | 39 | 0 |              JDBCUtils.closeQuietly(conn); | 
  | 40 | 0 |          } | 
  | 41 | 0 |      } | 
  | 42 |  |   | 
  | 43 |  |      public void dropDatabase() throws SQLException { | 
  | 44 | 0 |          JDBCConfiguration config = getJdbcUtils().getDatabaseConfiguration(getDbaUrl()); | 
  | 45 | 0 |          String sql = config.getDbaSql().getDropSql(); | 
  | 46 | 0 |          log.info("-- Dropping database --"); | 
  | 47 | 0 |          executeDbaSql(config, sql); | 
  | 48 | 0 |      } | 
  | 49 |  |   | 
  | 50 |  |      public void createDatabase() throws SQLException { | 
  | 51 | 0 |          JDBCConfiguration config = getJdbcUtils().getDatabaseConfiguration(getDbaUrl()); | 
  | 52 | 0 |          String sql = config.getDbaSql().getCreateSql(); | 
  | 53 | 0 |          log.info("-- Creating database --"); | 
  | 54 | 0 |          executeDbaSql(config, sql); | 
  | 55 | 0 |      } | 
  | 56 |  |   | 
  | 57 |  |       | 
  | 58 |  |   | 
  | 59 |  |   | 
  | 60 |  |      public Credentials getCredentials() { | 
  | 61 | 0 |          return credentials; | 
  | 62 |  |      } | 
  | 63 |  |   | 
  | 64 |  |       | 
  | 65 |  |   | 
  | 66 |  |   | 
  | 67 |  |   | 
  | 68 |  |      public void setCredentials(final Credentials credentials) { | 
  | 69 | 0 |          this.credentials = credentials; | 
  | 70 | 0 |      } | 
  | 71 |  |   | 
  | 72 |  |       | 
  | 73 |  |   | 
  | 74 |  |   | 
  | 75 |  |      public Credentials getDbaCredentials() { | 
  | 76 | 0 |          return dbaCredentials; | 
  | 77 |  |      } | 
  | 78 |  |   | 
  | 79 |  |       | 
  | 80 |  |   | 
  | 81 |  |   | 
  | 82 |  |   | 
  | 83 |  |      public void setDbaCredentials(final Credentials dbaCredentials) { | 
  | 84 | 0 |          this.dbaCredentials = dbaCredentials; | 
  | 85 | 0 |      } | 
  | 86 |  |   | 
  | 87 |  |       | 
  | 88 |  |   | 
  | 89 |  |   | 
  | 90 |  |      public DatabaseManagerMode getMode() { | 
  | 91 | 0 |          return mode; | 
  | 92 |  |      } | 
  | 93 |  |   | 
  | 94 |  |       | 
  | 95 |  |   | 
  | 96 |  |   | 
  | 97 |  |   | 
  | 98 |  |      public void setMode(final DatabaseManagerMode mode) { | 
  | 99 | 0 |          this.mode = mode; | 
  | 100 | 0 |      } | 
  | 101 |  |   | 
  | 102 |  |       | 
  | 103 |  |   | 
  | 104 |  |   | 
  | 105 |  |      public String getUrl() { | 
  | 106 | 0 |          return url; | 
  | 107 |  |      } | 
  | 108 |  |   | 
  | 109 |  |       | 
  | 110 |  |   | 
  | 111 |  |   | 
  | 112 |  |   | 
  | 113 |  |      public void setUrl(final String url) { | 
  | 114 | 0 |          this.url = url; | 
  | 115 | 0 |      } | 
  | 116 |  |   | 
  | 117 |  |       | 
  | 118 |  |   | 
  | 119 |  |   | 
  | 120 |  |      public JDBCUtils getJdbcUtils() { | 
  | 121 | 0 |          return jdbcUtils; | 
  | 122 |  |      } | 
  | 123 |  |   | 
  | 124 |  |       | 
  | 125 |  |   | 
  | 126 |  |   | 
  | 127 |  |   | 
  | 128 |  |      public void setJdbcUtils(final JDBCUtils jdbcUtils) { | 
  | 129 | 0 |          this.jdbcUtils = jdbcUtils; | 
  | 130 | 0 |      } | 
  | 131 |  |   | 
  | 132 |  |       | 
  | 133 |  |   | 
  | 134 |  |   | 
  | 135 |  |      public String getDbaUrl() { | 
  | 136 | 0 |          return dbaUrl; | 
  | 137 |  |      } | 
  | 138 |  |   | 
  | 139 |  |       | 
  | 140 |  |   | 
  | 141 |  |   | 
  | 142 |  |   | 
  | 143 |  |      public void setDbaUrl(final String dbaUrl) { | 
  | 144 | 0 |          this.dbaUrl = dbaUrl; | 
  | 145 | 0 |      } | 
  | 146 |  |   | 
  | 147 |  |  } |