/*
 * 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) 2002 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.dynamo.db;

import java.util.*;
import org.w3c.dom.*;
import com.silverwrist.util.xml.*;
import com.silverwrist.dynamo.Namespaces;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.dynamo.util.*;

public class NamespaceCacheObject
    implements NamedObject, ComponentInitialize, ComponentShutdown, NamespaceCache
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String m_name;                        // object name
  private NamespaceCacheOps m_ops;              // low-level database operations
  private HashMap m_cache = new HashMap();      // namespace cache object
  private HashMap m_rev_cache = new HashMap();  // namespace reverse cache object

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

  public NamespaceCacheObject()
  {
  } // end constructor

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

  public String getName()
  {
    return m_name;

  } // end getName

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

  public void initialize(Element config_root, ServiceProvider services) throws ConfigException
  {
    String conn_name = null;
    XMLLoader loader = XMLLoader.get();
    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 database configuration connection
      Element elt = loader.getSubElement(config_root,"database");
      conn_name = loader.getAttribute(elt,"connection");

    } // end try
    catch (XMLLoadException e)
    { // error loading XML config data
      throw new ConfigException(e);

    } // end catch

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

    // Get the database operations class.
    m_ops = NamespaceCacheOps.get(pool);

  } // end initialize

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

  public void shutdown()
  {
    m_ops.dispose();
    m_ops = null;

  } // end shutdown

  /*--------------------------------------------------------------------------------
   * Implementations from interface NamespaceCache
   *--------------------------------------------------------------------------------
   */

  public synchronized int namespaceNameToId(String name) throws DatabaseException
  {
    Integer rc = (Integer)(m_cache.get(name));
    if (rc!=null)
      return rc.intValue();
    int real_id = m_ops.namespaceNameToId(name);
    rc = new Integer(real_id);
    name = name.intern();
    m_cache.put(name,rc);
    m_rev_cache.put(rc,name);
    return real_id;

  } // end namespaceNameToId

  public synchronized String namespaceIdToName(int id) throws DatabaseException
  {
    Integer key = new Integer(id);
    String rc = (String)(m_rev_cache.get(key));
    if (rc!=null)
      return rc;
    rc = m_ops.namespaceIdToName(id);
    if (rc==null)
      throw new DatabaseException(NamespaceCacheObject.class,"DatabaseMessages","nsCache.notFound");
    rc = rc.intern();
    m_cache.put(rc,key);
    m_rev_cache.put(key,rc);
    return rc;

  } // end namespaceIdToName

} // end class NamespaceCacheObject
