package ibase.webitm.ejb.sys;  

import java.rmi.RemoteException;
import java.util.*;
import java.util.Date;
import java.text.*;
import java.sql.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.ejb.*;
import ibase.webitm.utility.ITMException;
import ibase.webitm.utility.*;
import ibase.system.config.*;
import ibase.webitm.ejb.*;
import ibase.webitm.utility.TransIDGenerator;//TID
import ibase.utility.CommonConstants;//TID
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3
public class FileFlt extends ValidatorEJB implements  FileFltLocal , FileFltRemote // SessionBean
{
	/*public void ejbCreate() throws RemoteException, CreateException 
	{
		System.out.println("Enter in to the the FileFltEJB........................");
	}
	public void ejbRemove()
	{
	}
	public void ejbActivate() 
	{
	}
	public void ejbPassivate() 
	{
	}*/
	public String wfValData() throws RemoteException,ITMException
	{
		return "";
	}

	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 valueXmlString = "";
		try
		{
			dom = parseString(xmlString); //returns the DOM Object for the passed XML Stirng
			System.out.println("xmlString" + xmlString);
			dom1 = parseString(xmlString1); //returns the DOM Object for the passed XML Stirng
			if (xmlString2.trim().length() > 0 )
			{
				dom2 = parseString("<Root>" + xmlString2+ "</Root>");
			}
			valueXmlString = itemChanged(dom,dom1,dom2,objContext,currentColumn,editFlag,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception : [FileFltEJB][itemChanged(String,String)] :==>\n"+e.getMessage());
		}
        return valueXmlString; 
	}
	
	public String itemChanged(Document dom, Document dom1,Document dom2, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		int ctr =0;
		int currentFormNo = 0;
		String errCode = "";
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		String errString = "";
		StringBuffer valueXmlString = new StringBuffer();
		String sql = "";
		String columnValue = "";
		String format = "";
		NodeList parentNodeList = null;
		Node parentNode = null;
		Node childNode = null;
		NodeList childNodeList = null;
		String childNodeName = "";
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		GenericUtility genericUtility = GenericUtility.getInstance();
		StringBuffer retString = new StringBuffer();
		String winName = "";
		String filePath ="";
		try
		{
			conn = getConnection(); //THIS FUNCTION IS TO CONNECT WITH ORACLE....
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			System.out.println("[FileFltEJB] [itemChanged] :currentFormNo ....." +currentFormNo);
			valueXmlString = new StringBuffer("<?xml version=\"1.0\"?><Root><header><editFlag>");
			valueXmlString.append(editFlag).append("</editFlag></header>");
			valueXmlString.append("<Detail>");	
			System.out.println("FORM NO-------------"+currentFormNo);							
			switch (currentFormNo)
			{
				case 1:
					
					/* SEARCHING THE DOM FOR THE INCOMING COLUMN VALUE START*/
					parentNodeList = dom.getElementsByTagName("Detail1");
					parentNode = parentNodeList.item(0);
					childNodeList = parentNode.getChildNodes();
					ctr = 0;
					int childNodeListLength = childNodeList.getLength();
					do
					{
						childNode = childNodeList.item(ctr);
						childNodeName = childNode.getNodeName();
						if(childNodeName.equals(currentColumn))
						{
							if (childNode.getFirstChild() != null)
							{
								columnValue=childNode.getFirstChild().getNodeValue().trim();
							}
						}
						ctr++;
					}while(ctr < childNodeListLength  && !childNodeName.equals(currentColumn));
					if (currentColumn.trim().equals("tran_id"))
					{
						if (columnValue != null)
						{
							System.out.println("columnValue.................."+columnValue);
							try					 
							{
								

								sql =" SELECT EDI_CONTROL_DET.WIN_NAME,EDI_CONTROL_FILE.FILE_PATH,EDI_CONTROL_FILE.FILE_NAME "                      
				     				   +" FROM EDI_CONTROL_DET,EDI_CONTROL_FILE "
				     				   +" WHERE ( EDI_CONTROL_DET.WIN_NAME = EDI_CONTROL_FILE.WIN_NAME ) AND "
				     				   +" ( EDI_CONTROL_DET.TRAN_IDENTIFIER = '"+ columnValue +"' )";
				     				   st = conn.createStatement();
				     				   rs = st.executeQuery(sql);
		     				   while(rs.next())
		     				   {
		     				   	winName = rs.getString(1);
		     				   	filePath = rs.getString(2)+ rs.getString(3);
		     				   	
		     				   }
		     				   System.out.println("File Name.................."+filePath);
		     				   System.out.println("Window Name................."+winName);
							}
							catch(Exception e)
							{
								System.out.println("Exception :[FileFltEJB[itemChanged::case 1] :==>\n"+e.getMessage());
								throw new ITMException(e);
							}
						}
						System.out.println("");
						valueXmlString.append("<file_path>").append(filePath).append("</file_path>");
						valueXmlString.append("<window_name>").append(winName).append("</window_name>");
					}
					valueXmlString.append("</Detail>");
					break;
				}//END OF SWITCH
				valueXmlString.append("</Root>");				
		}//END OF TRY
		catch(Exception e)
		{
			System.out.println("Exception ::"+e.getMessage());
		}
		finally
		{
				try
				{
					conn.close();
					conn = null;
				}catch(Exception s){}
		
		}
		return valueXmlString.toString();
	}//END OF ITEMCHNGE
}