/* $Id: HelloWorldExample.java,v 1.1.1.1 1999/10/09 00:19:59 duncan Exp $
 *
 */

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

/**
 * The simplest possible servlet.
 *
 * @author James Duncan Davidson
 */

public class HelloWorldExample extends HttpServlet {

  String headFile = "/usr/local/src/jakarta/build/tomcat/examples/WEB-INF/classes/head.txt";
  String bodyFile = "/usr/local/src/jakarta/build/tomcat/examples/WEB-INF/classes/body.txt";
  String footFile = "/usr/local/src/jakarta/build/tomcat/examples/WEB-INF/classes/foot.txt";

    ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");

  public static String getHTMLfile(String htmlSourceFile) {
    StringBuffer HTMLBuffer = new StringBuffer();
    String desc;
    char[] chars = null;
    int c;	
    FileReader in;
	
    try {
      File inputFile = new File(htmlSourceFile);
      in = new FileReader(inputFile);

      while ((c = in.read()) != -1)   
        HTMLBuffer.append((char) c);
		 
      in.close();	
    }
    catch (IOException ioe) {
      HTMLBuffer.append(ioe.toString());
    }

    return HTMLBuffer.toString();
  }	

  /** This method is the first method executed when the servlet loads.
   * @param HttpServletRequest
   * @param HttpServletResponse
   * @return void
   */ 
		 
  public void doGet (HttpServletRequest req, HttpServletResponse res)
  throws ServletException, IOException
  {

    PrintWriter out;
		
    res.setContentType("text/html");
    out = res.getWriter();	
    out.println(getHTMLfile(headFile));
    out.println(getHTMLfile(bodyFile));
    out.println(getHTMLfile(footFile));
  }

  /** This method will be called in repsonse to a Post request from the HTML page.
   * The parameter(s) will be parsed to determine what action to take.
   * @param HttpServletRequest
   * @param HttpServletResponse
   * @return void
   */

  public void doPost (HttpServletRequest req, HttpServletResponse res)
  throws ServletException, IOException
  {
    PrintWriter out;
		

    String command = req.getQueryString();	
    StringBuffer HTML = new StringBuffer();
    res.setContentType("text/html");
    out = res.getWriter();	
    HttpSession session = req.getSession(true);
    Class TransClass;

    HTML.append(getHTMLfile(headFile));
  
    try {
		
      // Determine which class to instantiate.	
      TransClass = (Class.forName(req.getParameter("transaction")));

      // Instantiate the class				
      session.putValue("Transaction", (Transaction)TransClass.newInstance());
						
      // Sets the TransID with the passed parameter
      ((Transaction)session.getValue("Transaction")).setTransID(req.getParameter("transaction"));

      // Adds the parameters passed in to the Transaction ParmTable Hashtable
      ((Transaction)session.getValue("Transaction")).ParmTable.put("Parm1",req.getParameter("parm1"));

      ((Transaction)session.getValue("Transaction")).ParmTable.put("Parm2",req.getParameter("parm2"));
				
      ((Transaction)session.getValue("Transaction")).ParmTable.put("Parm3",req.getParameter("parm3"));

      // Invoke the getHTML method of the desired transaction
      HTML.append(((Transaction)session.getValue("Transaction")).getHTML());
				
    }
    catch (ClassNotFoundException cnfe) {
      HTML.append("Could not find class: " + req.getParameter("transaction") + " to instantiate.<p>");
    }			
    catch(IllegalAccessException iae)  {
      HTML.append("Could not access class: " + req.getParameter("transaction") + " to instantiate.<p>");
    }			
    catch(InstantiationException ie) {
      HTML.append("Could not instantiate class: " + req.getParameter("transaction") + ".<p>");
    }
    catch(Exception e) {
      HTML.append("Could not load, init, or access class: " + req.getParameter("transaction") + ".<p>");
      HTML.append(e.toString() + ".<p>");
    }
    finally {
      // Add the footer to the HTML
      HTML.append(getHTMLfile(footFile));
      out.println(HTML.toString());
    }
  }


/*
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<body bgcolor=\"white\">");
        out.println("<head>");

	String title = rb.getString("helloworld.title");

	out.println("<title>1" + title + "</title>");
        out.println("</head>");
        out.println("<body>");

	// note that all links are created to be relative. this
	// ensures that we can move the web application that this
	// servlet belongs to to a different place in the url
	// tree and not have any harmful side effects.

        // XXX
        // making these absolute till we work out the
        // addition of a PathInfo issue 

	out.println("<a href=\"/examples/servlets/helloworld.html\">");
        out.println("<img src=\"/examples/images/code.gif\" height=24 " +
                    "width=24 align=right border=0 alt=\"view code\"></a>");
        out.println("<a href=\"/examples/servlets/index.html\">");
        out.println("<img src=\"/examples/images/return.gif\" height=24 " +
                    "width=24 align=right border=0 alt=\"return\"></a>");
        out.println("<h1>" + title + "</h1>");
        out.println("This is a test.</body>");
        out.println("</html>");
    }
*/
}




