package ibase.webitm.ejb.adm.adv;

import java.rmi.RemoteException;


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.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3

// Created by Chetna Shahu on 22/08/2014  for holiday cancellation of employee  [START]
public class HolChangeCanc extends ActionHandlerEJB implements HolChangeCancLocal,HolChangeCancRemote
{

    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;

		ITMDBAccessLocal itmDBAccess = null;
		
		int updStatus = 0;

		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();
		
		
		
		String userID = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"LoginCode");
		String siteCode = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginSiteCode");

		try 
		{	
			System.out.println("call HolCahngeCanc [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", "VTHOLCHG02", userID);
			}
			
			System.out.println(" confirm  [" + confirm + "] ");
			closePstmtRset(pstmt, rs);

			if (retString == null || retString.trim().length() == 0)
			{
				if (confirm == null || !confirm.equalsIgnoreCase("C") ) 
				{
					sql = "UPDATE CHANGE_HOLIDAY  SET CONFIRMED = ?  WHERE EMP_CODE  = ? AND HOL_DATE_FOR = ? AND HOL_DATE_TO = ?  AND HOL_TYPE = ?";
					
					System.out.println("[EmpTranPromCanc][confirm]sqlStatus::"+ sql);
					pstmt = conn.prepareStatement(sql);

					pstmt.setString(1, "C");
					pstmt.setString(2, checkNull(empCode));
					pstmt.setTimestamp(3, holDateForSql);
					pstmt.setTimestamp(4, holDateToSql);
					pstmt.setString(5, checkNull(holType));
					

					updStatus = pstmt.executeUpdate();
					
					System.out.println("canceled the request [::" + updStatus);
					if(updStatus  > 0)
					{

						retString =  itmDBAccess.getErrorString("emp_code", "VTCANC1", userID);
						conn.commit();
					
					}
					else
					{
						retString =  itmDBAccess.getErrorString("emp_code", "VTHOLCHG02", userID);
						conn.rollback();
					}
				} 
				else 
				{
					retString =  itmDBAccess.getErrorString("emp_code", "VTCANC2", 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 [HolCahngeCanc][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 cancellation of employee
// [END]
