package ibase.webitm.ejb.dis.adv;

import java.rmi.RemoteException;
import java.sql.*;
import org.w3c.dom.*;

import javax.ejb.*;

import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.*;
import ibase.utility.E12GenericUtility;
import ibase.system.config.*;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3
public class SorderAct extends ActionHandlerEJB implements SorderActLocal, SorderActRemote
{
	/*public void ejbCreate() throws RemoteException, CreateException 
	{
	}

	public void ejbRemove()
	{
	}

	public void ejbActivate() 
	{
	}

	public void ejbPassivate() 
	{
	}*/

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

	public String actionHandler(String actionType, String xmlString, String objContext, String xtraParams) throws RemoteException,ITMException
	{
		Document dom = null;
		String  retString = null;
		ibase.utility.E12GenericUtility genericUtility= new  ibase.utility.E12GenericUtility();
		try
		{
			if (actionType.equalsIgnoreCase("Default"))
			{
				System.out.println("XML String :"+xmlString);
				dom = genericUtility.parseString(xmlString); 
			}
			retString = actionDefault(dom, xtraParams);
		}
	   	catch(Exception e)
		{
			System.out.println("Exception :Sorder :actionHandler(String xmlString):" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
		System.out.println("returning from actionDefault actionHandler"+retString);
	    return (retString);
	}

	private String actionDefault(Document dom, String xtraParams) throws RemoteException , ITMException
	{
		String creditTerm = "";
		String errCode = "";
		String errString = "";
		String sql = "";
		ResultSet rs = null;
		Connection conn = null;
		Statement stmt = null;
		String detCnt = "0";
		StringBuffer valueXmlString = new StringBuffer("<?xml version=\"1.0\"?>\r\n<Root>\r\n");
		ibase.utility.E12GenericUtility genericUtility= new  ibase.utility.E12GenericUtility();
		ConnDriver connDriver = new ConnDriver();
		ITMDBAccessEJB itmDBAccess = new ITMDBAccessEJB();
		try
		{
			detCnt = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"detCnt"); //Will be Uncommented later - Jiten
			if (detCnt == null || detCnt.trim().length() == 0)
			{
				detCnt = "0";
			}
			if(detCnt.equals("0"))
			{
				conn = connDriver.getConnectDB("DriverITM");
				creditTerm = genericUtility.getColumnValue("cr_term",dom);	
				System.out.println("creditTerm :"+creditTerm);
				sql="SELECT "
						+"CRTERM_REL.REL_AGNST, "   
						+"CRTERM_REL.AMT_TYPE, "   
						+"CRTERM_REL.REL_AMT, "   
						+"CRTERM_REL.REL_AFTER, "   
						+"CRTERM_REL.ADJ_METHOD, "   
						+"CRTERM_REL.ACCT_CODE, "   
						+"CRTERM_REL.CCTR_CODE, "   
						+"CRTERM_REL.PROC_CODE, "   
						+"CRTERM_REL.NO_RELEASE, "   
						+"CRTERM_REL.LAST_RELEASE "   
					+"FROM CRTERM_REL "  
					+"WHERE CRTERM_REL.CR_TERM = '"+ creditTerm +"'";
				System.out.println("Sorder SQL :="+sql);
				stmt = conn.createStatement();
				rs = stmt.executeQuery(sql);
				while (rs.next())
				{
					valueXmlString.append("<Detail>\r\n");
					valueXmlString.append("<rel_agnst>").append("<![CDATA[").append(rs.getString(1)).append("]]>").append("</rel_agnst>\r\n");
					valueXmlString.append("<amt_type>").append("<![CDATA[").append(rs.getString(2)).append("]]>").append("</amt_type>\r\n");
					valueXmlString.append("<rel_amt>").append("<![CDATA[").append(rs.getString(3)).append("]]>").append("</rel_amt>\r\n");
					valueXmlString.append("<rel_after>").append("<![CDATA[").append(rs.getString(4)).append("]]>").append("</rel_after>\r\n");
					valueXmlString.append("<adj_method>").append("<![CDATA[").append(rs.getString(5)).append("]]>").append("</adj_method>\r\n");
					valueXmlString.append("<acct_code>").append("<![CDATA[").append(rs.getString(6)).append("]]>").append("</acct_code>\r\n");
					valueXmlString.append("<cctr_code>").append("<![CDATA[").append(rs.getString(7)).append("]]>").append("</cctr_code>\r\n");
					valueXmlString.append("<proc_code>").append("<![CDATA[").append(rs.getString(8)).append("]]>").append("</proc_code>\r\n");
					valueXmlString.append("<no_release>").append("<![CDATA[").append(rs.getString(9)).append("]]>").append("</no_release>\r\n");
					valueXmlString.append("<last_release>").append("<![CDATA[").append(rs.getString(10)).append("]]>").append("</last_release>\r\n");
					valueXmlString.append("</Detail>\r\n");
				}
				stmt.close();
				valueXmlString.append("</Root>\r\n");			
			}
			else
			{
				errCode = "VTDETCNT";
			}
			if (!errCode.equals(""))
			{
				errString = itmDBAccess.getErrorString("",errCode,"","",conn);
				return errString;
			}
		}
		catch(SQLException e)
		{
			System.out.println("Exception : Sorder : actionVoucher " +e.getMessage());
			throw new ITMException(e);
		}
		catch(Exception e)
		{
			System.out.println("Exception : Sorder : actionHandler " +e.getMessage());
			throw new ITMException(e);
		}
		finally
		{
			try{
			conn.close();
			}catch(Exception e){}
		}
		System.out.println("valueXmlString.toString() "+valueXmlString.toString());
		return valueXmlString.toString();		
	}
}