/********************************************************************
 *  Oracle JDeveloper
 *  Copyright (c) 1997, 1999, Oracle Corporation. All rights reserved.
 ********************************************************************/


package oracle.jdeveloper.html;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import oracle.jdeveloper.html.*;

  /**
  * Implements the base methods for a Web Bean.
  *
  **/

public class WebBeanImpl implements WebBean
{
	protected	HttpSession				session;
	protected	HttpServletRequest	request;
	protected	HttpServletResponse	response;
   protected   PrintWriter				out;
   protected   ServletContext			application;
   protected   PageContext          page;
   protected   HTMLRenderingContext ctx = null;

	public WebBeanImpl()
	{
	}
	
   /**
   * Initializes this Web Bean object to
   * access the important objects of the JSP: application, session,
   * request, response, and out. This implementation of <TT>initialize()</TT>
   * specifies a <TT>PrintWriter</TT> rendering object. This version of <tt>initialize()</tt>
   * is designed for use in servlets.
   * <p>
   * @param application    the JSP page's ServletContext.
   * @param session        the JSP page's HttpSession.
   * @param request        the JSP page's HttpServletRequest.
   * @param response       the JSP page's HttpServletResponse.
   * @param out            the PrintWriter to render to.
   */
	public void  initialize(ServletContext application, HttpSession session ,
                            HttpServletRequest request, HttpServletResponse response, PrintWriter out) throws Exception
	{
		this.application = application;
		this.session = session;
		this.request = request;
		this.response = response;
		this.out = out;

    internalInitialize();
	}

   public void initialize(PageContext page) throws Exception
	{
      this.page = page;
      
      initialize(page.getServletContext(), page.getSession(), (HttpServletRequest) page.getRequest(),
                 (HttpServletResponse) page.getResponse(), new PrintWriter(page.getOut()));
	}

   /**
   * Initializes this Web Bean object to
   * access the important objects of the JSP: application, session,
   * request, response, and out. This implementation of <TT>initialize()</TT>
   * specifies a <TT>JspWriter</TT> rendering object. This version of <tt>initialize()</tt>
   * is designed for use in JSP pages.
   * <p>
   * @param application    the JSP page's ServletContext
   * @param session        the JSP page's HttpSession
   * @param request        the JSP page's HttpServletRequest
   * @param response       the JSP page's HttpServletResponse
   * @param out            the JspWriter to render to
   */	
	public void initialize(ServletContext application, HttpSession session , HttpServletRequest request,
                           HttpServletResponse response, JspWriter out ) throws Exception
	{
      initialize(application, session, request,response, new PrintWriter(out));
	}

   /**
   * Internal initialize.
   * This method should be overriden by any WebBean needing to initialize some internal data
   * after all the base class member have been initialize properly.
   */
   public void internalInitialize() throws Exception
   {
      ctx = new HTMLRenderingContext(application,session,request,response);
      ctx.page = page; // <Charles> try to initialize the page context no matter what.
      ctx.userData.put("Current_DWB", this);
   }

  public HTMLRenderingContext getRenderingContext()
  {
    return ctx;
  }

  /**
  * <b>Internal:</b> <em>Applications should not use this method.</em>
  * <p>
  * Returns the name of the cookie associated with the JSP page's
  * <tt>HttpServletRequest</tt>.
  * <p>
  * @param sName name of the cookie
  **/
  public String   getCookie(String sName)
	{
		if(request == null)
			return null;

		String sCookie = null;

		if(request.getCookies() != null)
		{
			Cookie[] cookies = request.getCookies();

			for(int i =  0 ; i < cookies.length ; i++)
			{
				if(sName.equalsIgnoreCase(cookies[i].getName()))
				{
					sCookie = cookies[i].getValue();
					return sCookie;
				}
			}
		}
            
		return sCookie;	
	}

   public PrintWriter getOut()
   {
      return out;
   }

   public HttpServletRequest getRequest()
   {
      return request;
   }
   

