/*
 * 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 org.apache.log4j.Logger;
import org.w3c.dom.*;
import com.silverwrist.util.*;
import com.silverwrist.util.xml.*;
import com.silverwrist.dynamo.db.NamespaceCache;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.dynamo.security.SecurityReferenceMonitor;
import com.silverwrist.dynamo.util.*;
import com.silverwrist.venice.iface.*;

public class MenuManager implements NamedObject, ComponentInitialize, ComponentShutdown, MenuProvider
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

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

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

  private String m_name = null;
  private NamespaceCache m_ns_cache;
  private SecurityReferenceMonitor m_srm;
  private MenuDatabaseOps m_ops;
  private HardSoftCache m_menudef_cache;

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

  public MenuManager()
  { // do nothing
  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface NamedObject
   *--------------------------------------------------------------------------------
   */

  public synchronized String getName()
  {
    return m_name;

  } // end getName

  /*--------------------------------------------------------------------------------
   * Implementations from interface ComponentInitialize
   *--------------------------------------------------------------------------------
   */

  /**
   * Initialize the component.
   *
   * @param config_root Pointer to the section of the Dynamo XML configuration file that configures this
   *                    particular component.  This is to be considered "read-only" by the component.
   * @param services An implementation of {@link com.silverwrist.dynamo.iface.ServiceProvider ServiceProvider}
   *                 which provides initialization services to the component.  This will include an implementation
   *                 of {@link com.silverwrist.dynamo.iface.ObjectProvider ObjectProvider} which may be used to
   *                 get information about other objects previously initialized by the application.
   * @exception com.silverwrist.dynamo.except.ConfigException If an error is encountered in the component
   *            configuration.
   */
  public void initialize(Element config_root, ServiceProvider services) throws ConfigException
  {
    logger.info("MenuManager initializing");

    XMLLoader loader = XMLLoader.get();
    String name_pool = null, name_cache = null, name_srm = null;
    try
    { // verify the right node name
      loader.verifyNodeName(config_root,"object");

      // get the object's name
      m_name = loader.getAttribute(config_root,"name");

      // get the name of the database pool and namespace cache
      DOMElementHelper config_root_h = new DOMElementHelper(config_root);
      Element foo = loader.getSubElement(config_root_h,"database");
      name_pool = loader.getAttribute(foo,"connection");
      name_cache = loader.getAttribute(foo,"namespaces");

      // get the name of the security reference monitor
      foo = loader.getSubElement(config_root_h,"security");
      name_srm = loader.getAttribute(foo,"object");

    } // end try
    catch (XMLLoadException e)
    { // error loading XML config data
      logger.fatal("XML loader exception in StandardContentSupplier",e);
      throw new ConfigException(e);

    } // end catch

    // Get the database connection pool.
    DBConnectionPool pool = GetObjectUtils.getDatabaseConnection(services,name_pool);

    // Get the namespace cache.
    m_ns_cache = (NamespaceCache)(GetObjectUtils.getDynamoComponent(services,NamespaceCache.class,name_cache));

    // Get the security reference monitor.
    m_srm =
        (SecurityReferenceMonitor)(GetObjectUtils.getDynamoComponent(services,SecurityReferenceMonitor.class,
								     name_srm));

    // Get the menu database operations.
    m_ops = MenuDatabaseOps.get(pool);
    m_ops.setNamespaceCache(m_ns_cache);

    // Create the menu definition cache.
    m_menudef_cache = new HardSoftCache(3,10);

  } // end initialize

  /*--------------------------------------------------------------------------------
   * Implementations from interface ComponentShutdown
   *--------------------------------------------------------------------------------
   */

  public void shutdown()
  {
    m_ns_cache = null;
    m_srm = null;

  } // end shutdown

  /*--------------------------------------------------------------------------------
   * Implementations from interface MenuProvider
   *--------------------------------------------------------------------------------
   */

  public MenuRenderObject getStandardMenu(DynamoUser caller, String namespace, String name, int[] acl_ids)
      throws DatabaseException
  {
    QualifiedNameKey key = new QualifiedNameKey(namespace,name);
    MenuDefinition mdef = (MenuDefinition)(m_menudef_cache.get(key));
    if (mdef==null)
    { // get it and add it to the cache
      mdef = m_ops.getMenu(namespace,name);
      m_menudef_cache.put(key,mdef);

    } // end if

    return new StandardMenuRendering(caller,mdef,m_srm,acl_ids);

  } // end getStandardMenu

  public MenuRenderObject getLeftMenu(DynamoUser caller, String namespace, String name, int[] acl_ids)
      throws DatabaseException
  {
    QualifiedNameKey key = new QualifiedNameKey(namespace,name);
    MenuDefinition mdef = (MenuDefinition)(m_menudef_cache.get(key));
    if (mdef==null)
    { // get it and add it to the cache
      mdef = m_ops.getMenu(namespace,name);
      m_menudef_cache.put(key,mdef);

    } // end if

    return new LeftMenuRendering(caller,mdef,m_srm,acl_ids);

  } // end getLeftMenu

} // end class MenuManager
