/* This file is Cteated it incorporate the Post Save logic from Window (w_lc_det)
 * Gulzar 08/02/07
*/

package ibase.webitm.ejb.dis;

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

import org.w3c.dom.*;
import javax.ejb.*;
import javax.naming.InitialContext;

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


//public class LCDetPosEJB extends ValidatorEJB implements SessionBean
@Stateless // added for ejb3
public class LCDetPos extends ValidatorEJB implements LCDetPosLocal, LCDetPosRemote
{
	/*public void ejbCreate() throws RemoteException, CreateException
	{
	}
	public void ejbRemove()
	{
	}
	public void ejbActivate()
	{
	}
	public void ejbPassivate()
	{
	}*/
	public String postSaveRec() throws RemoteException, ITMException
	{
		return "";
	}
	public String postSaveRec(String xmlString1, String domId, String objContext, String editFlag, String xtraParams, Connection conn) throws RemoteException, ITMException
	{
		System.out.println("LCDetPosEJB Called............!");
		Document dom = null;
		try
		{
			if (xmlString1 != null && xmlString1.trim().length() > 0)
			{
				dom = GenericUtility.getInstance().parseString(xmlString1);
				executepostSaveRec(dom,domId,objContext,editFlag,xtraParams,conn);
			}
		}
		catch(Exception e)
		{
			System.out.println("Exception :LCDetPosEJB :postSaveRec(): " + e.getMessage()+ ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
		return "";
	}
	private String executepostSaveRec(Document dom, String domID, String ObjContext, String editFlag, String xtraParams, Connection conn) throws RemoteException, ITMException
	{
		String saleOrder = "", lineNoSord = "", descr = "", sqlUpd = "";
		String updateStatus = "";
		NodeList hdrDom = null;
		Node currDetail = null;
		Statement stmt = null;
		ResultSet rs = null;
		int  updCnt = 0;

		GenericUtility genericUtility = GenericUtility.getInstance();
		try
		{
			currDetail 	= getCurrentDetailFromDom(dom,domID);
			updateStatus = getCurrentUpdateFlag(currDetail);
			
			System.out.println("updateStatus :: "+updateStatus);

			if (currDetail != null)
			{
				saleOrder	= genericUtility.getColumnValueFromNode("sale_order",currDetail);
				lineNoSord	= genericUtility.getColumnValueFromNode("line_no__sord",currDetail);
				descr		= genericUtility.getColumnValueFromNode("descr",currDetail);

				System.out.println("saleOrder :: "+saleOrder);
				System.out.println("lineNoSord ::"+lineNoSord);
				System.out.println("descr ::"+descr);

				lineNoSord = "   " + lineNoSord;
				lineNoSord = lineNoSord.substring(lineNoSord.length()-3);
				System.out.println("lineNoSord ::"+lineNoSord);
				
				sqlUpd = "UPDATE SORDDET SET ITEM_DESCR = '"+descr+"' WHERE SALE_ORDER = '"+saleOrder+"' AND LINE_NO = '"+lineNoSord+"'";
				stmt = conn.createStatement();
				updCnt = stmt.executeUpdate(sqlUpd);
				if (updCnt > 0)
				{
					System.out.println("Update Into SORDDET Successfully....:: " + updCnt);
					conn.commit();
				}
				else if (updCnt == 0)
				{
					System.out.println("No Updation Occur Into SORDDET ....:: " + updCnt);
				}
				else if (updCnt < 0)
				{
					System.out.println("Update Into SORDDET Failed and Rolling Back The Changes....:: " + updCnt);
					conn.rollback();
				}
			}
		}
		catch (SQLException sqe)
		{
			System.out.println("The SQLException occurs in LCDetPosEJB...... :"+sqe);
			try
			{
				conn.rollback();
			}
			catch (Exception sqe1){}
			sqe.printStackTrace();
			throw new ITMException(sqe);
		}
		catch(Exception e)
		{
			System.out.println("The Exception occurs in LCDetPosEJB........ :"+e);	
			try
			{
				conn.rollback();
			}
			catch (Exception e1){}
			e.printStackTrace();
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				if(stmt != null)
				{
					stmt.close();
					stmt = null;
				}
			}
			catch(Exception e)
			{
				System.err.println("Exception :LCDetPosEJB......... :\n"+e.getMessage());
				e.printStackTrace();
				throw new ITMException(e);
			}
		}
		return "";
	}
	private Node getCurrentDetailFromDom(Document dom,String domId)
	{
		NodeList detailList = null;
		Node currDetail = null,reqDetail = null;
		String currDomId = "";
		int	detailListLength = 0;

		detailList = dom.getElementsByTagName("Detail2");
		detailListLength = detailList.getLength();
		for (int ctr = 0;ctr < detailListLength;ctr++)
		{
			currDetail = detailList.item(ctr);
			currDomId = currDetail.getAttributes().getNamedItem("domID").getNodeValue();
			if (currDomId.equals(domId))
			{
				reqDetail = currDetail;
				break;
			}			
		}
		return reqDetail;
	}

	private String getCurrentUpdateFlag(Node currDetail)
	{
		NodeList currDetailList = null;
		String updateStatus = "",nodeName = "";
		int currDetailListLength = 0;

		currDetailList = currDetail.getChildNodes();
		currDetailListLength = currDetailList.getLength();
		for (int i=0;i< currDetailListLength;i++)
		{
			nodeName = currDetailList.item(i).getNodeName();
			if (nodeName.equalsIgnoreCase("Attribute"))
			{
				updateStatus = currDetailList.item(i).getAttributes().getNamedItem("updateFlag").getNodeValue();
				break;
			}
		}
		return updateStatus;		
	}	
}
