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

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

public class MenuDatabaseOps_mysql extends MenuDatabaseOps
{
  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

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

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Abstract implementations from class MenuDatabaseOps
   *--------------------------------------------------------------------------------
   */

  MenuDefinition getMenu(String namespace, String name) throws DatabaseException
  {
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try
    { // get a connection
      conn = getConnection();

      // create the statement to retrieve the base data
      stmt = conn.prepareStatement("SELECT m.menuid, m.title, m.subtitle FROM menus m, namespaces n "
				   + "WHERE m.menu_nsid = n.nsid AND n.namespace = ? AND m.menu_name = ?;");
      stmt.setString(1,namespace);
      stmt.setString(2,name);
      rs = stmt.executeQuery();
      if (!(rs.next()))
	return null;  // not found

      // get the ID and create the initial menu definition
      int id = rs.getInt(1);
      MenuDefinition def = new MenuDefinition(id,namespace,name,rs.getString(2),rs.getString(3));

      // shut down things for the next operation
      SQLUtils.shutdown(rs);
      rs = null;
      SQLUtils.shutdown(stmt);

      // get the variable definitions
      stmt = conn.prepareStatement("SELECT var_name, default_val FROM menuvars WHERE menuid = ?;");
      stmt.setInt(1,id);
      rs = stmt.executeQuery();
      while (rs.next())
	def.addVariableDef(rs.getString(1),rs.getString(2));

      // shut down things for the next operation
      SQLUtils.shutdown(rs);
      rs = null;
      SQLUtils.shutdown(stmt);

      // get the menu items
      stmt = conn.prepareStatement("SELECT itemtype, enable, indent, text, linktype, link, target, title, "
				   + "on_click, perm_nsid, perm_name, ifdef_var, ifndef_var FROM menuitems "
				   + "WHERE menuid = ? ORDER BY sequence;");
      stmt.setInt(1,id);
      rs = stmt.executeQuery();
      while (rs.next())
      { // get the namespace and add the item definition
	int nsid = rs.getInt(10);
	String ns = (rs.wasNull() ? null : namespaceIdToName(nsid));
	MenuItemDefinition item = new MenuItemDefinition(rs.getString(1),(rs.getInt(2)!=0),rs.getInt(3),
							 rs.getString(4),rs.getString(5),rs.getString(6),
							 rs.getString(7),rs.getString(8),rs.getString(9),ns,
							 rs.getString(11),rs.getString(12),rs.getString(13));
	def.addItemDef(item);

      } // end while

      return def;

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

} // end class MenuDatabaseOps_mysql
