/*
 * 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@ricochet.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001-2004 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.ui.conf.jsp;

import java.io.IOException;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import org.apache.log4j.*;
import com.silverwrist.venice.ui.*;
import com.silverwrist.venice.ui.helpers.HTMLRendering;
import com.silverwrist.venice.ui.velocity.*;

public class PostBoxTag extends VeniceConfBodyTagSupport
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static Logger logger = Logger.getLogger(PostBoxTag.class);

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

  private String m_action = null;
  private String m_type = "servlet";
  private HashMap m_params = null;
  private ArrayList m_buttons = null;
  private HashMap m_render_data = null;

  /*--------------------------------------------------------------------------------
   * Overrides from class BodyTagSupport
   *--------------------------------------------------------------------------------
   */

  public int doStartTag() throws JspException
  {
    if (logger.isDebugEnabled())
      logger.debug("PostBoxTag.doStartTag(): entry");
    if (m_action==null)
      throw new JspTagException("<post:box/> action= attribute not specified!");
    HTMLRendering html = (HTMLRendering)(getRequestInput().queryService(HTMLRendering.class));
    if (html.convertLinkType(m_type)<0)
      throw new JspTagException("<post:box/> type= attribute not a valid link type");
    m_render_data = new HashMap();
    m_params = new HashMap();
    m_buttons = new ArrayList();
    m_render_data.put("action",m_action);
    m_render_data.put("action_type",m_type);
    m_render_data.put("params",m_params);
    m_render_data.put("buttons",m_buttons);
    return EVAL_BODY_BUFFERED;

  } // end doStartTag

  public int doAfterBody()
  {
    if (logger.isDebugEnabled())
      logger.debug("PostBoxTag.doAfterBody(): entry");
    return SKIP_BODY;

  } // end doAfterBody

  public int doEndTag() throws JspException
  {
    if (logger.isDebugEnabled())
      logger.debug("PostBoxTag.doEndTag(): entry");
    if (!(m_render_data.containsKey("pseud_name")))
      throw new JspTagException("<post:box/> tag has no <post:pseud/> tag inside it!");
    if (!(m_render_data.containsKey("attach_name")))
      throw new JspTagException("<post:box/> tag has no <post:attach/> tag inside it!");
    if (!(m_render_data.containsKey("text_name")))
      throw new JspTagException("<post:box/> tag has no <post:text/> tag inside it!");
    if (m_buttons.isEmpty())
      throw new JspTagException("<post:box/> tag has no <post:button/> tags inside it!");

    try
    { // render this as a template
      JspWriter out = pageContext.getOut();
      VelocityRenderer vr = VelocityRenderer.get(pageContext.getServletContext());
      vr.formatTemplate("conf/post_box.vm",new ParamMapWrapper(m_render_data),getRequestOutput(),out);

    } // end try
    catch (IOException e)
    { // convert the I/O error into something the servlet engine can deal with
      throw new JspTagException("error writing post box - " + e.getMessage());

    } // end catch
    catch (ServletException e)
    { // convert the I/O error into something the servlet engine can deal with
      throw new JspTagException("error writing post box - " + e.getMessage());

    } // end catch

    // anything set by subtags must be cleared in doEndTag, not release
    m_render_data = null;
    m_params = null;
    m_buttons = null;
    return EVAL_PAGE;

  } // end doEndTag

  public void release()
  {
    if (logger.isDebugEnabled())
      logger.debug("PostBoxTag.release(): entry");
    super.release();
    m_action = null;
    m_type = "servlet";

  } // end release

  /*--------------------------------------------------------------------------------
   * Attribute set functions
   *--------------------------------------------------------------------------------
   */

  public void setAction(String v)
  {
    m_action = v;

  } // end setAction

  public void setType(String v)
  {
    m_type = v;

  } // end setType

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

  final void addParam(String name, String value) throws JspException
  {
    if (m_params==null)
      throw new JspTagException("addParam() called at wrong time!");
    if (logger.isDebugEnabled())
      logger.debug("PostBoxTag.addParam(\"" + name + "\",\"" + value + "\") entry");
    m_params.put(name,value);

  } // end addParam

  final void setNewTopic(String param_name, String value) throws JspException
  {
    if (m_render_data==null)
      throw new JspTagException("setNewTopic() called at wrong time!");
    if (m_render_data.containsKey("newtopic_name"))
      throw new JspTagException("<post:newtopic/> may only appear once inside a <post:box/>!");
    if (param_name==null)
      throw new JspTagException("<post:newtopic/> parameter name not specified!");
    m_render_data.put("newtopic_name",param_name);
    m_render_data.put("newtopic_value",value);

  } // end setNewTopic

  final void setPseud(String param_name, String value) throws JspException
  {
    if (m_render_data==null)
      throw new JspTagException("setPseud() called at wrong time!");
    if (logger.isDebugEnabled())
      logger.debug("PostBoxTag.setPseud(\"" + param_name + "\",\"" + value + "\") entry");
    if (m_render_data.containsKey("pseud_name"))
      throw new JspTagException("<post:pseud/> may only appear once inside a <post:box/>!");
    if (param_name==null)
      throw new JspTagException("<post:pseud/> parameter name not specified!");
    m_render_data.put("pseud_name",param_name);
    m_render_data.put("pseud_value",value);

  } // end setPseud

  final void setAttach(String param_name, boolean value) throws JspException
  {
    if (m_render_data==null)
      throw new JspTagException("setAttach() called at wrong time!");
    if (m_render_data.containsKey("attach_name"))
      throw new JspTagException("<post:attach/> may only appear once inside a <post:box/>!");
    if (param_name==null)
      throw new JspTagException("<post:attach/> parameter name not specified!");
    m_render_data.put("attach_name",param_name);
    m_render_data.put("attach_value",(value ? Boolean.TRUE : Boolean.FALSE));

  } // end setAttach

  final void setText(String param_name, String value) throws JspException
  {
    if (m_render_data==null)
      throw new JspTagException("setText() called at wrong time!");
    if (logger.isDebugEnabled())
      logger.debug("PostBoxTag.setText(\"" + param_name + "\",<value>) entry");
    if (m_render_data.containsKey("text_name"))
      throw new JspTagException("<post:text/> may only appear once inside a <post:box/>!");
    if (param_name==null)
      throw new JspTagException("<post:text/> parameter name not specified!");
    m_render_data.put("text_name",param_name);
    m_render_data.put("text_value",value);

  } // end setText

  final void addButton(String id) throws JspException
  {
    if (m_buttons==null)
      throw new JspTagException("addButton() called at wrong time!");
    m_buttons.add(id);

  } // end addButton

} // end class PostBoxTag
