package ibase.webitm.ejb.dis;

import java.rmi.RemoteException;
import java.util.*;
import java.sql.*;
import java.text.*;
import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.ejb.*;
import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.*;
import javax.naming.InitialContext;
import ibase.webitm.utility.GenericUtility;
import ibase.ejb.*;
import ibase.system.config.*;
import javax.ejb.Stateless; // added for ejb3


//public class TaxExciseEJB extends ValidatorEJB implements SessionBean
@Stateless // added for ejb3
public class TaxExcise extends ValidatorEJB implements TaxExciseLocal, TaxExciseRemote
{
	
    /*public void ejbCreate() throws RemoteException,CreateException
	{
		System.out.println("Taxexcise Created........");
	}

    public void ejbRemove() 
	{
	}

	public void ejbActivate() 
	{
	}

	public void ejbPassivate() 
	{
	}*/

//code for wf_val_data

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

   public String wfValData(String xmlString, String xmlString1, String xmlString2, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		Document dom = null;
		Document dom2 = null;
		Document dom1 = null;
		String  errString = null;
		GenericUtility genericUtility = GenericUtility.getInstance();
		try
		{
			if(xmlString != null && xmlString.trim().length()!=0)
			{
				dom = genericUtility.parseString(xmlString); 
			}
			if(xmlString1 != null && xmlString1.trim().length()!=0)
			{
				dom1 = genericUtility.parseString(xmlString1); 
			}
			if(xmlString2 != null && xmlString2.trim().length()!=0)
			{
				dom2 = genericUtility.parseString(xmlString2); 
			}
			errString = wfValData(dom,dom1,dom2,objContext,editFlag,xtraParams);
			System.out.println("ErrString :" + errString);
		}
			catch(Exception e)
		{
			System.out.println("Exception :TaxexciseEJB :wfValData(String xmlString):" + e.getMessage() + ":");
			errString = genericUtility.createErrorString(e);
		}
		System.out.println("returning from TaxexciseEJB wfvaldata");
		return (errString); 
	}
	
	public String wfValData(Document dom, Document dom1, Document dom2, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		String columnName = "";
		String columnValue = "";
		String columnValue1 = "";
		String userId = "";
		String actionCode = "";
		String sql="";
		String sql2 = "";
		StringBuffer sql1 = new StringBuffer();
		boolean checkNextColumn = true;
		String errString = "";
		int currentFormNo = 0;
		Connection connectionObject = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		
		String editOption = "";
		NodeList parentList = null;
		NodeList childList = null;
		Node parentNode = null;
		Node childNode = null;
		String childNodeName = null;
		int ctr = 0;
		GenericUtility genericUtility = GenericUtility.getInstance();
		//ITMDBAccessHome itmDBAccessHome = null;
		ITMDBAccessLocal itmDBAccess = null; // for ejb3
		try
		{
			AppConnectParm appConnect = new AppConnectParm();
			Properties p = appConnect.getProperty();
			InitialContext ctx = new InitialContext(p);
			itmDBAccess = (ITMDBAccessLocal)ctx.lookup("ibase/ITMDBAccessEJB/local"); // for ejb3
			//itmDBAccess = itmDBAccessHome.create();
			connectionObject = itmDBAccess.getConnection();
			userId = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginCode");
			stmt = connectionObject.createStatement();
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			parentList = dom.getElementsByTagName("Detail" + currentFormNo);
			parentNode = parentList.item(0);
			childList = parentNode.getChildNodes();
			for(ctr = 0; ctr < childList.getLength(); ctr++)
			{
				childNode = childList.item(ctr);
				childNodeName = childNode.getNodeName();
				switch(currentFormNo)
				{
					case 1:
				   	break;
					
					case 2:
						if(childNodeName.equals("ref_ser"))
						{
							if(childNode.getFirstChild() == null)
							{
								System.out.println("ERROR");
								errString = itmDBAccess.getErrorString("ref_ser","VMREFNUL",userId,errString,connectionObject); 
							}
						}
						else if (childNodeName.equals("drcr_flag"))
						{
							if(childNode.getFirstChild() == null)
							{
								System.out.println("ERROR");
								errString = itmDBAccess.getErrorString("drcr_flag","VMDRCRNUL",userId,errString,connectionObject); 
							}
						}
					break;	
				}
				if (errString != null && errString.trim().length()>0)
				{
					break;
				}

			}
		}		
		catch(Exception e)
		{
			System.out.println("Exception :TaxexciseEJB :wfValData(Document dom):" + e.getMessage() + ":");
			errString = genericUtility.createErrorString(e);
		}
		return (errString);
	}
//code for item_changed

