package ibase.webitm.ejb.adm;

import ibase.webitm.utility.*;
//import ibase.utility.*;
import ibase.webitm.ejb.*;
import ibase.system.config.*;

import java.rmi.RemoteException;
import java.text.*;
import java.util.*;
import java.math.*;
import java.sql.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.ejb.*;
import javax.naming.InitialContext;
import java.io.*;
import java.io.File;
import java.sql.*;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3

public class SepCloseConf  extends ActionHandlerEJB implements SepCloseConfLocal,SepCloseConfRemote //SessionBean
{
	
	/*public void ejbCreate() throws RemoteException, CreateException 
	{
		System.out.println("Entering the EJB of confirmation[(SepCloseConfEJB)]...............");
	}
	public void ejbRemove()
	{
	}
	public void ejbActivate() 
	{
	}
	public void ejbPassivate() 
	{
	}*/

	public String confirm(String tranID,String xtraParams, String forcedFlag) throws RemoteException,ITMException
	{
		String result = "";
		try
		{
			System.out.println("Confirming SepClose....");
			result =  confirmSepCloseConf(tranID,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception ::"+e.getMessage());
			throw new ITMException(e);
		}
		System.out.println("Returning Result ::"+result);
		return result;
	}
	private	String confirmSepCloseConf(String tranID,String xtraParams)throws RemoteException,ITMException
	{
		Connection con = null;
		Statement stmt = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
										
		String empCodeApprv = "";
		java.util.Date chgDate = null;
		String chgUser = ""; 
		String chgTerm = "";
		String update = "";
		int upd;
		String retString = "";
			
		GenericUtility genericUtility = GenericUtility.getInstance();
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		ConnDriver connDriver = new ConnDriver();
		ValidatorEJB ve = new ValidatorEJB();
		String empCode = ve.getValueFromXTRA_PARAMS(xtraParams,"loginEmpCode");
		System.out.println("Emp code="+xtraParams);
		
		try
		{
		
			con = connDriver.getConnectDB("DriverITM");
			System.out.println("Confirm  Called.....");
		}
		catch(Exception ex){System.out.println("[Exception:]"+ex);}	
			
 
//		TESTING FOR CONFIRM FIELD
		if(dupConf( tranID, con))
		{
			retString =	itmDBAccessEJB.getErrorString("","VTCREDIT01","","",con);
			System.out.println("enter into confirm");
				
		}
		else
		{
//			CONFIRM THE SEPARATION_CLOSE TABLE BY UPDATING THE STATUS FIELD
			try
			{
				update = "UPDATE SEPARATION_CLOSE  SET STATUS  = 'X' WHERE TRAN_ID = ? ";
				pstmt = con.prepareStatement(update);
				pstmt.setString(1,tranID);
				upd = pstmt.executeUpdate();
			}
			catch(SQLException se2)
			{
				System.out.println("SQLException :[SepCloseConf][Excuting Query Failed]" + update + se2.getMessage());
			}
			catch(Exception ex2)
			{
			
				retString = "Not Confirmed";
				System.out.println("Exception ::"+ex2.getMessage());
				ex2.printStackTrace();
			}
		
		}
			return retString;
	}
	private boolean dupConf(String tarnID,Connection con) throws ITMException
	{
		Statement stmt = null;
		ResultSet rs = null;
		String sql="";
		boolean flag = false; 
		try
		{
			sql = " SELECT COUNT(*) FROM SEPARATION_CLOSE" +
						" WHERE TRAN_ID = '"+ tarnID +"' AND STATUS = 'X' ";
						
			System.out.println("[ConfirmSepCloseEJB:] [dupConf] EXECUTE SELECT: "+sql);
			
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			if(rs.next())
			{
				if(rs.getInt(1)!= 0)
				{
					flag = true;
				}
			}
			rs.close();
			stmt.close();
		}
		catch(SQLException e)
		{
			System.out.println("Exception : ConfirmGl_openEJB [dupConf]:==>\n"+e.getMessage());
			e.printStackTrace();
			throw new ITMException(e);
		}
		return flag;
	}
	

}