/*
	Window Name : w_payables_opening
*/

package ibase.webitm.ejb.fin;

import java.rmi.RemoteException;
import java.util.*;
import java.util.Date;
import java.text.*;
import java.sql.*;
import org.w3c.dom.*;
import javax.ejb.*;
import ibase.webitm.utility.ITMException;
//import ibase.webitm.utility.*;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.ejb.*;
import ibase.system.config.*;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3
public class AutoPmtGen extends ValidatorEJB implements AutoPmtGenLocal,AutoPmtGenRemote //SessionBean
{
	GenericUtility genericUtility = GenericUtility.getInstance();
	ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
	
	/*public void ejbCreate() throws RemoteException, CreateException 
	{   
		System.out.println("Entering into AutoPmtGenEJB.............");
	}
	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 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 = "";
		try
		{
			dom = parseString(xmlString); //returns the DOM Object for the passed XML Stirng
			System.out.println("xmlString :" + xmlString);
			dom1 = parseString(xmlString1); //returns the DOM Object for the passed XML Stirng
			System.out.println("xmlString1 :" + xmlString1);
			
			if (xmlString2.trim().length() > 0 )
			{
				dom2 = parseString("<Root>" + xmlString2+ "</Root>");
			}
			valueXmlString = itemChanged(dom,dom1,dom2,objContext,currentColumn,editFlag,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception : [AutoPmtGenEJB][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
	{
		ConnDriver connDriver = new ConnDriver();
		Connection conn = null;
		Statement stmt = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		ResultSet rs1 = null;
		StringBuffer valueXmlString = new StringBuffer();
		StringBuffer retString = new StringBuffer();
		String errCode = "";
		String sql = "";
		String sundrySql= "";
		String columnValue = "";
		String loginSite = "";
		String format = "";
		String accPrd = "";
		String childNodeName = "";
		NodeList parentNodeList = null;
		NodeList childNodeList = null;
		Node parentNode = null;
		Node childNode = null;
		int ctr = 0;
		int n = 0;
		int currentFormNo = 0;
				 
		String userId = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"userId");
		System.out.println("userId ::-"+userId);
		loginSite = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginSiteCode");
		System.out.println("loginSiteCode ::-"+loginSite);

		try
		{
			format= getApplDateFormat();
		}
		catch(Exception e)
		{
			System.out.println("Exception :[AutoPmtGenEJB]While Getting Date Format"+e.getMessage());		
		}
		java.util.Date DateX = new java.util.Date();
		java.text.SimpleDateFormat dtf= new SimpleDateFormat(format);
		
		try
		{
			//conn = getConnection(); 
			conn = connDriver.getConnectDB("DriverITM");
			conn.setAutoCommit(false);
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}

			System.out.println("[AutoPmtGenEJB] [itemChanged] :currentFormNo ....." +currentFormNo);
			valueXmlString = new StringBuffer("<?xml version=\"1.0\"?><Root><header><editFlag>");
			valueXmlString.append(editFlag).append("</editFlag></header>");
			valueXmlString.append("<Detail>");	
			System.out.println("FORM NO ::-"+currentFormNo);								
			switch (currentFormNo)
			{
				case 1:
				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"))
				{		
					System.out.println("***************************************************");
					System.out.println("***************************************************");
					System.out.println("----------In Auto Gen Pay--------");	
					System.out.println("***************************************************");
					
					
					String tranType = "";
					String bankCode = "",bankName= "";	
					java.util.Date dt15= new java.util.Date();
					SimpleDateFormat sdf = new SimpleDateFormat(genericUtility.getApplDateFormat());
					System.out.println("sdf"+sdf.toPattern());
					String onlyDate1 = sdf.format(dt15);
					System.out.println("Req Reg Date"+onlyDate1);
					valueXmlString.append("<date__pay>").append(onlyDate1).append("</date__pay>\r\n");
					
					String siteCode = genericUtility.getValueFromXTRA_PARAMS(xtraParams, "loginSiteCode");
					bankCode = genericUtility.getColumnValue("bank_code",dom);
					valueXmlString.append("<site_code__fr>").append(siteCode).append("</site_code__fr>\r\n");
					valueXmlString.append("<exch_rate>").append("1").append("</exch_rate>\r\n");
					try
					{
						sql="select var_value from finparm where var_name='TRAN_TYPE_AUTO_PAY' and prd_code='999999'";
						stmt = conn.createStatement();
						rs = stmt.executeQuery(sql);
						if (rs.next())
						{
							tranType = rs.getString(1);
						}
						rs.close();
						sql = "SELECT BANK_CODE FROM SITE WHERE SITE_CODE ='" +siteCode+ "'";
						pstmt = conn.prepareStatement(sql);							
						rs = pstmt.executeQuery();
						while (rs.next())
						{
							bankCode = rs.getString(1);								
						}			
						rs.close();	
						sql = "SELECT BANK_NAME FROM BANK WHERE BANK_CODE ='"+ bankCode +"'";
						pstmt = conn.prepareStatement(sql);							
						rs = pstmt.executeQuery();
						while (rs.next())
						{
							bankName = rs.getString(1);								
						}									
					
					}
					catch(Exception e)
					{
						System.out.println("Exception :[AutoPmtGen][itemChanged::case 1::itm_default] :==>"+e.getMessage());
						e.printStackTrace();
						throw new ITMException(e);
					}					
					valueXmlString.append("<tran_type>").append(tranType).append("</tran_type>\r\n");					
					valueXmlString.append("<bank_code>").append(bankCode).append("</bank_code>");	
					valueXmlString.append("<bank_name>").append(bankName).append("</bank_name>");	
					
				}//itm_default				
				else if (currentColumn.trim().equals("bank_code"))
				{
									
					if(columnValue != null)
					{
						try
						{  							
							String bankName="";
							String bankCode = genericUtility.getColumnValue("bank_code",dom); 							
							sql = "SELECT BANK_NAME FROM BANK WHERE BANK_CODE ='"+ bankCode +"'";
							pstmt = conn.prepareStatement(sql);							
							rs = pstmt.executeQuery();
							while (rs.next())
							{
								bankName = rs.getString(1);								
							}																
							valueXmlString.append("<bank_name>").append(bankName).append("</bank_name>");	


						}
						catch(Exception e)
						{
							System.out.println("Exception :[AutoPmtGen][itemChanged] :==>"+e.getMessage());
							throw new ITMException(e);
						}
					}
				}				
				valueXmlString.append("</Detail>");				
				break;				
			}	
			valueXmlString.append("</Root>");				
		}
		catch(Exception e)
		{
			System.out.println("Exception ::"+e.getMessage());
		}	
		finally
		{
			try
			{
				conn.close();
				conn = null;
			}
			catch(Exception s){}
		}
		return valueXmlString.toString();
	}
	
	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 = null;
		System.out.println("Entering into  validations ...................");
		try
		{
			dom = parseString(xmlString);
			dom1 = parseString(xmlString1); 
			if (xmlString2.trim().length() > 0 )
			{
				dom2 = parseString("<Root>" + xmlString2+ "</Root>");
			}
			errString = wfValData(dom,dom1,dom2,objContext,editFlag,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception : AutoPmtGenEJB : 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
	{
		ConnDriver connDriver = new ConnDriver();
		Connection conn = null;
		Statement stmt = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String errString = "";
		String errCode = "";
		String sql = "";
		String userId = ""; 
		String loginSite = "";
		String siteCode = "";
		String acctCode = "";
		String cctrCode = "";
		String itemSer = "";
		String currCode = "";
		String childNodeName = ""; 
		Date dateFr = new Date(); //Added by Jaimin 30/07/2007
		Date dateTo = new Date(); //Added by Jaimin 30/07/2007
		NodeList parentNodeList = null;
		NodeList childNodeList = null;
		Node parentNode = null;
		Node childNode = null;
		int ctr,currentFormNo=0;
		int childNodeListLength;
		int cnt;
		SimpleDateFormat simpleDateFormat = null;
		
		try
		{
//			conn = getConnection(); 
			conn = connDriver.getConnectDB("DriverITM");
			conn.setAutoCommit(false);

			stmt = conn.createStatement();
			userId = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"userId");
			System.out.println("userId = "+userId);
			loginSite = genericUtility.getValueFromXTRA_PARAMS(xtraParams, "loginSiteCode");
			System.out.println("xtraParams = "+xtraParams);
			simpleDateFormat = new SimpleDateFormat(genericUtility.getApplDateFormat());
		
			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++)
					{
						childNode = childNodeList.item(ctr);
						childNodeName = childNode.getNodeName();
						System.out.println("childNodeName : " + childNodeName);
						if (childNodeName.equals("site_code__fr"))
						{
							System.out.println("childNodeName 1. : " + childNodeName);
							siteCode = genericUtility.getColumnValue("site_code__fr",dom);
							System.out.println("childNode.getFirstChild() 1. : " + childNode.getFirstChild());
							if (childNode.getFirstChild() == null)
							{
								errCode = "VMSITECD1";
								errString = getErrorString("site_code__fr",errCode,userId);
								break;
							}
							errCode = itmDBAccessEJB.isSiteCode(siteCode, "", conn);
							if(errCode != "")
							{
								errString = getErrorString("site_code__fr",errCode,userId);
								break;
							}
							//Added by Jaimin 30/07/2007
							String payDate= genericUtility.getColumnValue("date__pay",dom);
							Date payDt = new Date(payDate);
							errCode = itmDBAccessEJB.nfCheckPeriod("FIN",payDt,siteCode,conn);
							if(errCode != "")
							{
								errString = getErrorString("site_code__fr",errCode,userId);
								break;
							}
							//end by Jaimin 30/07/2007

						}
						//Added by Jaimin 30/07/2007
						else if (childNodeName.equals("date__pay"))
						{
							if (childNode.getFirstChild() == null)
							{
								errString = getErrorString("date__pay","DATE1",userId); 
							}
						}
						else if (childNodeName.equals("date__fr"))
						{
							if (childNode.getFirstChild() == null)
							{
								errString = getErrorString("date__pay","VMDATENULL",userId); 
							}
						}
						else if (childNodeName.equals("date__to"))
						{
							if (childNode.getFirstChild() == null)
							{
								errString = getErrorString("date__to","VMDATENULL",userId); 
							}
							else 
							{
								dateFr= getDateObject(genericUtility.getColumnValue("date__fr",dom));
								dateTo = getDateObject(genericUtility.getColumnValue("date__to",dom));
								if ( dateFr.after(dateTo))
								{
									errString = getErrorString("date__to","VTLVEDATE2",userId); 
								}
							}
						}
						else if (childNodeName.equals("vouch_no__fr"))
						{
							if (childNode.getFirstChild() == null)
							{
								errString = getErrorString("vouch_no__fr","VMVOUHMT",userId);
							}
						}
						else if (childNodeName.equals("vouch_no__to"))
						{
							if (childNode.getFirstChild() == null)
							{
								errString = getErrorString("vouch_no__to","VMVOUHMT",userId);
							}
						}
						else if (childNodeName.equals("bank_code"))
						{
							if (childNode.getFirstChild() == null)
							{
								errString = getErrorString("bank_code","VMBANKCD1 ",userId);
							}
							else if (childNode.getFirstChild() != null)
							{
								String bankCode = genericUtility.getColumnValue("bank_code",dom);
								siteCode = genericUtility.getColumnValue("site_code__fr",dom);
								errCode = itmDBAccessEJB.isBankCode(siteCode,bankCode, "FIN", conn);
								if (errCode != "" && errCode.trim().length()>0)
								{
									errString = getErrorString("bank_code",errCode,userId);
								}								
							}
						}
						//End by Jaimin 30/07/2007
					}
				break;
				
			}//swich
		}//try
		catch(Exception e)
		{
			System.out.println("Exception ::"+e);
			e.printStackTrace();
		}
		finally
		{
			try
			{
				System.out.println("Closing Connection.....");
				conn.close();
				conn = null;
			}catch(Exception se){}
		}
		System.out.println("ErrString ::"+errString);
		return errString;
	}//wfValData	
	
}//class