/*
 * @(#)EditRecordTag.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.http.HttpServletRequest;
import oracle.jbo.html.HtmlServices;
import oracle.jbo.html.BC4JContext;

/**
 */
public class EditRecordTag extends DataComponentTag
{
   public static final String defaultUrl = "DataEditComponent.jsp";
   public static final String paramNames[] = new String[]{ "targetURL", "originURL", "encType" };
   protected String  sTargetUrl;
   protected String  sEncType;

   public EditRecordTag()
   {
      super();
   }

   protected void reset()
   {
      super.reset();
      sRelativeUrlPath = defaultUrl;
      sTargetUrl = null;
      sEncType = "application/x-www-form-urlencoded";
   }

   public void setTargetURL(String sValue)
   {
      this.sTargetUrl = sValue;
   }

   public void setEnctype(String sValue)
   {
      this.sEncType = sValue;
   }

   public String getUrl()
   {
      String url = super.getUrl();
      
      String originURL = HtmlServices.getRequestParameter(pageContext, "originURL");
      
      if (sTargetUrl == null)
      {
         // If we are using Struts, the default page is '/update.do'
         BC4JContext bc4jContext = null;
         Object contextObject = pageContext.getRequest().getAttribute(BC4JContext.ContextAttrName);
         // Only do the casting if object is found
         if (contextObject != null)
         {
            bc4jContext = (BC4JContext) contextObject;
         }
      
         if (bc4jContext != null)
         {
            sTargetUrl = "/update.do";
         }
         else
         {
            // If the target url is not specified, we use if possible originURL. If not we
            // use self.
            if (originURL != null)
            {
               sTargetUrl = originURL;
            }
            else
            {
               HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
               sTargetUrl = request.getRequestURI();
            }
         }
      }

      return buildUrl(url, paramNames, new String[]{ sTargetUrl, originURL, sEncType });
   }
}
