package ibase.webitm.ejb.dis;
/*
** Purpose : Validate columns in wf_val_data function of window w_tax_process_bal
	<p>Company: Base Information Managment Ltd.</p>
**
*/
import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.*;
import org.w3c.dom.*;
import java.rmi.RemoteException;
import java.util.*;
import java.sql.*;
import java.text.*;
import javax.xml.parsers.*;
//import javax.java.*;

import javax.naming.InitialContext;
import ibase.webitm.utility.GenericUtility;
import ibase.ejb.*;
import ibase.system.config.*;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3
public class TaxProcessBal extends ValidatorEJB implements TaxProcessBalLocal, TaxProcessBalRemote
{
	/*public void Create() throws RemoteException, CreateException 
    {
	   System.out.println("TaxProcessBal Created.ejb..ejb..ejb..ejb.");
    }

	public void Remove()
	{
	}

	public void Activate() 
	{
	}

	public void Passivate()
	{
	}*/

	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 dom1=null;
		Document dom2 = 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 :TaxProcessBal :wfValData(String xmlString):" + e.getMessage() + ":");
			errString = genericUtility.createErrorString(e);
		}
		System.out.println("returning from TaxProcessBal 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 userId = "";
		String sql = "";
        String colValue="";
		String coValue="";
		StringBuffer sqlValData = null;
		int currentFormNo = 0;
		String columnValue1 = "";
		String errString = "";
		Connection connectionObject = null;
		Statement stmt = null;
		ResultSet rs = null;
		NodeList parentList = null;
		NodeList childList = null;
		Node parentNode = null;
		Node childNode = null;
		String childNodeName = null;
		int noOfChilds = 0;
		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");
			//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();
			noOfChilds = childList.getLength();
			for(ctr = 0; ctr < noOfChilds; ctr++)
			{
				childNode = childList.item(ctr);
				childNodeName = childNode.getNodeName();
				System.out.println("childNodeName:  " + childNodeName );
				switch(currentFormNo)
				{
					case 1:
						
						if (childNodeName.equals("bal_group"))
						{
							System.out.println("childNode.getFirstChild(): " + childNode.getFirstChild()  + "***");
							if(childNode.getFirstChild() == null)
							{
								 errString  =  itmDBAccess.getErrorString("bal_group", "VMBGNUL", userId, errString, connectionObject);
							}
							//sachin
							else
							{
								columnValue = childNode.getFirstChild().getNodeValue().trim();
								String siteCode = genericUtility.getColumnValue("site_code",dom);
								String acctPrd = genericUtility.getColumnValue("acct_prd",dom);
								sqlValData = new StringBuffer();
								sqlValData.append("SELECT COUNT(1) FROM TAX_PROCESSED A,ACCTPRD B WHERE A.TRAN_DATE BETWEEN B.FR_DATE AND B.TO_DATE AND A.BAL_GROUP = '");
								sqlValData.append(columnValue).append("' AND A.SITE_CODE = '").append(siteCode).append("' AND B.CODE = '").append(acctPrd).append("'");
								rs = stmt.executeQuery(sqlValData.toString());
								if (rs.next())
								{
									int countRec = rs.getInt(1);
									if (countRec != 0)
									{
										errString  =  itmDBAccess.getErrorString("bal_group", "VMBGEDIT", userId, errString, connectionObject);
									}
								}
							}
							//end sachin
						}		
						else if (childNodeName.equals("bal_prd"))
						{
							System.out.println("childNode.getFirstChild(): " + childNode.getFirstChild()  + "***");
							if(childNode.getFirstChild() == null)
							{
								 errString  =  itmDBAccess.getErrorString("bal_prd", "VMBPNUL", userId, errString, connectionObject);
							 }	
						}
						else if (childNodeName.equals("site_code"))
						{
							System.out.println("childNode.getFirstChild(): " + childNode.getFirstChild()  + "***");
							if(childNode.getFirstChild() == null)
							{
								 errString  =  itmDBAccess.getErrorString("site_code", "VMSITECD1", userId, errString, connectionObject);
							 }	
							 else
							{
								 columnValue = childNode.getFirstChild().getNodeValue().trim();
								 sqlValData = new StringBuffer();
								 sqlValData.append("SELECT COUNT(*) FROM SITE WHERE SITE_CODE = '");
								 sqlValData.append(columnValue).append("'");
								 rs = stmt.executeQuery(sqlValData.toString());
								 while (rs.next() == false)
								 {
										errString  =  itmDBAccess.getErrorString("site_code", "VMSITE1", userId, errString, connectionObject);
								 }
							}
						}
						break;
				}
			}
			connectionObject.close();
		}
		catch(Exception e)
		{
			System.out.println("Exception :TaxProcessBal :wfValData(Document dom):" + e.getMessage() + ":");
			errString = genericUtility.createErrorString(e);
		}
		return errString;
	}


	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 dom = null;
		Document dom1=null;
		Document dom2 = 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 = itemChanged(dom,dom1,dom2,objContext,currentColumn,editFlag,xtraParams);
			System.out.println("ErrString :" + errString);
		}
		catch(Exception e)
		{
			System.out.println("Exception :TaxProcessBal :itemChanged(String,String):" + e.getMessage() + ":");
			errString = genericUtility.createErrorString(e);
		}
		System.out.println("returning from TaxProcessBal itemChanged");
		return errString;
	}

	public String itemChanged(Document dom, Document dom1, Document dom2, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		System.out.println("itemchanged dom.ejb..ejb..ejb.");
		ResultSet rs = null;
		ResultSet rsDescr = null;
		PreparedStatement pstmtDescr = null;
		String sql = "";
		String columnValue = "";
		String returnValue = "";
		String returnValue1 = ""; 
		String columnVal = "";
		float amt = 0.00f;
		int fr__lot = 0;
		int to_lot = 0; 
		int tot_items = 0;
		Connection connectionObject = null;
		Statement stmt;
		StringBuffer valueXmlString =null;
		StringBuffer sqlDescr  = new StringBuffer();
		GenericUtility genericUtility = GenericUtility.getInstance();
		//ITMDBAccessHome itmDBAccessHome = null; // for ejb3
		ITMDBAccessLocal 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();
			columnValue = genericUtility.getColumnValue(currentColumn,dom);
            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<Detail1>\r\n");
			if (columnValue != null)
			{
				if (currentColumn.trim().equals("bal_group"))
				{
					System.out.println("columnValue" + columnValue );
					if(columnValue.length() > 0)
					{
						sqlDescr.append("SELECT DESCR FROM TAX_BALANCE_GRP WHERE BAL_GROUP = '");
						sqlDescr.append(columnValue).append("'");
						pstmtDescr = connectionObject.prepareStatement(sqlDescr.toString());
						rsDescr = pstmtDescr.executeQuery();
						if (rsDescr.next())
						{
							System.out.println("In the Ref of Value xml String");
							returnValue = rsDescr.getString("descr");
							valueXmlString.append("<descr>").append(returnValue).append("</descr>\r\n");
							System.out.println("In the Ref of Value xml String After this one: "+returnValue);
						}
						else
						{
							returnValue = "";
							valueXmlString.append("<descr>").append(returnValue).append("</descr>\r\n");
						}
					}
				}
				else if (currentColumn.trim().equals("site_code"))
				{
					System.out.println("columnValue" + columnValue );
					if(columnValue.length() > 0)
					{
						sqlDescr.append("SELECT DESCR FROM SITE WHERE SITE_CODE = '");
						sqlDescr.append(columnValue).append("'");
						pstmtDescr = connectionObject.prepareStatement(sqlDescr.toString());
						rsDescr = pstmtDescr.executeQuery();
						if (rsDescr.next())
						{
							System.out.println("In the Ref of Value xml String");
							returnValue = rsDescr.getString("descr");
							valueXmlString.append("<sitedescr>").append(returnValue).append("</sitedescr>\r\n");
							System.out.println("In the Ref of Value xml String After this one: "+returnValue);
						}
						else
						{
							returnValue = "";
							valueXmlString.append("<sitedescr>").append(returnValue).append("</sitedescr>\r\n");
						}
					}
				}
			}
		}
		catch(Exception e)
		{
			System.out.println("Exception :TaxProcessBal :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();
	}
}