   public Object getRequestVariable(String sName)
   {
      Object obj = null;

      if(this.page != null)
      {
        obj = this.page.getAttribute(JS_LIBRARIES);
      }
      else
      {
        obj = request.getAttribute(JS_LIBRARIES);
      }
      return obj;
   }

   public void setRequestVariable(String sName, Object obj)
   {
      if(this.page != null)
      {
         this.page.setAttribute(sName, obj);
      }
      else
      {
         request.setAttribute(sName, obj);
      }
   }
   
   public void initBeanForJS(int libFlag)
   {
      int missingLib = 0;
      int currentFlag = 0;

      libFlag |= JSUtilitiesLib; // Always included the cabo_utilities library

      Object libInfo = null;

      libInfo = getRequestVariable(JS_LIBRARIES);

      if (libInfo == null)
      {
         missingLib = libFlag;
      }
      else
      {
         currentFlag = ((Integer)libInfo).intValue();

         // If everything needed is already set, nothing to do
         if ((currentFlag & libFlag) == libFlag)
         {
            return;
         }

         missingLib = ~currentFlag & libFlag;
      }

      if ((missingLib & JSUtilitiesLib) != 0)
      {
         generateScriptSrc("cabo_utilities.js");
         out.println("<script LANGUAGE=\"javascript\">");
         out.print("image_dir=\"");
         out.print(defaultCaboBase);
         out.println("/\"+image_dir;");
         out.print("jslib_dir=\"");
         out.print(defaultCaboBase);
         out.println("/\"+jslib_dir;");
         out.println("</script>");
      }

      if ((missingLib & JSTableConstructLib) != 0)
      {
         generateScriptSrc("table_constructor.js");
      }

      if ((missingLib & JSTreeConstructLib) != 0)
      {
         generateScriptSrc("tree_constructor.js");
      }

      if ((missingLib & JSDataConstructLib) != 0)
      {
         generateScriptSrc("data_constructor.js");
      }

      if ((missingLib & JSToolbarConstructorLib) != 0)
      {
         generateScriptSrc("toolbar_constructor.js");
      }

      if ((missingLib & JSCalendarConstructorLib) != 0)
      {
         generateScriptSrc("calendar_constructor.js");

         String NLSFormat = (String) ctx.userData.get("NLSFormat");
         if (NLSFormat != null)
         {
            out.println("<script language=\"javascript\">");
            out.println("NLSformat = \"" + NLSFormat + "\";");
            out.println("</script>");
         }
      }

      if ((missingLib & JSModalPageConstructorLib) != 0)
      {
         generateScriptSrc("modal_page_constructor.js");
      }

      if ((missingLib & JSButtonConstructorLib) != 0)
      {
         generateScriptSrc("button_constructor.js");
      }

      if ((missingLib & JSContainerConstructorLib) != 0)
      {
         generateScriptSrc("container_constructor.js");
      }

      libInfo = new Integer(currentFlag | libFlag);
//      session.putValue(request.toString() + JS_LIBRARIES, libInfo);
      setRequestVariable(JS_LIBRARIES, libInfo);
   }
	
	protected void generateScriptSrc(String libName)
   {
      out.print("<script src=\"");
      out.print(defaultCaboBase);
      out.print("/jslib/");
      out.print(libName);
      out.println("\" language=\"javascript\"></script>");
   }
   
   public String getUniqueName(String rootName)
   {
      int      id;
      //Integer  storedId = (Integer) session.getValue(request.toString() + JS_NAMEID);

      Integer  storedId = (Integer)this.getRequestVariable(JS_NAMEID);

      if (storedId == null)
      {
         id = 0;
      }
      else
      {
         id = storedId.intValue() + 1;
      }

      storedId = new Integer(id);
      setRequestVariable(JS_NAMEID, storedId);
      
      return rootName + storedId.toString();
   }
   
   /**
	*	Renders the Web Bean's contents to the output stream.
	*/
	public void render() throws Exception 
	{
		render(out);
	}

	/**
	*	Renders the Web Bean's contents to the specified PrintWriter.
	* <p>
	*	@param out the PrintWriter to render to.
	*/
	public void	render(PrintWriter outp)
	{
	 
	}	
}
