/*
 * 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.unistore;

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

public class ManagerOps_mysql extends ManagerOps
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private DBUtilities m_utils;

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  public ManagerOps_mysql(DBConnectionPool pool)
  {
    super(pool);
    m_utils = (DBUtilities)(pool.queryService(DBUtilities.class));

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Overrides from class ManagerOps
   *--------------------------------------------------------------------------------
   */

  public void dispose()
  {
    m_utils = null;
    super.dispose();

  } // end dispose

  /*--------------------------------------------------------------------------------
   * Abstract implementations from class ManagerOps
   *--------------------------------------------------------------------------------
   */

  protected MessageOps createMessageOps(DBConnectionPool pool)
  {
    return new MessageOps_mysql(pool);

  } // end createMessageOps

  Map getMessageData(long msgid) throws DatabaseException
  {
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try
    { // get a connection
      conn = getConnection();

      // prepare and execute a statement to find the header data
      stmt = conn.prepareStatement("SELECT parent, seq, creator, posted, aclid FROM us_head WHERE msgid = ?;");
      stmt.setLong(1,msgid);
      rs = stmt.executeQuery();
      if (!(rs.next()))
      { // the message was not found
	DatabaseException de = new DatabaseException(ManagerOps_mysql.class,"UniStoreMessages","no.message");
	de.setParameter(0,String.valueOf(msgid));
	throw de;

      } // end if

      // build the return value
      HashMap rc = new HashMap();
      rc.put(PARAM_MSGID,new Long(msgid));
      rc.put(PARAM_PARENT,new Long(rs.getLong(1)));
      rc.put(PARAM_SEQ,new Integer(rs.getInt(2)));
      rc.put(PARAM_CREATOR,new Integer(rs.getInt(3)));
      rc.put(PARAM_POSTED,m_utils.getDateTime(rs,4));
      int tmp = rs.getInt(5);
      if (!(rs.wasNull()))
	rc.put(PARAM_ACLID,new Integer(tmp));
      return 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 getMessageData

  long createMessage(int creator, java.util.Date postdate) throws DatabaseException
  {
    Connection conn = null;
    PreparedStatement stmt = null;
    Statement stmt2 = null;
    try
    { // get a connection
      conn = getConnection();

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

      // create and execute the INSERT statement
      stmt = conn.prepareStatement("INSERT INTO us_head (creator, posted) VALUES (?, ?);");
      stmt.setInt(1,creator);
      m_utils.setDateTime(stmt,2,postdate);
      stmt.executeUpdate();

      // return the new message ID
      return MySQLUtils.getLastInsertLong(conn);

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

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

    } // end finally

  } // end createMessage

} // end class ManagerOps_mysql
