package ibase.webitm.ejb.fin;

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

import org.w3c.dom.*;

import javax.ejb.*;

import ibase.webitm.utility.*;
import ibase.system.config.*;
import ibase.webitm.ejb.*;
import javax.ejb.Stateless; 

@Stateless
public class StockRevaluationAdjIC extends ValidatorEJB implements StockRevaluationAdjICLocal,StockRevaluationAdjICRemote{
	

	GenericUtility genericUtility = GenericUtility.getInstance(); 
	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
	{		
		System.out.println("Xml String : ["+xmlString+"]");
		String errString = null;
		Document dom = null;
		Document dom1 = null;
		Document dom2 = null;
		GenericUtility genericUtility = GenericUtility.getInstance();		
		try
		{
			
			if(xmlString != null && xmlString.trim().length()!=0)
			{
				dom = genericUtility.parseString(xmlString); 
				System.out.println("xmlString d" + xmlString);
			}
			if(xmlString1 != null && xmlString1.trim().length()!=0)
			{
				dom1 = genericUtility.parseString(xmlString1); 
				System.out.println("xmlString1 f" + xmlString1);
			}
			if(xmlString2 != null && xmlString2.trim().length()!=0)
			{
				dom2 = genericUtility.parseString(xmlString2); 
				System.out.println("xmlString2 f" + xmlString2);
			}			

			errString = wfValData(dom,dom1,dom2,objContext,editFlag,xtraParams);
			
		}
		catch(Exception e)
		{
			System.out.println("Exception : PODProcessIC.java : wfValData(String xmlString) : ==>\n"+e.getMessage());
			throw new ITMException(e);
		}
		
		return errString;
	} //end of wfValData 
	
	public String wfValData(Document dom, Document dom1,Document dom2, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		return "";
	}//END OF VALIDATION 

	public String itemChanged(String xmlString, String xmlString1,String xmlString2, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		System.out.println("------------ itemChanged method called process Stock Rev. Adj.-----------------");
		Document dom = null;
		Document dom1 = null;
		Document dom2 = null;
		String valueXmlString = null;
		try
		{
			dom = parseString(xmlString); 
			System.out.println("xmlString" + xmlString);
			dom1 = parseString(xmlString1); 
			System.out.println("xmlString1" + xmlString1);
			if (xmlString2.trim().length() > 0 )
			{
				System.out.println("xmlString2" + xmlString2);
				dom2 = parseString("<Root>" + xmlString2+ "</Root>");
			}
			valueXmlString = itemChanged(dom,dom1,dom2,objContext,currentColumn,editFlag,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception : [EpaymentICEJB][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();		
		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 childNodeListLength = 0;
		int ctr = 0;		
		String  loginSite = "",sql=""; 
		ResultSet rs=null;
		PreparedStatement pstmt=null;
		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			connDriver = null;
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			
			loginSite = getValueFromXTRA_PARAMS(xtraParams, "loginSiteCode");			
			valueXmlString = new StringBuffer("<?xml version=\"1.0\"?><Root><header><editFlag>");
			valueXmlString.append(editFlag).append("</editFlag></header>");										
			switch (currentFormNo)
			{
				case 1:
					valueXmlString.append("<Detail1>");	
					//SEARCHING THE DOM FOR THE INCOMING COLUMN VALUE START
					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().trim();
							}
						}
						ctr++;
					}while(ctr < childNodeListLength  && !childNodeName.equals(currentColumn));
					System.out.println("[" + currentColumn + "] ==> '" + columnValue + "'");
					
					if( currentColumn.trim().equalsIgnoreCase("itm_default"))
					{
						String siteDescr="",autoConf="Y",priceList="";												
						loginSite=loginSite==null ? "" :loginSite.trim();
						System.out.println("Login Site code-=-=-=>>["+loginSite+"]");
						sql="SELECT DESCR,PRICE_LIST FROM SITE WHERE SITE_CODE = ?";
						pstmt=conn.prepareStatement(sql);
						pstmt.setString(1,loginSite );
						rs=pstmt.executeQuery();
						if(rs.next()){
							siteDescr=rs.getString(1);
							priceList=rs.getString(2) ==null ? "" :rs.getString(2);
						}
						if(rs!=null){
							rs.close();
							rs=null;
						}
						if(pstmt!=null){
							pstmt.close();
							pstmt=null;
						}
						
						valueXmlString.append("<auto_confirm>").append("<![CDATA[" + autoConf + "]]>").append("</auto_confirm>");
						valueXmlString.append("<descr>").append("<![CDATA[" + siteDescr + "]]>").append("</descr>");
						valueXmlString.append("<site_code>").append("<![CDATA[" + loginSite + "]]>").append("</site_code>");
						valueXmlString.append("<price_list>").append("<![CDATA[" + priceList + "]]>").append("</price_list>");
						

					}
					if ( currentColumn.trim().equals( "site_code" ) )
					{
						String siteCode="",siteDescr="";
						siteCode = genericUtility.getColumnValue( "site_code", dom );
						siteCode=siteCode==null ? "" :siteCode.trim();
						System.out.println("item chng Site code-=-=-=>>["+siteCode+"]");
						if(siteCode.length() > 0){
							sql="SELECT DESCR FROM SITE WHERE SITE_CODE = ?";
							pstmt=conn.prepareStatement(sql);
							pstmt.setString(1,siteCode );
							rs=pstmt.executeQuery();
							if(rs.next()){
								siteDescr=rs.getString(1);
							}
							if(rs!=null){
								rs.close();
								rs=null;
							}
							if(pstmt!=null){
								pstmt.close();
								pstmt=null;
							}
						    valueXmlString.append("<descr>").append("<![CDATA[" + siteDescr + "]]>").append("</descr>");
						
						}else{
							 valueXmlString.append("<descr>").append("<![CDATA[]]>").append("</descr>");
						}	
						
												
					}			
					
					valueXmlString.append("</Detail1>");						
					break;
			}//END OF switch
			valueXmlString.append("</Root>");
			
		}
		catch(Exception e)
		{
			System.out.println("Exception ::"+ e.getMessage());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				if(conn!=null)
				{					
					conn.close();
				}
				conn = null;
			}
			catch(Exception d)
			{
			  d.printStackTrace();
			}
		}
		
		
		return valueXmlString.toString();
	}//END OF ITEMCHANGE	



}
