package ibase.webitm.ejb.fin;
import java.rmi.RemoteException;

import java.sql.*;
import javax.ejb.*;
import org.w3c.dom.*;
import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.*;
import ibase.webitm.utility.GenericUtility;
import java.io.*;
import ibase.utility.CommonConstants;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3


public class ChqNoChgPos extends ValidatorEJB implements  ChqNoChgPosLocal ,ChqNoChgPosRemote //SessionBean
{
	File filePtr ;
	File logDir ;
	String logDirName = "";		
	/*public void ejbCreate() throws RemoteException, CreateException 
	{
	}

	public void ejbRemove()
	{
	}

	public void ejbActivate() 
	{
	}

	public void ejbPassivate() 
	{
	}
	*/
    public String postSaveRec() throws RemoteException,ITMException
	{
		return "";
	}
	static void writeLog(File f,String Msg,boolean flag)
	{
		try{
			PrintWriter pw = new PrintWriter((new FileOutputStream(f,flag)),flag);				
			pw.println("PosSave ::"+Msg);		
			pw.close();		
		}
		catch(Exception exWm){exWm.printStackTrace();}
	}
	
	public String postSaveRec(String xmlString1,String domId,String objContext,String editFlag, String xtraParams, Connection conn)throws RemoteException,ITMException
	{
		if(CommonConstants.J2EE_VERSION.equals("1"))
		{
			logDirName =  CommonConstants.JBOSSHOME  + File.separator + "log" ;
		}
		else if(CommonConstants.J2EE_VERSION.equals("2"))
		{
			logDirName =  CommonConstants.JBOSSHOME   + File.separator + "server"+ File.separator +"default"+ File.separator +"log" ;
		}
		logDir = new File(logDirName);
		filePtr = new File(logDirName+ File.separator + "ChqNoChgPos.log");

		System.out.println("--------------------------------------->ChqNoChgPosEJB called");
		writeLog(filePtr,"ChqNoChgPosEJB called",true);
		Document dom = null;
		try
		{
			if (xmlString1 != null && xmlString1.trim().length() > 0)
			{
				dom = GenericUtility.getInstance().parseString(xmlString1);
				executepostSaveRec(dom,domId,objContext,editFlag,xtraParams,conn);
			}			
		}
	   	catch(Exception e)
		{
			System.out.println("Exception :ChqNoChgPosEJB :postSaveRec() :" + e.getMessage() + ":");
			writeLog(filePtr,"Exception in postSaveRec-->"+e,true);
			e.printStackTrace();
			throw new ITMException(e);
		}
		writeLog(filePtr,"Return form postSaveRec.............",true);
		return "";
	}

