package ibase.webitm.ejb.adm;

import ibase.webitm.ejb.*;
import ibase.system.config.*;
import ibase.webitm.utility.*;
import ibase.utility.CommonConstants;
import ibase.webitm.utility.GenericUtility;
import java.rmi.RemoteException;
import java.text.*;
import java.util.*;
import java.util.Date;
import java.sql.*;
import java.io.*;

import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.ejb.*;
import javax.naming.InitialContext;
import java.text.*;

@Stateless
public class MiscRcpLoanAdjIC extends ValidatorEJB implements MiscRcpLoanAdjICLocal,MiscRcpLoanAdjICRemote
{
	String userId ="";
	public String itemChanged(String xmlString, String xmlString1,String xmlString2, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException,ITMException 
	{
		System.out.println("Inside [MiscRcpLoanAdjIC][itemChanged(String xmlString, String xmlString1,String xmlString2, String objContext, String currentColumn, String editFlag, String xtraParams)]>>" );

		System.out.println("itemChanged xmlString ["+xmlString+"]");
		System.out.println("itemChanged xmlString1["+xmlString1+"]");
		System.out.println("itemChanged xmlString2["+xmlString2+"]");
		
		Document dom = null;
		Document dom1 = null;
		Document dom2 = null;
		String valueXmlString = "";
		try
		{
			if(xmlString != null && xmlString.trim().length()!=0)
			{
				dom = parseString(xmlString);
			}
			else
			{
				System.out.println("xmlstring is null");
			}
			if(xmlString1 != null && xmlString1.trim().length()!=0)
			{
				dom1 = parseString(xmlString1);
			}
			else
			{
				System.out.println("xmlstring1 is null");
			}
			if(xmlString2 != null && xmlString2.trim().length()!=0)
			{
				dom2 = parseString(xmlString2);
			}
			else
			{
				System.out.println("xmlstring2 is null");
			}

			valueXmlString = itemChanged(dom,dom1,dom2,objContext,currentColumn,editFlag,xtraParams);//Ratikant 23/Mar/2005
		}
		catch(Exception e)
		{
			System.out.println("Exception :MiscRcpLoanAdjIC :itemChanged(String,String) :==>\n"+e.getMessage());
			e.printStackTrace();
			
		}
		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("Inside [MiscRcpLoanAdjIC][itemChanged(Document dom, Document dom1,Document dom2, String objContext, String currentColumn, String editFlag, String xtraParams)]>>" );

		String columnName = "";
		String columnValue = "";
		String domId = "";
		String errCode = "";
		String resultString = "";
		String tanSer = "";
		String empCode = "";
		String empFname = "";
		String empLname = "";
		String empPaySite = "";
		String tranDate = "";
		Date tDate = null;
		StringBuffer valueXmlString = null;
		Connection  conn = null;
		PreparedStatement pstmt = null;
		ResultSet rSet = null ;
		Statement stmt = null;

		Node elementName = null, parentNode = null;
		NodeList elementList = null;
		Element elementAttr = null;

		boolean continueLoop = true;
		boolean isFormNo = false;
		GenericUtility genericUtility = GenericUtility.getInstance();
	

		int currentFormNo = 0;
		try
		{	
			System.out.println("\nMiscRcpLoanAdjIC : itemChanged["+currentColumn+"] : xtraParams :"+xtraParams);
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(genericUtility.getApplDateFormat());
			
			ConnDriver connDriver = null;
			connDriver = new ConnDriver();
			
			conn = connDriver.getConnectDB("DriverValidator");
			valueXmlString = new StringBuffer("<?xml version=\"1.0\"?>\r\n<Root>\r\n<Header>\r\n<editFlag>");
			valueXmlString.append(editFlag).append("</editFlag>\r\n</Header>\r\n");
			valueXmlString.append("<Detail domID='1' >\r\n");
			
			if ( currentColumn == null )
			{
				currentColumn = "";
			}
		
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			
			switch(currentFormNo)
			{
				case 1:
					if(currentColumn.equalsIgnoreCase("itm_default"))
					{
						columnValue = currentColumn;
						System.out.println("Inside emp_code item default---->");
						String userId = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginCode");
						
						empCode = getColumnValue("emp_code",dom1,objContext);
						System.out.println("emp_code ---->"+empCode);
						
						valueXmlString.append("<emp_code><![CDATA[").append(empCode).append("]]></emp_code>\r\n");
						
						String SQL = "SELECT UPPER(EMP_FNAME) AS EMP_FNAME, UPPER(EMP_LNAME) AS EMP_LNAME,EMP_SITE FROM EMPLOYEE WHERE EMP_CODE = ?";
						pstmt = conn.prepareStatement(SQL);
						pstmt.setString(1,empCode);
						rSet = pstmt.executeQuery();
						if( rSet.next())
						{
							empFname = checkNull(rSet.getString("EMP_FNAME")) ;
							empLname = checkNull(rSet.getString("EMP_LNAME"));
							empPaySite =  checkNull(rSet.getString("EMP_SITE")) ;
							
							System.out.println("empFname ---->"+empFname);
							System.out.println("empLname ---->"+empLname);
							System.out.println("empPaySite ---->"+empPaySite);
						} 
						if( rSet != null )
						{
							rSet.close();
							rSet = null;
						}
						if( pstmt != null )
						{
							pstmt.close();
							pstmt = null;
						}
						valueXmlString.append("<emp_fname><![CDATA[").append(empFname).append("]]></emp_fname>\r\n");
						valueXmlString.append("<emp_lname><![CDATA[").append(empLname).append("]]></emp_lname>\r\n");
						
						
					}
					else if (currentColumn.trim().equalsIgnoreCase("emp_code"))
					{
						columnValue = currentColumn;
						System.out.println("Inside item change of emp_code ItemChange---->");
						String userId = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginCode");
						
						empCode = getColumnValue("emp_code",dom1,objContext);
						System.out.println("emp_code ---->"+empCode);
						
						valueXmlString.append("<emp_code><![CDATA[").append(empCode).append("]]></emp_code>\r\n");
						
						String SQL = "SELECT UPPER(EMP_FNAME) AS EMP_FNAME, UPPER(EMP_LNAME) AS EMP_LNAME,EMP_SITE FROM EMPLOYEE WHERE EMP_CODE = ?";
						pstmt = conn.prepareStatement(SQL);
						pstmt.setString(1,empCode);
						rSet = pstmt.executeQuery();
						if( rSet.next())
						{
							empFname = checkNull(rSet.getString("EMP_FNAME")) ;
							empLname = checkNull(rSet.getString("EMP_LNAME"));
							empPaySite =  checkNull(rSet.getString("EMP_SITE")) ;
							
							System.out.println("empFname ---->"+empFname);
							System.out.println("empLname ---->"+empLname);
							System.out.println("empPaySite ---->"+empPaySite);
						} 
						if( rSet != null )
						{
							rSet.close();
							rSet = null;
						}
						if( pstmt != null )
						{
							pstmt.close();
							pstmt = null;
						}
						valueXmlString.append("<emp_fname><![CDATA[").append(empFname).append("]]></emp_fname>\r\n");
						valueXmlString.append("<emp_lname><![CDATA[").append(empLname).append("]]></emp_lname>\r\n");
											}
					else if (currentColumn.trim().equalsIgnoreCase("ref_no"))
					{ 
						tanSer =  getColumnValue("tran_ser",dom1,objContext);
						String refNo =  getColumnValue("ref_no",dom1,objContext);
						System.out.println("tanSer ---->"+tanSer);
						System.out.println(" refNo..["+refNo+"]");
						
						empCode = getColumnValue("emp_code",dom1,objContext);
						System.out.println("empCode ---->"+empCode);
						
						String SQL = "SELECT "+	 
								        " MISC_RECEIPT.NET_AMT AS NET_AMT ,   "+
								        "  MISC_PAYABLES.TOT_AMT - MISC_PAYABLES.ADJ_AMT  AS ADJ_AMT  ,"+  
								        " MISC_RECEIPT.SITE_CODE  AS PAY_SITE ," +  
								        " MISC_PAYABLES.TRAN_DATE AS TRAN_DATE " +
								  "  FROM MISC_PAY_RCP_TRACE,   "+
								        " MISC_RECEIPT,   "+
								        " MISC_PAYABLES,   "+
								        " EMPLOYEE,   "+
								        " SITE,   "+
								        " FINENT  "+ 
								  " WHERE ( MISC_RECEIPT.TRAN_ID = MISC_PAY_RCP_TRACE.TRAN_ID__RCP ) AND  "+
								        " ( MISC_PAY_RCP_TRACE.REF_NO__PAY = MISC_PAYABLES.REF_NO ) AND  "+
								        " ( MISC_PAYABLES.SUNDRY_CODE = EMPLOYEE.EMP_CODE ) AND"+  
								        " ( MISC_RECEIPT.SITE_CODE = SITE.SITE_CODE ) AND  "+
								        " ( SITE.FIN_ENTITY = FINENT.FIN_ENTITY ) AND  "+
								        " ( MISC_RECEIPT.CURR_CODE = FINENT.CURR_CODE ) AND  "+
								        " ( ( MISC_RECEIPT.EMP_LOAN_ADJUST = 'Y' ) AND  "+
										" ( MISC_RECEIPT.CONFIRMED = 'Y' ) AND  "+
										" ( MISC_PAYABLES.TRAN_SER = 'M-ADV' )  AND  "+
										" ( MISC_PAYABLES.TOT_AMT - MISC_PAYABLES.ADJ_AMT <> 0 ) ) AND "+
										" MISC_PAYABLES.REF_NO = ?  AND "+
										" MISC_PAYABLES.SUNDRY_CODE = ?";
						
						pstmt = conn.prepareStatement(SQL);
						//pstmt.setString(1,tanSer);
						pstmt.setString(1,refNo);
						
						pstmt.setString(2,empCode);
						rSet = pstmt.executeQuery();
						if( rSet.next())
						{
							 empPaySite = checkNull(rSet.getString("PAY_SITE"));
							 tDate = rSet.getDate("TRAN_DATE");
							 System.out.println("empPaySite --"+empPaySite);
							 System.out.println("tDate --"+tDate);
							 if(tDate==null)
						  	{
							    tranDate = "";
						  	}else
						  	{
						  		tranDate=simpleDateFormat.format(tDate) ;
						  	}
							
							valueXmlString.append("<net_amt><![CDATA[").append(rSet.getString("NET_AMT")).append("]]></net_amt>\r\n");
							valueXmlString.append("<adj_amt><![CDATA[").append(rSet.getString("ADJ_AMT")).append("]]></adj_amt>\r\n");
							valueXmlString.append("<pay_site><![CDATA[").append(empPaySite).append("]]></pay_site>\r\n");
							valueXmlString.append("<tran_date protect=\"1\"><![CDATA[").append(tranDate).append("]]></tran_date>\r\n");
							

						}
						if( rSet != null )
						{
							rSet.close();
							rSet = null;
						}
						if( pstmt != null )
						{
							pstmt.close();
							pstmt = null;
						}
					}
					
					break;

			
			}
			valueXmlString.append("</Detail>\r\n");
			valueXmlString.append("</Root>\r\n");	
		}
		catch(Exception e)
		{
			e.printStackTrace();
			resultString  = getErrorString("period_code","EXPINVDPRC",userId);
			return resultString;
		}
		finally
		{
			try
			{
				if( rSet != null )
				{
					rSet.close();
					rSet = null;
				}
				if( pstmt != null )
				{
					pstmt.close();
					pstmt = null;
				}
				if ( stmt != null )
				{
					stmt.close();
					stmt = null;
				}
				if (conn != null )
				{
					conn.close();
					conn = null;
				}
			}
			catch(Exception e1)
			{
				e1.printStackTrace();
				throw new ITMException(e1);
			}
		}
		resultString = valueXmlString.toString();
		System.out.println("resultString[itemchanged].....::["+resultString+"]");
		
		return resultString;
	}
	
	
	private String checkNull(String input)
	{
		if (input==null)
		{
			input="";
		}
		else
		{
			input = input.trim();
		}
		return input;
	}

}
