package ibase.webitm.ejb.fin;

import java.rmi.RemoteException;
import org.w3c.dom.*;
import javax.ejb.*;
 
import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.*;
import ibase.webitm.utility.GenericUtility;
import ibase.system.config.*;
import java.sql.*;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3
public class BankReco extends ValidatorEJB implements BankRecoLocal,BankRecoRemote // SessionBean
{
  	/*public void ejbCreate() throws RemoteException, CreateException
	{
		try
		{
			System.out.println("ValidatorEJB ejbCreate called.........");
			
		}
		catch (Exception e)
		{
			System.out.println("Exception :ValidatorEJB :ejbCreate :==>"+e);
			throw new CreateException();
		}
	}
	public void ejbRemove()
	{
	}
	public void ejbActivate() 
	{
	}
	public void ejbPassivate() 
	{
	}*/
	public String itemChanged() throws RemoteException,ITMException
	{
		return "";
	}
	public String itemChanged(String xmlString, String xmlString1,String xmlString2, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException,ITMException 
	{
		Document dom = null;
		Document dom1 = null;
		Document dom2 = null;

		String valueXmlString = "";
		GenericUtility genericUtility = GenericUtility.getInstance();
		try
		{
			System.out.println("xmlString :"+xmlString+ "\n xmlString1 :"+xmlString1+ " \nxmlString2 :"+xmlString2);
			if(xmlString != null && xmlString.trim().length()!=0)
			{
				dom = genericUtility.parseString(xmlString);
			}
			if (xmlString1 != null && xmlString1.trim().length()!=0)
			{
				dom1 = genericUtility.parseString(xmlString1);
			}
			if (xmlString2 != null && xmlString2.trim().length()!=0)
			{
				dom2 = genericUtility.parseString(xmlString2);
			}
			valueXmlString = itemChanged(dom,dom1,dom2,objContext,currentColumn,editFlag,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception :BankRecoItmChngEJB :itemChanged(String,String) :==>\n"+e.getMessage());
			valueXmlString = genericUtility.createErrorString(e);			
		}
		return valueXmlString.toString(); 
	}
	public String itemChanged(Document dom, Document dom1, Document dom2, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		String columnValue = "";
		String sql = "";
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		StringBuffer valueXmlString = new StringBuffer();
	    ConnDriver connDriver = new ConnDriver();
		GenericUtility genericUtility = GenericUtility.getInstance();
		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			columnValue = genericUtility.getColumnValue(currentColumn,dom);
			System.out.println("columnValue :"+columnValue);
			valueXmlString = new StringBuffer("<?xml version=\"1.0\"?><Root><Header><editFlag>");
			valueXmlString.append(editFlag).append("</editFlag></Header>");
			valueXmlString.append("<Detail1>");
			if (currentColumn.trim().equals("bank_code"))
			{
				sql = "SELECT BANK_NAME FROM BANK WHERE BANK_CODE = '"+ columnValue +"'";
				System.out.println("sql :"+sql);
				stmt = conn.createStatement();
				rs = stmt.executeQuery(sql);
				if (rs.next())
				{
					valueXmlString.append("<bank_name>").append(rs.getString(1)).append("</bank_name>");					
				}
			}
			valueXmlString.append("</Detail1>");
			valueXmlString.append("</Root>");			
		}
		catch(Exception e)
		{
			System.out.println("Exception in BankRecoItmChngEJB.java occured");
		}
		finally
		{
			try
			{
				System.out.println("Closing Connection.......");
				conn.close();
				conn = null;
			}catch(Exception se){}
		}
		return valueXmlString.toString(); 
	}
}