/*
 * 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.util.*;
import org.apache.log4j.Logger;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.venice.iface.*;

class MenuEditImpl implements MenuEditObject
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static Logger logger = Logger.getLogger(MenuEditImpl.class);

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

  private MenuDatabaseOps m_ops;
  private MenuDefinition m_mdef;

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

  MenuEditImpl(MenuDatabaseOps ops, MenuDefinition mdef)
  {
    m_ops = ops;
    m_mdef = mdef;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Internal operations
   *--------------------------------------------------------------------------------
   */

  private final void baleeted() throws DatabaseException
  {
    if ((m_ops==null) || (m_mdef==null))
      throw new DatabaseException(MenuEditImpl.class,"MenuMessages","menu.deleted");
    if (m_mdef.isDeleted())
    { // isolate the object and throw the exception
      m_ops = null;
      m_mdef = null;
      throw new DatabaseException(MenuEditImpl.class,"MenuMessages","menu.deleted");
      
    } // end if

  } // end baleeted

  /*--------------------------------------------------------------------------------
   * Implementations from interface MenuEditObject
   *--------------------------------------------------------------------------------
   */

  public String getTitle()
  {
    return (m_mdef==null) ? null : m_mdef.getTitle();

  } // end getTitle

  public void setTitle(String title) throws DatabaseException
  {
    baleeted();
    m_ops.setMenuTitle(m_mdef.getID(),title);
    m_mdef.setTitle(title);

  } // end setTitle

  public String getSubtitle()
  {
    return (m_mdef==null) ? null : m_mdef.getSubtitle();

  } // end getSubtitle

  public void setSubtitle(String subtitle) throws DatabaseException
  {
    baleeted();
    m_ops.setMenuSubtitle(m_mdef.getID(),subtitle);
    m_mdef.setSubtitle(subtitle);

  } // end setSubtitle

  public Set getVariables()
  {
    return (m_mdef==null) ? null : m_mdef.getVars();

  } // end getVariables

  public String getDefaultVal(String variable)
  {
    return (m_mdef==null) ? null : (String)(m_mdef.getVarDefaults().get(variable));

  } // end getDefaultVal

  public void setDefaultVal(String variable, String value) throws DatabaseException
  {
    baleeted();
    m_ops.setDefaultVal(m_mdef.getID(),variable,value);
    m_mdef.setDefaultVal(variable,value);

  } // end setDefaultVal

  public void addVariable(String variable, String value) throws DatabaseException
  {
    baleeted();
    m_ops.addVariable(m_mdef.getID(),variable,value);
    m_mdef.addVariableDef(variable,value);

  } // end addVariable

  public void removeVariable(String variable) throws DatabaseException
  {
    baleeted();
    m_ops.removeVariable(m_mdef.getID(),variable);
    m_mdef.removeVariableDef(variable);

  } // end removeVariable

  public int getItemCount()
  {
    return (m_mdef==null) ? -1 : m_mdef.getItemCount();

  } // end getItemCount

  public MenuEditItemObject getItem(int ndx)
  {
    if (m_mdef==null)
      return null;
    MenuItemDefinition item = m_mdef.getItem(ndx);
    return new MenuEditItemImpl(m_ops,m_mdef,item);

  } // end getItem

  public MenuEditItemObject getItemBySequence(int seq)
  {
    if (m_mdef==null)
      return null;
    int ndx = m_mdef.getItemBySequence(seq);
    return (ndx<0) ? null : this.getItem(ndx);

  } // end getItemBySequence

  public MenuEditItemObject getItemByTypeText(String type, String text)
  {
    if (m_mdef==null)
      return null;
    int ndx = m_mdef.getItemByTypeText(type,text);
    return (ndx<0) ? null : this.getItem(ndx);

  } // end getItemByTypeText

  public MenuEditItemObject getItemContainingLinkText(String text)
  {
    if (m_mdef==null)
      return null;
    int ndx = m_mdef.getItemContainingLinkText(text);
    return (ndx<0) ? null : this.getItem(ndx);

  } // end getItemContainingLinkText

  public MenuEditItemObject getNextItem(MenuEditItemObject obj)
  {
    return this.getItem(obj.getIndexOfItem() + 1);

  } // end getNextItem

  public MenuEditItemObject getPreviousItem(MenuEditItemObject obj)
  {
    return this.getItem(obj.getIndexOfItem() - 1);

  } // end getPreviousItem

  public MenuEditItemObject addItem(String type, int sequence, String text, String linktype, String link)
      throws DatabaseException
  {
    if (logger.isDebugEnabled())
      logger.debug("MenuEditImpl.addItem(type=\"" + type + "\", sequence=" + sequence + ", text=\"" + text
		   + "\", linktype=\"" + linktype + "\", link=\"" + link + "\")");
    baleeted();
    m_ops.addItem(m_mdef.getID(),type,sequence,text,linktype,link);
    MenuItemDefinition item = new MenuItemDefinition(type,sequence,true,0,text,linktype,link,null,null,null,null,
						     null,null,null);
    m_mdef.addItemDef(item);
    m_mdef.resequence();
    return new MenuEditItemImpl(m_ops,m_mdef,item);

  } // end addItem

  public void delete() throws DatabaseException
  {
    baleeted();
    m_ops.deleteMenu(m_mdef.getID());
    m_mdef.delete();
    m_mdef = null;
    m_ops = null;

  } // end delete

} // end class MenuEditImpl
