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

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

/**
 * A Class class.
 * <P>
 * @author Juan Oropeza
 */
public class ViewObjectTag extends TagSupport
{
  String sAppName;
  String sid = "not set";
  String sViewObject;
  String sWhere = null;
  int nRangeSize = 0;
  
  public ViewObjectTag()
  {
  }

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

  public void setAppid(String sAppName)
  {
    this.sAppName = sAppName;
  }

  public void setViewobject(String sViewObject)
  {
    this.sViewObject = sViewObject;
  }

  public void setWhereclause(String sWhere)
  {
    this.sWhere = sWhere;
  }

  public void setRangesize(String sValue) throws JspException
  {
   try
   {
      nRangeSize = Integer.parseInt(sValue);
   }
   catch(Exception ex)
   {
      throw new JspException(ex.getMessage());
   }
  }

  /**
     * 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
    {
      oracle.jdeveloper.html.DataWebBeanImpl wb = new oracle.jdeveloper.html.DataWebBeanImpl();

      wb.initialize(this.pageContext, this.sAppName + "." + this.sViewObject);

      RowSet rs = wb.getRowSet();
      
      if(nRangeSize != 0)
         rs.setRangeSize(nRangeSize);
         
      System.out.println("Range Size=" + rs.getRangeSize());

      if(sWhere != null)
      {
        String sCurrentClause = rs.getViewObject().getWhereClause();

        if(sCurrentClause == null || !sCurrentClause.equals(sWhere))
        {
          rs.getViewObject().setWhereClause(sWhere);
          rs.executeQuery();
        }
      }
      
      this.pageContext.setAttribute(sid, wb);
      this.pageContext.setAttribute(sid + "_RowSet", rs);
    }
    catch(Exception ex)
    {
      throw new JspException(ex.getMessage());
    }
    return Tag.EVAL_BODY_INCLUDE;
  }

  /**
     * release() called after doEndTag() to reset state
     */
  public void release()
  {
    super.release();

    sAppName = null;
    sid = "not set";
    sViewObject = null;
    sWhere = null;
    nRangeSize = 0;
  }
}

 