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

import com.silverwrist.dynamo.db.NamespaceCache;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.dynamo.util.*;

class PermObject implements DynamoPermission
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private PropertyKey m_data;
  private NamespaceCache m_ns_cache;
  private String m_cached_ns;

  /*--------------------------------------------------------------------------------
   * Constructors
   *--------------------------------------------------------------------------------
   */

  PermObject(PropertyKey data, NamespaceCache ns_cache)
  {
    m_data = data;
    m_ns_cache = ns_cache;
    m_cached_ns = null;

  } // end constructor

  PermObject(int nsid, String name, NamespaceCache ns_cache)
  {
    m_data = new PropertyKey(nsid,name);
    m_ns_cache = ns_cache;
    m_cached_ns = null;

  } // end constructor

  PermObject(String namespace, String name, NamespaceCache ns_cache) throws DatabaseException
  {
    m_data = new PropertyKey(ns_cache.namespaceNameToId(namespace),name);
    m_ns_cache = null;
    m_cached_ns = namespace;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Overrides from class Object
   *--------------------------------------------------------------------------------
   */

  public int hashCode()
  {
    return m_data.hashCode();

  } // end hashCode
  
  /*--------------------------------------------------------------------------------
   * Implementations from interface Permission
   *--------------------------------------------------------------------------------
   */
  
  public boolean equals(Object another)
  {
    if (another==null)
      return false;
    if (another instanceof PermObject)
    { // compare the data contents
      PermObject other = (PermObject)another;
      return m_data.equals(other.m_data);

    } // end if
    
    if (another instanceof DynamoPermission)
    { // compare through the public interface
      DynamoPermission other = (DynamoPermission)another;
      return (getNamespace().equals(other.getNamespace()) && getName().equals(other.getName()));

    } // end if

    return false;

  } // end equals

  public String toString()
  {
    return "DynamoPermission: {" + getNamespace() + "} " + m_data.getName();

  } // end toString

  /*--------------------------------------------------------------------------------
   * Implementations from interface DynamoPermission
   *--------------------------------------------------------------------------------
   */
  
  public synchronized String getNamespace()
  {
    if (m_cached_ns==null)
    { // we don't know what namespace to return yet
      try
      { // attempt to get the namespace from the cache
	m_cached_ns = m_ns_cache.namespaceIdToName(m_data.getNamespaceID());
	m_ns_cache = null;

      } // end try
      catch (DatabaseException e)
      { // just return null here and let the chips fall where they may
	return null;

      } // end catch

    } // end if

    return m_cached_ns;

  } // end getNamespace

  public String getName()
  {
    return m_data.getName();

  } // end getName

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  PropertyKey getPKey()
  {
    return m_data;

  } // end getPKey
  
} // end class PermObject
