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 EmpAddChgCnc extends ActionHandlerEJB implements EmpAddChgCncLocal,EmpAddChgCncRemote //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;
		GenericUtility genericUtility = GenericUtility.getInstance();
		try
		{
			retString = actionCancel(tranId, xtraParams, forcedFlag);
		}
		catch(Exception e)
		{
			System.out.println("Exception :EmpAddChgCncEJB :actionHandler :" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
		System.out.println("returning String from EmpAddChgCncEJB :actionHandler :"+retString);
		return retString;
	}

	private String actionCancel(String tranId, String xtraParams, String forcedFlag) throws RemoteException,ITMException
	{
		String sql = "", empCodeAprove = "", loginEmpCode = "", errCode = "", errString = "", confirmed = "", updSql = "";
		Connection conn = null;
		Statement stmt = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int updCnt = 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("Tran ID : "+ tranId);
			sql = "SELECT CONFIRMED FROM EMPLOYEE_ADD_CHANGE WHERE TRAN_ID = '"+tranId+"' ";
			System.out.println("sql :"+sql);
			rs = stmt.executeQuery(sql);
			if (rs.next())
			{
				confirmed = rs.getString(1);
			}
			System.out.println();
			loginEmpCode = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginCode");
			System.out.println("confirmed :"+confirmed+"loginEmpCode :"+loginEmpCode);
			if (confirmed != null && confirmed.equals("N"))
			{
				empCodeAprove = loginEmpCode;
				updSql = "UPDATE EMPLOYEE_ADD_CHANGE SET CONFIRMED = ?, "
						+"CONF_DATE = ? , "
						+"EMP_CODE__APRV = ? "
						+"WHERE TRAN_ID = ?";
				pstmt = conn.prepareStatement(updSql);
				pstmt.setString(1,"X");
				pstmt.setDate(2,new java.sql.Date(System.currentTimeMillis()));
				pstmt.setString(3,empCodeAprove);
				pstmt.setString(4,tranId);
				updCnt = pstmt.executeUpdate();
				if (updCnt > 0)
				{
					errCode = "VTSUCC";
				}
			}
			else if(confirmed != null && confirmed.equals("Y"))
			{
				errCode = "VTMCONF1";
			}
			else if(confirmed != null && confirmed.equals("X"))
			{
				errCode = "VTMCANL1";
			}
			if (errCode != null  && errCode.trim().length() > 0)
			{
				System.out.println("EmpAddChgCnc:errCode:"+errCode);
				errString = itmDBAccessEJB.getErrorString("",errCode,"","",conn);				
			}
		}
		catch (SQLException sqx)
		{
			System.out.println("The sqlException occure in EmpAddChgCncEJB :"+sqx);
			throw new ITMException(sqx);
		}
		catch (Exception e)
		{
			System.out.println("The Exception occure in EmpAddChgCncEJB :"+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;
	}
}