
/********************************************************
	Title : CTFORMEJB
	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 CTFORMEJB extends ValidatorEJB implements SessionBean
public class CTFORM extends ValidatorEJB implements CTFORMLocal,CTFORMRemote
{

/*
	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 = "" ;
		String saleOrder="";	
		String lineNoSord = "";
		String validUpto = "";
		String effFrom = "";
		String supplierCode = "",formNo = "";
		Timestamp EffFrom = null ,ValidUpto = null;
		String purcOrder = "",quantityStr = "",cntStr = "";
		double  quantity = 0.0,  quantityDom = 0.0,dlvQty = 0,pendingQty = 0,quantityUsed=0;
	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 = "VMSITE1";
								errString = getErrorString("site_code",errCode,userId);
								break;
							}						
						}
						else if ( childNodeName.equalsIgnoreCase( "valid_upto" ) )
						{
							validUpto = getColumnValue( "valid_upto", dom );
							effFrom = getColumnValue( "eff_from", dom );
							System.out.println(" 'VALID UPTO' FROM DOM  =========>"+validUpto);
							System.out.println(" 'effFrom' FROM DOM  =========>"+effFrom);
							if( effFrom == null || effFrom.trim().length() ==0 || validUpto == null || validUpto.trim().length() ==0 )
							{
								errCode = "VTVLEFN";
								errString = getErrorString("valid_upto",errCode,userId);
								break;
							}
							else
							{						  
								
								SimpleDateFormat sdf = new SimpleDateFormat(genericUtility.getApplDateFormat());
								java.util.Date effFrom1 = sdf.parse(effFrom);
								java.util.Date validUpto1 = sdf.parse(validUpto);
								if(validUpto1.before(effFrom1))
								{	
						            System.out.println(" ====> 'VALID UPTO' DATE  MUST BE GREATER THAN 'EFF FROM' DATE <===== ");
									errCode = "VTDATE6";
									errString = getErrorString("valid_upto",errCode,userId);
						          	break;
								}	
							} // End of (startDate!=null)
						}    // End Of end_
						else if (childNodeName.equalsIgnoreCase("form_no"))
						{
							formNo = genericUtility.getColumnValue("form_no",dom);
							if(formNo == null || formNo.trim().length() == 0)
							{
								errCode = "VTCTFNL";
								errString = getErrorString("form_no",errCode,userId);
								break;
							}
							else
							{
								sql= "select count(*) from ct3form_hdr where form_no ='"+ formNo +"' " ;
								System.out.println("sql= for count form no:- " + sql);
								pstmt= conn.prepareStatement(sql);
								rs = pstmt.executeQuery(); 
								if(rs.next())
								{
								 cnt = rs.getInt(1);
									
								}
								pstmt.close();
								rs.close();
								pstmt = null;
								rs = null;
								if(editFlag.equalsIgnoreCase("E"))
								{
									if (cnt > 1)
									{
										errCode = "VTDUPCF";
										errString = getErrorString("form_no",errCode,userId);
										break;
									}
								}
								else if(editFlag.equalsIgnoreCase("A"))
								{
									if (cnt > 0)
									{
										errCode = "VTDUPCF";
										errString = getErrorString("form_no",errCode,userId);
										break;
									}
								}
							}
						}
					}//end for	
					break;
				case 2 :
					parentNodeList = dom.getElementsByTagName("Detail2");
					parentNode = parentNodeList.item(0);
					childNodeList = parentNode.getChildNodes();
					childNodeListLength = childNodeList.getLength();
					for(ctr = 0; ctr < childNodeListLength; ctr++)
					{
						childNode = childNodeList.item(ctr);
						childNodeName = childNode.getNodeName();
						System.out.println(" validation for :-------------" + childNodeName);
						//  line_no__sord field is not in table
						 if (childNodeName.equalsIgnoreCase("ct3_value"))
						{
							System.out.println("In CT3 validation");
							String ct3Value = "";
							ct3Value = genericUtility.getColumnValue("ct3_value",dom);
							if(ct3Value == null || ct3Value.trim().length() == 0)
							{
								errCode = "VTCTNL";
								errString = getErrorString("ct3_value",errCode,userId);
								break;
							}
							System.out.println("ct3Value.."+ct3Value);
							
						}
						 else if (childNodeName.equalsIgnoreCase("item_code"))
						{
							itemCode = genericUtility.getColumnValue("item_code",dom);
							System.out.println("itemCode.."+itemCode);
							if( itemCode == null || itemCode.trim().length() == 0)
							{
								errCode = "VMITEMCD1 ";
								errString = getErrorString("item_code",errCode,userId);
								break;
							}
							else
							{
								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 item_code
						else if (childNodeName.equalsIgnoreCase("purc_order"))
						{
							System.out.println("validation for purchase order and item");
							purcOrder = genericUtility.getColumnValue("purc_order",dom);
							itemCode = genericUtility.getColumnValue("item_code",dom);
							System.out.println("purcOrder.."+purcOrder);
							System.out.println("itemCode.."+itemCode);
							quantityStr = genericUtility.getColumnValue("quantity",dom);
							if( purcOrder == null || purcOrder.trim().length() == 0)
							{
								errCode = "VTPORD1 ";
								errString = getErrorString("purc_order",errCode,userId);
								break;
							}
							else
							{
								sql= " select count(*)  "
										+ " from porddet "
										+ " where purc_order = '" + purcOrder + "'"
										+ " and item_code = '" + itemCode + "'" 
									+ " and (quantity is not null or quantity <= 0 ) ";
								System.out.println("Sql for quantity validation:- " + sql);
								pstmt= conn.prepareStatement(sql);
								rs = pstmt.executeQuery(); 
								if(rs.next())
								{
									cntStr = rs.getString(1);
								}
								
								pstmt.close();
								rs.close();
								pstmt = null;
								rs = null;
								if( cntStr != null && cntStr.trim().length() > 0)
								{
									cnt = Integer.parseInt(cntStr);
								}
								if ( cntStr == null || cntStr.trim().length() == 0 || cnt == 0 )
								{
									errCode = "VTCTFIT";
									errString = getErrorString("purc_order",errCode,userId);
									break;
								}
								else
								{
									if(quantityStr == null || quantityStr.trim().length() == 0 )
									{
										errCode = "VTINVQTY01 ";
										errString = getErrorString("quantity",errCode,userId);
										break;
									}
									else
									{
										quantityDom = Double.parseDouble(quantityStr);
										if(quantityDom ==  0 )
										{
											errCode = "VTINVQTY01 ";
											errString = getErrorString("quantity",errCode,userId);
											break;
										}
									}
									System.out.println("purcOrder.."+purcOrder);
									quantityDom = qtyTotal( dom2);	
									sql= " select sum(quantity) from ct3form_det"
											+ " where purc_order = '" + purcOrder + "'"
											+ " and item_code = '" + itemCode + "'" ;
									System.out.println("Sql for quantity validation:- " + sql);
									pstmt= conn.prepareStatement(sql);
									rs = pstmt.executeQuery(); 
									if(rs.next())
									{
										quantityUsed = rs.getDouble(1);
									}
									System.out.println("quantity used in ct3 " + quantityUsed);
									pstmt.close();
									rs.close();
									pstmt = null;
									rs = null;
									sql= " select quantity,dlv_qty"
										+ " from porddet "
										+ " where purc_order = '" + purcOrder + "'"
										+ " and item_code = '" + itemCode + "'" 
										+ " and (quantity is not null or quantity <= 0 ) ";
									System.out.println("Sql for quantity validation:- " + sql);
									pstmt= conn.prepareStatement(sql);
									rs = pstmt.executeQuery(); 
									if(rs.next())
									{
										quantity = rs.getDouble(1);
										dlvQty = rs.getDouble(2);
									}
									pendingQty = quantity - dlvQty;
									System.out.println("pendingQty in purcase order " + pendingQty);
									pendingQty = pendingQty - quantityUsed;
									System.out.println("final pendingQty tat can be used " + pendingQty);
									pstmt.close();
									rs.close();
									pstmt = null;
									rs = null;
									System.out.println(" quantity from db:- " + quantity + " quantity from dom" + quantityDom);
									if (( quantityDom > pendingQty) )
									{
										errCode = "VTQTYGT";
										errString = getErrorString("purc_order",errCode,userId);
										break;
									}
								}//emn delse cnt = 0
							}
						}//end purc_order
						/*else if (childNodeName.equalsIgnoreCase("quantity"))
						{
							purcOrder = genericUtility.getColumnValue("purc_order",dom);
							itemCode = genericUtility.getColumnValue("item_code",dom);
							quantityStr = genericUtility.getColumnValue("quantity",dom);
							if(quantityStr == null || quantityStr.trim().length() == 0 )
							{
								errCode = "VTINVQTY01 ";
								errString = getErrorString("quantity",errCode,userId);
								break;
							}
							else
							{
								quantityDom = Double.parseDouble(quantityStr);
								if(quantityDom ==  0 )
								{
									errCode = "VTINVQTY01 ";
									errString = getErrorString("quantity",errCode,userId);
									break;
								}
							}
							System.out.println("purcOrder.."+purcOrder);
							quantityDom = qtyTotal( dom2);	
							sql= " select quantity,dlv_qty"
								+ " from porddet "
								+ " where purc_order = '" + purcOrder + "'"
								+ " and item_code = '" + itemCode + "'" 
								+ " and (quantity is not null or quantity <= 0 ) ";
							System.out.println("Sql for quantity validation:- " + sql);
							pstmt= conn.prepareStatement(sql);
							rs = pstmt.executeQuery(); 
							if(rs.next())
							{
								dlvQty = rs.getDouble(1);
								cntStr = rs.getString(2);
							}
							pendingQty = quantity - dlvQty;
							pstmt.close();
							rs.close();
							pstmt = null;
							rs = null;
							System.out.println(" quantity from db:- " + quantity + " quantity from dom" + quantityDom);
							if (( quantityDom > pendingQty) )
							{
								errCode = "VTQTYGT";
								errString = getErrorString("purc_order",errCode,userId);
								break;
							}
						}//end quantity
						 else if (childNodeName.equalsIgnoreCase("qty_used"))
						{
							String qtyUsedStr = "",quantityStr= "";
							double qtyUsed = 0.0,quantityDom = 0.0;
							qtyUsedStr = genericUtility.getColumnValue("qty_used",dom);
							quantityStr = genericUtility.getColumnValue("quantity",dom);
							if(quantityStr != null && quantityStr.trim().length()> 0 )
							{
								quantityDom = Double.parseDouble(quantityStr);
							}
							if(qtyUsedStr != null && qtyUsedStr.trim().length()> 0 )
							{
								qtyUsed = Double.parseDouble(qtyUsedStr);
							}
							if(qtyUsed > quantityDom)
							{
									errCode = "VTQTYGQT";
									errString = getErrorString("qty_used",errCode,userId);
									break;
							}
						} *///end qty_used
						/* else if (childNodeName.equalsIgnoreCase("supp_code"))
						{
							supplierCode = "";
							supplierCode = genericUtility.getColumnValue("supp_code",dom);
							System.out.println("supplierCode.."+supplierCode);
							if( supplierCode == null || supplierCode.trim().length() == 0)
							{
								errCode = "VESUPP2";
								errString = getErrorString("supp_code",errCode,userId);
								break;
							}
							else
							{
								sql= "select  count(*) from supplier where supp_code= '"+ supplierCode +"' " ;
								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 = "VMSUPP1";
									errString = getErrorString("supp_code",errCode,userId);
									break;
								}
								else
								{
									itemCode = genericUtility.getColumnValue("item_code",dom);
									validUpto = genericUtility.getColumnValue( "valid_upto", dom1 );
									siteCode = genericUtility.getColumnValue( "site_code", dom1 );
									effFrom = genericUtility.getColumnValue( "eff_from", dom1 );
									if(effFrom != null && effFrom.trim().length() >0)
									{
										effFrom = genericUtility.getValidDateString(effFrom,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat());
										EffFrom = java.sql.Timestamp.valueOf(effFrom + " 00:00:00.00");
									}
									if(validUpto != null && effFrom.trim().length() >0)
									{
										validUpto = genericUtility.getValidDateString(validUpto,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat());
										ValidUpto = java.sql.Timestamp.valueOf(validUpto + " 00:00:00.00");
									}
									
									sql = "select count(*)"
												+ " from ct3form_hdr h,ct3form_det d "
												+ " where h.site_code = '" + siteCode + "' "
												+ " and h.eff_from >= ?"
												+ " and h.valid_upto <= ?"
												+ " and d.item_code = '" + itemCode + "'"
												+ " and d.supp_code = '" + supplierCode + "'";
									pstmt= conn.prepareStatement(sql);
									pstmt.setTimestamp(1,EffFrom);
									pstmt.setTimestamp(2,ValidUpto);
									rs = pstmt.executeQuery(); 
									System.out.println("sql:- " + sql);
									if(rs.next())
									{
									 cnt = rs.getInt(1);
										
									}
									pstmt.close();
									rs.close();
									pstmt = null;
									rs = null;
									if (cnt > 1)
									{
										errCode = "VTDUPCT";
										errString = getErrorString("supp_code",errCode,userId);
										break;
									}
								}
							}
						} // end supp_code
						 else if(childNodeName.equalsIgnoreCase("purc_order"))
						{
							String purchaseOrder = "";
							purchaseOrder = genericUtility.getColumnValue("purc_order",dom);
							sql = "select count(*) from porder where purc_order= '" + purchaseOrder + "'";
							pstmt= conn.prepareStatement(sql);
							rs = pstmt.executeQuery(); 
							System.out.println("sql:- " + sql);
							if(rs.next())
							{
							 cnt = rs.getInt(1);
								
							}
							pstmt.close();
							rs.close();
							pstmt = null;
							rs = null;
							if (cnt == 0)
							{
								errCode = "VTPORD1";
								errString = getErrorString("supp_code",errCode,userId);
								break;
							}
						}//end pur ord */
					}// end for
			} //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 = "";
		String empCodeAprv="";
		String lName="";
		String fName="";
		String mName="";
		String saleOrder = "";
		String lineNoSord = "";		
		String supplier = "",purchaseOrder = "";
		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 ="",currAppdateDb = "",frDate = "",toDate = "";
						java.sql.Timestamp currDate = null,currDateTs = null,frDateTs = null,toDateTs = null;
						currDate = new java.sql.Timestamp(System.currentTimeMillis()) ;
						System.out.println("\n today date----" + currDate);
						currAppdate = new SimpleDateFormat(genericUtility.getApplDateFormat()).format(currDate).toString();
						//todayDateDb =  Timestamp.valueOf(currAppdate +" 00:00:00.00");	
						System.out.println("\n today currDate in apl----" + currAppdate);
						currAppdate = genericUtility.getValidDateString(currAppdate,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat());
						System.out.println("\n today currDate in DB----" + currAppdate);
						currDateTs = Timestamp.valueOf(currAppdate + " 00:00:00.00");
						System.out.println("\n today currDate timeatamp----" + currDateTs);
						sql = "select fr_date,to_date from acctprd "
								+ " where ? between fr_date and to_date";
						System.out.println("select qry from fin date.." + sql);
						pstmt= conn.prepareStatement(sql);
						pstmt.setTimestamp(1,currDateTs);
						rs = pstmt.executeQuery(); 
						if(rs.next())
						{
						   frDateTs = rs.getTimestamp(1);
						   toDateTs = rs.getTimestamp(2);
						}
						System.out.println("frDateTs:- " + frDateTs + "toDate " + toDateTs);
						frDate = genericUtility.getValidDateString(frDateTs.toString(),genericUtility.getDBDateFormat(),genericUtility.getApplDateFormat());
						toDate = genericUtility.getValidDateString(toDateTs.toString(),genericUtility.getDBDateFormat(),genericUtility.getApplDateFormat());
						System.out.println("frDate:- " + frDate + "toDate " + toDate);
						pstmt.close();
						rs.close();
						pstmt = null;
						rs = null;
						valueXmlString.append("<eff_from>").append("<![CDATA["+frDate+"]]>").append("</eff_from>");					
						valueXmlString.append("<valid_upto>").append("<![CDATA["+toDate+"]]>").append("</valid_upto>");					
						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>");			
						valueXmlString.append("<tran_date>").append("<![CDATA["+getCurrdateAppFormat()+"]]>").append("</tran_date>");					
						valueXmlString.append("<status_date>").append("<![CDATA["+getCurrdateAppFormat()+"]]>").append("</status_date>");					
					}					
					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 )
						{
							valueXmlString.append("<site_descr>").append("").append("</site_descr>");	
						}
						else
						{
							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 if(currentColumn.trim().equalsIgnoreCase("emp_code__aprv"))
					{
						empCodeAprv = getColumnValue("emp_code__aprv",dom);
						sql = "select emp_fname,emp_mname,emp_lname from employee where emp_code = '" + empCodeAprv + "'";
						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("<employee_emp_fname>").append("<![CDATA["+fName.trim()+"]]>").append("</employee_emp_fname>");
						valueXmlString.append("<employee_emp_mname>").append("<![CDATA["+mName.trim()+"]]>").append("</employee_emp_mname>");
						valueXmlString.append("<employee_emp_lname>").append("<![CDATA["+lName.trim()+"]]>").append("</employee_emp_lname>");	
					}		
					valueXmlString.append("</Detail1>");					
					break;
					case 2:
							parentNodeList = dom.getElementsByTagName("Detail2");
							parentNode = parentNodeList.item(0);
							childNodeList = parentNode.getChildNodes();
							ctr = 0;
							valueXmlString.append("<Detail2>");
							childNodeListLength = childNodeList.getLength();
							do
							{
								childNode = childNodeList.item(ctr);
								childNodeName = childNode.getNodeName();
								if(childNodeName.equals(currentColumn.trim()))
								{
									if (childNode.getFirstChild() != null)
									{
										columnValue = childNode.getFirstChild().getNodeValue();
									}
								}
								ctr++;
							}
						    while(ctr < childNodeListLength && !childNodeName.equals(currentColumn));
							System.out.println("currentColumn..." + currentColumn);
							if(currentColumn.trim().equalsIgnoreCase("itm_default"))
							{
							 valueXmlString.append("<date_of_iss>").append("<![CDATA["+getCurrdateAppFormat()+"]]>").append("</date_of_iss>");					
								
							} // end itm_default	
							if(currentColumn.trim().equalsIgnoreCase("itm_defaultedit"))
							{
								supplier = genericUtility.getColumnValue("supp_code",dom);
								if(supplier != null && supplier.trim().length() > 0 )
								{
									valueXmlString.append("<supp_code protect =\"1\">").append("<![CDATA["+supplier+"]]>").append("</supp_code>");					
								}
							}
							else if(currentColumn.trim().equalsIgnoreCase("item_code"))
							{
								itemCode =genericUtility.getColumnValue("item_code",dom);
								if(itemCode == null || itemCode.trim().length() == 0)
								{
									valueXmlString.append("<item_descr>").append("").append("</item_descr>");	
								}
								else
								{
									System.out.println("itemCode....."+itemCode);
									sql = " select descr from item where item_code= '"+ itemCode+" ' " ; 
									pstmt= conn.prepareStatement(sql);
									rs = pstmt.executeQuery();
									if(rs.next())
									{
									
										descr = rs.getString("descr") == null ? "" : rs.getString("descr");
									}
									pstmt.close();
									rs.close();
									pstmt = null;
									rs = null;								
									if (descr == null || descr.trim().length() == 0)
									{
										valueXmlString.append("<item_descr>").append("").append("</item_descr>");	
									}
									else
									{
										valueXmlString.append("<item_descr>").append(descr).append("</item_descr>");	
									}
								}
							} // e
							else if(currentColumn.trim().equalsIgnoreCase("purc_order"))
							{
								
								purchaseOrder = genericUtility.getColumnValue("purc_order",dom);
								if(purchaseOrder == null || purchaseOrder.trim().length() == 0 )
								{
									valueXmlString.append("<supp_code protect =\"0\">").append("").append("</supp_code>");					
									valueXmlString.append("<supplier_supp_name>").append(" ").append("</supplier_supp_name>");					
								}
								else
								{
									sql = " select supp_code from porder where purc_order = '"+ purchaseOrder+" ' " ; 
									System.out.println("sql for pur order:----- " +sql);
									pstmt= conn.prepareStatement(sql);
									rs = pstmt.executeQuery();
									if(rs.next())
									{
									
										supplier = rs.getString("supp_code") == null ? "" : rs.getString("supp_code");
									}
									pstmt.close();
									rs.close();
									pstmt = null;
									rs = null;	
									System.out.println("supplier....."+supplier);
									if(supplier == null || supplier.trim().length() == 0)
									{
										valueXmlString.append("<supp_code protect =\"0\">").append(" ").append("</supp_code>");					
									}
									else
									{
										valueXmlString.append("<supp_code protect =\"1\">").append("<![CDATA["+ supplier +"]]>").append("</supp_code>");					
									}
									
									sql = " select supp_name from supplier where supp_code =  '"+ supplier+" ' " ; 
									System.out.println("sql for supplier:----- " + sql);
									pstmt= conn.prepareStatement(sql);
									rs = pstmt.executeQuery();
									if(rs.next())
									{
									
										descr = rs.getString("supp_name") == null ? "" : rs.getString("supp_name");
									}
									pstmt.close();
									rs.close();
									pstmt = null;
									rs = null;	
									if (descr == null || descr.trim().length() == 0)
									{
										valueXmlString.append("<supplier_supp_name>").append("").append("</supplier_supp_name>");					
									}
									else
									{
										valueXmlString.append("<supplier_supp_name>").append("<![CDATA["+ descr +"]]>").append("</supplier_supp_name>");					
									}
								}
							} // e
					valueXmlString.append("</Detail2>");
					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("[CTFORMEJB] CONNECTION is CLOSED");
		}
		System.out.println("valueXmlString:::::"+valueXmlString.toString());
		return valueXmlString.toString();
	 }//END OF ITEMCHANGE
	 
	  private String getCurrdateAppFormat()
    {
        String s = "";				  
        GenericUtility genericUtility = GenericUtility.getInstance();
        try
        {
            java.util.Date date = null;
            Timestamp 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 s;
    }
	private double  qtyTotal(Document dom2)throws ITMException
	{
		NodeList parentNodeList1=null;
		NodeList childList = null;
		Node parentNode1=null;
		Node childNode = null;
		GenericUtility genericUtility = GenericUtility.getInstance();
		parentNodeList1 = dom2.getElementsByTagName("Detail2");
		int childNodeListLength1 = parentNodeList1.getLength();
		double quantiy=0,qtyDoub=0;
		String qty="";
		System.out.println("childNodeListLength1-------------"+childNodeListLength1);
		System.out.println("toatal quatity-------------"+quantiy);
		for(int ctr1 = 0; ctr1 < childNodeListLength1; ctr1++)
		{
			parentNode1 = parentNodeList1.item(ctr1);
			qty = genericUtility.getColumnValueFromNode("quantity", parentNode1);
			//qty = genericUtility.getColumnValueFromNode("tot_amt", parentNode1);
			//qty = genericUtility.getColumnValueFromNode("tot_amt", parentNode1);
			if(qty != null && qty.trim().length() > 0)
			{
				qtyDoub =Double.parseDouble(qty);
			}
			System.out.println("totAmt-------------"+qtyDoub);
			quantiy = quantiy+qtyDoub;
			System.out.println("quantiy-------------"+quantiy); //totalamt
		}
		return quantiy;
	}
 }// END OF MAIN CLASS