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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.jsp.tagext.TagSupport;

import javax.servlet.jsp.JspException;

import java.io.StringWriter;
import java.io.StringBufferInputStream;

import oracle.jbo.html.jsp.ConnectionInfo;

import oracle.jbo.common.ampool.PoolMgr;
import oracle.jbo.common.ampool.ApplicationPool;

import oracle.jbo.ApplicationModule;
import oracle.jbo.common.*;
import oracle.jbo.*;
import java.util.*;

/**
 * A Class class.
 * <P>
 * @author Juan Oropeza
 */
public class ApplicationModuleTag extends TagSupport
{
  String sApplicationName;
  String sConfigName;
  String sUserName;
  String sPassword;
  String siiopUserName;
  String siiopPassword;

  public ApplicationModuleTag()
  {
  }

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

  public void setConfigname(String sValue)
  {
      this.sConfigName = sValue;
  }

  public void setUsername(String sValue)
  {
   this.sUserName = sValue;
  }


  public void setPassword(String sValue)
  {
   this.sPassword = sValue;
  }

  public void setIiop_username(String sValue)
  {
   this.siiopUserName = sValue;
  }


  public void setIiop_password(String sValue)
  {
   this.siiopPassword = sValue;
  }

  /**
   * doEndTag
   * @return int
   * @exception javax.servlet.jsp.JspException
   */
   public int doEndTag() throws JspException
   {
      try
      {
         PoolMgr  poolManager = PoolMgr.getInstance();

         if(!poolManager.isPoolCreated(sApplicationName))
         {
            String sConfigPackage = sConfigName.substring(0, sConfigName.lastIndexOf('.'));
            String sConfigSection = sConfigName.substring(sConfigName.lastIndexOf('.') + 1);

            // strip out the am class
            sConfigPackage = sConfigPackage.substring(0, sConfigPackage.lastIndexOf('.'));

            ApplicationPool pool = poolManager.createPool(sApplicationName, sConfigPackage, sConfigSection, null);

            if(sUserName != null)
            {
              pool.setUserName(sUserName);
            }

            if(sPassword != null)
            {
              pool.setPassword(sPassword);
            }
            
            Hashtable env = pool.getEnvironment();
            
            // configure IIOP security
            if(siiopUserName != null)
            {
              env.put(PropertyMetadata.SECURITY_PRINCIPAL.pName,siiopUserName);
            }

            if(siiopPassword != null)
            {
              env.put(PropertyMetadata.SECURITY_CREDENTIALS.pName, siiopPassword);
            }

         }

         // Get an application module resource.
         oracle.jbo.html.jsp.JSPApplicationRegistry appRegistry =
            oracle.jbo.html.jsp.JSPApplicationRegistry.getInstance();

         ApplicationModule am = appRegistry.getAppModuleInstance(
            sApplicationName
            , pageContext);

        // place default renderers into session, these will not be
        // exposed via config file
        pageContext.getSession().putValue("oracle_ord_im_OrdImageDomain_Renderer", "oracle.ord.html.OrdBuildURL");
        pageContext.getSession().putValue("oracle_ord_im_OrdAudioDomain_Renderer","oracle.ord.html.OrdBuildURL");
        pageContext.getSession().putValue("oracle_ord_im_OrdVideoDomain_Renderer","oracle.ord.html.OrdBuildURL");
        pageContext.getSession().putValue("oracle_ord_im_OrdVirDomain_Renderer", "oracle.ord.html.OrdBuildURL");

        pageContext.getSession().putValue("oracle_ord_im_OrdImageDomain_EditRenderer",  "oracle.ord.html.FileUploadField");
        pageContext.getSession().putValue("oracle_ord_im_OrdAudioDomain_EditRenderer",  "oracle.ord.html.FileUploadField");
        pageContext.getSession().putValue("oracle_ord_im_OrdVideoDomain_EditRenderer",  "oracle.ord.html.FileUploadField");
        pageContext.getSession().putValue("oracle_ord_im_OrdVirDomain_EditRenderer",    "oracle.ord.html.FileUploadField");
      }
      catch(Exception ex)
      {
         throw new JspException(ex.getMessage());
      }
      return SKIP_BODY;
   }

}

 