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

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

public class VeniceAccountOps_mysql extends VeniceAccountOps
{
  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

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

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Abstract implementations from class VeniceAccountOps
   *--------------------------------------------------------------------------------
   */

  void addFederation(int uid, String provider, String local, String remote) throws DatabaseException
  {
    Connection conn = null;
    PreparedStatement stmt = null;
    try
    { // get a connection
      conn = getConnection();

      // add the federation record
      stmt = conn.prepareStatement("INSERT INTO sso_federate (uid, provider, local_name, remote_name) "
				   + "VALUES (?, ?, ?, ?);");
      stmt.setInt(1,uid);
      stmt.setString(2,provider);
      stmt.setString(3,local);
      stmt.setString(4,remote);
      stmt.executeUpdate();

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

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

    } // end finally

  } // end addFederation

  void removeFederation(int uid, String provider) throws DatabaseException
  {
    Connection conn = null;
    PreparedStatement stmt = null;
    try
    { // get a connection
      conn = getConnection();

      // remove the federation record
      stmt = conn.prepareStatement("DELETE FROM sso_federate WHERE uid = ? AND provider = ?;");
      stmt.setInt(1,uid);
      stmt.setString(2,provider);
      stmt.executeUpdate();

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

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

    } // end finally

  } // end removeFederation

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

      // create the lookup statement and run it
      stmt = conn.prepareStatement("SELECT uid FROM sso_federate WHERE provider = ? AND local_name = ?;");
      stmt.setString(1,provider);
      stmt.setString(2,name);
      rs = stmt.executeQuery();
      if (rs.next())
	return rs.getInt(1);
      else
	return -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

  } // end lookupByLocalName

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

      // create the lookup statement and run it
      stmt = conn.prepareStatement("SELECT uid FROM sso_federate WHERE provider = ? AND remote_name = ?;");
      stmt.setString(1,provider);
      stmt.setString(2,name);
      rs = stmt.executeQuery();
      if (rs.next())
	return rs.getInt(1);
      else
	return -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

  } // end lookupByRemoteName

  String[] getNames(int uid, String provider) throws DatabaseException
  {
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try
    { // get a connection
      conn = getConnection();

      // create and run the query
      stmt = conn.prepareStatement("SELECT local_name, remote_name FROM sso_federate WHERE uid = ? AND provider = ?;");
      stmt.setInt(1,uid);
      stmt.setString(2,provider);
      rs = stmt.executeQuery();

      // unpack and return the query result
      if (!(rs.next()))
	return null;
      String[] rc = new String[2];
      rc[0] = rs.getString(1);
      rc[1] = rs.getString(2);
      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 getNames

} // end class VeniceAccountOps_mysql
