package com.srccodes.clienttest;


import java.io.IOException;


import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.naming.InitialContext;
import javax.naming.NameNotFoundException;
import javax.naming.NamingException;
import javax.resource.cci.Connection;
import javax.resource.cci.ConnectionFactory;
import javax.resource.cci.Interaction;
import javax.resource.cci.MappedRecord;
import javax.resource.cci.Record;
import javax.resource.cci.RecordFactory;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class ETPTest
 */
@WebServlet("/ETPTest")
public class ETPTest extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
	private static final long serialVersionUID = 1L;
       
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ETPTest() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/html");
		PrintWriter writer = response.getWriter();
        writer.println("<html>");
        writer.println("<head><title>Hello World Servlet</title></head>");
        writer.println("<body>");
        // call the JCA passing the response object's writer
        callJCA(writer);
        writer.println("</body>");
        writer.println("</html>");
        writer.close();
		
	
		
	}
	/**
	 * dump - print output to both system.out and a printwriter
	 * @param writer printwriter to write text to
	 * @param out text to write
	 */
	protected void dump(PrintWriter writer,String out) {
		System.out.println(out);
		writer.println(out + "<BR>");
		
	}
	/**
	 * callJCA - call the JCA adapter passing and recieving a commArea
	 * @param writer
	 */
	protected void callJCA(PrintWriter writer) {
		String res = null;
		Object obj;
		String jndiName="HCI_POOL" ;
		try
		{
			// get a reference to the JCA based on the JNDI name
			InitialContext ic = new InitialContext();
			obj = ic.lookup(jndiName);
			if (obj == null){
				dump(writer,"The Object returned is null,could not find JNDI pool: " + jndiName);
				return;
			}
			dump(writer,"client:Connection: " + obj.getClass().getName());
			ConnectionFactory connectionFactory = (ConnectionFactory)obj;
			// get a connection from the connection factory
			Connection connection = connectionFactory.getConnection();
			dump(writer,"client:EisBean connection = " + connection);
			// create an interaction we can use to call the execute function on
			Interaction interaction = connection.createInteraction();
			dump(writer,"client:EisBean interaction = " + interaction);
			// get the record factory and create a record we can use to pass commArea and config with
			RecordFactory recordFactory = connectionFactory.getRecordFactory();
			dump(writer,"client:EisBean recordFactory = " + recordFactory);
			// create the record, passing the outgoing comm area
			MappedRecord mappedRecord = recordFactory.createMappedRecord("Hello ETP transaction from Java        ");
			dump(writer,"client:EisBean mappedRecord = " + mappedRecord);
			// pass the transaction and or program id as a map in the record
			Map<String, String> m = new HashMap<String, String>();
			m.put("TRANSACTION", "ETRN");
			m.put("PROGRAM", "etpetrn");
			mappedRecord.putAll(m);
			// call the interaction passing the record and get the result record back
			Record resultRecord = interaction.execute(null, mappedRecord);
			dump(writer,"client:EisBean resultRecord = " + resultRecord);
			// close the connection,releasing it back to the pool
			connection.close();
			// the incoming commArea is stored as the record name.
			res = resultRecord.getRecordName();
			dump(writer,"client:returned commArea= " + res);
		}
		catch (NameNotFoundException e1) {
			dump(writer,"\nJNDI binding was not found "+jndiName);
			e1.printStackTrace();
		}
		catch (NamingException e) {
			dump(writer,"\nJNDI binding error");
			e.printStackTrace();
 		}
		catch (Exception ne)
		{
			dump(writer,"Error When invoking Pool");
			ne.printStackTrace();
		}
	}
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
        
        PrintWriter writer = response.getWriter();
        writer.println("<html>");
        writer.println("<head><title>ETPTest Servlet</title></head>");
        writer.println("</html>");
        writer.close();
	}

}
