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.text.SimpleDateFormat;
import java.rmi.RemoteException;
import javax.ejb.Stateless; 

@Stateless 
public class EmpBondDefConfirm extends ActionHandlerEJB implements EmpBondDefConfirmLocal,EmpBondDefConfirmRemote 
{
	String loginEmpCode="";
	int updcnt1=0;
	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
		{		
			 loginEmpCode = GenericUtility.getInstance().getValueFromXTRA_PARAMS(xtraParams, "loginEmpCode");
			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;
				}	
				updcnt1 =updateConfirm(conn,tranId);			
				System.out.println("UPDATE COUNT --updCnt-->>"+updCnt+" updcnt1 -->>"+updcnt1);
				if (updCnt > 0 && updcnt1 >0 )
				{			
					retString=new ITMDBAccessEJB().getErrorString("", "VCONFSUC", "", "", conn);
					conn.commit();
					System.out.println("Bond amount and bond period updated successfully.......");
					
				}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 int updateConfirm(Connection conn,String tranId)
	{
		PreparedStatement pstmt1=null;
		int count=0;
		String sql = "UPDATE EMPLOYEE_BOND_DEF SET CONFIRMED = ? ,CONF_DATE = ?,EMP_CODE__APRV=? WHERE TRAN_ID = ?";
		try
		{	
			pstmt1 = conn.prepareStatement( sql );	
			pstmt1.setString(1, "Y");
			pstmt1.setTimestamp(2, this.getCurrdateAppFormat()); 
			pstmt1.setString(3, loginEmpCode);
			pstmt1.setString(4, tranId);
			
			count = pstmt1.executeUpdate();
			System.out.println("confirm updated -------->>>>>>>>>updCount :"+count);
			
			
		}
		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();
			
		}
		return count;
	}
	 private java.sql.Timestamp getCurrdateAppFormat()
	    {
	        GenericUtility genericUtility = GenericUtility.getInstance();
			Timestamp timestamp = null;
	        try
	        {
	            java.util.Date date = null;
	            timestamp = new Timestamp(System.currentTimeMillis());            
	            SimpleDateFormat simpledateformat = new SimpleDateFormat(genericUtility.getDBDateFormat());
	            date = simpledateformat.parse(timestamp.toString());
	            timestamp = Timestamp.valueOf(simpledateformat.format(date).toString() + " 00:00:00.0");
	        }
	        catch(Exception exception)
	        {
	            System.out.println("Exception in [EmpBondDefConfirm] getCurrdateAppFormat " + exception.getMessage());
	        }
	        return timestamp;
	    }
	
}
	
	