/*
Window Name : w_bank_unreco
Button Name : Undo
Action      : Undo the Transaction i.e detele the record from BANKRECO_TRACE Table and 
              update BANK_STATEMENT Table as per the Conditions.
*/
package ibase.webitm.ejb.fin.adv;

import java.rmi.RemoteException;
import java.sql.*;
import javax.ejb.*;

import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.*;
import ibase.system.config.*;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3

public class BankUnRecoUndo extends ActionHandlerEJB implements  BankUnRecoUndoLocal,BankUnRecoUndoRemote //SessionBean
{
	/*public void ejbCreate() throws RemoteException, CreateException 
	{
	}

	public void ejbRemove()
	{
	}

	public void ejbActivate() 
	{
	}

	public void ejbPassivate() 
	{
	}*/

    public String actionHandler() throws RemoteException,ITMException
	{
		return "";
	}
	//New Function is added to run in jboss4 - Gulzar - 21/01/09 (FI89PHE024)	
	public String confirm(String componentName, String tranID, String xtraParams, String forcedFlag) throws RemoteException,ITMException
	{
		System.out.println("Calling confirm......... from BankUnRecoEJB");
		String errStr = "";
		try
		{
			System.out.println("Tran Id :"+tranID);
			errStr = actionUndo(tranID,xtraParams);			
		}
	   	catch(Exception e)
		{
			System.out.println("Exception :BankUnReco :confirm():" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
	    return errStr;
	}
	//New Function is added to run in jboss2.4.4 if method name = 'confirm' - Gulzar - 21/01/09 (FI89PHE024)	
	public String confirm(String tranID, String xtraParams, String forcedFlag) throws RemoteException,ITMException
	{
		System.out.println("Calling confirm......... from BankUnRecoEJB");
		String errStr = "";
		try
		{
			System.out.println("Tran Id :"+tranID);
			errStr = actionUndo(tranID,xtraParams);			
		}
	   	catch(Exception e)
		{
			System.out.println("Exception :BankUnReco :confirm():" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
	    return errStr;
	}
	//End Added - Gulzar - 21/01/09 (FI89PHE024)	

	public String actionHandler(String tranID, String xtraParams,String forcedFlag) throws RemoteException,ITMException
	{
		System.out.println("Calling actionHandler from BankUnRecoEJB");
		String errStr = "";
		try
		{
			System.out.println("Tran Id :"+tranID);
			errStr = actionUndo(tranID,xtraParams);			
		}
	   	catch(Exception e)
		{
			System.out.println("Exception :BankUnReco :actionHandler(String xmlString):" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
	    return errStr;
	}
//Aviprash Start 9/22/2005
	private String actionUndo(String tranID, String xtraParams) throws RemoteException,ITMException
	{
		double amtAdj = 0;
		double amt = 0;
		double baseAmount = 0;
		int rowsDelete = 0;
		int rowsUpdate = 0;
		int rows = 0;
		String sql = "";
		String sql1 = "";
		String sql2 = "";
		String tranIdStat = "";
		String errCode = "";
		String autoEntry = "";
		String stat = "";
		String errString = ""; 
		PreparedStatement pstmt = null, pstmt1 = null;
		Connection conn = null;
		ResultSet rs = null;
		ResultSet rs1 = null;
		Statement stmt = null, stmt1 = null;
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		ConnDriver connDriver = new ConnDriver();
		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			conn.setAutoCommit(false); //Added by Gulzar 06/01/07
		}
		catch(Exception e){}
		try
		{
			//conn = getConnection();
			System.out.println("tranId :="+tranID);
			sql = "SELECT AMT_ADJ FROM BANKTRAN_LOG WHERE TRAN_ID = '"+ tranID +"'";
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			if (rs.next())
			{
				amtAdj  = rs.getDouble(1);
				System.out.println("amtAdj :"+amtAdj); 			
			}
			if (amtAdj > 0)
			{
  				sql = "SELECT TRAN_ID__STAT, AMOUNT FROM  BANKRECO_TRACE WHERE TRAN_ID__LOG = '"+ tranID + "'";
				System.out.println("The sql of Select Query :"+sql);
				rs = stmt.executeQuery(sql);
				if (rs.next())
				{
					do
					{
						tranIdStat = rs.getString(1);
						amt = rs.getDouble(2);
						System.out.println("tranID using delete :"+tranID+ "\n tranIdStat using delete :"+tranIdStat);					
						sql1 = "DELETE FROM BANKRECO_TRACE WHERE TRAN_ID__LOG = '" + tranID + "'"+" AND TRAN_ID__STAT = '"+ tranIdStat +"'";
						stmt1 = conn.createStatement(); //Gulzar 06/01/07
						rowsDelete = stmt1.executeUpdate(sql1); //Changes from stmt to stmt1 Gulzar 06/01/07
						System.out.println("The sql of Delete Query :"+sql1);
						System.out.println("Rows Deleted ::"+rowsDelete);					
						if (rowsDelete == 0)
						{
							conn.rollback();
							errCode = "VTPROCUS";
							break;
						}
						stmt1.close();//Gulzar 06/01/07
						stmt1 = null; //Gulzar 06/01/07
						sql1 = "SELECT AMOUNT, AUTO_ENTRY FROM BANK_STATEMENT WHERE TRAN_ID = '"+ tranIdStat + "'";
						System.out.println("The sql of Select Query:"+sql1);
						stmt1 = conn.createStatement(); //Gulzar 06/01/07
						rs1 = stmt1.executeQuery(sql1);//Changes from stmt to stmt1 Gulzar 06/01/07
						if (rs1.next())
						{
							baseAmount = rs1.getDouble(1);
							autoEntry = rs1.getString(2);
						}
						else
						{
							conn.rollback();
							errCode = "VTPROCUS";
							break;
						}
						stmt1.close();//Gulzar 06/01/07
						stmt1 = null; //Gulzar 06/01/07
						System.out.println("autoEntry :"+autoEntry);
						if (autoEntry == null)
						{
							autoEntry = "N";
						}
						System.out.println("autoEntry :"+autoEntry);
						if (autoEntry.equalsIgnoreCase("N"))
						{
							System.out.println("baseAmount :"+baseAmount);
							System.out.println("amt :"+amt);
							if (baseAmount == amt)
							{
								stat = "E";
							}
							else
							{
								stat = "P";
							}
							System.out.println("Date :"+new java.sql.Date(System.currentTimeMillis()));
							sql2 = "UPDATE BANK_STATEMENT SET AMT_ADJ = AMT_ADJ - ?, STATUS =?, status_date = ? WHERE TRAN_ID = ?";
							pstmt = conn.prepareStatement(sql2);
							pstmt.setDouble(1,amt);
							pstmt.setString(2,stat);
							pstmt.setTimestamp(3,new java.sql.Timestamp(System.currentTimeMillis()));
							pstmt.setString(4,tranIdStat);
							rowsUpdate = pstmt.executeUpdate();
							if (rowsUpdate < 1)
							{
								conn.rollback();
							}
							else
							{
								System.out.println("Update SQl ::"+sql2);
								System.out.println("Rows Updated ::"+rowsUpdate);
							}
							pstmt.close();//Gulzar 06/01/07
							pstmt = null; //Gulzar 06/01/07
						}
						else
						{
							System.out.println("baseAmount :"+baseAmount);
							System.out.println("amt :"+amt);
							if (baseAmount == amt)
							{
								sql2 = "DELETE FROM BANK_STATEMENT WHERE TRAN_ID ='"+ tranIdStat +"'";
								System.out.println("The sql of Delete Query:"+sql2);
								stmt1 = conn.createStatement(); //Gulzar 06/01/07
								rowsUpdate = stmt1.executeUpdate(sql2);
								stmt1.close();//Gulzar 06/01/07
								stmt1 = null; //Gulzar 06/01/07
								System.out.println("Delete Sql ::"+sql2);	
								System.out.println("Rows Deleted ::"+rowsUpdate);					
							}
							else
							{
								sql2 = "UPDATE BANK_STATEMENT SET AMT_ADJ = AMT_ADJ - ?, AMOUNT = AMOUNT - ? WHERE TRAN_ID =? ";
								pstmt = conn.prepareStatement(sql2);
								pstmt.setDouble(1,amt);
								pstmt.setDouble(2,amt);
								pstmt.setString(3,tranIdStat);
								rowsUpdate = pstmt.executeUpdate();
								pstmt.close();//Gulzar 06/01/07
								pstmt = null; //Gulzar 06/01/07
								System.out.println("The sql of Update Query:"+sql2);
								System.out.println("Rows Updated ::"+rowsUpdate);					
							}
						}
						if (rowsUpdate == 0)
						{
							conn.rollback();
							errCode = "VTPROCUS";
							break;
						}
					}while (rs.next());
				}
				else
				{
					errCode = "VTPROCUS";
				}
				stmt.close();
				stmt = null;
				if (errCode.equals(""))
				{
					sql1 = "UPDATE BANKTRAN_LOG SET AMT_ADJ = 0, STATUS = 'E' WHERE TRAN_ID ='"+ tranID +"'";	
					stmt = conn.createStatement(); //Gulzar 06/01/07
					rows = stmt.executeUpdate(sql1);
					stmt.close();//Gulzar 06/01/07
					stmt = null;//Gulzar 06/01/07
					System.out.println("The sql of Update Query:"+sql1);
					System.out.println("Rows Updated :"+rows);					
					if (rows == 0)
					{
						conn.rollback();
						errCode = "VTPROCUS";
					}
					else
					{
						conn.commit();
						errCode = "VTSUCC";
					}
				}
			}
			else
			{
				errCode = "VTPROCUS";
			}
			System.out.println("errCode: "+errCode);
			if (errCode != null  && errCode.trim().length() > 0)
			{
				//errString = itmDBAccessEJB.getErrorString("", errCode, "", "", conn);//Gulzar 06/01/07
				errString = itmDBAccessEJB.getErrorString("", errCode, "");//Gulzar 06/01/07
				System.out.println("errString: "+errString);
			}		
		}
		catch(SQLException se)
		{
			System.out.println("SQLException[] :[BankUnRecoUndoEJB][actionUndo()][Delete / Update Query Failed]:: " + se.getMessage());
			se.printStackTrace();
			errString = se.getMessage();
			return errString;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			errString = e.getMessage();
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				System.out.println("Closing Connection......");
				if (conn != null)
				{
					conn.close();
					conn = null;
				}
				if (stmt != null)
				{
					stmt.close();
					stmt = null;
				}
				if (stmt1 != null)
				{
					stmt1.close();
					stmt1 = null;
				}
				if (pstmt != null)
				{
					pstmt.close();
					pstmt = null;
				}
				if (rs != null)
				{
					rs.close();
					rs = null;
				}
				if (rs1 != null)
				{
					rs1.close();
					rs1 = null;
				}
			}
			catch(SQLException se){}
		}		
		System.out.println("errString: "+errString);
		return errString;
	} 
}