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); 
	}
   //Commented by Chandni as this method not working properly
   /*
    
    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);
	}
    
    */
	
	public String wfValData(Document dom, Document dom1, Document dom2, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		String userId = "";
		String errString = "";
		int currentFormNo = 0;
		Connection connectionObject = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql="";
		int noOfChilds = 0;
		String childNodeValue ="";
		NodeList parentList = null;
		NodeList childList = null;
		Node parentNode = null;
		Node childNode = null;
		String childNodeName = null;
		int ctr = 0;
		String balGroup="",balType="",drcrType="",balPrd="",refSer="",drcrFlag="";
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		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("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)
						if("".equalsIgnoreCase(balGroup))
						{
							System.out.println("inside if 1");
							errString  =  itmDBAccessEJB.getErrorString("bal_group", "VMBGNUL", userId, errString, connectionObject);
							System.out.println("inside if 1"+errString);
							break ;
						}
					}
					else if (childNodeName.equalsIgnoreCase("bal_type"))
					{
						balType = genericUtility.getColumnValue("bal_type", dom);
						balType = balType == null ? "" : balType.trim();		
						System.out.println("balGroupType::"+balType);
						if("".equalsIgnoreCase(balType))
						{
							System.out.println("inside if 2");
							errString  =  itmDBAccessEJB.getErrorString("bal_type", "VMBTNUL", userId, errString, connectionObject);
							System.out.println("inside if 2"+errString);
							break ;
						}
					}
					else if (childNodeName.equalsIgnoreCase("drcr_type"))
					{
						drcrType = genericUtility.getColumnValue("drcr_type", dom);
						drcrType = drcrType == null ? "" : drcrType.trim();		
						System.out.println("drcrType::"+drcrType);
						//if(drcrType == null && drcrType.trim().length() == 0)
						if("".equalsIgnoreCase(drcrType))
						{
							errString  =  itmDBAccessEJB.getErrorString("drcr_type", "VMBDRNUL", userId, errString, connectionObject);
							break ;
						}
					}
					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);
							break ;
						}	
					}
				} // end for
				break;
				case 2 :						
					System.out.println("Validation for Detail 2..........");
					parentList = dom.getElementsByTagName("Detail2");
					parentNode = parentList.item(0);
					childList = parentNode.getChildNodes();
					noOfChilds = childList.getLength();
					for(int cnt= 0; cnt < noOfChilds; cnt++)
					{
						ctr = 0;
						childNode = childList.item(cnt);
						childNodeName = childNode.getNodeName();
						if(childNode.getFirstChild()!= null)
						{
							childNodeValue = childNode.getFirstChild().getNodeValue();
						}
						if(childNodeName.equalsIgnoreCase("itm_default"))
						{
							System.out.println("Itm Default Validation" );
						}
						if(childNodeName.equalsIgnoreCase("ref_ser"))
						{
							refSer = genericUtility.getColumnValue("ref_ser", dom);
							refSer = refSer == null ? "" : refSer.trim();		
							System.out.println("refSer::"+refSer);
							if(refSer != null && refSer.trim().length() > 0)
							{
								sql  =" select count(1) from refser where ref_ser = ? " ;
								pstmt =  connectionObject.prepareStatement(sql);
								pstmt.setString(1,refSer);
								rs = pstmt.executeQuery();
								if(rs.next())
								{
									ctr =  rs.getInt(1);
									if(ctr == 0) 
									{
										errString = itmDBAccessEJB.getErrorString("","VMBRNUL",userId);
										break ;
									}									
								}
								rs.close();
								rs = null;
								pstmt.close();
								pstmt = null;
							}								
							else
							{
								 errString  =  itmDBAccessEJB.getErrorString("ref_ser", "VMREFNUL", userId, errString, connectionObject);
								 break ;
							}
						}
						else if (childNodeName.equalsIgnoreCase("drcr_flag"))
						{
							drcrFlag = genericUtility.getColumnValue("drcr_flag", dom);
							drcrFlag = drcrFlag == null ? "" : drcrFlag.trim();		
							System.out.println("drcrFlag::"+drcrFlag);								
							//if(drcrFlag == null && drcrFlag.trim().length() == 0)
							if("".equalsIgnoreCase(drcrFlag))
							{
								errString  =  itmDBAccessEJB.getErrorString("drcr_flag", "VMDRCRNUL", userId, errString, connectionObject);
								break ;
							}	
						}
					}// end of for
					break;
				} //switch end		
			
		}	//end try 	
		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(" TaxExcise Validation End and Connection is Closed");
		}
		System.out.println("ErrString ::"+errString);
		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); 
	}
	// Commented by chandni as below method is not working
	/*

	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();
	}

	 */
	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 userId = "";
		String coVal = "";
		String coVal1 = "";
		String coVal2 = "";
		StringBuffer valueXmlString = new StringBuffer();
		int currentFormNo = 0;
		GenericUtility genericUtility = GenericUtility.getInstance();
		ITMDBAccessLocal itmDBAccess = null; // for ejb3
		try
		{
			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
			valueXmlString.append("</Root>\r\n");	
		}//try block ends here
		catch(Exception e)
		{	
			 e.printStackTrace();
			 throw new ITMException(e);
		}
		return valueXmlString.toString();
	}
}
		