/*
Window Name : w_sales_comm
Button name : Default
Action		: Default Action
*/
package ibase.webitm.ejb.fin.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.webitm.utility.GenericUtility;
import ibase.system.config.*;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3

public class SalesCommAct extends ActionHandlerEJB implements SalesCommActLocal,SalesCommActRemote //SessionBean
{
	/*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  resString = null;
		try
		{
			if (actionType.equalsIgnoreCase("Default"))
			{
				if(xmlString != null && xmlString.trim().length()!=0)
				{
					GenericUtility genericUtility = GenericUtility.getInstance(); 
					System.out.println("XML String :"+xmlString);
					dom = genericUtility.parseString(xmlString); 
				}
				resString = actionDefault(dom,objContext,xtraParams);
			}
		}
	   	catch(Exception e)
		{
			System.out.println("Exception :SalesComm :actionHandler(String xmlString):" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
		System.out.println("returning from SalesComm actionHandler"+resString);
	    return (resString);
	}

	private String actionDefault(Document dom, String objContext, String xtraParams) throws RemoteException , ITMException
	{
		String itemSer = "";
		String tranId = "";
		String tableNoHire = "";
		String errCode = "";
		String errString = "";
		StringBuffer valueXmlString = new StringBuffer("<?xml version=\"1.0\"?>\r\n<Root>\r\n");
		String sql = "";
		String detCnt = "0";
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		ConnDriver connDriver = new ConnDriver();
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		GenericUtility genericUtility = GenericUtility.getInstance();
		try
		{
			//detCnt = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"detCnt"); //Will be Uncommented later - Jiten
			if (detCnt.equals("0"))
			{
				conn = connDriver.getConnectDB("DriverITM");
				itemSer = genericUtility.getColumnValue("item_ser",dom);
				tranId = genericUtility.getColumnValue("tran_id",dom);
				sql = "SELECT TABLE_NO__HIER FROM ITEMSER WHERE ITEM_SER = '"+ itemSer +"'";
				System.out.println("sql ::"+sql);
				stmt = conn.createStatement();
				rs = stmt.executeQuery(sql);
				if (!rs.next())
				{
					errCode = "VTITEMSER1";
				}
				else
				{
					tableNoHire = rs.getString(1);
					sql = "SELECT DISTINCT SALES_PERS FROM HIERARCHY_DET WHERE TABLE_NO ='"+ tableNoHire +"'"; 
					System.out.println("SQL ::"+sql);
					rs = stmt.executeQuery(sql);
					while (rs.next())
					{
						valueXmlString.append("<Detail>\r\n");
						valueXmlString.append("<sales_pers>").append("<![CDATA[").append(rs.getString(1)).append("]]>").append("</sales_pers>\r\n");
						valueXmlString.append("<amtount>").append("<![CDATA[").append("0.00").append("]]>").append("</amtount>\r\n");
						valueXmlString.append("</Detail>\r\n");
					}
					stmt.close();
					valueXmlString.append("</Root>\r\n");			
				}
			}
			else
			{
				errCode = "VTDETCNT";
			}			
		}
		catch(Exception e)
		{
			System.out.println("Exception : SalesComm : actionHandler :(Document dom)" +e.getMessage());
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				System.out.println("Closing Connection.....");
				conn.close();
				conn = null;
			}catch(Exception e){}
		}
		if (errCode != null  && errCode.trim().length() > 0)
		{
			errString = itmDBAccessEJB.getErrorString("", errCode, "", "", conn);			
			return errString;
		}
		else
		{
			System.out.println("valueXmlString.toString() :"+valueXmlString.toString());
			return valueXmlString.toString();
		}
	}
}