package ibase.webitm.ejb.sys;

import ibase.system.config.ConnDriver;
import ibase.webitm.ejb.ActionHandlerEJB;
import ibase.webitm.ejb.fin.GlobalFinanceConfLocal;
import ibase.webitm.ejb.fin.GlobalFinanceConfRemote;
import ibase.webitm.utility.ITMException;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.ejb.Stateless;
import javax.xml.rpc.ParameterMode;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

@Stateless
public class CallConfirmService extends ActionHandlerEJB implements CallConfirmServiceLocal,CallConfirmServiceRemote {

	@Override
	public String confirm(String objName, String tranID, String xtraParams,	String forcedFlag) throws RemoteException, ITMException 
	{
		Connection  connectionObject = null;
		String retString = "";
		try
		{
			retString =  confirmRecord(objName,tranID,connectionObject,xtraParams,forcedFlag);
		}
		catch(Exception e)
		{
			System.out.println("Exception ::"+e.getMessage());
			throw new ITMException(e);
		}
		System.out.println("Returning Result ::"+retString);
		return retString;
	}
	
	public String confirmRecord(String businessObj,String tranID ,Connection conn, String xtraParams, String forcedFlag) throws ITMException
	{
		String methodName = "gbf_post";
		String actionURI="",sql="",serviceCode="",compName="",serviceURI="",retString="";
		PreparedStatement pStmtSysEvent =null ,pStmtSysEvService=null ;
		ResultSet rsSysEvent,rsSysEvService;
		try{
			ConnDriver connDriver = new ConnDriver();
			conn = connDriver.getConnectDB("DriverITM");

			actionURI = "http://NvoServiceurl.org/" + methodName;
			
			sql = "SELECT SERVICE_CODE,COMP_NAME FROM SYSTEM_EVENTS WHERE OBJ_NAME = ? AND EVENT_CODE = 'pre_confirm' ";
			pStmtSysEvent = conn.prepareStatement(sql);
			pStmtSysEvent.setString(1,businessObj);
			rsSysEvent = pStmtSysEvent.executeQuery();
			if ( rsSysEvent.next() )
			{
				serviceCode = rsSysEvent.getString("SERVICE_CODE");
				compName = rsSysEvent.getString("COMP_NAME");
			}
			
			pStmtSysEvent.close();
			pStmtSysEvent = null;
			rsSysEvent.close();
			rsSysEvent = null;
			
			System.out.println("===========serviceCode========= = "+serviceCode+" compName "+compName);
			
			sql = "SELECT SERVICE_URI,METHOD_NAME FROM SYSTEM_EVENT_SERVICES WHERE SERVICE_CODE = ? ";
			pStmtSysEvService = conn.prepareStatement(sql);
			pStmtSysEvService.setString(1,serviceCode);
			rsSysEvService = pStmtSysEvService.executeQuery();
			if ( rsSysEvService.next() )
			{
				serviceURI = rsSysEvService.getString("SERVICE_URI");
				methodName=rsSysEvService.getString("METHOD_NAME");
			}
			
			pStmtSysEvService.close();
			pStmtSysEvService = null;
			rsSysEvService.close();
			rsSysEvService = null;
			
			
			System.out.println("==============serviceURI=========== = "+serviceURI+" compName = "+compName);
			
			Service service = new Service();
			Call call = (Call)service.createCall();
			call.setTargetEndpointAddress(new java.net.URL(serviceURI));
			call.setOperationName( new javax.xml.namespace.QName("http://NvoServiceurl.org", methodName ) );
			call.setUseSOAPAction(true);
			call.setSOAPActionURI(actionURI);
			
			Object[] aobj = new Object[4];

			call.addParameter( new javax.xml.namespace.QName("http://NvoServiceurl.org", "component_name"), XMLType.XSD_STRING, ParameterMode.IN);
			call.addParameter( new javax.xml.namespace.QName("http://NvoServiceurl.org", "tran_id"), XMLType.XSD_STRING, ParameterMode.IN);
			call.addParameter( new javax.xml.namespace.QName("http://NvoServiceurl.org", "xtra_params"), XMLType.XSD_STRING, ParameterMode.IN);
			call.addParameter( new javax.xml.namespace.QName("http://NvoServiceurl.org", "forced_flag"), XMLType.XSD_STRING, ParameterMode.IN);

			aobj[0] = new String(compName);
			aobj[1] = new String(tranID);
			aobj[2] = new String(xtraParams);
			aobj[3] = new String(forcedFlag);

			call.setReturnType(XMLType.XSD_STRING);
			retString = (String)call.invoke(aobj);
			System.out.println("Return string from NVO is:==>["+retString+"]");

		}
		catch(Exception e)
		{
			e.printStackTrace();
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				if (pStmtSysEvent != null )
				{
					pStmtSysEvent.close();
					pStmtSysEvent = null;
				}
				if (pStmtSysEvService != null )
				{
					pStmtSysEvService.close();
					pStmtSysEvService = null;
				}
				if( conn != null ){
					conn.close();
					conn = null;
				}
			}
			catch(Exception e)
			{}
		}
		
		return retString;
	}
	
}
