/*
 * 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) 2001-02 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.core.impl;

import java.sql.*;
import org.apache.log4j.*;
import com.silverwrist.venice.core.*;
import com.silverwrist.venice.core.internals.*;
import com.silverwrist.venice.db.*;
import com.silverwrist.venice.except.*;

class SideBoxDescriptorImpl implements UserSideBoxDescriptor
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static Category logger = Category.getInstance(SideBoxDescriptorImpl.class);

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private EnvUser env;                     // the execution environment
  private SideBoxDescriptor parent;        // the master side box descriptor
  private int sequence;                    // the sequence number

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

  SideBoxDescriptorImpl(EnvUser env, SideBoxDescriptor parent, int sequence)
  {
    this.env = env;
    this.parent = parent;
    this.sequence = sequence;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface SideBoxDescriptor
   *--------------------------------------------------------------------------------
   */

  public int getID()
  {
    return parent.getID();

  } // end getID

  public int getSideBoxID()
  {
    return parent.getID();

  } // end getSideBoxID

  public int getSequence()
  {
    return sequence;

  } // end getSequence

  public String getTitle(boolean anonymous)
  {
    return parent.getTitle(anonymous);

  } // end getTitle

  public String getFactoryClassName()
  {
    return parent.getFactoryClassName();

  } // end getFactoryClassName

  /*--------------------------------------------------------------------------------
   * Implementations from interface UserSideBoxDescriptor
   *--------------------------------------------------------------------------------
   */

  public void setSequence(int seq) throws DataException
  {
    Connection conn = null;
    Statement stmt = null;

    try
    { // retrieve a connection
      conn = env.getConnection();
      stmt = conn.createStatement();

      // create the UPDATE statement and just execute it blind (if this sidebox is not in the list,
      // the UPDATE is a no-op).
      StringBuffer sql = new StringBuffer("UPDATE sideboxes SET sequence = ");
      sql.append(seq).append(" WHERE uid = ").append(env.getUserID()).append(" AND boxid = ");
      sql.append(parent.getID()).append(';');
      stmt.executeUpdate(sql.toString());
      this.sequence = seq;

    } // end try
    catch (SQLException e)
    { // this becomes a DataException
      logger.error("DB error setting sidebox sequence: " + e.getMessage(),e);
      throw new DataException("error setting sidebox sequence: " + e.getMessage(),e);

    } // end catch
    finally
    { // make sure we release the connection before we go
      SQLUtil.shutdown(stmt);
      SQLUtil.shutdown(conn);

    } // end finally

  } // end setSequence

  public void remove() throws DataException
  {
    Connection conn = null;
    Statement stmt = null;
      
    try
    { // retrieve a connection
      conn = env.getConnection();
      stmt = conn.createStatement();

      // create the DELETE statement and just execute it blind (if this conference is not in the hotlist,
      // the DELETE is a no-op).
      StringBuffer sql = new StringBuffer("DELETE FROM sideboxes WHERE uid = ");
      sql.append(env.getUserID()).append(" AND boxid = ").append(parent.getID()).append(';');
      stmt.executeUpdate(sql.toString());

    } // end try
    catch (SQLException e)
    { // this becomes a DataException
      logger.error("DB error removing from sidebox list: " + e.getMessage(),e);
      throw new DataException("error removing from sidebox list: " + e.getMessage(),e);

    } // end catch
    finally
    { // make sure we release the connection before we go
      SQLUtil.shutdown(stmt);
      SQLUtil.shutdown(conn);

    } // end finally

  } // end remove

} // end class SideBoxDescriptorImpl
