package ibase.webitm.ejb.sys;

import java.rmi.RemoteException;
import java.util.*;
import java.lang.String;
import java.sql.*;
import org.w3c.dom.*;
import javax.ejb.*;

import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.*;
import ibase.webitm.utility.GenericUtility;
import ibase.system.config.*;
import ibase.utility.CommonConstants;
import ibase.webitm.ejb.sys.NFCreateEdiMultiLogic;
import javax.ejb.Stateless;// added for ejb3

@Stateless // added for ejb3
public class GenerateEDI extends ActionHandlerEJB implements GenerateEDILocal , GenerateEDIRemote //SessionBean //commented for ejb3
{
	ibase.webitm.utility.GenericUtility genericUtility = ibase.webitm.utility.GenericUtility.getInstance();
	
	/*public void ejbCreate() throws RemoteException, CreateException 
	{
	}

	public void ejbRemove()
	{
	}

	public void ejbActivate() 
	{
	}

	public void ejbPassivate() 
	{
	}*/

	public String genEDI() throws RemoteException,ITMException
	{
		return "";
	}
	
	public String genEDI( String winName, String editFlag, String xmlString, String xtraParams,  Connection conn ) throws RemoteException,ITMException
	{
		System.out.println("Generate Edi called");
		Document dom = null;
		String  retString = null;
		//Connection conn = null;
		try
		{
			if( xmlString != null && xmlString.trim().length() > 0 )
			{
				System.out.println("XML String :" + xmlString );
				dom = genericUtility.parseString( xmlString ); 
			}
			
			//ConnDriver connDriver = new ConnDriver();
			//conn = connDriver.getConnectDB("DriverITM");
			//connDriver = null;
			
			NFCreateEdiMultiLogic createEdi = new NFCreateEdiMultiLogic();
			
			retString = createEdi.nfCreateEdiMultiLogic( dom, winName, xtraParams, conn );	
		}
	   	catch(Exception e)
		{
			System.out.println("Exception :GenerateEDIEJB :" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				/*
				if( retString != null || retString.trim().length() > 0 )
				{
					conn.rollback();
				}
				else
				{
					conn.commit();
				}
				
				if( conn != null )
				{
					conn.close();
				}
				conn = null;
				*/
			}catch( Exception ex )
			{
				ex.printStackTrace();
			}
		}
		
	    return retString;	
	}
	//generate edi for transaction id provided
	public String genEDI( String winName, String tranId, String xtraParams, Connection conn ) throws RemoteException,ITMException
	{
		Document dom = null;
		String xmlString = null;
		String  retString = null;
		try
		{
			CreateRCPXML tranXmlObj = new CreateRCPXML( winName, "" );

			xmlString = tranXmlObj.getTranXML( tranId, conn );
			
			//System.out.println( "EDI String :: " + xmlString );
			if( xmlString != null && xmlString.trim().length() > 0 )
			{
				dom = genericUtility.parseString( xmlString ); 
			}
						
			NFCreateEdiMultiLogic createEdi = new NFCreateEdiMultiLogic();
			
			retString = createEdi.nfCreateEdiMultiLogic( dom, winName, xtraParams, conn );	
		}
	   	catch(Exception e)
		{
			e.printStackTrace();
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				/*
				if( retString != null || retString.trim().length() > 0 )
				{
					conn.rollback();
				}
				else
				{
					conn.commit();
				}
				
				if( conn != null )
				{
					conn.close();
				}
				conn = null;
				*/
			}catch( Exception ex )
			{
				ex.printStackTrace();
			}
		}
		
	    return retString;	
	}
	//
}