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.xml.parsers.*;
import javax.ejb.*;

import ibase.webitm.utility.*;
import ibase.system.config.*;
import ibase.webitm.ejb.*;
import ibase.webitm.utility.TransIDGenerator;//TID
import ibase.utility.CommonConstants;//TID
import javax.ejb.Stateless; // added for 3

@Stateless // added for 3
public class InvoiceDownloadIc extends ValidatorEJB implements InvoiceDownloadIcLocal, InvoiceDownloadIcRemote
{
	java.sql.Connection conn=null; 
	GenericUtility genericUtility = GenericUtility.getInstance(); 
	ITMDBAccessEJB itmDBAccess = new ITMDBAccessEJB(); 
	java.sql.PreparedStatement pstmt=null;
	java.sql.ResultSet rs = null;
	String sql = null;

	/*public void Create() throws RemoteException, CreateException 
	{
		System.out.println("InvoiceDownloadIc is in Process.ejb..ejb..ejb..ejb..ejb.");
	}
	public void Remove()
	{
	}
	public void Activate() 
	{
	}
	public void Passivate() 
	{
	}*/
	

	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
		{
			System.out.println("@@@@@..... itemChanged  called......");
			dom = parseString(xmlString); 
			System.out.println("xmlString["+ xmlString+"]");
			dom1 = parseString(xmlString1); 
			if (xmlString2.trim().length() > 0 )
			{
				dom2 = parseString(xmlString2);
			}
			valueXmlString = itemChanged(dom,dom1,dom2,objContext,currentColumn,editFlag,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception : [InvoiceDownloadIc][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
	{
		StringBuffer valueXmlString = new StringBuffer();
		//String columnValue = "";
		String loginSite = "",invoiceIdFrom="",invoiceIdTo="",invoiceDateFromStr="",invoiceDateToStr="";
		Timestamp invoiceDateFrom = null,invoiceDateTo = null;
		
		Connection conn = null;
		ConnDriver connDriver = new ConnDriver();
		int currentFormNo = 0;
	
		String columnValue = null;
		NodeList parentNodeList = null;
		Node parentNode = null; 
		Node childNode = null;
		NodeList childNodeList = null;
		String childNodeName = null;
		int childNodeListLength = 0;
		int ctr = 0;
		loginSite = getValueFromXTRA_PARAMS(xtraParams,"loginSiteCode");
		valueXmlString = new StringBuffer("<?xml version=\"1.0\"?><Root><header><editFlag>");
		valueXmlString.append(editFlag).append("</editFlag></header>");
		valueXmlString.append("<Detail1>");	
		
		System.out.println("@@@@@ itemChanged  called......");
			
		try
		{
			
			//////////////
			
			conn = connDriver.getConnectDB("DriverITM");
			connDriver = null;
			if(objContext != null && objContext.length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			
			loginSite = getValueFromXTRA_PARAMS(xtraParams, "loginSiteCode");
			
			System.out.println("[invoicedownloadICEJB] [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:
					parentNodeList = dom.getElementsByTagName("Detail1");
					parentNode = parentNodeList.item(0);
					childNodeList = parentNode.getChildNodes();
					ctr = 0;
					childNodeListLength = childNodeList.getLength();
					do
					{
						childNode = childNodeList.item(ctr);
						childNodeName = childNode.getNodeName();
						if(childNodeName.equals(currentColumn))
						{
							if (childNode.getFirstChild() != null)
							{
								columnValue=childNode.getFirstChild().getNodeValue();
							}
						}
						ctr++;
					}while(ctr < childNodeListLength  && !childNodeName.equals(currentColumn));
					System.out.println("[" + currentColumn + "] ==> '" + columnValue + "'");
					if (currentColumn.equals( "itm_default" ))
					{

						System.out.println("@@@@@ itm_default called......");
						valueXmlString.append("<site_code_from>").append("00").append("</site_code_from>");
						valueXmlString.append("<site_code_to>").append("ZZ").append("</site_code_to>");
						valueXmlString.append("<cust_code_from>").append("00").append("</cust_code_from>");
						valueXmlString.append("<cust_code_to>").append("ZZ").append("</cust_code_to>");
						valueXmlString.append("<invoice_id_from>").append("00").append("</invoice_id_from>");
						valueXmlString.append("<invoice_id_to>").append("ZZ").append("</invoice_id_to>");
						valueXmlString.append("<invoice_date_from>").append(getCurrdateAppFormat()).append("</invoice_date_from>");
						valueXmlString.append("<invoice_date_to>").append(getCurrdateAppFormat()).append("</invoice_date_to>");
					
					}
					else if(currentColumn.trim().equalsIgnoreCase("invoice_id_from"))
					{
						invoiceIdFrom = getColumnValue("invoice_id_from",dom);
						sql = "select tran_date from invoice where invoice_id = ?";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, invoiceIdFrom);
						rs = pstmt.executeQuery();
					    if (rs.next())
					    {
					    	invoiceDateFrom = rs.getTimestamp("tran_date");
					    }
					    pstmt.close();
					    rs.close();
					    pstmt = null;
					    rs = null;
					    System.out.println("@@@@@ invoiceDateFrom["+invoiceDateFrom+"]");
					    invoiceDateFromStr = new SimpleDateFormat(genericUtility.getApplDateFormat()).format(invoiceDateFrom).toString();
						valueXmlString.append("<invoice_date_from>").append( invoiceDateFromStr ).append("</invoice_date_from>");
					}
					
					else if (currentColumn.trim().equalsIgnoreCase("invoice_id_to"))
					{
					
						invoiceIdTo = getColumnValue("invoice_id_to",dom);
						sql = "select tran_date from invoice where invoice_id = ? ";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, invoiceIdTo);
						rs = pstmt.executeQuery();
						if (rs.next())
						{
							invoiceDateTo = rs.getTimestamp("tran_date");
						}
						pstmt.close();
						rs.close();
						pstmt = null;
						rs = null;
						 System.out.println("@@@@@ invoiceDateTo["+invoiceDateTo+"]");
						 invoiceDateToStr = new SimpleDateFormat(genericUtility.getApplDateFormat()).format(invoiceDateTo).toString();
						 valueXmlString.append("<invoice_date_to>").append( invoiceDateToStr ).append("</invoice_date_to>");
					
					}
					
					valueXmlString.append("</Detail1>");					
					valueXmlString.append("</Root>");				
			}//END OF switch
		}//END OF TRY
	
		catch(Exception e)
		{
			e.printStackTrace();
			System.out.println("Exception ::"+e.getMessage());
		}
		finally
		{
			try{conn.close();conn = null;}catch(Exception d){}
			
		}
		System.out.println("valueXmlString:::::"+valueXmlString.toString());
		return valueXmlString.toString();

	}//END OF ITEMCHNGE

	
	private String getCurrdateAppFormat()
	{
		String currAppdate ="";
		java.sql.Timestamp currDate = null;
		try
		{
			Object date = null;
			currDate =new java.sql.Timestamp(System.currentTimeMillis()) ;
			System.out.println(genericUtility.getDBDateFormat());
			SimpleDateFormat sdf = new SimpleDateFormat(genericUtility.getDBDateFormat());
			date = sdf.parse(currDate.toString());
			currDate =	java.sql.Timestamp.valueOf(sdf.format(date).toString() + " 00:00:00.0");
			currAppdate = new SimpleDateFormat(genericUtility.getApplDateFormat()).format(currDate).toString();
		}
		catch(Exception e)
		{
		System.out.println("Exception in getCurrdateAppFormat"+e.getMessage());
		}
		return (currAppdate);
	}

	

}