package com.sap.mw.jco.jra.examples;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javax.naming.*;

public class ExampleServlet extends HttpServlet
{
  private static final String CONTENT_TYPE = "text/html";
  /**Initialize global variables*/
  private String prefix = "ejb/";
  private String appServerName = "an unknown Application Server";
  private String contextFactory = "";

  public void init() throws ServletException
  {
  }
  /**Process the HTTP Get request*/
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
      Context ctx = null;
      String m_bankNumber = "";

      response.setContentType(CONTENT_TYPE);
      PrintWriter out = response.getWriter();
      out.println("<html>");
      out.println("<head><title>ExampleServlet</title></head>");
      out.println("<body>");
      out.println("<p>The servlet ExampleServlet received following text:</p>");

      try
      {
        //obtain the initial JNDI context
        ctx = new InitialContext();
      }
      catch (Exception e)
      {
        out.println("<p>Can't get context."+e.toString()+"</p>");
        out.println("</body></html>");
        return;
      }

      // determine J2EE Server origin
      try
      {
          contextFactory = (String)ctx.getEnvironment().get("java.naming.factory.initial");

          if (contextFactory != null) this.determineServer(contextFactory);
          out.println("<br><br>Your application is running on "+appServerName);

      }//try
      catch (Exception ex)
      {
          out.println("<br>Can't determine J2EE Server origin.");
          ex.printStackTrace(out);

      }//catch

      ExampleHome home = null;
      try
      {
          home = (ExampleHome)ctx.lookup(prefix+"ExampleBean");
      }
      catch(Exception e)
      {
          out.println("Can't look up "+prefix+"ExampleBean.");
          e.printStackTrace(out);
      }
      Example bean = null;
      try
      {
          bean = home.create();
      }
      catch(Exception e)
      {
          out.println("Can't create new ExampleBean .");
          e.printStackTrace(out);
      }
      ExampleBMHome bmhome = null;
      try
      {
          bmhome = (ExampleBMHome)ctx.lookup(prefix+"ExampleBMBean");
      }
      catch(Exception e)
      {
          System.out.println("Can't look up "+prefix+"ExampleBMBean.");
          e.printStackTrace(out);
      }
      ExampleBM bmbean = null;
      try
      {
          bmbean = bmhome.create();
      }
      catch(Exception e1)
      {
          out.println("Can't create new ExampleBMBean .");
      }

      out.println("<br>");
      try
      {
          out.println("<br><br>Calling method callBapiBankGetList() from component (bean) Managed bean<br>");
          out.println("List of banks:");
          out.println((Util.transformTableToString("<br>", bean.callBapiBankGetList("DE", 5))));

          //call methods from beans
          out.println("<br>Calling method testSimpleConnection()<br>");
          out.println(bean.testSimpleConnection());

          out.println("<br><br>Calling method callFunctionStfcConnection()<br>");
          out.println(bean.callFunctionStfcConnection("Function STFC_CONNECTION successfully called"));

          out.println("<br><br>Calling method callBapiBankCreateUsingContainerTransaction()<br>");
          m_bankNumber = Util.generateBankNumber();
          out.println(bean.callBapiBankCreateUsingContainerTransaction("DE", m_bankNumber, "ContainerTransaction", "Walldorf"));
          out.println("<br>Calling method callBapiBankGetDetail()<br>");
          out.println(bean.callBapiBankGetDetail("DE", m_bankNumber));

          out.println("<br><br>Calling method callBapiBankCreateUsingUserTransaction<br>");
          m_bankNumber = Util.generateBankNumber();
          out.println(bmbean.callBapiBankCreateUsingUserTransaction("DE", m_bankNumber, "UserTransaction", "Walldorf"));
          out.println("<br>Calling method callBapiBankGetDetail()<br>");
          out.println(bean.callBapiBankGetDetail("DE", m_bankNumber));

          out.println("<br><br>Calling method callBapiBankCreateUsingLocalTransaction<br>");
          m_bankNumber = Util.generateBankNumber();
          out.println(bmbean.callBapiBankCreateUsingLocalTransaction("DE", m_bankNumber, "UserTransaction", "Walldorf"));
          out.println("<br>Calling method callBapiBankGetDetail()<br>");
          out.println(bean.callBapiBankGetDetail("DE", m_bankNumber));



      }
      catch(Throwable e)
      {
          out.println("<br>Exception on ExampleServlet invocating bean methods");
          e.printStackTrace(new PrintWriter(out));
      }

      out.println("</body></html>");

  }
  /**Clean up resources*/
  public void destroy()
  {
  }
  private void determineServer(String contextFactory)
  {
      if (contextFactory.equals("com.inqmy.services.jndi.InitialContextFactoryImpl"))
      {
          appServerName = "Inqmy Application Server / SAP J2EE 620";
          prefix = "";
      }
      else if (contextFactory.equals("com.sap.engine.services.jndi.InitialContextFactoryImpl"))
      {
          appServerName = "SAP J2EE Engine 630";
          prefix = "Examples/";
      }

      else if (contextFactory.equals("com.sun.enterprise.naming.SerialInitContextFactory"))
      {
          appServerName = "SUN Reference Implementation 1.3.X";
      }
  }
}