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 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;
	}
	//Commented by Channdi as below code is not working 
	/*
	
	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 wfValData(Document dom, Document dom1, Document dom2, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		String userId = "";
		String sql = "";
        String childNodeValue ="";
		int currentFormNo = 0;
		String errString = "";
		
		Connection connectionObject = null;
		PreparedStatement pstmt = 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;
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		String balGroup="",finEntity = "",balPrd="",acctPrd="";
		try
		{
			ConnDriver connDriver = new ConnDriver();
			connectionObject = connDriver.getConnectDB("DriverITM");
			userId = getValueFromXTRA_PARAMS(xtraParams,"loginCode");
			System.out.println("userId::["+userId+"]");
			GenericUtility genericUtility = GenericUtility.getInstance();
		
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			switch(currentFormNo)
			{
				case 1:
				parentList = dom.getElementsByTagName("Detail1");
				parentNode = parentList.item(0);
				childList = parentNode.getChildNodes();
				noOfChilds = childList.getLength();
				System.out.println("noOfChilds equals ::["+noOfChilds+"]");
				for(int cnt = 0; cnt < noOfChilds; cnt++)
				{
					ctr = 0;
					childNode = childList.item(cnt);
					childNodeName = childNode.getNodeName();
					System.out.println("childNodeName:  " + childNodeName );
					System.out.println("Validation for Detail 1 tax process bal ejb ..........");
					if(childNode.getFirstChild()!= null)
					{
						childNodeValue = childNode.getFirstChild().getNodeValue();
					}
					if (childNodeName.equalsIgnoreCase("fin_entity"))
					{
						finEntity = genericUtility.getColumnValue("fin_entity", dom);
						finEntity = finEntity == null ? "" : finEntity.trim();		
						System.out.println("finEntity::"+finEntity);
						if(finEntity != null && finEntity.trim().length() > 0)
						{
							sql  = " select count(1) from finent where fin_entity = ? " ;
							pstmt =  connectionObject.prepareStatement(sql);
							pstmt.setString(1,finEntity);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								ctr =  rs.getInt(1);
								if(ctr == 0) 
								{
									errString = itmDBAccessEJB.getErrorString("","VTFINENT1",userId);
									break ;
								}									
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;
						}								
						else
						{
							errString  =  itmDBAccessEJB.getErrorString("fin_entity", "VMFINENT1", userId, errString, connectionObject);
						}
					} // END OF FIN_ENTITY					
					else if (childNodeName.equalsIgnoreCase("site_code"))
					{
						String siteCode = genericUtility.getColumnValue("site_code", dom);
						siteCode = siteCode == null ? "" : siteCode.trim();		
						System.out.println("siteCode::"+siteCode);
						if(siteCode != null && siteCode.trim().length() > 0)
						{
							sql  =" select count(1) from site where site_code = ? " ;
							pstmt =  connectionObject.prepareStatement(sql);
							pstmt.setString(1,siteCode);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								ctr =  rs.getInt(1);
								if(ctr == 0) 
								{
									errString = itmDBAccessEJB.getErrorString("","VMSITE1",userId);
									break ;
								}									
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;
						}
						else
						{
							errString  =  itmDBAccessEJB.getErrorString("","VMSITECD1",userId);
						}
					} // END OF SITE_CODE
					/*
					else if (childNodeName.equalsIgnoreCase("bal_group"))
					{
						balGroup = genericUtility.getColumnValue("bal_group", dom);
						balGroup = balGroup == null ? "" : balGroup.trim();		
						System.out.println("balGroup::"+balGroup);
						if(balGroup != null && balGroup.trim().length() > 0)
						{
							sql = " 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 = ? ";
							pstmt =  connectionObject.prepareStatement(sql);
							pstmt.setString(1,balGroup);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								ctr =  rs.getInt(1);
								if(ctr == 0) 
								{
									errString = itmDBAccessEJB.getErrorString("","VMBGEDIT",userId);
									break ;
								}									
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;
										
						}
						else
						{
							errString  =  itmDBAccessEJB.getErrorString("bal_group","VMBGNUL",userId);
						}
					}
					*/
					else if (childNodeName.equalsIgnoreCase("bal_prd"))
					{
						balPrd = genericUtility.getColumnValue("bal_prd", dom);
						balPrd = balPrd == null ? "" : balPrd.trim();		
						System.out.println("balPrd::"+balPrd);								
						//if(balPrd == null && balPrd.trim().length() == 0)
						if("".equalsIgnoreCase(balPrd))
						{
							errString  =  itmDBAccessEJB.getErrorString("bal_prd", "VMBPNUL", userId, errString, connectionObject);
						}	
					}
					else if (childNodeName.equalsIgnoreCase("acct_prd"))
					{
						acctPrd = genericUtility.getColumnValue("acct_prd", dom);
						acctPrd = acctPrd == null ? "" : acctPrd.trim();		
						System.out.println("acctPrd::"+acctPrd);
						if(acctPrd != null && acctPrd.trim().length() > 0)
						{
							sql  =" select count(1) from acctprd where code = ? " ;
							pstmt =  connectionObject.prepareStatement(sql);
							pstmt.setString(1,acctPrd);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								ctr =  rs.getInt(1);
								if(ctr == 0) 
								{
									errString = itmDBAccessEJB.getErrorString("","VTACCTPRD2",userId);
									break ;
								}									
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;
						}								
						else
						{
							 errString  =  itmDBAccessEJB.getErrorString("acct_prd", "VMACCNULL", userId, errString, connectionObject);
						}
					}	
				}  // for end
				break;
			} // switch end
		} // try end
		catch(Exception e)
		{
			System.out.println("Exception ::"+e);
			e.printStackTrace();
			errString=e.getMessage();
		}
		finally
		{
			try
			{
				if(connectionObject!=null)
				{
					connectionObject.close();
				}
				connectionObject = null;
			}
			catch(Exception d)
			{
				d.printStackTrace();
			}			
			System.out.println(" TaxProcessBal Validation End and Connection is Closed");
		}
		System.out.println("ErrString ::"+errString);
		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
	{
		ResultSet rsDescr = null;
		PreparedStatement pstmtDescr = null;
		String columnValue = "";
		String returnValue = "";
		Connection connectionObject = null;
		Statement stmt;
		StringBuffer valueXmlString =null;
		StringBuffer sqlDescr  = new StringBuffer();
		GenericUtility genericUtility = GenericUtility.getInstance();
		ITMDBAccessLocal itmDBAccess = null;
		try
		{
			ConnDriver conndriver = new ConnDriver(); // added by Chandni Shah 27/6/2012
			connectionObject = conndriver.getConnectDB("DriverITM"); // added by Chandni Shah 27/6/2012
			AppConnectParm appConnect = new AppConnectParm();
			Properties p = appConnect.getProperty();
			InitialContext ctx = new InitialContext(p);
			itmDBAccess = (ITMDBAccessLocal)ctx.lookup("ibase/ITMDBAccessEJB/local");
			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");
						}
					}
				}
				valueXmlString.append("</Detail1>\r\n");
				//	Commentde by Chandni Shah 			
				/*
				}
				}
				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();
				}
				 */
				valueXmlString.append("</Root>\r\n");	
			}
		} // end try
		catch(Exception e)
		{	
			 e.printStackTrace();
			 throw new ITMException(e);
		}
		finally 
		{
			try
			{
				if(connectionObject != null)
				connectionObject.close();
				connectionObject = null;
			}
			catch(Exception e1)
			{
				e1.printStackTrace();
			}
		}        
		return valueXmlString.toString();
		
	}	
		
}