package ibase.webitm.ejb.fin;

import java.rmi.RemoteException;
import java.util.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.ejb.*;
import javax.naming.InitialContext;

import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.*;
import ibase.webitm.utility.GenericUtility;
import ibase.ejb.*;
import ibase.system.config.*;
import java.sql.*;
import java.lang.*;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3

public class EmpCctr extends ValidatorEJB implements  EmpCctrLocal ,EmpCctrRemote //SessionBean
{
	String  returnValue12 = "";
  	/*public void ejbCreate() throws RemoteException, CreateException
	{
		try
		{
			System.out.println("ValidatorEJB ejbCreate called.........");
			
		}
		catch (Exception e)
		{
			System.out.println("Exception :ValidatorEJB :ejbCreate :==>"+e);
			throw new 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
	{
		Document dom = null;
		Document dom1 = null;
		Document dom2 = null;		
		String errString = "";
		System.out.println("xmlString : "  + xmlString);
		System.out.println("xmlString1 : "  + xmlString1);
		System.out.println("xmlString2 : "  + xmlString2);
		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 :EmpCctrEJB :wfValData(String xmlString):" + e.getMessage() + ":");
			errString = genericUtility.createErrorString(e);
			e.printStackTrace();
		}
		System.out.println("returning from EmpCctrEJB wfValData");
		return (errString);
	}
	public String wfValData(Document dom, Document dom1, Document dom2, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		boolean checkNextCol = true;
		//String count = "";
		String columnName = "";
		String columnValue = "";
		String columnValue1 = "";
		String userId = "";
		String sql = "";
		String sql2 = "";
		String errString = "";
		Connection connectionObject = null;
		Statement stmt = null;
		ResultSet rs = null;
		ResultSet rs2 = null;
		String returnValue = "";
		String returnValue2 = "";
		java.util.Date returnDate = null;
		//Date columnDate = null;
		//SimpleDateFormat columnDate1 = null;
		NodeList parentList = null;
		NodeList childList = null;
		Node parentNode = null;
		Node childNode = null;
		String childNodeName = null;
		int ctr;
		int count = 0;
		int noOfChilds = 0;
		int currentFormNo = 0;
		GenericUtility genericUtility = GenericUtility.getInstance();
		ITMDBAccessLocal itmDBAccess = null;// added for ejb3
		//ITMDBAccess itmDBAccess = null;
		try
		{
			AppConnectParm appConnect = new AppConnectParm();
			Properties p = appConnect.getProperty();
			InitialContext ctx = new InitialContext(p);
			itmDBAccess = (ITMDBAccessLocal)ctx.lookup("ITMDBAccess");//added for ejb3
			//itmDBAccess = itmDBAccessHome.create();//commented for ejb3
			connectionObject = itmDBAccess.getConnection(); 
			stmt = connectionObject.createStatement();
			userId = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginCode");
			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();
				if (childNodeName.equals("cctr_code"))
				{
					if(childNode.getFirstChild() == null)
					{
						errString = itmDBAccess.getErrorString("cctr_code","VMCCTRCD1",userId,errString,connectionObject);     
					} 
					else
					{ 
						columnValue = childNode.getFirstChild().getNodeValue().trim();
						sql = "SELECT COUNT(*) FROM COSTCTR WHERE CCTR_CODE='" + columnValue+"'";
						System.out.println("sql :"+sql);
						rs = stmt.executeQuery(sql);
						//count = rs.getInt(1);
						if(rs.next())
						{
							count = rs.getInt(1);
						}
						if(count == 0)
						{
							errString = itmDBAccess.getErrorString("cctr_code","VMCCTR1",userId,errString,connectionObject); 

						}
					}
				}
			}
		}
		catch(Exception e)
		{
			System.out.println("Exception :EmpCctrEJB :wfValData(Document dom):" + e.getMessage() + ":");
			errString = genericUtility.createErrorString(e);
			e.printStackTrace();
		}
		System.out.println("error :"+errString);
		return (errString);
	}
//item change logic
	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 = "";
		GenericUtility genericUtility = GenericUtility.getInstance();
		try
		{
			System.out.println("xmlString :"+xmlString+ "\n xmlString1 :"+xmlString1+ " \nxmlString2 :"+xmlString2);
			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);
		}
		catch(Exception e)
		{
			System.out.println("Exception :EmpCctrItmChngEJB :itemChanged(String,String) :==>\n"+e.getMessage());
			valueXmlString = genericUtility.createErrorString(e);			
		}
		return valueXmlString.toString(); 
	}
	public String itemChanged(Document dom, Document dom1, Document dom2, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		String columnValue = "";
		String emp_code = "";
		String sql = "";
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		StringBuffer valueXmlString = new StringBuffer();
	    ConnDriver connDriver = new ConnDriver();
		GenericUtility genericUtility = GenericUtility.getInstance();
		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			columnValue = genericUtility.getColumnValue(currentColumn,dom);
			System.out.println("columnValue :"+columnValue);
			valueXmlString = new StringBuffer("<?xml version=\"1.0\"?><Root><Header><editFlag>");
			valueXmlString.append(editFlag).append("</editFlag></Header>");
			valueXmlString.append("<Detail1>");
			
			if (currentColumn.trim().equals("itm_default"))
			{
				emp_code = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"emp_code");
				valueXmlString.append("<emp_code>").append(emp_code).append("</emp_code>\r\n");				
				
			}	
			
			else if (currentColumn.trim().equals("cctr_code"))
			{
				sql = "SELECT DESCR, SH_DESCR FROM COSTCTR WHERE CCTR_CODE = '"+ columnValue +"'";
				System.out.println("sql :"+sql);
				stmt = conn.createStatement();
				rs = stmt.executeQuery(sql);
				if (rs.next())
				{
					valueXmlString.append("<descr>").append(rs.getString(1)).append("</descr>\r\n");	
					valueXmlString.append("<sh_descr>").append(rs.getString(1)).append("</sh_descr>\r\n");
				}
				else	
				{
					valueXmlString.append("<descr>").append("").append("</descr>\r\n");	
					valueXmlString.append("<sh_descr>").append("").append("</sh_descr>\r\n");
				}
				
			}
			valueXmlString.append("</Detail1>\r\n");
			valueXmlString.append("</Root>\r\n");			
		}
		catch(Exception e)
		{
			System.out.println("Exception in EmpCctrItmChngEJB.java occured");
		}
		finally
		{
			try
			{
				System.out.println("Closing Connection.......");
				conn.close();
				conn = null;
			}catch(Exception se){}
		}
		System.out.println("Return XML" + valueXmlString.toString() );
		return valueXmlString.toString(); 

	}
}