
/********************************************************
	Title : WorkOrderAmdEJB
	Date  : 21/01/09
	Author: pankaj singh

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

package ibase.webitm.ejb.mfg;

import ibase.system.config.ConnDriver;
import ibase.webitm.ejb.ValidatorEJB;
import ibase.webitm.utility.GenericUtility;
import ibase.utility.E12GenericUtility;
import ibase.webitm.utility.ITMException;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.util.Date;
import java.util.ArrayList;
import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import java.text.SimpleDateFormat;
import java.sql.Timestamp;
import ibase.webitm.ejb.sys.UtilMethods;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3

public class WorkOrderAmd extends ValidatorEJB implements  WorkOrderAmdLocal,  WorkOrderAmdRemote //SessionBean
{
	/*public void ejbCreate() throws RemoteException, CreateException
	{
		System.out.println("<======= WorkOrderAmdEJB 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(" in WorkOrderAmd : wfValData : 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 : WorkOrderAmdEJB : 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;
		String divHead="",tranIdRef = "";
		double minAmount = 0;
		double maxAmount = 0;
		int cnt = 0,cnt1 = 0;
		int currentFormNo=0;
		int childNodeListLength;
		String mfgDateStart = "",expDate = "",planDueDate = "";
		Timestamp mfgDateStartTs = null,mfgDateStartOldTs  = null;
		Timestamp expDateTs = null,expDateOldTs  = null;
		String expDateOld = "";
		String mfgDateStartOld = "";
        ConnDriver connDriver = new ConnDriver();
		String siteCode = "",empCodeAmd = "",startTime = "",confirmed = "",status = "",projCode = "";
		Timestamp planDueDateTs = null,planDueDateOldTs  = null;
		String planDueDateOld = "";
		ArrayList<String> errList = new ArrayList<String>();
		ArrayList<String> errFields = new ArrayList<String>();
		StringBuffer errStringXml = new StringBuffer("<?xml version = \"1.0\"?> \r\n <Root> <Errors>");
		String ordDate="";
		Timestamp ordDateTs = null;
		Timestamp currDate = null;
		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			connDriver = null;
			userId = getValueFromXTRA_PARAMS(xtraParams,"loginCode");
			E12GenericUtility genericUtility = new E12GenericUtility(); //GenericUtility.getInstance();
			currDate =new Timestamp(System.currentTimeMillis());
			SimpleDateFormat sdf = new SimpleDateFormat(genericUtility.getDBDateFormat());
			currDate = java.sql.Timestamp.valueOf(sdf.format(currDate) + " 00:00:00.000");

			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			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);
							siteCode = siteCode == null ? "" : siteCode;
							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 = "VMSITE1";
								errList.add(errCode);
								errFields.add(childNodeName.toLowerCase());
							}
						}// End Of site_code
						else if (childNodeName.equalsIgnoreCase("work_order__ref"))
						{
							tranIdRef = getColumnValue("work_order__ref",dom);
							sql = "Select status from workorder where work_order = ?" ; //'"+tranIdRef+"'";
							//System.out.println("Sql for status................:- " + sql);
							pstmt= conn.prepareStatement(sql);
							pstmt.setString(1,tranIdRef);
							rs = pstmt.executeQuery(); 
							if(rs.next())
							 {
							   status = rs.getString(1) == null ? "" : rs.getString(1);
							 }
							pstmt.close();
							rs.close();
							pstmt = null;
							rs = null;
							System.out.println("status>>>>>>>>>>>>>> " + status);
							if( "X".equalsIgnoreCase(status) ||  "C".equalsIgnoreCase(status))
							{
								errCode = "VTWOSA";
									errList.add(errCode);
									errFields.add(childNodeName.toLowerCase());
							}	
						}// End Of tran_id__ref
						
						else if (childNodeName.equalsIgnoreCase("mfg_date__start"))
						{
							mfgDateStart = getColumnValue("mfg_date__start",dom);
							mfgDateStartOld = getColumnValue("mfg_date__start__o",dom);
							expDate = getColumnValue("exp_date",dom);
							planDueDate = getColumnValue("plan__due_date",dom);
							if( mfgDateStart == null || mfgDateStart.trim().length() == 0 || "null".equals(mfgDateStart))
							{
								mfgDateStart = "";
							}
							if( mfgDateStartOld == null || mfgDateStartOld.trim().length() == 0 || "null".equals(mfgDateStartOld))
							{
								mfgDateStartOld = "";
							}
							if( expDate == null || expDate.trim().length() == 0 || "null".equals(expDate))
							{
								expDate = "";
							}
							if( planDueDate == null || planDueDate.trim().length() == 0 || "null".equals(planDueDate))
							{
								planDueDate = "";
							}
							if( (mfgDateStart == null || mfgDateStart.trim().length() == 0) && (expDate == null || expDate.trim().length() == 0)&&(planDueDate == null || planDueDate.trim().length() == 0))
							{
								errCode = "VTWOVLNL";
								// 23/11/14 manoharan not to give error
								//errList.add(errCode);
								//errFields.add(childNodeName.toLowerCase());
							}// end if any value is null
							else
							{
								System.out.println("mfgDateStart before [" + mfgDateStart + "]");
								//added by priyanka on 24/11/14
								if(mfgDateStart!=null && mfgDateStart.trim().length()>0)
								{
									mfgDateStart  = genericUtility.getValidDateString(mfgDateStart,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat());
									System.out.println("mfgDateStart after " + mfgDateStart);
									//System.out.println("mfgDateStart + 00:00:00.0 in dbdate format:- " + (mfgDateStart + " 00:00:00.0"));
									mfgDateStartTs = Timestamp.valueOf(mfgDateStart + " 00:00:00.0");
								}
								System.out.println("mfgDateStartTs in Timestamp:- [" + mfgDateStartTs + "]");
								System.out.println("mfgDateStart in Timestamp:- [" + mfgDateStart + "]");
								if(mfgDateStartOld != null && mfgDateStartOld.trim().length() > 0)
								{
									mfgDateStartOld  = genericUtility.getValidDateString(mfgDateStartOld,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat());
									System.out.println("mfgDateStartOld in dbdate format:- " + mfgDateStartOld);
									System.out.println("mfgDateStartOld + 00:00:00.0 in dbdate format:- " + (mfgDateStartOld + " 00:00:00.0"));
									mfgDateStartOldTs = Timestamp.valueOf(mfgDateStartOld + " 00:00:00.0");
									System.out.println("mfgDateStartOldTs in Timestamp:- " + mfgDateStartOldTs);
									//added by priyanka on 28/11/14
									if(mfgDateStartTs!=null && mfgDateStartTs.compareTo(mfgDateStartOldTs) == 0)
									{
										errCode = "VTWOMFNL";
										// 23/11/14 manohar not to give error
										//errList.add(errCode);
										//errFields.add(childNodeName.toLowerCase());
									}
									else if(mfgDateStartTs!=null && mfgDateStartTs.before(mfgDateStartOldTs))
									{
										errCode = "VTWOMFIN";
										errList.add(errCode);
										errFields.add(childNodeName.toLowerCase());
									}
									else if(mfgDateStartTs!=null && currDate.before(mfgDateStartTs))
									{
										errCode = "VTWOMDOF";
										errList.add(errCode);
										errFields.add(childNodeName.toLowerCase());
									}
									
								}else
								{
									// start added by chnadrashekar as per manoj sir instruction ON 05/11/14
									tranIdRef = getColumnValue("work_order__ref", dom);
									sql = "Select  ord_date from workorder where work_order = ?";
									pstmt = conn.prepareStatement(sql);
									pstmt.setString(1, tranIdRef);
									rs = pstmt.executeQuery();
									if (rs.next())
									{
										ordDateTs = rs.getTimestamp("ord_date");
									}
									pstmt.close();
									rs.close();
									pstmt = null;
									rs = null;
									System.out.println("mfgDateStartTs:" + mfgDateStartTs);
									System.out.println("ordDateTs:" + ordDateTs);
								//	//added by priyanka on 24/11/14
									if (mfgDateStartTs!=null && mfgDateStartTs.before(ordDateTs))
									{
										errCode = "VTMFGORDDT";
										errList.add(errCode);
										errFields.add(childNodeName.toLowerCase());
									}
									//added by priyanka on 24/11/14
									else if(mfgDateStartTs!=null && currDate.before(mfgDateStartTs))
									{
										errCode = "VTWOMDOF";
										errList.add(errCode);
										errFields.add(childNodeName.toLowerCase());
									}
								}
							// end added by chnadrashekar as per manoj sir instruction ON 05/11/14	
							}
						}//end mfg_date
						else if (childNodeName.equalsIgnoreCase("exp_date"))
						{
							
							mfgDateStart = getColumnValue("mfg_date__start",dom);
							expDateOld = getColumnValue("exp_date__o",dom);
							expDate = getColumnValue("exp_date",dom);
							planDueDate = getColumnValue("plan__due_date",dom);
							if( expDateOld == null || expDateOld.trim().length() == 0 || "null".equals(expDateOld))
							{
								expDateOld = "";
							}
							if( mfgDateStart == null || mfgDateStart.trim().length() == 0 || "null".equals(mfgDateStart))
							{
								mfgDateStart = "";
							}
							if( expDate == null || expDate.trim().length() == 0 || "null".equals(expDate))
							{
								expDate = "";
							}
							if( planDueDate == null || planDueDate.trim().length() == 0 || "null".equals(planDueDate))
							{
								planDueDate = "";
							}
							
							if(  expDate == null || expDate.trim().length() == 0 )
							{
								
							}// end if expDate is null
							else
							{
								expDate  = genericUtility.getValidDateString(expDate,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat());
								System.out.println("expDate in dbdate format:- " + expDate);
								System.out.println("expDate + 00:00:00.0 in dbdate format:- " + (expDate + " 00:00:00.0"));
								expDateTs = Timestamp.valueOf(expDate + " 00:00:00.0");
								if(mfgDateStart != null && mfgDateStart.trim().length() > 0)
								{
									mfgDateStart  = genericUtility.getValidDateString(mfgDateStart,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat());
									System.out.println("mfgDateStart in dbdate format:- " + mfgDateStart);
									System.out.println("mfgDateStart + 00:00:00.0 in dbdate format:- " + (mfgDateStart + " 00:00:00.0"));
									mfgDateStartTs = Timestamp.valueOf(mfgDateStart + " 00:00:00.0");
									System.out.println("mfgDateStart in Timestamp:- " + mfgDateStart);	
								}
								if(expDateOld != null && expDateOld.trim().length() > 0)
								{
									expDateOld  = genericUtility.getValidDateString(expDateOld,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat());
									System.out.println("expDateOld in dbdate format:- " + expDateOld);
									System.out.println("expDateOld + 00:00:00.0 in dbdate format:- " + (expDateOld + " 00:00:00.0"));
									expDateOldTs = Timestamp.valueOf(expDateOld + " 00:00:00.0");
									System.out.println("mfgDateStartOldTs in Timestamp:- " + expDateOldTs);
									//System.out.println("expDateTs.compareTo(mfgDateStartTs)>>>>>>>>> " + expDateTs.compareTo(mfgDateStartTs));
										//added by priyanka on 24/11/14
									if(mfgDateStartTs!=null && expDateTs.compareTo(mfgDateStartTs) <= 0)
									{

										errCode = "VTWOEXMF";
										errList.add(errCode);
										errFields.add(childNodeName.toLowerCase());
									}
									if(expDateTs.before(expDateOldTs))
									{
										System.out.println("Changed expiry date less the old exp date:");
										errCode = "VTWOEXIN";
										errList.add(errCode);
										errFields.add(childNodeName.toLowerCase());
									}
								}
//								if(expDateTs.compareTo(expDateOldTs) == 0)
//								{
//									errCode = "VTWOVLNL";
//									errString = getErrorString("mfg_date__start",errCode,userId);
//									break;
//								}
								
								if(expDateTs.before(currDate))
								{
									System.out.println("Previous day expdate:");
									errCode = "VTWOMFMFT";
									errList.add(errCode);
									errFields.add(childNodeName.toLowerCase());
								}
							}
						}// end exp date
						else if (childNodeName.equalsIgnoreCase("plan__due_date"))
						{
						
							mfgDateStart = getColumnValue("mfg_date__start",dom);
							planDueDateOld = getColumnValue("plan_due_date_o",dom);
							expDate = getColumnValue("exp_date",dom);
							planDueDate = getColumnValue("plan__due_date",dom);
							if( mfgDateStart == null || mfgDateStart.trim().length() == 0 || "null".equals(mfgDateStart))
							{
								mfgDateStart = "";
							}
							if( planDueDateOld == null || planDueDateOld.trim().length() == 0 || "null".equals(planDueDateOld))
							{
								planDueDateOld = "";
							}
							if( expDate == null || expDate.trim().length() == 0 || "null".equals(expDate))
							{
								expDate = "";
							}
							if( planDueDate == null || planDueDate.trim().length() == 0 || "null".equals(planDueDate))
							{
								planDueDate = "";
							}

							if(  planDueDate == null || planDueDate.trim().length() == 0)
							{
								
							}// end if planDueDate is null
							else
							{
								planDueDate  = genericUtility.getValidDateString(planDueDate,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat());
								System.out.println("planDueDate in dbdate format:- " + planDueDate);
								System.out.println("planDueDate + 00:00:00.0 in dbdate format:- " + (planDueDate + " 00:00:00.0"));
								planDueDateTs = Timestamp.valueOf(planDueDate + " 00:00:00.0");
								System.out.println("planDueDateTs in Timestamp:- " + planDueDateTs);
								System.out.println("planDueDateOld:- " + planDueDateOld);
								if(planDueDateOld != null && planDueDateOld.trim().length() > 0)
								{
									planDueDateOld  = genericUtility.getValidDateString(planDueDateOld,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat());
									System.out.println("planDueDateOld in dbdate format:- " + planDueDateOld);
									System.out.println("planDueDateOld + 00:00:00.0 in dbdate format:- " + (planDueDateOld + " 00:00:00.0"));
									planDueDateOldTs = Timestamp.valueOf(planDueDateOld + " 00:00:00.0");
									System.out.println("planDueDateOldTs in Timestamp:- " + planDueDateOldTs);
									if(planDueDateTs.compareTo(planDueDateOldTs) == 0)
									{
										errCode = "VTWOPLNL";
										//errList.add(errCode);
										//errFields.add(childNodeName.toLowerCase());
									}
									if(planDueDateTs.before(planDueDateOldTs))
									{
										errCode = "VTWOPLIN";
										errList.add(errCode);
										errFields.add(childNodeName.toLowerCase());
									}
								}
								if(planDueDateTs.before(currDate))
								{
									System.out.println("Previous day plan date:");
									errCode = "VTWOMFPLT";
									errList.add(errCode);
									errFields.add(childNodeName.toLowerCase());
								}
							}//plan__due_date not null
						}// end plan__due_date
						// end proj_time
					}//end for	
			} //END switch
			int errListSize = errList.size();
			String errFldName = null, errorType = null;
			System.out.println("errListSize ..........[" + errListSize + "]");
			if(errList != null && errListSize > 0)
			{
				for(cnt = 0; cnt < errListSize; cnt ++)
				{
					errCode = errList.get(cnt);
					errFldName = errFields.get(cnt);
					System.out.println("errCode ..........[" + errCode + "]");
					errString = getErrorString(errFldName, errCode, userId);
					errorType = errorType(conn , errCode);
					if(errString.length() > 0)
					{
						String bifurErrString = errString.substring(errString.indexOf("<Errors>") + 8, errString.indexOf("<trace>"));
						bifurErrString = bifurErrString + errString.substring(errString.indexOf("</trace>") + 8, errString.indexOf("</Errors>"));
						errStringXml.append(bifurErrString);
						errString = "";
					}
					if(errorType.equalsIgnoreCase("E"))
					{
						break;
					}
				}
				errList.clear();
				errList = null;
				errFields.clear();
				errFields = null;
				errStringXml.append("</Errors> </Root> \r\n");
			}
			else
			{
				errStringXml = new StringBuffer("");
			}
		}//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("[WorkOrderAmdEJB] Connection is Closed");
		}
		errString = errStringXml.toString();
		System.out.println("ErrString ::"+errString);
		return errString;
	}//END OF VALIDATION
	private String errorType(Connection conn, String errorCode) throws ITMException
	{
		String msgType = "";
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try
		{
			String sql = "SELECT MSG_TYPE FROM MESSAGES WHERE MSG_NO = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, errorCode);
			rs = pstmt.executeQuery();
			if (rs.next())
			{
				msgType = rs.getString("MSG_TYPE");
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;
		} catch (Exception ex)
		{
			ex.printStackTrace();
			throw new ITMException(ex);
		} finally
		{
			try
			{
				if (rs != null)
				{
					rs.close();
					rs = null;
				}
				if (pstmt != null)
				{
					pstmt.close();
					pstmt = null;
				}
			} catch (Exception e)
			{
				e.printStackTrace();
				throw new ITMException(e);
			}
		}
		return msgType;
	}

	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 : [WorkOrderAmdEJB][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;
		int  ShortId = 0;
		String salesPers = "" ,itemSer = "",descr = "", empCodeAprv = "",empCodeAmd = "",tranIdRef = "";
		String fName = "",lName = "",mName = "";
		String sql = "",sql1 = "",startTime = "",noOfHour = "",endTime = "";
		double noOfHourD = 0.0;
		int noOfHr =0,noOfMin = 0;
		String trackShelfLife = "",mfgDateStartStr = "",expDateStr = "",planDueDateStr = "";
		int shelfLife = 0;
		Timestamp mfgDateStart = null,expDate = null,planDueDate = null;
		String noOfHourStr = "",startTimeStr = "",endTimeStr = "";
		Timestamp startTs = null,endTs = null;
		String loginSite = "", siteCode = "",status= "",siteDescr = "";
		Calendar calender = Calendar.getInstance();
		ConnDriver connDriver = new ConnDriver();
		Timestamp startTimeTs = null,endTimeTs = null,statusDateTs = null;
		GenericUtility genericUtility = GenericUtility.getInstance();
		String statusDate = "",projCode="",workCntr = "",workCntrDescr = "";
		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			connDriver=null;
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			System.out.println("Itemchange called for WorderAmd>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			System.out.println("FORM NO:::"+currentFormNo);
			System.out.println("\n Extra param........"+xtraParams);
			valueXmlString = new StringBuffer("<?xml version=\"1.0\"?><Root><header><editFlag>");
			valueXmlString.append(editFlag).append("</editFlag></header>");
			valueXmlString.append("<Detail>");

			switch(currentFormNo)
			{
				case 1 :
					parentNodeList = dom.getElementsByTagName("Detail1");
					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));	
					
					 if(currentColumn.trim().equalsIgnoreCase("itm_default"))
					{
						loginSite = getValueFromXTRA_PARAMS(xtraParams, "loginSiteCode");
						empCodeAmd = getValueFromXTRA_PARAMS(xtraParams, "loginEmpCode");
						sql = "select descr from site where site_code = '"+loginSite+"'";
						pstmt= conn.prepareStatement(sql);
						rs = pstmt.executeQuery();
						if(rs.next())
						 {
						   descr = rs.getString(1) == null ? "" : rs.getString(1);
						 }
						pstmt.close();
						rs.close();
						pstmt = null;
						rs = null;
						String s = (new SimpleDateFormat(genericUtility.getApplDateFormat())).format(getCurrdateAppFormat()).toString();
						valueXmlString.append("<site_code>").append("<![CDATA["+loginSite.trim()+"]]>").append("</site_code>");
						valueXmlString.append("<tran_date>").append("<![CDATA["+s+"]]>").append("</tran_date>");
						valueXmlString.append("<site_descr>").append("<![CDATA["+descr.trim()+"]]>").append("</site_descr>");
						sql = "select emp_fname,emp_mname,emp_lname from employee where emp_code = '" + empCodeAmd.toUpperCase() + "'";
						pstmt= conn.prepareStatement(sql);
						rs = pstmt.executeQuery();
						if(rs.next())
						 {
						   fName = rs.getString("emp_fname") == null ? "" : rs.getString("emp_fname");
						   mName = rs.getString("emp_mname") == null ? "" : rs.getString("emp_mname");
						   lName = rs.getString("emp_lname") == null ? "" : rs.getString("emp_lname");
						 }
						pstmt.close();
						rs.close();
						pstmt = null;
						rs = null;
						valueXmlString.append("<emp_code__amd protect =\"1\">").append("<![CDATA["+empCodeAmd.toUpperCase().trim()+"]]>").append("</emp_code__amd>");
						valueXmlString.append("<emp_fname_1>").append("<![CDATA["+fName.trim()+"]]>").append("</emp_fname_1>");
						valueXmlString.append("<emp_mname_1>").append("<![CDATA["+mName.trim()+"]]>").append("</emp_mname_1>");
						valueXmlString.append("<emp_lname_1>").append("<![CDATA["+lName.trim()+"]]>").append("</emp_lname_1>");
					}
					else if(currentColumn.trim().equalsIgnoreCase("itm_defaultedit"))
					{
						tranIdRef = getColumnValue("work_order__ref",dom);
						expDateStr = getColumnValue("exp_date",dom);
						sql =" select track_shelf_life,shelf_life "
									+ " from item "
									+ " where item_code = "
									+ " (Select item_code from "
									+ " workorder where tran_id = '"+tranIdRef +"')" ;
						System.out.println("exp date qry>>>>>> " +sql);
						pstmt= conn.prepareStatement(sql);
						rs = pstmt.executeQuery();
						if(rs.next()) 
						 {
						   trackShelfLife = rs.getString(1);
						   shelfLife = rs.getInt(2);
						 }
						pstmt.close();
						rs.close();
						pstmt = null;
						rs = null;
						System.out.println("trackShelfLife>>>>> " +trackShelfLife + " shelfLife>>>>>>>> " + shelfLife);
						if(trackShelfLife.equalsIgnoreCase("Y"))
						{
							valueXmlString.append("<exp_date protect =\"1\">").append("<![CDATA["+expDateStr.trim()+"]]>").append("</exp_date>");
						}
						else
						{
							valueXmlString.append("<exp_date protect =\"0\">").append("<![CDATA["+expDateStr.trim()+"]]>").append("</exp_date>");
						}
					}
					else if(currentColumn.trim().equalsIgnoreCase("site_code"))
					{
						siteCode = getColumnValue("site_code",dom);
						sql = "select descr from site where site_code = '"+siteCode.toUpperCase()+"'";
						pstmt= conn.prepareStatement(sql);
						rs = pstmt.executeQuery();
						if(rs.next())
						 {
						   descr = rs.getString(1) == null ? "" : rs.getString(1);
						 }
						pstmt.close();
						rs.close();
						pstmt = null;
						rs = null;
						valueXmlString.append("<site_descr>").append("<![CDATA["+descr.trim()+"]]>").append("</site_descr>");
						valueXmlString.append("<site_code>").append("<![CDATA["+siteCode.toUpperCase().trim()+"]]>").append("</site_code>");
					}
					else if(currentColumn.trim().equalsIgnoreCase("work_order__ref"))
					{
						
						tranIdRef = getColumnValue("work_order__ref",dom);
						sql = " select mfg_date__start,exp_date,plan_due_date " 
								+ " from workorder where  tran_id = '"+tranIdRef +"'";
						System.out.println("values from workorder qry>>>>>> " +sql);
						pstmt= conn.prepareStatement(sql);
						rs = pstmt.executeQuery();
						if(rs.next()) 
						 {
							mfgDateStart = rs.getTimestamp(1);
							expDate = rs.getTimestamp(2);
							planDueDate = rs.getTimestamp(3);
						 }
						pstmt.close();
						rs.close();
						pstmt = null;
						rs = null;
						if(mfgDateStart != null )
						{
							mfgDateStartStr  = genericUtility.getValidDateString(mfgDateStart.toString(),genericUtility.getDBDateFormat(),genericUtility.getApplDateFormat());
						}
						if(expDate != null )
						{
							expDateStr  = genericUtility.getValidDateString(expDate.toString(),genericUtility.getDBDateFormat(),genericUtility.getApplDateFormat());
						}
						if(planDueDate != null)
						{
							planDueDateStr  = genericUtility.getValidDateString(planDueDate.toString(),genericUtility.getDBDateFormat(),genericUtility.getApplDateFormat());
						}
						valueXmlString.append("<mfg_date__start__o>").append("<![CDATA["+mfgDateStartStr.trim()+"]]>").append("</mfg_date__start__o>");
						valueXmlString.append("<exp_date__o>").append("<![CDATA["+expDateStr.trim()+"]]>").append("</exp_date__o>");
						valueXmlString.append("<plan_due_date_o>").append("<![CDATA["+planDueDateStr.trim()+"]]>").append("</plan_due_date_o>");
//						sql =" select track_shelf_life,shelf_life "
//									+ " from item "
//									+ " where item_code = "
//									+ " (Select item_code from "
//									+ " workorder where tran_id = '"+tranIdRef +"')" ;
//						System.out.println("exp date qry>>>>>> " +sql);
//						pstmt= conn.prepareStatement(sql);
//						rs = pstmt.executeQuery();
//						if(rs.next()) 
//						 {
//						   trackShelfLife = rs.getString(1);
//						   shelfLife = rs.getInt(2);
//						 }
//						pstmt.close();
//						rs.close();
//						pstmt = null;
//						rs = null;
//						System.out.println("trackShelfLife>>>>> " +trackShelfLife + " shelfLife>>>>>>>> " + shelfLife);
//						if(trackShelfLife.equalsIgnoreCase("Y"))
//						{
//							calender.setTimeInMillis(mfgDateStart.getTime());
//							calender.add(Calendar.MONTH,shelfLife);
//							System.out.println("exp date old>>>>>> " + expDateStr );
//							System.out.println("exp date calculated>>>>>> " + calender.getTime() );
//							sql = "select last_day(?)  from dual";
//							pstmt= conn.prepareStatement(sql);
//							pstmt.setTimestamp(1,new Timestamp(calender.getTime().getTime()));
//							rs = pstmt.executeQuery();
//							if(rs.next()) 
//							 {
//							   expDate = rs.getTimestamp(1);
//							 }
//							System.out.println("last exp date calculated>>>>>> " + expDate + " expDateStr>> " +expDateStr );
//							pstmt.close();
//							rs.close();
//							pstmt = null;
//							rs = null;
//							expDateStr  = genericUtility.getValidDateString(expDate.toString(),genericUtility.getDBDateFormat(),genericUtility.getApplDateFormat());
//							valueXmlString.append("<exp_date>").append("<![CDATA["+expDateStr.trim()+"]]>").append("</exp_date>");
//						}
//						else
//						{
//							valueXmlString.append("<exp_date>").append("").append("</exp_date>");
//						}
					}
					else if(currentColumn.trim().equalsIgnoreCase("emp_code__aprv"))
					{
						empCodeAprv = getColumnValue("mfg_date__start",dom);
						System.out.println("empCodeAprv:- " + empCodeAprv);
						sql = "select emp_fname,emp_mname,emp_lname from employee where emp_code = '" + empCodeAprv.toUpperCase() + "'";
						pstmt= conn.prepareStatement(sql);
						rs = pstmt.executeQuery();
						if(rs.next())
						 {
						   fName = rs.getString("emp_fname") == null ? "" : rs.getString("emp_fname");
						   mName = rs.getString("emp_mname") == null ? "" : rs.getString("emp_mname");
						   lName = rs.getString("emp_lname") == null ? "" : rs.getString("emp_lname");
						 }
						pstmt.close();
						rs.close();
						pstmt = null;
						rs = null;
						valueXmlString.append("<emp_fname>").append("<![CDATA["+fName.trim()+"]]>").append("</emp_fname>");
						valueXmlString.append("<emp_fname>").append("<![CDATA["+mName.trim()+"]]>").append("</emp_fname>");
						valueXmlString.append("<emp_fname>").append("<![CDATA["+lName.trim()+"]]>").append("</emp_fname>");
						System.out.println("valueXmlString for emp_code aprv:- " + valueXmlString);
					}
					else if(currentColumn.trim().equalsIgnoreCase("mfg_date__start"))
					{
						mfgDateStartStr = getColumnValue("mfg_date__start",dom);
						System.out.println("mfgDateStartStr:- " + mfgDateStartStr);
						if(mfgDateStartStr == null || mfgDateStartStr.trim().length() == 0)
						{
							tranIdRef = getColumnValue("work_order__ref",dom);
							sql =" select track_shelf_life,shelf_life "
										+ " from item "
										+ " where item_code = "
										+ " (Select item_code from "
										+ " workorder where tran_id = '"+tranIdRef +"')" ;
							System.out.println("exp date qry>>>>>> " +sql);
							pstmt= conn.prepareStatement(sql);
							rs = pstmt.executeQuery();
							if(rs.next()) 
							 {
							   trackShelfLife = rs.getString(1);
							   shelfLife = rs.getInt(2);
							 }
							pstmt.close();
							rs.close();
							pstmt = null;
							rs = null;
							System.out.println("trackShelfLife>>>>> " +trackShelfLife + " shelfLife>>>>>>>> " + shelfLife);
							if(trackShelfLife.equalsIgnoreCase("Y"))
							{
								valueXmlString.append("<exp_date protect =\"1\">").append("<![CDATA[]]>").append("</exp_date>");
							}
							else
							{
								valueXmlString.append("<exp_date protect =\"0\">").append("<![CDATA[]]>").append("</exp_date>");
							}
							}
						else
						{
							mfgDateStartStr  = genericUtility.getValidDateString(mfgDateStartStr,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat());
							System.out.println("mfgDateStartStr in dbdate format:- " + mfgDateStartStr);
							System.out.println("mfgDateStartStr + 00:00:00.0 in dbdate format:- " + (mfgDateStartStr + " 00:00:00.0"));
							mfgDateStart = Timestamp.valueOf(mfgDateStartStr + " 00:00:00.0");
							System.out.println("mfgDateStart in Timestamp:- " + mfgDateStart);
							tranIdRef = getColumnValue("work_order__ref",dom);
							sql =" select track_shelf_life,shelf_life "
										+ " from item "
										+ " where item_code = "
										+ " (Select item_code from "
										+ " workorder where tran_id = '"+tranIdRef +"')" ;
							System.out.println("exp date qry>>>>>> " +sql);
							pstmt= conn.prepareStatement(sql);
							rs = pstmt.executeQuery();
							if(rs.next()) 
							 {
							   trackShelfLife = rs.getString(1);
							   shelfLife = rs.getInt(2);
							 }
							pstmt.close();
							rs.close();
							pstmt = null;
							rs = null;
							System.out.println("trackShelfLife>>>>> " +trackShelfLife + " shelfLife>>>>>>>> " + shelfLife);
							if(trackShelfLife.equalsIgnoreCase("Y"))
							{
								calender.setTimeInMillis(mfgDateStart.getTime());
								calender.add(Calendar.MONTH, (shelfLife - 1));
								System.out.println("exp date old>>>>>> " + expDateStr );
								System.out.println("exp date calculated>>>>>> " + calender.getTime() );

								sql = "select last_day(?)  from dual";
								pstmt= conn.prepareStatement(sql);
								pstmt.setTimestamp(1,new Timestamp(calender.getTime().getTime()));
								rs = pstmt.executeQuery();
								if(rs.next()) 
								 {
								   expDate = rs.getTimestamp(1);
								 }
								System.out.println("last exp date calculated>>>>>> " + expDate + " expDateStr>> " +expDateStr );
								pstmt.close();
								rs.close();
								pstmt = null;
								rs = null;
								expDateStr  = genericUtility.getValidDateString(expDate.toString(),genericUtility.getDBDateFormat(),genericUtility.getApplDateFormat());
								valueXmlString.append("<exp_date protect =\"1\">").append("<![CDATA["+expDateStr.trim()+"]]>").append("</exp_date>");
							}
							else
							{
								valueXmlString.append("<exp_date protect =\"0\">").append("").append("</exp_date>");
							}
						}
					}
					valueXmlString.append("</Detail>");
				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("[WorkOrderAmdEJB] CONNECTION is CLOSED");
		}
		System.out.println("valueXmlString:::::"+valueXmlString.toString());
		return valueXmlString.toString();
	 }//END OF ITEMCHANGE
	  private Timestamp getCurrdateAppFormat()
    {
        String s = "";	
		 Timestamp timestamp = null;	
        GenericUtility genericUtility = GenericUtility.getInstance();
        try
        {
            java.util.Date date = null;
             timestamp = new Timestamp(System.currentTimeMillis());
            System.out.println(genericUtility.getDBDateFormat());
            SimpleDateFormat simpledateformat = new SimpleDateFormat(genericUtility.getDBDateFormat());
            date = simpledateformat.parse(timestamp.toString());
            timestamp = Timestamp.valueOf(simpledateformat.format(date).toString() + " 00:00:00.0");
           // s = (new SimpleDateFormat(genericUtility.getApplDateFormat())).format(timestamp).toString();
        }
        catch(Exception exception)
        {
            System.out.println("Exception in getCurrdateAppFormat " + exception.getMessage());
        }
        return timestamp;
    }
 }// END OF MAIN CLASS