/*
 * @(#)ComponentTag.java
 *
 * Copyright 2001-2002 by Oracle Corporation,
 * 500 Oracle Parkway, Redwood Shores, California, 94065, U.S.A.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of Oracle Corporation.
 */

package oracle.jbo.html.jsp.datatags;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;
import oracle.jbo.html.HtmlServices;
import oracle.jbo.html.RequestParameters;

/**
 */
public class ComponentTag extends TagSupport
{
   public static final String defaultUrl = "";
   protected String  sRelativeUrlPath;

   public ComponentTag()
   {
      reset();
   }

   protected void reset()
   {
      sRelativeUrlPath = defaultUrl;
   }

   public void setRelativeUrlPath(String sValue)
   {
      this.sRelativeUrlPath = sValue;
   }

   public final String buildUrl(String url, String[] names, String[] values)
   {
      RequestParameters parameters;
      
      // Because the parameter are stored differently when using the ORD request class
      // we need to also add the new parameters to the parameters handling classes.
      // Failure to do so, will impair the components tag to retrieve request parameter.
      parameters = HtmlServices.getRequestParameters(pageContext);
      if (parameters != null)
      {
         for (int i = 0; i < names.length; i++)
         {
            parameters.addParameter(names[i], values[i]);
         }
      }
      
      return Utils.buildUrl(url, names, values);
   }
   
   public String getUrl()
   {
      return buildUrl(sRelativeUrlPath, null, null);
   }

   /**
     * 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
   {
      String url = getUrl();

      // Include
      try
      {
         pageContext.getOut().flush();
         pageContext.include(url);
      }
      catch (java.io.IOException ex)
      {
         pageContext.getServletContext().log(Res.getString(Res.IO_ERROR), ex);
         throw new JspTagException(ex.getMessage());
      }
      catch (javax.servlet.ServletException ex)
      {
         pageContext.getServletContext().log(Res.getString(Res.INCLUDE_ERROR), ex);
         throw new JspTagException(ex.getMessage());
      }

      return Tag.SKIP_BODY;
   }

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

      super.release();
   }
}
