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

import java.io.IOException;
import java.util.*;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.dynamo.servlet.ChainForward;

/**
 * The abstract base class for Dynamo output objects that forward their request somewhere else within the
 * same Web application.  It relies on the hidden functionality provided by the
 * {@link com.silverwrist.dynamo.servlet.ChainForward ChainForward} interface, a service provided by
 * {@link com.silverwrist.dynamo.iface.SelfRenderControl SelfRenderControl}.
 *
 * @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
 * @version X
 */
abstract class Forwarder implements SelfRenderable, RenderFlags
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String m_param;                    // forwarding parameter
  private HashMap m_attrs = new HashMap();   // request attributes to be set before forwarding takes place

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

  /**
   * Constructs a new <CODE>Forwarder</CODE> object.
   *
   * @param param The forwarding parameter, which will later be passed to the
   *              {@link #doForward(com.silverwrist.dynamo.servlet.ChainForward,java.lang.String) doForward()} method.
   */
  protected Forwarder(String param)
  {
    m_param = param;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Internal abstract operations
   *--------------------------------------------------------------------------------
   */

  /**
   * Actually performs the forwarding operation.
   *
   * @param chainer External object by which we gain access to the forwarding functionality.
   * @param param The forwarding parameter originally passed to this object by means of the constructor.
   * @exception java.io.IOException If an I/O error occurs while forwarding the request.
   * @exception com.silverwrist.dynamo.except.RenderingException If we are unable to forward the request.
   */
  protected abstract void doForward(ChainForward chainer, String param) throws IOException, RenderingException;

  /*--------------------------------------------------------------------------------
   * Implementations from interface SelfRenderable
   *--------------------------------------------------------------------------------
   */

  /**
   * Renders this object to the output.
   *
   * @param control A rendering control object that provides the means to render this object to the output, as
   *                well as providing "output services" (through the
   *                {@link com.silverwrist.dynamo.iface.ServiceProvider ServiceProvider} interface) for the
   *                rendering process to use.
   * @exception java.io.IOException If an I/O error occurs while writing the output.
   * @exception com.silverwrist.dynamo.except.RenderingException If the rendering process fails.
   */
  public void render(SelfRenderControl control) throws IOException, RenderingException
  {
    ChainForward chainer = (ChainForward)(control.queryService(ChainForward.class));
    Iterator it = m_attrs.entrySet().iterator();
    while (it.hasNext())
    { // add all the parameters
      Map.Entry ntry = (Map.Entry)(it.next());
      chainer.setChainParameter(ntry.getKey().toString(),ntry.getValue());

    } // end while

    doForward(chainer,m_param);

  } // end render

  /*--------------------------------------------------------------------------------
   * Implementations from interface RenderFlags
   *--------------------------------------------------------------------------------
   */

  /**
   * Returns the rendering flags to be used by this object.
   *
   * @return The rendering flags to be used by this object.
   * @see com.silverwrist.dynamo.iface.RenderFlags
   */
  public long getRenderFlags()
  {
    return (NO_COOKIE_FLUSH | NO_HEADER_FLUSH | NO_RESPONSE_FLUSH);

  } // end getRenderFlags

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

  /**
   * Sets a parameter for the forwarding request.  This translates, eventually, into an attribute on the
   * underlying <CODE>ServletRequest</CODE>.
   *
   * @param name Name of the parameter to set.
   * @param value Value of the parameter to set.
   */
  public void setParameter(String name, Object value)
  {
    m_attrs.put(name,value);

  } // end setParameter

} // end class Forwarder
