/*
 * 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 Community 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) 2001 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.servlets.format;

import java.util.Hashtable;
import javax.servlet.ServletContext;
import org.apache.log4j.*;

public class DialogCache
{
  /*--------------------------------------------------------------------------------
   * Static data values
   *--------------------------------------------------------------------------------
   */

  protected static final String ATTR_NAME = "com.silverwrist.venice.servlets.DialogCache";

  private static Category logger = Category.getInstance(DialogCache.class.getName());

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

  Hashtable cache;

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

  protected DialogCache()
  {
    cache = new Hashtable();

  } // end constructor

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

  public boolean isCached(String name)
  {
    return (cache.get(name)!=null);

  } // end isCached

  public void saveTemplate(ContentDialog template)
  {
    String fullname = template.getClass().getName();
    int clip_pos = fullname.lastIndexOf('.');
    if (clip_pos>=0)
      fullname = fullname.substring(clip_pos+1);
    cache.put(fullname,template);

  } // end saveTemplate

  public ContentDialog getNewDialog(String name)
  {
    ContentDialog template = (ContentDialog)(cache.get(name));
    if (template!=null)
      return (ContentDialog)(template.clone());
    else
      return null;

  } // end getNewDialog

  /*--------------------------------------------------------------------------------
   * Static operations for use by servlets
   *--------------------------------------------------------------------------------
   */

  public static DialogCache getDialogCache(ServletContext ctxt)
  {
    // Look in the servlet attributes first.
    Object foo = ctxt.getAttribute(ATTR_NAME);
    if (foo!=null)
      return (DialogCache)foo;

    // create a new one and return it
    DialogCache cache = new DialogCache();
    ctxt.setAttribute(ATTR_NAME,cache);
    return cache;

  } // end getDialogCache

} // end class DialogCache
