package ibase.webitm.ejb.sys;

import java.rmi.RemoteException;
import java.util.*;
import java.util.Date;
import java.sql.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.ejb.*;
import java.io.*;
import java.text.*;
// Rajesh on 9/20/2005 to use BaseLogger functions
import ibase.utility.BaseLogger;

import ibase.webitm.ejb.*;
import ibase.webitm.utility.ITMException;
import ibase.webitm.utility.GenericUtility;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3

public class ItmMenu extends ValidatorEJB implements  ItmMenuLocal , ItmMenuRemote // SessionBean
{
	/*public void ejbCreate() throws RemoteException, CreateException
	{
	}

	public void ejbRemove() 
	{
	}

	public void ejbActivate() 
	{
	}

	public void ejbPassivate()
	{
	}*/
	
	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//Ratikant 30/Mar/2005
	{
		Document dom = null;
		Document dom1 = null;
		Document dom2 = null;//Ratikant 30/Mar/2005
		String  errString = null;
		try
		{
			dom = parseString(xmlString);
			dom1 = parseString(xmlString1); 
//		dom2 = parseString(xmlString2);//Ratikant 30/Mar/2005 
			errString = wfValData(dom,dom1,null,objContext,editFlag,xtraParams);//Ratikant 30/Mar/2005		
		}
		catch(Exception e)
		{
			BaseLogger.error(e);
		}
		return (errString);
	}

