package ibase.webitm.ejb.adm.adv;

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

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

@Stateless // added for ejb3

public class EmpLoanSanctionAprv extends ActionHandlerEJB implements EmpLoanSanctionAprvLocal,EmpLoanSanctionAprvRemote //SessionBean
{
	/*public void ejbCreate() throws RemoteException, CreateException 
	{
	}

	public void ejbRemove()
	{
	}

	public void ejbActivate() 
	{
	}

	public void ejbPassivate() 
	{
	}
	*/
    public String actionHandler() throws RemoteException,ITMException
	{
		return "";
	}
	
	public String actionHandler(String tranId, String xtraParams, String forcedFlag) throws RemoteException,ITMException
	{

		String  retString = null;
		try
		{
			retString = actionApprove(tranId, xtraParams, forcedFlag);
		}
		catch(Exception e)
		{
			System.out.println("Exception :EmpLoanSanctionAprvEJB :actionHandler :" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
		System.out.println("returning String from EmpLoanSanctionAprvEJB :actionHandler :"+retString);
		return retString;
	}

	private String actionApprove(String tranId, String xtraParams, String forcedFlag) throws RemoteException,ITMException
	{
		String updSql = "", sql = "", empCode = "", errCode = "", errString = "", empCodeApprove = "", schemeNo = "", status = "";
		Connection conn = null;
		Statement stmt = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int updCnt = 0, count = 0;
		java.sql.Date currDate = null;
		ConnDriver connDriver = new ConnDriver();
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		GenericUtility genericUtility = GenericUtility.getInstance();
		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			stmt = conn.createStatement();
			System.out.println("Sanction No :: "+tranId);

			sql = "SELECT EMP_CODE,EMP_CODE__APRV,SCHEME_NO,STATUS "
		         +"FROM EMPLOAN_SANCTION WHERE SANCTION_NO = '"+tranId+"'";
			System.out.println("sql :"+sql);
			rs = stmt.executeQuery(sql);
			if (rs.next())
			{
				empCode = rs.getString(1);
				empCodeApprove = rs.getString(2);
				schemeNo = rs.getString(3);
				status = rs.getString(4);
			}
			System.out.println("empCode :"+empCode+" empCodeApprove :"+empCodeApprove+" scheme No :"+schemeNo+" status :"+status);
			if (!status.equals("R"))
			{
				errCode = "VTMCONF1";
			}
			else
			{
				sql = "SELECT COUNT(*) FROM EMPLOAN_SANCTION "
			         +"WHERE EMP_CODE = '"+empCode+"' "
					 +"AND SCHEME_NO = '"+schemeNo+"' "
					 +"AND STATUS    = 'A' AND SANCTION_BAL > 0";
				rs = stmt.executeQuery(sql);
				if (rs.next())
				{
					count = rs.getInt(1);
				}
				System.out.println("count  :"+count);
				if (count > 0)
				{
					errCode = "VTLOANSAN2";
				}
				if (errCode != null && errCode.trim().length() == 0)
				{
					updSql = "UPDATE EMPLOAN_SANCTION SET STATUS = ?, "
							+"APRV_DATE = ? "
							+"WHERE SANCTION_NO = ? ";
					pstmt = conn.prepareStatement(updSql);
					pstmt.setString(1,"A");
					pstmt.setDate(2,new java.sql.Date(System.currentTimeMillis()));
					pstmt.setString(3,tranId);
					updCnt = pstmt.executeUpdate();
					if (updCnt > 0)
					{
						errCode = "VTMCONF2";
						conn.commit();
					}
				}
			}
			if (errCode != null  && errCode.trim().length() > 0)
			{
				errString = itmDBAccessEJB.getErrorString("",errCode,"","",conn);				
			}
		}
		catch (SQLException sqx)
		{
			System.out.println("The sqlException occure EmpLoanSanctionAprvEJB in  :"+sqx);
			throw new ITMException(sqx);
		}
		catch (Exception e)
		{
			System.out.println("The Exception occure in EmpLoanSanctionAprvEJB :"+e);
			throw new ITMException(e);
		}
		finally
		{
			try
			{	
				if (conn!=null)
				{
					conn.close();
					conn = null;
				}
				if (stmt != null)
				{
					stmt.close();
					stmt = null;
				}
				if (pstmt != null)
				{
					pstmt.close();
					pstmt = null;
				}
				if (rs != null)
				{
					rs.close();
					rs = null;
				}
			}
			catch (Exception e)
			{
				System.out.println("Exception in finally: " + e);
			}
		}
		return errString;
	}
}