package ibase.webitm.ejb.adm;

import java.rmi.RemoteException;
import java.text.*;
import java.sql.*;

import java.util.Date;

import org.w3c.dom.*;

import javax.ejb.*;

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

@Stateless // added for ejb3

public class Form16Payment1 extends ValidatorEJB implements Form16Payment1Local,Form16Payment1Remote//SessionBean
{
	GenericUtility genericUtility = GenericUtility.getInstance(); 

	/*public void ejbCreate() throws RemoteException, CreateException 
	{
		System.out.println("Form16Payment1EJB is in Process..........");
	}
	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 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("Validation Start..........");
		try
		{
			System.out.println("xmlString:::"+xmlString);
			dom = parseString(xmlString);
			dom1 = parseString(xmlString1); 
			if (xmlString2.trim().length() > 0 )
			{
				dom2 = parseString(xmlString2);
			}
			errString = wfValData(dom,dom1,dom2,objContext,editFlag,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception : Form16Payment1EJB : wfValData(String xmlString) : ==>\n"+e.getMessage());
		}
		return (errString);
	}
	public String wfValData(Document dom, Document dom1,Document dom2, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		String errString = "";
		String errCode = null;
		ConnDriver connDriver = new ConnDriver();	
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = null;
		String loginCode = null; 
		String siteCode = null; //For Site Code Validation
		String prdCode = null; // For Period Code Validation
		String empCode = null;
		String datePayment = null;
		Date dtPymt = null;
		NodeList parentNodeList = null;
		NodeList childNodeList = null;
		Node parentNode = null;
		Node childNode = null;
		String childNodeName = null; 
		int ctr,currentFormNo=0;
		int childNodeListLength;
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB(); 
		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			loginCode = getValueFromXTRA_PARAMS(xtraParams,"loginCode");
			System.out.println("logincode from xtraParams in wfvaldata ::::: " + loginCode);
			GenericUtility genericUtility = GenericUtility.getInstance(); 
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			switch(currentFormNo)
			{
				case 1:
					parentNodeList = dom.getElementsByTagName("Detail1");
					parentNode = parentNodeList.item(0);
					childNodeList = parentNode.getChildNodes();
					childNodeListLength = childNodeList.getLength();
					for(ctr = 0; ctr < childNodeListLength; ctr++)
					{
						int cnt; //for no. of record available in the table
						childNode = childNodeList.item(ctr);
						childNodeName = childNode.getNodeName();
						
						if (childNodeName.equals("site_code"))
						{
							siteCode = getColumnValue("site_code",dom);
							System.out.println("site_code:::"+ siteCode);
							sql = "SELECT COUNT(*) AS COUNT FROM SITE WHERE SITE_CODE = '"+ siteCode + "'";
							System.out.println("sql:::"+ sql);
							pstmt = conn.prepareStatement(sql);
							rs = pstmt.executeQuery();
							if (rs.next())
							{
								cnt = rs.getInt("COUNT");
								if (cnt == 0)
								{
									errCode = "VMSITE1";
									errString = getErrorString("site_code",errCode,loginCode);
									break;
								}
							}
						}
						else if (childNodeName.equals("emp_code"))
						{
							empCode = getColumnValue("emp_code", dom);
							System.out.println("emp_code ::: " + empCode );
							if (empCode != null)
							{
								sql = "SELECT COUNT(*) AS COUNT FROM EMPLOYEE WHERE EMP_CODE = '"+ empCode + "'";
								System.out.println("sql :::: " + sql);
								pstmt= conn.prepareStatement(sql);
								rs = pstmt.executeQuery();
								if (rs.next())
								{
									cnt = rs.getInt("COUNT");
									if (cnt == 0)
									{
										errCode = "VMEMPORD2";
										errString = getErrorString("emp_code",errCode,loginCode);
										break;
									}
								}
								rs.close(); 
								pstmt.close();
								sql = "SELECT COUNT(*) AS COUNT FROM FORM16_PAYMENT1 WHERE EMP_CODE = ? AND PRD_CODE = ?";
								System.out.println("sql:::"+ sql);
								pstmt = conn.prepareStatement(sql);
								pstmt.setString(1, empCode);
								pstmt.setString(2, prdCode);								
								rs = pstmt.executeQuery();
								if (rs.next())
								{
									cnt = rs.getInt("COUNT");
									if (cnt != 0)
									{
										errCode = "VMPDEM2";
										errString = getErrorString("emp_code",errCode,loginCode);
										break;
									}
								}
							}
							else
							{
								errCode = "VNEMPNULL";
								errString = getErrorString("emp_code",errCode,loginCode);
								break;
							}
						}
						else if (childNodeName.equals("prd_code"))
						{
							prdCode = getColumnValue("prd_code",dom);
							empCode = getColumnValue("emp_code", dom);
							System.out.println("prdCode:::"+ prdCode);
							if (prdCode!=null)
							{
								sql = "SELECT COUNT(*) AS COUNT FROM PERIOD WHERE CODE = '"+ prdCode + "'";
								System.out.println("sql:::"+ sql);
								pstmt = conn.prepareStatement(sql);
								rs = pstmt.executeQuery();
								if (rs.next())
								{
									cnt = rs.getInt("COUNT");
									if (cnt == 0)
									{
										errCode = "VMPRD1";
										errString = getErrorString("prd_code",errCode,loginCode);
										break;
									}
								}
							}
							else 
							{
								errCode = "VPDNULL";
								errString = getErrorString("prd_code",errCode,loginCode);
								break;
							}
						}
						//Changed by Pawan on 5/14/2011 [AD01SUN010] to check blank bank_name.start
						else if (childNodeName.equals("bank_name"))		
						{
							if (childNode.getFirstChild() == null)
							{
								errCode = "INVBANKNAM";
								errString = getErrorString("bank_name",errCode,loginCode);
								break;
							}
						}
						//Changed by Pawan on 5/14/2011 [AD01SUN010] to check blank bank_name.end
						else if (childNodeName.equals("date__payment"))
						{
							datePayment = getColumnValue("date__payment",dom);
							if (datePayment != null)
							{
								SimpleDateFormat sdf = new SimpleDateFormat(genericUtility.getApplDateFormat());
								siteCode = getColumnValue("site_code", dom); 
								System.out.println("date__payment ::::" + datePayment + " &&& siteCode :::: " + siteCode);
								
								dtPymt = sdf.parse(datePayment);
								System.out.println("dtPymt ::: " + dtPymt);
								errString = nfCheckPeriod("ADM",dtPymt, siteCode);
								System.out.println("errstring from nfcheckperiod :::: " + errString);
							}
						}
					}//END FOR
					break;
			}//END SWITCH
		}//END TRY
		catch(Exception e)
		{
			System.out.println("Exception ::"+e);
			e.printStackTrace();
		}
		finally
		{
			try{conn.close();conn = null;}catch(Exception d){}
		}

		System.out.println("ErrString ::"+ errString);
		return errString;
	}//END OF VALIDATION 



	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 = null;
		try
		{
			System.out.println("xmlString in itemchanged :::: " + xmlString + "\n xmlString1" + xmlString1 + "\n xmlString2" + xmlString2);
			dom = parseString(xmlString); 
			dom1 = parseString(xmlString1); 
			if (xmlString2.trim().length() > 0 )
			{
//				dom2 = parseString("<Root>" + xmlString2+ "</Root>");
				dom2 = parseString(xmlString2);
			}
			valueXmlString = itemChanged(dom,dom1,dom2,objContext,currentColumn,editFlag,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception : [Form16Payment1][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
	{
		Connection conn = null;
		ConnDriver connDriver = new ConnDriver();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = null;
		int currentFormNo = 0;
		StringBuffer valueXmlString = new StringBuffer();
		String columnValue = null;

		NodeList parentNodeList = null;
		Node parentNode = null; 
		Node childNode = null;
		NodeList childNodeList = null;
		String childNodeName = null;
		int ctr = 0;
		String loginSite = null;
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		GenericUtility genericUtility = GenericUtility.getInstance();
		
//		String userId = getValueFromXTRA_PARAMS(xtraParams,"userId");
//		String termId = getValueFromXTRA_PARAMS(xtraParams,"termId");
//		String empCode = getValueFromXTRA_PARAMS(xtraParams,"empCode");
		String siteDescr = null, prdCode = "", periodDescr = "";
		try
		{
			System.out.println("xtra parm is : "+xtraParams);
			System.out.println("currentColumn is : "+currentColumn);

			conn = connDriver.getConnectDB("DriverITM");
			
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			System.out.println("[Form16Payment1EJB] [itemChanged] :currentFormNo ....." +currentFormNo);
			valueXmlString = new StringBuffer("<?xml version=\"1.0\"?><Root><header><editFlag>");
			valueXmlString.append(editFlag).append("</editFlag></header>");
			valueXmlString.append("<Detail1>");	
			System.out.println("Current 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("itm_default"))
					{
						try
						{
							
							loginSite = getValueFromXTRA_PARAMS(xtraParams, "loginSiteCode");
							
							System.out.println("loginSite in ItemChange ::: [" + loginSite + "]");
							if ( loginSite != null )
							{
								sql = "SELECT DESCR FROM SITE WHERE SITE_CODE = '"+ loginSite + "'";
								System.out.println("sql ::: [" + sql + "]");
								pstmt = conn.prepareStatement(sql);
								rs = pstmt.executeQuery();
								while (rs.next())
 								{
									siteDescr = rs.getString(1);
								}
								System.out.println("sitecode ::: " + loginSite + "& siteDescr :::" + siteDescr);
								valueXmlString.append("<site_code>").append("<![CDATA[" + loginSite + "]]>").append("</site_code>\r\n");
								valueXmlString.append("<site_descr>").append("<![CDATA[" + siteDescr + "]]>").append("</site_descr>\r\n");
								}
						}
						catch(Exception e)
						{
							System.out.println("Exception ::"+e);
							e.printStackTrace();
						}
					}
					else if (currentColumn.trim().equals("site_code"))
					{
						loginSite = getColumnValue("site_code", dom);
						try
						{
							System.out.println("loginSite in ItemChange ::: [" + loginSite + "]");
							if (loginSite != null)
							{
								sql = "SELECT DESCR FROM SITE WHERE SITE_CODE = '"+ loginSite + "'";
								System.out.println("sql ::: [" + sql + "]");
								pstmt = conn.prepareStatement(sql);
								rs = pstmt.executeQuery();
								while (rs.next())
 								{
									siteDescr = rs.getString(1);
								}
								System.out.println(" siteDescr :::" + siteDescr);
								valueXmlString.append("<site_descr>").append("<![CDATA[" + siteDescr + "]]>").append("</site_descr>\r\n");
								}
						}
						catch(Exception e)
						{
							System.out.println("Exception ::"+e);
							e.printStackTrace();
						}
					}
					else if (currentColumn.trim().equals("prd_code"))
					{
						prdCode = getColumnValue("prd_code", dom);
						try
						{
							System.out.println("prdCode in ItemChange ::: [" + prdCode + "]");
							if (prdCode != null)
							{
								sql = "SELECT DESCR FROM PERIOD WHERE CODE = '"+ prdCode + "'";
								System.out.println("sql ::: [" + sql + "]");
								pstmt = conn.prepareStatement(sql);
								rs = pstmt.executeQuery();
								while (rs.next())
 								{
									periodDescr = rs.getString(1);
								}
								System.out.println(" prdCodeDescr :::" + periodDescr);
								valueXmlString.append("<period_descr>").append("<![CDATA[" + periodDescr + "]]>").append("</period_descr>\r\n");
								}
						}
						catch(Exception e)
						{
							System.out.println("Exception ::"+e);
							e.printStackTrace();
						}
					}
					else if (currentColumn.trim().equals("emp_code"))
					{
						String empCode = getColumnValue("emp_code", dom);
						String fName = null;
						String lName = null;

						if (empCode != null)
						{
							sql = "SELECT EMP_FNAME, EMP_LNAME FROM EMPLOYEE"
									+ " WHERE EMP_CODE = '" + empCode + "'";
							System.out.println("sql ::: [" + sql + "]");
							pstmt = conn.prepareStatement(sql);
							rs = pstmt.executeQuery();
							while (rs.next())
								{
								fName = rs.getString(1);
								lName = rs.getString(2);
							}
							valueXmlString.append("<emp_fname>").append("<![CDATA[" + fName + "]]>").append("</emp_fname>\r\n");
							valueXmlString.append("<emp_lname>").append("<![CDATA[" + lName + "]]>").append("</emp_lname>\r\n");
						}
					}
					valueXmlString.append("</Detail1>");
					break;
				
				}//END OF SWITCH
				valueXmlString.append("</Root>");				
			}//END OF TRY
		
		catch(Exception e)
		{
			System.out.println("Exception ::"+ e.getMessage());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				if (rs != null)
				{
					rs.close(); rs = null;
					pstmt.close(); pstmt = null;
					conn.close();conn = null;
				}
			}
			catch(Exception e)
			{
				System.out.println("Exception ::"+e);
				e.printStackTrace();
			}			
		}
		System.out.println("valueXmlString :::::" + valueXmlString.toString());
		return valueXmlString.toString();
	}//END OF ITEMCHANGE
}
	