package ibase.webitm.ejb.sys;

import ibase.system.config.ConnDriver;
//import ibase.utility.CommonConstants;
import ibase.utility.GenericUtility;
import ibase.webitm.ejb.ActionHandlerEJB;
import ibase.webitm.ejb.ITMDBAccessEJB;
import ibase.webitm.utility.ITMException;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
//import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.ejb.Stateless;

@Stateless
public class UpdateLoadStatus extends ActionHandlerEJB implements UpdateLoadStatusLocal,UpdateLoadStatusRemote 
{
	
	@Override
	public String actionHandler(String tranId, String xtraParams, String forcedFlag) throws RemoteException, ITMException 
	{
		//Connection  connectionObject = null;
		String errString = "";
		try
		{
			errString =  UpdateLoadStat(tranId,xtraParams,forcedFlag);
		}
		catch(Exception e)
		{
			System.out.println("Exception ::"+e.getMessage());
			throw new ITMException(e);
		}
		System.out.println("Returning Result ::"+errString);
		return errString;
	}
	
	public String UpdateLoadStat(String tranID ,String xtraParams, String forcedFlag) throws ITMException
	{
		String sql="",errString="",loadStat="",userId="",tmpTranID="";
		PreparedStatement pstmt =null  ;
		ResultSet rs=null;
		int cnt=0;
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		java.util.Date dt=new Date();
		Connection conn=null;
		String[] temp=null;
		
		try{
			ConnDriver connDriver = new ConnDriver();
			conn = connDriver.getConnectDB("DriverITM");
			GenericUtility genericUtility = GenericUtility.getInstance();
			userId = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginCode");
			SimpleDateFormat sdf1= new SimpleDateFormat(genericUtility.getDBDateFormat());	
			java.sql.Timestamp chgDate =java.sql.Timestamp.valueOf(sdf1.format(dt).toString() + " 00:00:00.0");
			System.out.println("@@@@@@ chgDate["+chgDate+"]");
			
			if ( tranID!= null && tranID.trim().length() > 0  )
			{
				
				temp = tranID.split(":");
				tmpTranID = temp[0];
				System.out.println("@@@@@@ tranID["+tmpTranID+"]");
				
			sql = " SELECT LOAD_STAT FROM BATCHLOAD WHERE BATCH_ID = ?  ";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,tmpTranID);
			rs = pstmt.executeQuery();
				if(rs.next())
				{
					loadStat = rs.getString("LOAD_STAT");
				}
			rs.close();
			rs =null;
			pstmt.close();
			pstmt =null;
			System.out.println("@@@@@@ loadStat["+loadStat+"]");
			
			if( "Y".equalsIgnoreCase(loadStat) )
			{
				errString = itmDBAccessEJB.getErrorString("","VMLOADSTAY",userId);
				return errString;
			}
			
			if( "N".equalsIgnoreCase(loadStat) )
			{
				errString = itmDBAccessEJB.getErrorString("","VMLOADSTAN",userId);
				return errString;
			}
			
				
			sql = " SELECT COUNT(1) FROM BATCHLOAD WHERE BATCH_ID = ? AND LOAD_STAT = 'E' ";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,tmpTranID);
			rs = pstmt.executeQuery();
			if(rs.next())
				{
					cnt = rs.getInt(1);
				}
			rs.close();
			rs =null;
			pstmt.close();
			pstmt =null;
			System.out.println("@@@@@ cnt:["+cnt+"]");
			
			if( cnt > 0 )
			{
				sql = " update BATCHLOAD  set LOAD_STAT = 'N', chg_date = ?  where  BATCH_ID = ? ";
				System.out.println("sql :"+sql);
				pstmt = conn.prepareStatement(sql);
				pstmt.setTimestamp(1,chgDate);
				pstmt.setString(2,tmpTranID);
				cnt = pstmt.executeUpdate();
				pstmt.close();
				pstmt = null;
				System.out.println("@@@@@@ BATCHLOAD updated sucessfully. for tranID ["+tmpTranID+"]......["+cnt+"]");
				
				if( cnt > 0 )
				{
					conn.commit();  
					System.out.println("@@@@ Transaction commit... ");
					
					errString = itmDBAccessEJB.getErrorString("","VMLOADSTAU",userId);
					return errString;
				}
			}
			
			}
			}
		catch(Exception e)
		{
			e.printStackTrace();
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				if (pstmt != null )
				{
					pstmt.close();
					pstmt = null;
				}
				if( rs != null )
				{
					rs.close();
					rs =null;
				}
				if( conn != null ){
					conn.close();
					conn = null;
				}
			}
			catch(Exception e)
			{
				
			}
		}
		System.out.println("@@@@ return errString ... ["+errString+"]");
		return errString;
	}
	
	
}
