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

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

/**
 * A Class class.
 * <P>
 * @author Juan Oropeza
 */
public class DataNavigateTag extends TagSupport
{
   String sDataSource;
   String sAction;
      
   /**
    * Constructor
    */
   public DataNavigateTag()
   {
   }

   public void setDatasource(String sname)
   {
      this.sDataSource = sname;
   }

   public void setAction(String sAction)
   {
      this.sAction = sAction;
   }

   /**
     * 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
         {
            RowSet rs = (RowSet)pageContext.getAttribute(sDataSource + "_RowSet");

            if(rs == null)
            {
               throw new Exception(Res.format(Res.DATA_SOURCE_NOT_FOUND,sDataSource));
            }

            if(sAction.equalsIgnoreCase("Next"))
            {
               if(!rs.hasNext())
               {
                throw new JspException(Res.getString(Res.CANT_MOVE_AFTER_LAST_RECORD));
               }
               rs.next();
            }
            else if(sAction.equalsIgnoreCase("Previous"))
            {
               if(!rs.hasPrevious())
               {
                throw new JspException(Res.getString(Res.CANT_MOVE_BEFORE_FIRST_RECORD));
               }
               rs.previous();
            }
            else if(sAction.equalsIgnoreCase("First"))
            {
               rs.first();
            }
            else if(sAction.equalsIgnoreCase("Last"))
            {
               rs.last();
            }
            else
            {
               throw new JspException(Res.getString(Res.INVALID_NAVIGATE_ACTION));
            }
         }
         catch(Exception ex)
         {
            throw new JspException(ex.getMessage());
         }
         return Tag.SKIP_BODY;
   }

   /**
   * release() called after doEndTag() to reset state
   */
   public void release()
   {
      sDataSource = null;
      sAction = null;
      
      super.release();
   }
}

 