package ibase.webitm.ejb.sys;

import ibase.webitm.utility.ITMException;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.ejb.*;

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

import java.io.File;

/* This EJB will Validate the Data entered in the Object Actions Master */
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3

public class ObjectActions extends ValidatorEJB implements  ObjectActionsLocal , ObjectActionsRemote // SessionBean//Ratikant 30/Mar/2005
{
	/*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 = "";
		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 errString = "";
		String errCode = "";
		String evtCode = "";
		String objName = "";
		String lineNo = "";
		String sql = "";
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;	
		int cnt = 0;
		int currentFormNo = 0;
		NodeList parentNodeList = null;
		NodeList childNodeList = null;
		Node parentNode = null;
		Node childNode = null;
		String childNodeName = null;
		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("obj_name"))
						{
							if(childNode.getFirstChild() == null)
							{
								BaseLogger.debug("Obj Name Is Null");
								errString = getErrorString("obj_name","NULLOBJNM",userId,errString);
								break;
							}
							else
							{
								objName=childNode.getFirstChild().getNodeValue();
							}

						}
						else if (childNodeName.equals("line_no"))
						{
								
							if(childNode.getFirstChild() == null)
							{
								BaseLogger.debug("Line Number is null");
								errString = getErrorString("line_no","NULLLNNO",userId,errString);
								break;
							}
							else
							{
									lineNo = childNode.getFirstChild().getNodeValue();
									if (editFlag.equalsIgnoreCase("A"))
									{
										sql = " SELECT COUNT(*) FROM OBJ_ACTIONS WHERE obj_name = '"+objName+"'" +" AND line_no = '"+lineNo+ "'";
										BaseLogger.debug("SQL is -->" +sql);
										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("event_code","DUPLOBJACD ",userId,errString);
													break;
											}
										}
									}
							}
						}
						else if(childNodeName.equals("title"))
						{
							if(childNode.getFirstChild() == null)
							{
								BaseLogger.debug("Obj Name Is Null");
								errString = getErrorString("title","NULLTITLCD",userId,errString);
								break;
							}

						}
					}
				break;
			}
		}
		catch(Exception e)
		{
			BaseLogger.error(e);
			e.printStackTrace();
			throw new ITMException(e);
		}
		
		return errString;
	}//wfValData(Document dom) method ends here
}//class ends

		
		
		
