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

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

public class ProfileOps_mysql extends ProfileOps
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private PropertySerializer m_psz;

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

  public ProfileOps_mysql(DBConnectionPool pool)
  {
    super(pool);
    m_psz = (PropertySerializer)(pool.queryService(PropertySerializer.class));

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Overrides from class OpsBase
   *--------------------------------------------------------------------------------
   */

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

  } // end dispose

  /*--------------------------------------------------------------------------------
   * Abstract implementations from class ProfileOps
   *--------------------------------------------------------------------------------
   */

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

      // prepare a statement to look up the profile index
      stmt = conn.prepareStatement("SELECT profid FROM htmlprofiles WHERE nsid = ? AND name = ?;");
      stmt.setInt(1,nsid);
      stmt.setString(2,name);
      rs = stmt.executeQuery();
      if (rs.next())
	return rs.getInt(1);
      throw new DatabaseException(ProfileOps_mysql.class,"HTMLCheckerMessages","no.profile");

    } // 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 getProfileIndex

  Object getProperty(int profid, PropertyKey key) throws DatabaseException
  {
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    String rc_str = null;
    try
    { // get a connection
      conn = getConnection();

      // look up the property
      stmt = conn.prepareStatement("SELECT prop_value FROM htmlprops WHERE profid = ? AND nsid = ? "
				   + "AND prop_name = ?;");
      stmt.setInt(1,profid);
      stmt.setInt(2,key.getNamespaceID());
      stmt.setString(3,key.getName());
      rs = stmt.executeQuery();
      if (!(rs.next()))
	return null;  // property not found

      rc_str = rs.getString(1);

    } // 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

    // Deserialize the property value.
    Object rc = m_psz.deserializeProperty(rc_str);
    if (rc!=null)
      return rc;

    // deserialization exception - throw it
    DatabaseException de = new DatabaseException(ProfileOps_mysql.class,"HTMLCheckerMessages","property.deserialize");
    de.setParameter(0,key.getName());
    throw de;

  } // end getProperty

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

      // create and execute the statement
      stmt = conn.prepareStatement("SELECT tagset FROM htmltagsets WHERE profid = ?;");
      stmt.setInt(1,profid);
      rs = stmt.executeQuery();

      // prepare the return value
      HashSet rc = new HashSet();
      while (rs.next())
      { // convert strings to enum values and add them
	HTMLTagSet ts = HTMLTagSet.getEnum(rs.getString(1));
	if (ts!=null)
	  rc.add(ts);

      } // end while

      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 getAllowedTagSet

} // end class ProfileOps_mysql
