/********************************************************
	Title : ItemDeptEJB
	Date Of Modification : 18/07/07
	Name of Developer : Imteyaz-ul haque

********************************************************/
package ibase.webitm.ejb.mfg;
import java.rmi.RemoteException;
import java.util.*;
import java.sql.*;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import org.w3c.dom.*;
import java.text.NumberFormat;
import java.io.*;
import javax.ejb.SessionBean;
import javax.ejb.CreateException;
import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.*;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.TransIDGenerator;
import ibase.utility.CommonConstants;
import ibase.system.config.*;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3


public class ItemDept extends ValidatorEJB implements ItemDeptLocal,ItemDeptRemote //SessionBean
{
	ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
	java.sql.Connection conn=null;
	GenericUtility genericUtility = GenericUtility.getInstance(); 
	String sql = null;
	Statement stmt = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;

	/*public void ejbCreate() throws RemoteException, CreateException 
	{
		System.out.println("<========= ITEM DEPT_EJB IS IN PROCESS ========>");
	}
	public void ejbRemove()
	{
	}
	public void ejbActivate() 
	{
	}
	public void ejbPassivate() 
	{
	}*/
	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;
		String valueXmlString = "";
		try
		{
			dom = parseString(xmlString); 
			System.out.println("xmlString" + xmlString);
			System.out.println("<========= ItemDeptEJB ItemChanged Called =========>");
			dom = parseString(xmlString1); 
			if (xmlString.trim().length() > 0 )
			{
				dom = parseString(xmlString);
			}
			valueXmlString = itemChanged(dom,objContext,currentColumn,editFlag,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception : [ItemDeptEJB][itemChanged(String,String)] :==>\n"+e.getMessage());
		}
        return valueXmlString; 
	}

	public String itemChanged(Document dom,String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		boolean isFound=false;
		StringBuffer valueXmlString = new StringBuffer();
		NodeList parentNodeList = null;
		NodeList childNodeList = null;
		Node parentNode = null;
		Node childNode = null;
		String childNodeName = null;
	    String columnValue = "";
		int ctr=0;
		String ChgDate = "";
		String ChgUser = "";
		String ChgTerm = "";
		String ItemCode="";
		String ItDescr="";
		String DpDescr="";
		String DeptCode="";			
		int currentFormNo=0;	

		try
		{
			ConnDriver connDriver = new ConnDriver();	
			conn = connDriver.getConnectDB("DriverITM");
			System.out.println("<=======[ItemDeptEJB]connection  is opened =====>");
			ChgDate = getValueFromXTRA_PARAMS(xtraParams, "chg_date");
			ChgUser = getValueFromXTRA_PARAMS(xtraParams, "chg_user");
			ChgTerm = getValueFromXTRA_PARAMS(xtraParams, "chg_term");
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			
			System.out.println("[ItemDeptEJB] [itemChanged] :currentFormNo ====>" +currentFormNo);
			System.out.println("FORM NO ====>"+currentFormNo);	
			valueXmlString = new StringBuffer("<?xml version=\"1.0\"?><Root><header><editFlag>");
			valueXmlString.append(editFlag).append("</editFlag></header>");
	
			try
			  {		   
		   //	SEARCHING THE DOM FOR THE INCOMING COLUMN VALUE START
	
					parentNodeList = dom.getElementsByTagName("Detail");
					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));
					System.out.println("[ItemDeptEJB]Column Value ====>"+columnValue);
					System.out.println("[ItemDeptEJB]currentColumn =====>"+currentColumn);

					valueXmlString.append("<Detail>\r\n");
				
				 if (currentColumn.trim().equals("itm_default"))
					 {
						valueXmlString.append("<chg_date>").append(ChgDate).append("</chg_date>\r\n");
						valueXmlString.append("<chg_user>").append(ChgUser).append("</chg_user>\r\n");
						valueXmlString.append("<chg_term>").append(ChgTerm).append("</chg_term>\r\n");
						System.out.println("Chg Date:FROM Dom:::::["+ChgDate+"]");
						System.out.println("Chg User:FROM Dom:::::["+ChgUser+"]");
						System.out.println("Chg Term:FROM Dom:::::["+ChgTerm+"]");
						
					  }	
				
 				 if(currentColumn.trim().equalsIgnoreCase("item_code"))
					{
						ItemCode = getColumnValue("item_code",dom);
						System.out.println("ITEM_CODE:FROM Dom ======>["+ItemCode+"]");
						sql ="select DESCR from ITEM where item_code= '"+ItemCode+"'";
						System.out.println("sql::::=="  +sql);				
						pstmt = conn.prepareStatement(sql);
						rs = pstmt.executeQuery();
						if(rs.next())
						{
						  ItDescr = rs.getString(1);														 
						}
						rs.close();
						rs = null;
						pstmt.close();
						pstmt = null;
						System.out.println("ItDescr:::["+ItDescr+"]");
					 	valueXmlString.append("<item_descr>").append(ItDescr).append("</item_descr>\r\n");
					}			
				
				if(currentColumn.trim().equalsIgnoreCase("dept_code"))
					{
						DeptCode = getColumnValue("dept_code",dom);
						System.out.println("DEPT_CODE:FROM Dom ======>["+DeptCode+"]");
						sql ="select DESCR from DEPARTMENT where dept_code= '"+DeptCode+"'";
						System.out.println("sql::::=="  +sql);				
						pstmt = conn.prepareStatement(sql);
						rs = pstmt.executeQuery();
						if(rs.next())
						{
						  DpDescr = rs.getString(1);														 
						}
						rs.close();
						rs = null;
						pstmt.close();
						pstmt = null;
						System.out.println("DpDescr =====> ["+DpDescr+"]");
					 	valueXmlString.append("<department_descr>").append(DpDescr).append("</department_descr>\r\n");
					}			
				
					valueXmlString.append("</Detail>");
					valueXmlString.append("</Root>");
			}//end of Inner Try
			
			catch(Exception e)
			  {
				e.printStackTrace();
				System.out.println("Exception ::"+e.getMessage());
				throw new ITMException(e);
		      }
		
		}//END OF OUTER TRY
		
		catch(Exception e)
		{
			e.printStackTrace();
			System.out.println("Exception ::"+e.getMessage());
			throw new ITMException(e);
		}
		
		finally
		{
			try
			{
				if(conn!=null)
				{
					rs.close();
					rs = null;
					stmt.close();
					stmt = null;
					pstmt.close();
					pstmt = null;
					conn.close();					
					conn = null;
					System.out.println("[ItemDeptEJB] CONNECTION IS CLOSED =====>");
				}
			}catch(Exception d){d.printStackTrace();}			
			System.out.println("[ItemDeptEJB] Connection is Closed");
		}
		System.out.println("valueXmlString:::::"+valueXmlString.toString());
		return valueXmlString.toString();

	}//END OF ITEMCHNGE	


 }    // End Of Main


