package ibase.webitm.ejb.adm.adv;

import java.rmi.RemoteException;

import ibase.system.config.ConnDriver;
import ibase.utility.EMail;
import ibase.webitm.ejb.ITMDBAccessEJB;
import ibase.webitm.ejb.ITMDBAccessLocal;
import ibase.webitm.ejb.ValidatorEJB;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;
import java.io.PrintStream;
import java.net.URL;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.StringTokenizer;
import java.rmi.RemoteException;
import javax.naming.InitialContext;
import java.sql.*;
import java.text.*;
import ibase.system.config.*;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.*;

import javax.naming.InitialContext;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3

// Created by Chetna Shahu on 22/08/2014  for holiday Confirmation of employee  [START]
public class HolChangeConf extends ActionHandlerEJB implements HolChangeConfLocal, HolChangeConfRemote
{

    public String actionHandler() throws RemoteException,ITMException
	{
		return "";
	}

	public String actionHandler(String tranId, String xtraParams, String forcedFlag) throws RemoteException,ITMException
	{
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;

		int updStatus = 0;

		ITMDBAccessLocal itmDBAccess = null;
		
		String sql = "";
		String empCode = "";
		String holDateFor = "";
		String holDateTo = "";
		String holType = "";
		String holTable = "";
		String confirm = "";
		String retString = "";

		java.sql.Timestamp holDateForSql = null;
		java.sql.Timestamp holDateToSql = null;

		GenericUtility genericUtility = GenericUtility.getInstance();
		//ITMDBAccessLocal itmDBAccessLocal = (ITMDBAccessLocal) new ITMDBAccessEJB();

		String userID = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"LoginCode");
		String siteCode = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginSiteCode");

		try 
		{	
			System.out.println("call HolChangeConf [actionHandler] method .....for tran id"+ tranId);
			System.out.println(" xtraParams [" + xtraParams + "] ");
			System.out.println(" forcedFlag [" + forcedFlag + "] ");

			AppConnectParm appConnect = new AppConnectParm();
			InitialContext ctx = new InitialContext(appConnect.getProperty());
			itmDBAccess = (ITMDBAccessLocal)ctx.lookup("ibase/ITMDBAccessEJB/local");			
			System.out.println(" I N S I D E C A S E  ");
			
			System.out.println("tranId [" + tranId + "]");

			String tranIDArray[] = tranId.split("\\:");

			empCode = tranIDArray[0];
			holDateFor = tranIDArray[1];
			holDateTo = tranIDArray[2];
			holType = tranIDArray[3];

			System.out.println("FROM DATE STRING BEFORE DBDATE [" + holDateFor + "]");
			
			holDateForSql = java.sql.Timestamp.valueOf(genericUtility.getValidDateTimeString(holDateFor,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat()));

			System.out.println("FROM DATE STRING AFTER DBDATE [" + holDateForSql + "]");

			
			System.out.println("TO DATE STRING BEFORE DBDATE [" + holDateTo + "]");

			holDateToSql = java.sql.Timestamp.valueOf(genericUtility.getValidDateTimeString(holDateTo,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat()));

			System.out.println("TO DATE STRING AFTER DBDATE [" + holDateToSql + "]");			
			
			
			ConnDriver connDriver = new ConnDriver();
			conn = connDriver.getConnectDB("DriverITM");
			conn.setAutoCommit(false);

			System.out.println("emp Code [" + empCode + "] hol date for [" + holDateForSql + "]  hol date to [" + holDateToSql + "] hol type" +holType );


			sql = "SELECT CONFIRMED FROM CHANGE_HOLIDAY  WHERE EMP_CODE  = ? AND HOL_DATE_FOR = ? AND HOL_DATE_TO = ?  AND HOL_TYPE = ?";

			pstmt = conn.prepareStatement(sql);

			System.out.println("[HolCahngeCanc][confirm]sqlStatus::" + sql);
			pstmt.setString(1, checkNull(empCode));
			pstmt.setTimestamp(2, holDateForSql);
			pstmt.setTimestamp(3, holDateToSql);
			pstmt.setString(4, checkNull(holType));
			

			rs = pstmt.executeQuery();

			if (rs.next())
			{
				confirm = rs.getString("CONFIRMED");
			}
			else
			{
				retString =  itmDBAccess.getErrorString("emp_code", "VTHOLCHG03", userID);
			}

			System.out.println(" confirm  [" + confirm + "] ");

			closePstmtRset(pstmt, rs);

			if (retString == null || retString.trim().length() == 0)
			{
				if (confirm == null || confirm.equalsIgnoreCase("N") ) 
				{
					sql = "UPDATE CHANGE_HOLIDAY  SET CONFIRMED = ?  WHERE EMP_CODE  = ? AND HOL_DATE_FOR = ? AND HOL_DATE_TO = ?  AND HOL_TYPE = ?";
					pstmt = conn.prepareStatement(sql);
					System.out.println("[EmpTranPromCanc][confirm]sqlStatus::"+ sql);
					
					pstmt.setString(1, "Y");
					pstmt.setString(2, checkNull(empCode));
					pstmt.setTimestamp(3, holDateForSql);
					pstmt.setTimestamp(4, holDateToSql);
					pstmt.setString(5, checkNull(holType));
					
					updStatus = pstmt.executeUpdate();
					System.out.println("confirm the request [::" + updStatus);
					
					if(updStatus > 0)
					{
						//retString =  itmDBAccess.getErrorString("emp_code", "VTSUCC", userID);
						retString =  itmDBAccess.getErrorString("emp_code", "VTHOLCHG04", userID);
						conn.commit();
					}
					else
					{
						retString =  itmDBAccess.getErrorString("emp_code", "VTHOLCHG03", userID);
						conn.rollback();
					}
						
				} 
				else if(confirm.equalsIgnoreCase("C"))
				{
					retString = itmDBAccess.getErrorString("emp_code", "VTHOLCHG05", userID);
				} 
				else if(confirm.equalsIgnoreCase("Y"))
				{
					retString = itmDBAccess.getErrorString("emp_code", "VTCONF1", userID);
				} 
			}
		}
		catch (Exception e)
		{
			try
			{
				conn.rollback();
				System.out.println("Exception " + e.getMessage());
				e.printStackTrace();
				throw new ITMException(e);
			}
			catch (Exception e1) 
			{
				e1.printStackTrace();
				throw new ITMException(e1);
			}
		} 
		finally 
		{
			try {
				
					if (rs != null)
					{
						rs.close();
						rs = null;
					}
					if (pstmt != null) 
					{
						pstmt.close();
						pstmt = null;
					}
					if (conn != null)
					{
						conn.close();
						conn = null;
					}

			} 
			catch (Exception e)
			{
				e.printStackTrace();
				throw new ITMException(e);
			}
		}
		System.out.println(" retString..  [" + retString + "] ");
		return retString;

	}

	private void closePstmtRset(PreparedStatement pstmt, ResultSet rset)throws Exception {
		System.out.println("Closing PreparedStatement and result.........");
		try 
		{
			if (rset != null)
			{
				rset.close();
				rset = null;
			}
			if (pstmt != null)
			{
				pstmt.close();
				pstmt = null;
			}
		}
		catch (Exception exp) 
		{
			System.out.println("Inside Exception [HolCahngeConf][closePstmtRset]::"+ exp.getMessage());
			exp.printStackTrace();
			throw new Exception(exp);
		}
	}
	private String checkNull(String input) {
		if (input == null) {
			input = "";
		} else {
			input = input.trim();
		}
		return input;
	}

}
// Created by Chetna Shahu on 22/08/2014 for holiday Confirmation of employee
// [END]
