
// Copyright (c) 2000 Oracle Corporation
package oracle.jbo.html.jsp.datatags;

import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.JspException;
import oracle.jdeveloper.html.*;

/**
 * A Class class.
 * <P>
 * @author Juan Oropeza
 */
public class WebBeanTag extends TagSupport
{
  String sClass;
  String sid = "not set";
  
  public WebBeanTag()
  {
  }

  public void setId(String sid)
  {
    this.sid = sid;
  }

  public void setWbclass(String sClass)
  {
    this.sClass = sClass;
  }

  /**
     * Process the start tag for this instance.
     *
     * The doStartTag() method assumes that all setter methods have been
     * invoked before.
     *
     * When this method is invoked, the body has not yet been invoked.
     *
     * @returns EVAL_BODY_INCLUDE if the tag wants to process body, SKIP_BODY if it
     * does ont want to process it.
     */
  public int doStartTag() throws JspException
  {
    try
    {
      if(sClass == null)
      {
        throw new Exception("WebBean class name is required");
      }

      Class wbClass = Class.forName(sClass);

      Object instance = wbClass.newInstance();
      oracle.jdeveloper.html.WebBean wb = (oracle.jdeveloper.html.WebBean)instance;

      wb.initialize(this.pageContext);

      this.pageContext.setAttribute(sid, wb);
    }
    catch(Exception ex)
    {
      throw new JspException(ex.getMessage());
    }
    return Tag.EVAL_BODY_INCLUDE;
  }

  /**
     * Process the end tag. This method will be called on all Tag objects.
     *
     * All instance state associated with this instance must be reset.
     */
  public int doEndTag() throws JspException
  {
    try
    {
      oracle.jdeveloper.html.WebBean wb = (oracle.jdeveloper.html.WebBean)pageContext.getAttribute(sid);
      wb.render();
    }
    catch(Exception ex)
    {
      throw new JspException(ex.getMessage());
    }
    return super.doEndTag();
  }

  /**
     * release() called after doEndTag() to reset state
     */
  public void release()
  {
    pageContext.removeAttribute(sid);
    
    sClass = null;
    sid = "not set";

    super.release();
  }

}

