
/********************************************************
	Title : LOPEJB
	Date  : 28/08/08
	Author: Manazir Hasan

********************************************************/

package ibase.webitm.ejb.eou;

import ibase.system.config.ConnDriver;
import ibase.webitm.ejb.ValidatorEJB;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;
import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import java.text.SimpleDateFormat;
import java.sql.Timestamp;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.ejb.Stateless; // added for ejb3


@Stateless
//public class LOPEJB extends ValidatorEJB implements SessionBean
public class LOP extends ValidatorEJB implements LOPLocal,LOPRemote
{

	/*
	public void ejbCreate() throws RemoteException, CreateException
	{
		System.out.println("<======= SOrderForm DISPLAY IS IN PROCESS ! \n Welcome!========>");
	}
	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 wfValData(String xmlString, String xmlString1,String xmlString2, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		String errString = null;
		Document dom = null;
		Document dom1 = null;
		Document dom2 = null;
		try
		{
			System.out.println("xmlString:-" + xmlString );
			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 : DistOrderEJB : 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
	{
		NodeList parentNodeList = null;
		NodeList childNodeList = null;
		Node parentNode = null;
		Node childNode = null;
		int ctr=0;
		String childNodeName = null;
		String errString = "";
		String errCode = "",errcode = "";
		Connection conn = null;
		PreparedStatement pstmt = null ;
		ResultSet rs = null , rs1 = null;
		String sql = "" ,sql1 = "";
		String userId = "";
		String effFromStr = null;
		String validUptoStr = null;
		int cnt = 0,cnt1 = 0;
		int currentFormNo=0;
		int childNodeListLength;
        ConnDriver connDriver = new ConnDriver();
		String custCode = null;		
		String siteCode = null;	
		String itemCodeDescr = "";
		String itemDescr = "";
		String itemCode = "" ;	
	try
	{
			System.out.println(".....call validate method ....");
			conn = connDriver.getConnectDB("DriverITM");
			connDriver = null;
			userId = getValueFromXTRA_PARAMS(xtraParams,"loginCode");
			GenericUtility genericUtility = GenericUtility.getInstance();
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			System.out.println("currentFormNo...." + currentFormNo);
			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();					
						if (childNodeName.equalsIgnoreCase("site_code"))
						{
							siteCode = getColumnValue("site_code",dom);
							sql=" Select Count(*)  from site where site_code ='" + siteCode + "' ";
							pstmt= conn.prepareStatement(sql);
							rs = pstmt.executeQuery(); 
							if(rs.next())
							{
							 cnt = rs.getInt(1);
								
							}
							pstmt.close();
							rs.close();
							pstmt = null;
							rs = null;
							if (cnt == 0)
							{
								errCode = "VTSITE1";
								errString = getErrorString("site_code",errCode,userId);
								break;
							}						
						}
						else if (childNodeName.equalsIgnoreCase("item_code"))
						{
							itemCode = getColumnValue("item_code",dom);
							sql=" Select Count(*)  from item where item_code ='" + itemCode + "' ";
							pstmt= conn.prepareStatement(sql);
							rs = pstmt.executeQuery(); 
							if(rs.next())
							{
							 cnt = rs.getInt(1);
								
							}
							pstmt.close();
							rs.close();
							pstmt = null;
							rs = null;
							if (cnt == 0)
							{
								errCode = "VTITEMCD1";
								errString = getErrorString("item_code",errCode,userId);
								break;
							}						
						}	
					}//end for	
					break;
				
			} //END switch
		}//END TRY
		catch(Exception e)
		{
			System.out.println("Exception ::"+e);
			e.printStackTrace();
			errString=e.getMessage();
		}
		finally
		{
			try
			{
				if(conn!=null)
				{
					if(pstmt != null )pstmt.close();
					if(rs != null )rs.close();
					if(rs1 != null )rs.close();
					rs = null;
					rs1 = null;
					pstmt =null;
					conn.close();
				}
				conn = null;
			}catch(Exception d)
				{
				  d.printStackTrace();
				}
			System.out.println("[SOrderFormEJB] Connection is Closed");
		}
		System.out.println("ErrString ::"+errString);
		return errString;
	}//END OF VALIDATION	
	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);
			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 : [LOPEJB][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();
		NodeList parentNodeList = null;
		NodeList childNodeList = null;
		Node parentNode = null;
		Node childNode = null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null ;
		int ctr=0;
		String childNodeName = null;
		String columnValue = null;
		String Col_name = "";
		int currentFormNo = 0 ,cnt = 0;
		String cadreCode="" ;
		String sql = "",descr="",locCode = "",empCode="",locDescr="";
		String siteCode = null;
		String empLname = null,empMname = null,empFname = null;
		String itemDescr = null,itemCode = null;
		ConnDriver connDriver = new ConnDriver();
		Date today = null;
		Timestamp todayDate = null;
		Timestamp todayDateDb = null;
		String todayStr = null;
		String loginSiteCode = null;
		String siteDescr = null;
		String tranId = null;
		String siteCodeShip = null;
		String custCode = null;
		GenericUtility genericUtility = GenericUtility.getInstance();
		int maxScheduleNo = 0;
		String itemCodeDescr = "";
		try
		{
			
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			System.out.println("FORM NO:::"+currentFormNo);
			valueXmlString = new StringBuffer("<?xml version=\"1.0\"?><Root><header><editFlag>");
			valueXmlString.append(editFlag).append("</editFlag></header>");
			System.out.println(".....call item change ....");
			conn = connDriver.getConnectDB("DriverITM");
			connDriver=null;
			
			switch(currentFormNo)
			{			
				
				case 1 :
					parentNodeList = dom.getElementsByTagName("Detail1");
					parentNode = parentNodeList.item(0);
					childNodeList = parentNode.getChildNodes();
					ctr = 0;
					valueXmlString.append("<Detail1>");
					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("xtraParams :::::::::::::::::::::::::::::::::: "+xtraParams);
						loginSiteCode = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginSiteCode");
						System.out.println("loginSiteCode :::::::::::::::::::::::::::::::::: "+loginSiteCode);
						String currAppdate ="";
						java.sql.Timestamp currDate = null;
						currDate = new java.sql.Timestamp(System.currentTimeMillis()) ;
						System.out.println("\n today date----" + currDate);
						currAppdate = new SimpleDateFormat(genericUtility.getApplDateFormat()).format(currDate).toString();
						valueXmlString.append("<tran_date>").append("<![CDATA["+currAppdate+"]]>").append("</tran_date>");
						//todayDateDb =  Timestamp.valueOf(currAppdate +" 00:00:00.00");						
						sql = "Select  descr from site where site_code ='" + loginSiteCode + "'";
						System.out.println("select qry from item.." + sql);
						pstmt= conn.prepareStatement(sql);
						rs = pstmt.executeQuery(); 
						if(rs.next())
						{
						   siteDescr = rs.getString(1) == null ? "" : rs.getString(1);
						}
						System.out.println("siteDescr .." + siteDescr);
						pstmt.close();
						rs.close();
						pstmt = null;
						rs = null;
						valueXmlString.append("<site_code>").append(loginSiteCode).append("</site_code>");
						valueXmlString.append("<site_descr>").append(siteDescr).append("</site_descr>");			
											
					}
					else if(currentColumn.trim().equalsIgnoreCase("site_code"))
					{
						System.out.println("calling the site_code.....");
						siteCode = getColumnValue("site_code",dom);
						if(siteCode != null || siteCode.trim().length() > 0)
						{
							sql = "Select descr from site where site_code ='" + siteCode + "'";
							System.out.println("select qry from item.." + sql);
							pstmt= conn.prepareStatement(sql);
							rs = pstmt.executeQuery(); 
							if(rs.next())
							{
							   siteDescr = rs.getString("descr") == null ? "" : rs.getString("descr");
							}
							System.out.println("siteDescr .." + siteDescr);
							pstmt.close();
							rs.close();
							pstmt = null;
							rs = null;
							valueXmlString.append("<site_code>").append(siteCode).append("</site_code>");
							if(siteDescr == null || siteDescr.trim().length() ==0 )
							{
								valueXmlString.append("<site_descr>").append("").append("</site_descr>");	
							}
							else
							{
								valueXmlString.append("<site_descr>").append(siteDescr).append("</site_descr>");	
							}
						}
						else
						{
							valueXmlString.append("<site_descr>").append("").append("</site_descr>");	
						}
					}
					else if(currentColumn.trim().equalsIgnoreCase("item_code"))
					{
						itemCode = getColumnValue("item_code",dom);
						if(itemCode == null || itemCode.trim().length() == 0 )
						{
							valueXmlString.append("<item_descr>").append("").append("</item_descr>");	
						}
						else
						{
							sql = "Select  descr from item where item_code ='" + itemCode + "'";
							System.out.println("select qry from item.." + sql);
							pstmt= conn.prepareStatement(sql);
							rs = pstmt.executeQuery(); 
							if(rs.next())
							{
							   itemDescr = rs.getString(1) == null ? "" : rs.getString(1);
							}
							System.out.println("itemDescr .." + itemDescr);
							pstmt.close();
							rs.close();
							pstmt = null;
							rs = null;
							valueXmlString.append("<item_code>").append(itemCode).append("</item_code>");
							if (itemDescr == null || itemDescr.trim().length() == 0)
							{
								valueXmlString.append("<item_descr>").append("").append("</item_descr>");	
							}
							else
							{
								valueXmlString.append("<item_descr>").append(itemDescr).append("</item_descr>");	
							}
						}
					}	
					valueXmlString.append("</Detail1>");					
					break;
		   	}//end of switch
			valueXmlString.append("</Root>");
		}//END OF TRY
		catch(Exception e)
		{
			e.printStackTrace();
			System.out.println("Exception ::"+e.getMessage());
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				if(conn!=null)
				{
					if(pstmt != null)pstmt.close();
					if(rs != null)rs.close();
					rs = null;
					pstmt = null;
					conn.close();
				}
				conn = null;
			}catch(Exception d)
			{
			  d.printStackTrace();
			}
			System.out.println("[LOPEJB] CONNECTION is CLOSED");
		}
		System.out.println("valueXmlString:::::"+valueXmlString.toString());
		return valueXmlString.toString();
	 }//END OF ITEMCHANGE
 }// END OF MAIN CLASS