/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2003 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.dynamo.module;

import java.sql.*;
import java.util.*;
import com.silverwrist.util.*;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;

public class ModuleDBOps_mysql extends ModuleDBOps
{
  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  public ModuleDBOps_mysql(DBConnectionPool pool)
  {
    super(pool);

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Abstract implementations from class ModuleDBOps
   *--------------------------------------------------------------------------------
   */

  boolean isModuleInstalled(String filename) throws DatabaseException
  {
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try
    { // get a connection
      conn = getConnection();

      // prepare and execute the query
      stmt = conn.prepareStatement("SELECT mod_nsid FROM modinstall WHERE filename = ?;");
      stmt.setString(1,filename);
      rs = stmt.executeQuery();
      return rs.next();

    } // end try
    catch (SQLException e)
    { // translate to a general DatabaseException
      throw generalException(e);

    } // end catch
    finally
    { // shut everything down
      SQLUtils.shutdown(rs);
      SQLUtils.shutdown(stmt);
      SQLUtils.shutdown(conn);

    } // end finally

  } // end isModuleInstalled

  Set getInstalledModules() throws DatabaseException
  {
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try
    { // get a connection
      conn = getConnection();

      // prepare and execute the query
      stmt = conn.prepareStatement("SELECT filename FROM modinstall ORDER BY filename;");
      rs = stmt.executeQuery();

      // output the set in sorted order
      TreeSet rc = new TreeSet();
      while (rs.next())
	rc.add(rs.getString(1));
      return Collections.unmodifiableSet(rc);

    } // end try
    catch (SQLException e)
    { // translate to a general DatabaseException
      throw generalException(e);

    } // end catch
    finally
    { // shut everything down
      SQLUtils.shutdown(rs);
      SQLUtils.shutdown(stmt);
      SQLUtils.shutdown(conn);

    } // end finally

  } // end getInstalledModules

  void markInstalled(String filename, int nsid, String name) throws DatabaseException
  {
    Connection conn = null;
    PreparedStatement stmt = null;
    Statement stmt2 = null;
    ResultSet rs = null;
    try
    { // get a connection
      conn = getConnection();

      // lock the database table
      stmt2 = conn.createStatement();
      stmt2.executeUpdate("LOCK TABLES modinstall WRITE;");

      // execute the update
      stmt = conn.prepareStatement("INSERT INTO modinstall (filename, mod_nsid, mod_name) VALUES (?, ?, ?);");
      stmt.setString(1,filename);
      stmt.setInt(2,nsid);
      stmt.setString(3,name);
      stmt.executeUpdate();

    } // end try
    catch (SQLException e)
    { // translate to a general DatabaseException
      throw generalException(e);

    } // end catch
    finally
    { // shut everything down
      MySQLUtils.unlockTables(conn);
      SQLUtils.shutdown(rs);
      SQLUtils.shutdown(stmt);
      SQLUtils.shutdown(stmt2);
      SQLUtils.shutdown(conn);

    } // end finally

  } // end markInstalled

  void unmarkInstalled(String filename) throws DatabaseException
  {
    Connection conn = null;
    PreparedStatement stmt = null;
    Statement stmt2 = null;
    ResultSet rs = null;
    try
    { // get a connection
      conn = getConnection();

      // lock the database table
      stmt2 = conn.createStatement();
      stmt2.executeUpdate("LOCK TABLES modinstall WRITE;");

      // execute the update
      stmt = conn.prepareStatement("DELETE FROM modinstall WHERE filename = ?;");
      stmt.setString(1,filename);
      stmt.executeUpdate();

    } // end try
    catch (SQLException e)
    { // translate to a general DatabaseException
      throw generalException(e);

    } // end catch
    finally
    { // shut everything down
      MySQLUtils.unlockTables(conn);
      SQLUtils.shutdown(rs);
      SQLUtils.shutdown(stmt);
      SQLUtils.shutdown(stmt2);
      SQLUtils.shutdown(conn);

    } // end finally

  } // end unmarkInstalled

} // end class ModuleDBOps_mysql