	public String itemChanged() throws RemoteException,ITMException
	{
		return "";
	}
	
	public String itemChanged(String xmlString, String xmlString1, String xmlString2, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		Document dom1 = null;
		Document dom = null;
		Document dom2 = null;
		String valueXmlString = "";
		GenericUtility genericUtility = GenericUtility.getInstance();
		try
		{
			if(xmlString != null && xmlString.trim().length()!=0)
			{
				dom = genericUtility.parseString(xmlString); 
			}
			if(xmlString1 != null && xmlString1.trim().length()!=0)
			{
				dom1 = genericUtility.parseString(xmlString1); 
			}
			if(xmlString2 != null && xmlString2.trim().length()!=0)
			{
				dom2 = genericUtility.parseString(xmlString2); 
			}
			valueXmlString = itemChanged(dom,dom1,dom2,objContext,currentColumn,editFlag,xtraParams);
			System.out.println(valueXmlString);
		}
		catch(Exception e)
		{
			System.out.println("Exception :TaxexciseEJB :itemChanged(String,String):" + e.getMessage() + ":");
			valueXmlString = genericUtility.createErrorString(e);
		}
		System.out.println("returning from TaxexciseEJB itemChanged");
		return (valueXmlString); 
	}

	public String itemChanged(Document dom, Document dom1, Document dom2, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		ResultSet rs = null;
		String sql = "";
		String columnValue = "";
		String date = "";
		String userId = "";
		SimpleDateFormat formater = null;
		String columnValue1 = "";
		String columnValue2 = "";
		String coVal = "";
		String coVal1 = "";
		String coVal2 = "";
		int returnInt = 0;
		String returnDate = null;
		String getValue = "";
		Connection connectionObject = null;
		Statement stmt = null;
		StringBuffer valueXmlString = new StringBuffer();
		int currentFormNo = 0;
		GenericUtility genericUtility = GenericUtility.getInstance();
		ITMDBAccessLocal itmDBAccess = null; // for ejb3
		//ITMDBAccess itmDBAccess = null;
		try
		{
		 	AppConnectParm appConnect = new AppConnectParm();
			Properties p = appConnect.getProperty();
			InitialContext ctx = new InitialContext(p);
			itmDBAccess = (ITMDBAccessLocal)ctx.lookup("ibase/ITMDBAccessEJB/local");
			//itmDBAccess = itmDBAccessHome.create();
			connectionObject = itmDBAccess.getConnection(); 
			stmt = connectionObject.createStatement();
			stmt = connectionObject.createStatement();
			userId = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginCode");
			columnValue = genericUtility.getColumnValue(currentColumn,dom);
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
            valueXmlString = new StringBuffer("<?xml version=\"1.0\"?>\r\n<Root>\r\n<Header>\r\n<editFlag>");
			valueXmlString.append(editFlag).append("</editFlag>\r\n</Header>\r\n");
			switch (currentFormNo)
			{
				case 1:
				 
				break;
				
				case 2: 
				valueXmlString.append("<Detail2>\r\n");
				System.out.println("In detail2 Item Changed");
						if (currentColumn.trim().equals("itm_default"))
						{
							System.out.println("xtraParams :" + xtraParams);
							
							coVal = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"line_no");
							valueXmlString.append("<line_no>").append(coVal).append("</line_no>\r\n");
							coVal1 = genericUtility.getColumnValue("bal_group",dom1); 
							coVal2 = genericUtility.getColumnValue("descr",dom1);
							System.out.println("line_no"+coVal);
							System.out.println("bal_group"+coVal1);
							System.out.println("descr"+coVal2);
							valueXmlString.append("<bal_group>").append(coVal1).append("</bal_group>\r\n");
							
							valueXmlString.append("<descr>").append(coVal2).append("</descr>\r\n");
							
						}

							valueXmlString.append("</Detail2>\r\n");
				break;
			 
			}//switch statement ends here
		}//try block ends here
		catch(Exception e)
		{
			System.out.println("Exception :TaxexciseEJB :itemChanged(Document,String):" + e.getMessage() + ":");
			valueXmlString = valueXmlString.append(genericUtility.createErrorString(e));
		}
		valueXmlString.append("</Root>\r\n");	
		System.out.println("\n****ValueXmlString :" + valueXmlString + ":********");
		return valueXmlString.toString();
	}
}
		