package ibase.webitm.ejb.fin;

import ibase.system.config.ConnDriver;
import ibase.webitm.ejb.ValidatorEJB;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import javax.ejb.Stateless;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

// added for ejb3
@Stateless
public class AcctBalTransIC extends ValidatorEJB implements AcctBalTransICLocal,AcctBalTransICRemote
{
	public String itemChanged(String xmlString, String xmlString1,String xmlString2, String objContext, String currentColumn,String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		System.out.println("@@@@@@@ AcctBalTransIC itemChanged called");
		Document dom = null;
		Document dom1 = null;
		Document dom2 = null;
		String valueXmlString = "";
		try
		{
			if (xmlString != null && xmlString.trim().length() > 0)
			{
				dom = parseString(xmlString);
			}
			if (xmlString1 != null && xmlString1.trim().length() > 0)
			{
				dom1 = parseString(xmlString1);
			}
			if (xmlString2 != null && xmlString2.trim().length() > 0)
			{
				dom2 = parseString(xmlString2);
			}
			valueXmlString = itemChanged(dom, dom1, dom2, objContext,
					currentColumn, editFlag, xtraParams);
		} catch (Exception e)
		{
			System.out.println("Exception :[itemChanged( String, String )] :==>\n"+ e.getMessage());
		}
		return valueXmlString;

	}

	public String itemChanged(Document dom, Document dom1, Document dom2,String objContext, String currentColumn, String editFlag,String xtraParams) throws RemoteException, ITMException
	{
		System.out.println("@@@@@@@ AcctBalTransIC itemChanged called");
		StringBuffer valueXmlString = new StringBuffer();
		NodeList parentNodeList = null;
		NodeList childNodeList = null;
		Node parentNode = null;
		Node childNode = null;
		//Node parentNode1 = null;
		//String childNodeName = null;
		Timestamp currDate = null;
		String sql = "",loginSite="",siteCodePay="",siteCodeAcct="";
		String empCode = "",fname="",mname="",lname="";
		int currentFormNo = 0;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		GenericUtility genericUtility = null;
		ConnDriver connDriver = null;
		try
		{
			genericUtility = GenericUtility.getInstance();
			connDriver =  new ConnDriver();
			conn = connDriver.getConnectDB("DriverITM");
			conn.setAutoCommit(false);
			connDriver = null;
			if (objContext != null && objContext.trim().length() > 0)
			{
				currentFormNo = Integer.parseInt(objContext.trim());
			}
			valueXmlString = new StringBuffer("<?xml version = \"1.0\"?> <Root> <header> <editFlag>");
			valueXmlString.append(editFlag).append("</editFlag> </header>");
			switch (currentFormNo)
			{
			 case 1:
				parentNodeList = dom.getElementsByTagName("Detail1");
				parentNode = parentNodeList.item(0);
				childNodeList = parentNode.getChildNodes();
				loginSite = getValueFromXTRA_PARAMS(xtraParams, "loginSiteCode");
				valueXmlString.append("<Detail1>");
			//	int childNodeListLength = childNodeList.getLength();
				SimpleDateFormat sdf = new SimpleDateFormat(genericUtility.getApplDateFormat());
				SimpleDateFormat sdf1 = new SimpleDateFormat(genericUtility.getDBDateFormat());
				currDate =  java.sql.Timestamp.valueOf(sdf1.format(new java.util.Date()).toString() + " 00:00:00.0");
				if (currentColumn.trim().equalsIgnoreCase("emp_code"))
				{
					empCode = checkNull(genericUtility.getColumnValue("emp_code", dom));
					System.out.println("::emp_code - " + empCode);
					sql = " select emp_fname,emp_mname,emp_lname,pay_site from employee where emp_code = ? "; 
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, empCode);
					rs = pstmt.executeQuery();
					if(rs.next())
					{
						fname = checkNull(rs.getString("emp_fname"));
						mname = checkNull(rs.getString("emp_mname"));
						lname = checkNull(rs.getString("emp_lname"));
						siteCodePay = checkNull(rs.getString("pay_site"));
					}
					if (pstmt != null)
						pstmt.close();
					pstmt = null;
					if (rs != null)
						rs.close();
					rs = null;
					sql = " select site_code__acct from site where site_code = ? "; 
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, siteCodePay);
					rs = pstmt.executeQuery();
					if(rs.next())
					{
						siteCodeAcct = checkNull(rs.getString("site_code__acct"));
					}
					if (pstmt != null)
						pstmt.close();
					pstmt = null;
					if (rs != null)
						rs.close();
					rs = null;
					valueXmlString.append("<emp_name>").append("<![CDATA["+ fname+" "+mname+" "+lname +"]]>").append("</emp_name>");
					valueXmlString.append("<site_code__to>").append("<![CDATA["+ siteCodeAcct +"]]>").append("</site_code__to>");
				}
				if (currentColumn.trim().equalsIgnoreCase("itm_default"))
				{
					valueXmlString.append("<eff_date>").append("<![CDATA["+ sdf.format(currDate).toString() +"]]>").append("</eff_date>");
					valueXmlString.append("<site_code__fr>").append("<![CDATA["+ loginSite +"]]>").append("</site_code__fr>");
				}
				valueXmlString.append("</Detail1>");
		    }
			valueXmlString.append("</Root>");

		} catch (Exception e)
		{
			e.printStackTrace();
			System.out.println("Exception ::" + e.getMessage());
			throw new ITMException(e);
		} finally
		{
			try
			{
				if (conn != null)
				{
					if (pstmt != null)
						pstmt.close();
					pstmt = null;
					if (rs != null)
						rs.close();
					rs = null;
					conn.close();
					conn = null;
				}
			} catch (Exception d)
			{
				d.printStackTrace();
			}
		}
		return valueXmlString.toString();
	}
	private String checkNull(String str)
	{
		if(str == null) return "";		
		else return str;
	}
}