	public String wfValData(Document dom,Document dom1,Document dom2, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException//Ratikant 30/Mar/2005 
	{
		String columnName = "";
		String columnValue = "";
		String userId = "";
		String sql = "";
		String errString = "";
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		int currentFormNo=0;
		NodeList parentNodeList = null;
		NodeList childNodeList = null;
		Node parentNode = null;
		Node childNode = null;
		String childNodeName = null;
		String appln = "";
		String level1 = "";
		String level2 = "";
		String level3 = "";
		String level4 = "";
		String level5 = "";
		int ctr;
		

		try
		{
			conn = getConnection(); //This function is to connect with oracle....
			userId = GenericUtility.getInstance().getValueFromXTRA_PARAMS(xtraParams,"userId");
			
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			
			stmt = conn.createStatement();
			switch (currentFormNo)
			{
				case 1:
					parentNodeList = dom.getElementsByTagName("Detail1");
					parentNode = parentNodeList.item(0);
					childNodeList = parentNode.getChildNodes();
					
					int childNodeListLength = childNodeList.getLength();
					for(ctr = 0; ctr < childNodeListLength; ctr++)
					{
						childNode = childNodeList.item(ctr);
						childNodeName = childNode.getNodeName();
						BaseLogger.debug("Child Node::"+childNodeName);
						
						
					 if(childNodeName.equals("application"))
						{
							if(childNode.getFirstChild() == null)
							{
								BaseLogger.debug("Application Is Null");
								errString = getErrorString("application","NULLAPPLCD",userId,errString);
								break;
							}
							else
							{
								appln = childNode.getFirstChild().getNodeValue();
							}

						}
						else if(childNodeName.equals("level_1"))
						{
							if(childNode.getFirstChild() == null)
							{
								BaseLogger.debug("Level1 Is Null");
								errString = getErrorString("level_1","NULLLEV1",userId,errString);
								break;
							}
							else
							{
								level1 = childNode.getFirstChild().getNodeValue();
							}

						}
						else if(childNodeName.equals("level_2"))
						{
							if(childNode.getFirstChild() == null)
							{
								BaseLogger.debug("Level2 Is Null");
								errString = getErrorString("level_2","NULLLEV2",userId,errString);
								break;
							}
							else
							{
								level2 = childNode.getFirstChild().getNodeValue();
							}

						}
						else if(childNodeName.equals("level_3"))
						{
							if(childNode.getFirstChild() == null)
							{
								BaseLogger.debug("Level3 Is Null");
								errString = getErrorString("level_3","NULLLEV3",userId,errString);
								break;
							}
							else
							{
								level3 = childNode.getFirstChild().getNodeValue();
							}

						}
						else if(childNodeName.equals("level_4"))
						{
							if(childNode.getFirstChild() == null)
							{
								BaseLogger.debug("Level4 Is Null");
								errString = getErrorString("level_4","NULLLEV4",userId,errString);
								break;
							}
							else
							{
								level4 = childNode.getFirstChild().getNodeValue();
							}

						}
						else if(childNodeName.equals("level_5"))
						{
							if(childNode.getFirstChild() == null)
							{
								BaseLogger.debug("Level5 Is Null");
								errString = getErrorString("level_5","NULLLEV5",userId,errString);
								break;
							}
							else
							{
									level5 = childNode.getFirstChild().getNodeValue();
									BaseLogger.debug("Level 5 is " +level5);//17/Mar/2005
									if (editFlag.equalsIgnoreCase("A"))
									{
										BaseLogger.debug("The Edit Flag Is Ratikant....."+editFlag);//17/Mar/2005
										sql = " SELECT COUNT(*) FROM ITM2MENU WHERE application = '"+appln+ "'  AND level_1 = '"+level1+"' AND level_2 = '"+level2+"' AND level_3 ='"+level3+ "' AND level_4 = '"+level4+ "' AND level_5 = '"+level5+"'";
										BaseLogger.debug("SQL is " +sql);//17/Mar/2005
										stmt = conn.createStatement();
										rs	= stmt.executeQuery(sql);
										if (rs.next())
										{
											if(rs.getInt(1) > 0)
											{
													BaseLogger.debug("Tried to enter Duplicate key combination");
													errString = getErrorString("user_id","DUPLITM2CD ",userId,errString);
													break;
											}
										}
									}
						   }
						}
					}
					break;
			   }

		}
		catch (Exception e)
		{
			BaseLogger.error(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//Ratikant 30/Mar/2005
	{
		Document dom = null;
		Document dom1 = null;
		Document dom2 = null;//Ratikant 30/Mar/2005
		String valueXmlString = "";
		try
		{
			dom = parseString(xmlString); //returns the DOM Object for the passed XML Stirng
			dom1 = parseString(xmlString1); //returns the DOM Object for the passed XML Stirng
			
			//			dom2 = parseString(xmlString2);//Ratikant 29/Mar/2005
			
			valueXmlString = itemChanged(dom,dom1,null,objContext,currentColumn,editFlag,xtraParams);//Ratikant 30/Mar/2005
		}
		catch(Exception e)
		{
			BaseLogger.error(e);
		}
        BaseLogger.debug("currentColumn ....." +currentColumn);
		return (valueXmlString); //calls itemChanged(Document dom,String currentColumn) method and returns string
	}

	public String itemChanged(Document dom, Document dom1,Document dom2, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException,ITMException//Ratikant 30/Mar/2005
	{
		ResultSet rs = null;
		int n;
		int currentFormNo = 0;
		Connection conn = null;
		Statement stmt;
		StringBuffer valueXmlString = new StringBuffer();
		String sql = "";
		String columnValue = "";
		String returnValue = "";
		String application = "";
		String level1 = "";
		String level2 = "";
		String level3 = "";
		String level4 = "";
		String level5 = "";
		StringBuffer menuPath = new StringBuffer();//19/Mar/2005


		try
		{
			conn = getConnection(); //This function is to connect with oracle....
			stmt = conn.createStatement();
			columnValue = getColumnValue(currentColumn,dom);
            
			BaseLogger.debug("columnValue ....." +columnValue);
			
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}

			BaseLogger.debug("currentFormNo ....." +currentFormNo);
      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");
			valueXmlString.append("<Detail>\r\n");
		
			switch (currentFormNo)
			{
				case 1:
				{	

						if (columnValue != null)

							{
								if (currentColumn.trim().equals("level_5"))
								{
								application = getColumnValue("application", dom);
								level1 = getColumnValue("level_1", dom);
								level2 = getColumnValue("level_2", dom);
								level3 = getColumnValue("level_3", dom);
								level4 = getColumnValue("level_4", dom);
								level5 = getColumnValue("level_5", dom);
								menuPath.append(application);
								menuPath.append(".");
								menuPath.append(level1);
								menuPath.append(".");
								menuPath.append(level2);
								menuPath.append(".");
								menuPath.append(level3);
								menuPath.append(".");
								menuPath.append(level4);
								menuPath.append(".");
								menuPath.append(level5);
								valueXmlString.append("<menu_path>").append(menuPath.toString()).append("</menu_path>");
								}
							
					}
					valueXmlString.append("</Detail>\r\n");
					break;
				}
			}
			
		}
		catch (Exception e)
		{
			BaseLogger.error(e);
		}
		valueXmlString.append("</Root>\r\n");
		return valueXmlString.toString();
	}
}
	

		
		
		
		
		
		
	