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

import java.util.*;
import org.apache.log4j.*;
import org.w3c.dom.*;
import com.silverwrist.util.*;
import com.silverwrist.venice.core.VeniceEngine;
import com.silverwrist.venice.core.UserContext;
import com.silverwrist.venice.except.*;
import com.silverwrist.venice.ui.Content;
import com.silverwrist.venice.ui.sidebox.SideBoxFactory;
import com.silverwrist.venice.util.XMLLoader;

public class SideBoxManager
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static Category logger = Category.getInstance(SideBoxManager.class);

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

  private Map factories;

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

  SideBoxManager(Element elt) throws ConfigException
  {
    XMLLoader loader = XMLLoader.get();

    // Examine the child nodes for sidebox configuration data.
    HashMap tmp_factories = new HashMap();
    NodeList nl = elt.getChildNodes();
    for (int i=0; i<nl.getLength(); i++)
    { // extract each sidebox section from the list and build a new master sidebox object
      Node n = nl.item(i);
      if ((n.getNodeType()==Node.ELEMENT_NODE) && (n.getNodeName().equals("sidebox")))
      { // get the ID for this sidebox
	int my_id = loader.configGetAttributeInt((Element)n,"id");
	String cname = loader.configGetSubElementText((Element)n,"factory-class");
	SideBoxFactory factory = null;

	try
	{ // load the factory class and create an instance of the factory
	  factory = (SideBoxFactory)(Class.forName(cname).newInstance());
	  factory.setConfiguration((Element)n);

	} // end try
	catch (ClassNotFoundException cnfe)
	{ // sidebox could not be configured
	  logger.fatal("<sidebox/> config <factory-class/> not valid!",cnfe);
	  throw new ConfigException("<factory-class/> in <sidebox/> is not a valid class!",(Element)n);

	} // end catch
	catch (IllegalAccessException iae)
	{ // could not access class and/or constructor
	  logger.fatal("<sidebox/> <factory-class/> is not accessible!",iae);
	  throw new ConfigException("<factory-class/> in <sidebox/> is not accessible!",(Element)n);

	} // end catch
	catch (InstantiationException ie)
	{ // unable to create the class
	  logger.fatal("<sidebox/> <factory-class/> could not be instantiated!",ie);
	  throw new ConfigException("<factory-class/> in <sidebox/> could not be created!",(Element)n);

	} // end catch
	catch (ClassCastException cce)
	{ // the class is not a SideBoxFactory implementor - cannot create
	  logger.fatal("<sidebox/> <factory-class/> is not a SideBoxFactory!",cce);
	  throw new ConfigException("<factory-class/> in <sidebox/> is not the correct type!",(Element)n);

	} // end catch

	tmp_factories.put(new Integer(my_id),factory);  // save it off

      } // end if
      // else just ignore node

    } // end for

    if (tmp_factories.isEmpty())
      factories = Collections.EMPTY_MAP;
    else
      factories = Collections.unmodifiableMap(tmp_factories);

  } // end constructor

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

  public final Content createSideBox(int id, VeniceEngine engine, UserContext uc)
      throws AccessError, DataException
  {
    SideBoxFactory fact = (SideBoxFactory)(factories.get(new Integer(id)));
    if (fact==null)
      throw new DataException("invalid sidebox ID!");
    return fact.create(engine,uc);

  } // end createSideBox

} // end class SideBoxManager
