package ibase.webitm.ejb.adm.adv;
import ibase.webitm.ejb.*;
import ibase.webitm.utility.ITMException;
import ibase.webitm.utility.GenericUtility;

import ibase.system.config.ConnDriver;
import java.sql.*;
import java.rmi.RemoteException;
import javax.ejb.Stateless; 

@Stateless 
public class EmpBondDefConfirm extends ActionHandlerEJB implements EmpBondDefConfirmLocal,EmpBondDefConfirmRemote 
{
	public String confirm(String tranId, String xtraParams, String forcedFlag) throws RemoteException,ITMException
	{
		
		String retString = "";
		try
		{
			
			Connection conn=null;		
			retString = confirm( tranId, xtraParams, forcedFlag, conn);		
		
		}
		catch(Exception e)
		{
			System.out.println("Exception :EmpBondDefConfirm :confirm():" + e.getMessage() + ":");
			retString = GenericUtility.getInstance().createErrorString(e);
			e.printStackTrace();
		}
		return retString;
	}
	
	public String confirm( String tranId, String xtraParams, String forcedFlag, Connection conn)
	{
		System.out.println("--------------confirm method of EmpBondDefConfirm ------------- ");
		ResultSet rs = null;
		PreparedStatement pstmt = null;	
		 conn = null;
		 ITMDBAccessEJB itmdbAccess=new ITMDBAccessEJB();
		String retString = "",errString="";
		String sql = "",empcode="",confirmed="";
		double bondamt=0.0;
		int lineno=0,bondprd=0,updCnt=0;
		
		try
		{		
			ConnDriver connDriver = new ConnDriver();
			conn = connDriver.getConnectDB("DriverValidator");
			conn.setAutoCommit(false);		
			
			sql = "SELECT CONFIRMED FROM EMPLOYEE_BOND_DEF WHERE TRAN_ID = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, tranId);
			rs = pstmt.executeQuery();
			if( rs.next() )
			{
				confirmed =rs.getString("CONFIRMED")==null ? "":rs.getString("CONFIRMED");
			}	
			if( pstmt != null )
			{
				pstmt.close();
				pstmt = null;
			}
			if ( rs != null )
			{
				rs.close();
				rs = null;
			}
			if( confirmed != null && confirmed.equalsIgnoreCase("Y") )
			{
				errString = itmdbAccess.getErrorString("", "AEBCONF", "", "", conn);
				return errString;
			}
			
			
				sql="select ed.tran_id,e.emp_code,ed.line_no,ed.bond_amt,ed.bond_prd from employee_bond_def_det ed,employee_bond_def e "+ 
							" where ed.tran_id=?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, tranId);
				rs = pstmt.executeQuery();
				sql="update employee_bond set bond_amt = ?,bond_prd = ? where line_no = ? and emp_code = ?";
				pstmt = conn.prepareStatement( sql );
				
				while(rs.next())
				{
					lineno=rs.getInt("line_no");
					bondamt=rs.getInt("bond_amt");
					bondprd=rs.getInt("bond_prd");
					empcode=rs.getString("emp_code");
					
					pstmt.setDouble(1, bondamt); 
					pstmt.setInt(2, bondprd);
					pstmt.setInt(3, lineno);
					pstmt.setString(4,empcode);
					updCnt = pstmt.executeUpdate();
					
				}
				
				if( pstmt != null )
				{
					pstmt.close();
					pstmt = null;
				}
				if ( rs != null )
				{
					rs.close();
					rs = null;
				}	
								
				System.out.println("UPDATE COUNT ---->>"+updCnt);
				if (updCnt > 0)
				{			
					retString=new ITMDBAccessEJB().getErrorString("", "VCONFSUC", "", "", conn);
					updateConfirm(conn,tranId);
					conn.commit();
					System.out.println("Bond amount and bond period updated successfully.......row(s) updated :"+updCnt);
					
				}else
				{
					
					retString=new ITMDBAccessEJB().getErrorString("", "VCONFUSUC", "", "", conn);
					conn.rollback();
					System.out.println("Not Updated Bond amount and bond period........");
				}
				
		}
		catch(Exception e)
		{
			try
			{
				conn.rollback();
			}
			catch (Exception e1)
			{
			}
			e.printStackTrace();
			System.out.println("Exception ::"+e.getMessage());
			
		}
		finally
		{
			try
			{
				if(pstmt != null)
				{
					pstmt.close();
					pstmt = null;					
				}	
				if (conn!=null)
				{
					conn.close();
					conn=null;
				}
							
			}
			catch(Exception e)
			{
				System.out.println(e.getMessage());
				e.printStackTrace();
				
			}
		}
		System.out.println("Returning Result ::"+retString);
		return retString;
		
		
	}
	public void updateConfirm(Connection conn,String tranId)
	{
		System.out.println("in updateConfirm method -------->>>");
		String sql = "UPDATE EMPLOYEE_BOND_DEF SET CONFIRMED = 'Y' WHERE TRAN_ID = ?";
		PreparedStatement pstmt1=null;
		try
		{
			pstmt1 = conn.prepareStatement( sql );	 
			pstmt1.setString(1, tranId); 
			int updCnt = pstmt1.executeUpdate();
			System.out.println("confirm updated -------->>>>>>>>>updCount :"+updCnt);
		}
		catch(Exception e)
		{
			try
			{
				conn.rollback();
			}
			catch(Exception e1)
			{
				e1.printStackTrace();
			}
			e.printStackTrace();					
		}
		try
		{
			if(pstmt1 != null)
			{
				pstmt1.close();
				pstmt1 = null;					
			}
				
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
			e.printStackTrace();
			
		}
	}
	
}
	
	