/*
 * 
 */
package ibase.webitm.ejb.adm;
import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.ValidatorEJB;
import ibase.webitm.ejb.ITMDBAccessEJB;
//import ibase.webitm.ejb.ITMDBAccess;//commented for ejb3
//import ibase.webitm.ejb.ITMDBAccessHome;//commented for ejb3
import ibase.webitm.utility.GenericUtility;
import java.rmi.RemoteException;
import java.util.Date;
import java.util.Calendar;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
//import javax.ejb.SessionBean;
import javax.ejb.CreateException;
import javax.naming.InitialContext;

import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3
public class EmpITDeclMulti extends ValidatorEJB implements EmpITDeclMultiLocal,EmpITDeclMultiRemote //SessionBean
{
	
   /* public void ejbCreate() throws RemoteException,CreateException
	{
		System.out.println("EmpITDeclMultiEJB  Created........");
	}
    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 = " ";
		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 :EmpITDeclMultiEJB :wfValData(String xmlString):" + e.getMessage() + ":");
			errString = genericUtility.createErrorString(e);
			e.printStackTrace();
		}
		System.out.println("returning from EmpITDeclMulti wfValData");
		return (errString);
	}

	public String wfValData(Document dom, Document dom1, Document dom2,String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		String columnValue = null;
		String userId = null;
		String sql= null;
		String sql1 = null;
		String errString = " ";
		String empCode = "";
		String acctPrd = "";
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		NodeList parentList = null;
		NodeList childList = null;
		Node parentNode = null;
		Node childNode = null;
		String childNodeName = null;		
		int counterChild = 0;
		int noOfChilds = 0;
		int currentFormNo = 0;
		int count = 0;
		GenericUtility genericUtility = GenericUtility.getInstance();
		try
		{
			
			userId = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginCode");
			empCode = genericUtility.getColumnValue("emp_code",dom);
			acctPrd = genericUtility.getColumnValue("acct_prd",dom);
			conn = getConnection(); 
			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(counterChild = 0; counterChild < noOfChilds; counterChild++)
			{
				childNode = childList.item(counterChild);
				childNodeName = childNode.getNodeName();
				switch(currentFormNo)
				{
					case 1:
						System.out.println("In case 1 ::::::::");
						if (childNodeName.equals("emp_code"))
						{
							sql = "SELECT employee.emp_fname || ' ' || employee.emp_mname || ' ' || employee.emp_lname , "+
								"(case when it_no is null then ' ' else it_no end) pan_no FROM employee where employee.emp_code = '"+empCode+"'" ;
							System.out.println("sql :: "+sql);
							stmt = conn.createStatement();
							rs = stmt.executeQuery(sql);
							if (rs.next())
							{
								sql = "select count ( *) from acctprd where code = '"+acctPrd+"'";
								System.out.println("sql :: "+sql);
								rs = stmt.executeQuery(sql);
								if (rs.next())
								{
									count = rs.getInt(1);
									if ( count == 0 )
									{
										errString = getErrorString("emp_code","VTACCTPRD1",userId);
									}
								}
							}
							else
							{
								errString = getErrorString("emp_code","VMEMP1",userId);
							}
							rs.close();rs = null;
							stmt.close();stmt = null;
						}
						
						break;
				}
				if(errString != null && errString.trim().length()> 0)
				{
					break;
				}
			}
		}//try ends here
		catch(Exception e)
		{
			System.out.println("Exception :EmpITDeclMultiEJB :wfValData(Document dom):" + e.getMessage() + ":");
			errString = genericUtility.createErrorString(e);
			e.printStackTrace();
		}
		finally
		{
			try
			{
				System.out.println("Closing Connection...........");
				if(rs != null)
				{
					rs.close();
					rs = null;
				}
				if(stmt != null)
				{
					stmt.close();
					stmt = null;
				}
				if(conn != null)
				{
					conn.close();
					conn = null;
				}
			}
			catch(Exception e){}
		}
		return errString;
	}//wfValData(Document dom) method ends here

    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);
		}
		catch(Exception e)
		{
			System.out.println("Exception :EmpITDeclMulti :itemChanged(String,String):" + e.getMessage() + ":");
			valueXmlString = genericUtility.createErrorString(e);
			e.printStackTrace();
		}
		System.out.println("returning from EmpITDeclMultiEJB itemChanged");
		return (valueXmlString); 
	}
	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 = "";
		Connection conn = null;
		Statement stmt = null;
		PreparedStatement pstmt=null;
		StringBuffer valueXmlString = new StringBuffer();	
		String userId=null;
		String termId=null;
		String sysDate = null;
		String empCodeParam = null;
		String empCode = null;
		String acctPrd = null;
		String empName = "";
		String empPAN = "";
		int currentFormNo = 0;

		GenericUtility genericUtility = GenericUtility.getInstance();
		try
		{
			userId = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginCode");
			termId = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"termId");
			empCode = genericUtility.getColumnValue("emp_code",dom);

			
			conn = getConnection(); 
			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");
			System.out.println("itemchanged dom....");
			System.out.println("current form no: " + currentFormNo);
			switch (currentFormNo)
			{
				case 1:
					System.out.println("Case1");
					valueXmlString.append("<Detail1>\r\n");	
					if (currentColumn.trim().equals("itm_default"))
					{
						java.util.Date date= new java.util.Date();
						SimpleDateFormat simpleDateFormat = new SimpleDateFormat(genericUtility.getApplDateFormat());
						System.out.println("simpleDateFormat"+simpleDateFormat.toPattern());
						sysDate = simpleDateFormat.format(date);
						System.out.println("sysDate"+sysDate);
						try
						{
							sql = "select code from acctprd where fr_date <= ? and to_date >= ? ";
							System.out.println("SQL ::"+sql);
							pstmt = conn.prepareStatement(sql);							
							pstmt.setTimestamp(1,java.sql.Timestamp.valueOf(genericUtility.getValidDateTimeString(sysDate,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat())));
							pstmt.setTimestamp(2,java.sql.Timestamp.valueOf(genericUtility.getValidDateTimeString(sysDate,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat())));
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								acctPrd = rs.getString(1);
							}	
							System.out.println("AcctPRD :: "+acctPrd);
							rs.close();rs = null;
							pstmt.close();pstmt = null;
							empCodeParam = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginEmpCode");
							sql = "SELECT (case when employee.emp_fname is null then ' ' else employee.emp_fname end) " + 
								  "|| ' ' || (case when employee.emp_mname is null then ' ' else employee.emp_mname end) " + 
								  "|| ' ' || (case when employee.emp_lname is null then ' ' else employee.emp_lname end) "+
								",(case when it_no is null then ' ' else it_no end) pan_no FROM employee where employee.emp_code = '" + empCodeParam + "'";
							System.out.println("sql :: "+sql);
							stmt = conn.createStatement();		
							rs = stmt.executeQuery(sql);
							if(rs.next())
							{
								empName = rs.getString(1);
								empPAN = rs.getString(2);
								valueXmlString.append("<emp_code>").append(empCodeParam).append("</emp_code>\r\n");
								valueXmlString.append("<emp_name>").append(empName).append("</emp_name>\r\n");
								valueXmlString.append("<pan_no>").append(empPAN).append("</pan_no>\r\n");
							}
							rs.close(); rs = null;
							stmt.close();stmt = null;
							valueXmlString.append("<acct_prd>").append(acctPrd).append("</acct_prd>\r\n");
						}
						catch (Exception e)
						{
							System.out.println("Exception :[itemChanged::case 1::itm_default] :==>"+e.getMessage());
							e.printStackTrace();
							throw new ITMException(e);
						}
					}
					if (currentColumn.trim().equals("emp_code"))
					{
						sql = "SELECT (case when employee.emp_fname is null then ' ' else employee.emp_fname end) " + 
							  "|| ' ' || (case when employee.emp_mname is null then ' ' else employee.emp_mname end) " + 
							  "|| ' ' || (case when employee.emp_lname is null then ' ' else employee.emp_lname end) "+
							",(case when it_no is null then ' ' else it_no end) pan_no FROM employee where employee.emp_code = '" + empCode + "'";
						System.out.println("sql :: "+sql);
						stmt = conn.createStatement();		
						rs = stmt.executeQuery(sql);
						if(rs.next())
						{
							empName = rs.getString(1);
							empPAN = rs.getString(2);
						}						
						System.out.println("Employee Name:"+empName);
						System.out.println("Employee PAN:"+empPAN);
						valueXmlString.append("<emp_name>").append(empName).append("</emp_name>\r\n");
						valueXmlString.append("<pan_no>").append(empPAN).append("</pan_no>\r\n");
						rs.close();rs = null;
						stmt.close();stmt = null;
					}
					valueXmlString.append("</Detail1>\r\n");
				break;
			}
			valueXmlString.append("</Root>\r\n");	
		}
		catch(Exception e)
		{
			System.out.println("Exception :EmpITDeclMultiEJB :itemChanged(Document dom,String currentColumn):" + e.getMessage() + ":");
			valueXmlString.delete(0,valueXmlString.length());			
			valueXmlString = valueXmlString.append(genericUtility.createErrorString(e));
			e.printStackTrace();
		}
		finally
		{
			try
			{
				System.out.println("Closing Connection...........");
				if(rs != null)
				{
					rs.close();
					rs = null;
				}
				if(stmt != null)
				{
					stmt.close();
					stmt = null;
				}
				if(pstmt != null)
				{
					pstmt.close();
					pstmt = null;
				}
				if(conn != null)
				{
					conn.close();
					conn = null;
				}
			}
			catch(Exception e)
			{
				System.out.println("Exception in Closing Connection......");
			}
		}
		System.out.println("\n****ValueXmlString :" + valueXmlString + ":********");
		return valueXmlString.toString();
	}
}