	private String executepostSaveRec(Document dom, String domID,String objContext,String editFlag, String xtraParams, Connection conn)throws RemoteException,ITMException
	{
		PreparedStatement pstmt = null, pstmt1 = null;
		ResultSet rs = null;
		String oldChqNo = "", newChqNo = "", tranIDPay = "" ;
		String sqlUpd = "";
		String oldRefDate = "", newRefDate = "";
		int updCnt = 0, updCnt1 = 0;

		writeLog(filePtr,"dom-->"+dom,true);
		writeLog(filePtr,"domID-->"+domID,true);
		writeLog(filePtr,"objContext-->"+objContext,true);
		writeLog(filePtr,"editFlag-->"+editFlag,true);
		writeLog(filePtr,"xtraParams-->"+xtraParams,true);
		writeLog(filePtr,"conn-->"+conn,true);

		try
		{
			conn.setAutoCommit(false); 
			GenericUtility genericUtility = GenericUtility.getInstance();
			if (dom != null )
			{
				oldChqNo	= genericUtility.getColumnValue("chq_no__old",dom);
				writeLog(filePtr,"oldChqNo-->"+oldChqNo,true);
				newChqNo	= genericUtility.getColumnValue("chq_no__new",dom);
				writeLog(filePtr,"newChqNo-->"+newChqNo,true);
				tranIDPay	= genericUtility.getColumnValue("tran_id__pay",dom);
				writeLog(filePtr,"tranIDPay-->"+tranIDPay,true);
				oldRefDate	= genericUtility.getColumnValue("ref_date__old",dom);
				writeLog(filePtr,"oldRefDate-->"+oldRefDate,true);
				newRefDate	= genericUtility.getColumnValue("ref_date",dom);
				writeLog(filePtr,"newRefDate-->"+newRefDate,true);
			}
			//changed by msalam on 24/10/07 for
			//directly appending tran_id in sqlUpd and removing ? and setString
			//next line commented and changed line added.
			//sqlUpd = "UPDATE MISC_PAYMENT SET REF_NO = ? , REF_NO__OLD = ? ,REF_DATE = ?, REF_DATE__OLD = ? where tran_id = ? ";	
			sqlUpd = " UPDATE MISC_PAYMENT SET REF_NO = ? , REF_NO__OLD = ? ,REF_DATE = ?, REF_DATE__OLD = ? "
				   + " where tran_id = '" + tranIDPay.trim() + "'";	
			writeLog(filePtr,"sqlUpd-->"+sqlUpd,true);
			System.out.println("Update Sql :"+sqlUpd);
			pstmt = conn.prepareStatement(sqlUpd);
			pstmt.setString(1,newChqNo); 
			pstmt.setString(2,oldChqNo);
			pstmt.setTimestamp(3,java.sql.Timestamp.valueOf(genericUtility.getValidDateTimeString(newRefDate,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat())));
			pstmt.setTimestamp(4,java.sql.Timestamp.valueOf(genericUtility.getValidDateTimeString(oldRefDate,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat())));
			//changed by msalam on 24/10/07 for
			//directly appending tran_id in sqlUpd and removing ? and setString
			//next line commented as not required.
			//pstmt.setString(5,tranIDPay);
			updCnt = pstmt.executeUpdate();
			if (updCnt == 1)
			{
				System.out.println("Update Into MISC_PAYMENT Sucessfully : updCnt : " + updCnt);
				writeLog(filePtr,"updCnt-->"+updCnt,true);
			}
			//changed by msalam on 24/10/07 for
			//directly appending tran_id in sqlUpd and removing ? and setString
			//next line commented and changed line added.
			//sqlUpd = "UPDATE BANKTRAN_LOG SET REF_NO = ?, REF_DATE = ? WHERE TRAN_SER = 'M-PAY' AND TRAN_NO = ? ";
			sqlUpd = " UPDATE BANKTRAN_LOG SET REF_NO = ?, REF_DATE = ? "
				   + " WHERE TRAN_SER = 'M-PAY' AND TRAN_NO = '" + tranIDPay.trim() + "'";
			System.out.println("Update Sql :"+sqlUpd);
			writeLog(filePtr,"sqlUpd-->"+sqlUpd,true);
			pstmt1 = conn.prepareStatement(sqlUpd);
			pstmt1.setString(1,newChqNo); 
			pstmt1.setTimestamp(2,java.sql.Timestamp.valueOf(genericUtility.getValidDateTimeString(newRefDate,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat())));
			//changed by msalam on 24/10/07 for
			//directly appending tran_id in sqlUpd and removing ? and setString
			//next line commented as not required.
			//pstmt1.setString(3,tranIDPay);
			updCnt1 = pstmt1.executeUpdate();
			if (updCnt1 == 1)
			{
				System.out.println("Update Into BANKTRAN_LOG Sucessfully : updCnt1 : " + updCnt1);
				writeLog(filePtr,"Update Into BANKTRAN_LOG Sucessfully",true);
			}
			if (updCnt == 1 && updCnt1 == 1)
			{
				System.out.println("Update Into MISC_PAYMENT and BANKTRAN_LOG Sucessfully : And conn.commit() called ::");
				writeLog(filePtr,"Update Into MISC_PAYMENT and BANKTRAN_LOG Sucessfully : And conn.commit() called ::",true);
				//conn.commit();
			}
		}
		catch (SQLException sqe)
		{
			System.out.println("The SQLException occurs in ChqNoChgPosEJB :: executepostSaveRec():: "+sqe);
			writeLog(filePtr,"The SQLException occurs in ChqNoChgPosEJB :: executepostSaveRec()::--> "+sqe,true);
			sqe.printStackTrace();
			try
			{
				conn.rollback();
			}
			catch(Exception e1)
			{
				writeLog(filePtr,"The Exception occurs in ChqNoChgPosEJB -->"+e1,true);
			}
			throw new ITMException(sqe);
		}
		catch(Exception e)
		{
			System.out.println("The Exception occurs in ChqNoChgPosEJB :: executepostSaveRec():: "+e);			
			writeLog(filePtr,"The Exception occurs in ChqNoChgPosEJB -->"+e,true);
			e.printStackTrace();
			try
			{
				conn.rollback();
			}
			catch(Exception e1){}
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				System.out.println("Closing stmt and pstmt......");
				writeLog(filePtr,"ChqNoChgPosEJB in finally............",true);
				if(pstmt != null)
				{
					pstmt.close();
					pstmt = null;
				}
				if(pstmt1 != null)
				{
					pstmt1.close();
					pstmt1 = null;
				}
			}
			catch(Exception e)
			{
				System.err.println("Exception :ChqNoChgPosEJB :: executepostSaveRec():: \n"+e.getMessage());
				writeLog(filePtr,"Exception :ChqNoChgPosEJB :: executepostSaveRec()-->"+e,true);
				e.printStackTrace();
				throw new ITMException(e);
			}
		}
		writeLog(filePtr,"Return from executepostSaveRec()",true);
		return "";
	}
}