package ibase.webitm.ejb.dis;

import ibase.scheduler.utility.interfaces.Schedule;
import java.rmi.RemoteException;
import java.util.*;
import java.util.Date;
import java.sql.*;
import java.io.*;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.omg.CORBA.ORB;
import org.w3c.dom.*;

import java.util.Properties;

import javax.swing.text.NumberFormatter;
import javax.xml.parsers.*;
import javax.xml.rpc.ParameterMode;
import javax.ejb.*;
import javax.naming.InitialContext;

import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.*;
import ibase.webitm.ejb.dis.DistCommon;
import ibase.webitm.ejb.fin.GenerateReceiptPrc;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.TransIDGenerator;

import ibase.utility.BaseException;
import ibase.utility.CommonConstants;
import ibase.utility.UserInfoBean;
import ibase.ejb.*;
import ibase.system.config.*;

import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.lang.reflect.Method;
import java.math.*;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import java.net.URLEncoder;

import ibase.webitm.ejb.sys.UtilMethods;

import java.net.*;
import java.io.*;

public class GenTrackTraceSchedule implements Schedule ,Serializable {

	// GenericUtility genericUtility = GenericUtility.getInstance();
	// ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
	// ConnDriver connDriver = new ConnDriver();
	// CommonConstants commonConstants = new CommonConstants();
	String chgUser = null;
	String chgTerm = null;
	static long count_records = 0;
	boolean isError = false;
	// Change by Rohan on 2/7/12
	String loginSiteCode = "";
	String xmlString = "";

	public static void main(String args[]) 
	{
		GenTrackTraceSchedule pcs = new GenTrackTraceSchedule();
		// pcs.insertSchedule();
		System.out.println("Main Method Calling******************************************************88");
	}

	public String schedulePriority(String wrkflwPriority) throws Exception {
		return "";
	}

	public String schedule(HashMap map) throws Exception {
		return "";
	}

	public String schedule(String scheduleParamXML) throws Exception {
		ibase.utility.UserInfoBean userInfo = null;
		String disparmLoginSiteCode = "";
		ConnDriver connDriver = new ConnDriver();
		Connection conn = null;
		conn = connDriver.getConnectDB("DriverITM");
		connDriver = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String queryString = "";
		String sql = "";
		
		ArrayList<String> despIdList = new ArrayList<String>();
		ArrayList<String> despIDFailedList = new ArrayList<String>();
		String despatchId = "";
		String retString = "";
		String trackRequest="";
		
		try 
		{
			System.out.println("call genTrackTraceShedular 30-08-2014 04:48!!!!!## @@!");
			System.out.println("scheduleParamXML  !!!!!! "+scheduleParamXML);
			
			/*----------------To get IP Address From shedular URL--------------------*/
			
			Document dom = null;
			String xtraParams = "",ipAddress="",port="";
			Node currDetail = null ;
			int noOfParam=0;
			System.out.println("************ ["+scheduleParamXML+"]");
			
			GenericUtility genericUtility=GenericUtility.getInstance();
			System.out.println("scheduleParamXML-----"+scheduleParamXML);
			dom=genericUtility.parseString(scheduleParamXML);
			NodeList paramList = dom.getElementsByTagName( "SCHEDULE" );
            noOfParam = paramList.getLength();            
            NodeList parentNodeList = null,childNodeList = null;
			Node parentNode = null,childNode = null;
			//String DisparmSites = "";
			int childNodeListLength = 0;
			String childNodeName = null;

			parentNodeList = dom.getElementsByTagName("ACTUALPARAMETERS");

			parentNode = parentNodeList.item(0);
			childNodeList = parentNode.getChildNodes();
			childNodeListLength = childNodeList.getLength();
			System.out.println("childNodeListLength  !!!!!"+childNodeListLength);
			for(int ctr = 0; ctr < childNodeListLength; ctr++)
			{
				childNode = childNodeList.item(ctr);
				childNodeName = childNode.getNodeName();
							
				if(childNodeName!=null && !"#text".equalsIgnoreCase(childNodeName))
				{
					if(ctr==0)
					{
						ipAddress = childNode.getFirstChild().getNodeValue();
					}
					if(ctr==1)
					{
						port = childNode.getFirstChild().getNodeValue();
					}
								
				}
			}
			System.out.println("ipAddress----"+ipAddress);
			System.out.println("port----"+port);
			
			
			/*-------------------------------------------------*/
						
		
			pstmt = conn.prepareStatement("SELECT DESP_ID FROM DESPATCH WHERE CONFIRMED='Y' AND CASE WHEN TRACE_STATUS IS NULL THEN 'N' ELSE TRACE_STATUS END='N'");
			rs = pstmt.executeQuery();
			while (rs.next()) 
			{
				despIdList.add(rs.getString("DESP_ID"));

			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;
			ipAddress=ipAddress+":"+port;
			System.out.println("ipAddress is !!!!!!!!!: "+ipAddress);
			System.out.println("despIdList.size()  !!!!! "+despIdList.size());
			for (int i = 0; i < despIdList.size(); i++) 
			{
				despatchId = despIdList.get(i);
				System.out.println(despatchId);
				retString = generateTrackInfo(despatchId, conn);
				
				trackRequest=trackInfoServlet(retString,ipAddress);
				trackRequest=trackRequest.trim();
				System.out.println("trackRequest :::"+trackRequest);
				if(trackRequest.equalsIgnoreCase("success"))
				{
					try
					{
						System.out.println("execute update query starts!!!!!!!!!");
						sql="update DESPATCH set TRACE_STATUS='Y' where DESP_ID =?";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, despatchId);
						pstmt.executeUpdate();
						conn.commit();
						System.out.println("despatch Id : "+despatchId+" updated TRACE_STATUS 'Y' succefully!!!");
					}
					catch(Exception e)
					{
						conn.rollback();
						e.printStackTrace();
						System.out.println("Exception :"+e.getMessage());
					}
					finally
					{
						try
						{
							if(pstmt != null)
							{
								pstmt.close();
							}
							
						}
						catch(Exception e)
						{
							e.printStackTrace();
						}
						
						
					}
				}
				else
				{
					despIDFailedList.add(despatchId);
					System.out.println("Failed despatch id transction : "+despIDFailedList);
				}
				
				
			}

		} 
		catch (Exception e)
		{
			throw new Exception(e);
		}
		finally
		{
			try
			{
				if(conn != null)
				{
					conn.close();
				}
				if(pstmt != null)
				{
					pstmt.close();
				}
				
				
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
			
			
		}
		return "";
	}

	public String generateTrackInfo(String desptchId,Connection conn)
			throws RemoteException, ITMException {

		String retString = "";
		String errString = "";
		StringBuffer xmlBuff = null;
		String xmlString = null;
		// Connection conn = null;
		String sql = "";
		PreparedStatement pstmt = null;
		ResultSet rs = null, rs1 = null;
		String lineNoStr = "", frtTerm = "";
		double grossWtHD = 0.0;
		double tareWtHD = 0.0;
		double netWtHD = 0.0;
		double grossWtDT = 0.0;
		double tareWtDT = 0.0;
		double netWtDT = 0.0;
		String refId = "",custCode="", custCodeDlv = "", dlvAdd1 = "", dlvAdd2 = "", dlvCity = "", dlvPin = "", dlvCountry = "", siteDescr = "", siteAdd1 = "", siteAdd2 = "", siteCity = "", sitePin = "", noArt = "", itemStrength = "",lrNo="";
		String tranCode = "", unitShip = "", bolNo = "", scac = "", planStartDt = "", planEndDt = "", shipMtDt = "", expDt = "";
		String despatchId = "", invoiceId = "", itemCode = "", lotNo = "", lotSl = "", quantity = "", quantityOrd = "", convQtyStduom = "", unitStd = "", unitDescr = "";
		String unit = "", qtyStduom = "", packQty = "", sscc_18 = "", refSer = "", locCode = "", itmDescr = "", itmCodeUPC = "", itmCodeNDC = "", custProd = "", purcDate = "", drugLicNo = "", strengthValue = "", strengthUnit = "",shipmentId="";
		String siteCode_ch="",sordNo="";
		Date planStart = null;
		Date planEnd = null;
		Date shipmentDate = null;
		Date prodDate = null;
		Date expDate = null;
		Date despDate=null;
		String siteCode = "", siteCode_dlv = "",suppCode="",asnRequired="",asnCreated="";
		String descr = "";
		int lineNo = 0;
		int cnt = 0,count=0;
		HashMap tempMapHdr = null;
		HashMap<String, String> tempMap = new HashMap<String, String>();
		ConnDriver connDriver = new ConnDriver();
		ITMDBAccessLocal itmDBAccessLocal = new ITMDBAccessEJB();

		try 
		{
			System.out.println("mahendra testing shipmentLocConf.java --18-06-2014-11:39---");
			System.out.println("shipment id ::::" + desptchId);
			Date currentDate = new Date();
			xmlBuff = null;
			xmlBuff = new StringBuffer();
			GenericUtility genericUtility = GenericUtility.getInstance();
			SimpleDateFormat sdf = new SimpleDateFormat(genericUtility.getApplDateFormat());
			SimpleDateFormat sdf1 = new SimpleDateFormat(genericUtility.getDBDateFormat());
			conn = connDriver.getConnectDB("DriverITM");
			refSer="S-DSP";

		
				sql = "select DESP_DATE,SITE_CODE,CUST_CODE,CUST_CODE__DLV,DLV_ADD1,DLV_ADD2,DLV_CITY,DLV_PIN,COUNT_CODE__DLV, ";
				sql = sql+ " GROSS_WEIGHT,TARE_WEIGHT ,NETT_WEIGHT,UNIT__SHIP,BOL_NO,SCAC,NO_ART,SHIPMENT_ID,TRAN_CODE,SORD_NO,LR_NO from despatch where desp_id=? ";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, desptchId);
				rs = pstmt.executeQuery();
				if (rs.next())
				{
					siteCode = rs.getString("SITE_CODE");
					custCode=rs.getString("CUST_CODE");
					custCodeDlv = rs.getString("CUST_CODE__DLV");
					dlvAdd1 = rs.getString("DLV_ADD1");
					dlvAdd2 = rs.getString("DLV_ADD2");
					dlvCity = rs.getString("DLV_CITY");
					dlvPin = rs.getString("DLV_PIN");
					dlvCountry = rs.getString("COUNT_CODE__DLV");
					// tranCode = rs.getString("TRAN_CODE");
					grossWtHD = rs.getDouble("GROSS_WEIGHT");
					tareWtHD = rs.getDouble("TARE_WEIGHT");
					netWtHD = rs.getDouble("NETT_WEIGHT");
					unitShip = rs.getString("UNIT__SHIP");
					bolNo = rs.getString("BOL_NO");
					scac = rs.getString("SCAC");
					noArt = rs.getString("NO_ART");
					tranCode=rs.getString("TRAN_CODE");
					shipmentId=rs.getString("SHIPMENT_ID");
					sordNo=rs.getString("SORD_NO");
					despDate=rs.getDate("DESP_DATE");
					lrNo=rs.getString("LR_NO");
							
				}
				

				rs.close();
				rs = null;
				pstmt.close();
				pstmt = null;
				System.out.println("generateTrackInfo S-DSP:@siteCode: "+ siteCode + " @custCodeDlv :" + custCodeDlv+"@despDate :"+despDate);
				System.out.println("generateTrackInfo S-DSP:@dlvAdd1: "	+ dlvAdd1 + " @dlvAdd2 :" + dlvAdd2);
				System.out.println("generateTrackInfo S-DSP:@dlvCity: "+ dlvCity + " @dlvPin :" + dlvPin);
				System.out.println("generateTrackInfo S-DSP:@siteCode: "+ siteCode + " @custCodeDlv :" + custCodeDlv);
				System.out.println("generateTrackInfo S-DSP:@dlvCountry: "+ dlvCountry + " @grossWtHD :" + grossWtHD);
				System.out.println("generateTrackInfo S-DSP:@tareWtHD: "+ tareWtHD + " @netWtHD :" + netWtHD);
				System.out.println("generateTrackInfo S-DSP:@unitShip: "+ unitShip + " @bolNo :" + bolNo);
				System.out.println("generateTrackInfo S-DSP:@scac: " + scac	+ " @noArt :" + noArt+" @tranCode :"+tranCode+"  @shipmentId"+shipmentId);

				sql = "select INVOICE_ID ,ITEM_CODE ,LOT_NO ,LOT_SL,QUANTITY ,QUANTITY__ORD,LOC_CODE,CONV__QTY_STDUOM , ";
				sql = sql+ " UNIT__STD,UNIT ,QUANTITY__STDUOM ,PACK_QTY,GROSS_WEIGHT,TARE_WEIGHT,NETT_WEIGHT,SSCC_18  ";
				sql = sql + " from despatchdet where desp_id=?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, desptchId);
				rs = pstmt.executeQuery();
				while (rs.next())
				{
					cnt++;
					invoiceId = rs.getString("INVOICE_ID");
					itemCode = rs.getString("ITEM_CODE");
					lotNo = rs.getString("LOT_NO");
					lotSl = rs.getString("LOT_SL");
					quantity = rs.getString("QUANTITY");
					quantityOrd = rs.getString("QUANTITY__ORD");
					convQtyStduom = rs.getString("CONV__QTY_STDUOM");
					unitStd = rs.getString("UNIT__STD");
					unit = rs.getString("UNIT");
					qtyStduom = rs.getString("QUANTITY__STDUOM");
					locCode = rs.getString("LOC_CODE");
					packQty = rs.getString("PACK_QTY");
					grossWtDT = rs.getDouble("GROSS_WEIGHT");
					tareWtDT = rs.getDouble("TARE_WEIGHT");
					netWtDT = rs.getDouble("NETT_WEIGHT");
					sscc_18 = rs.getString("SSCC_18");

					invoiceId = invoiceId == null ? "" : invoiceId.trim();
					itemCode = itemCode == null ? "" : itemCode.trim();
					lotNo = lotNo == null ? "" : lotNo.trim();
					lotSl = lotSl == null ? "" : lotSl.trim();
					quantity = quantity == null ? "" : quantity.trim();
					quantityOrd = quantityOrd == null ? "" : quantityOrd.trim();
					convQtyStduom = convQtyStduom == null ? "" : convQtyStduom.trim();
					unitStd = unitStd == null ? "" : unitStd.trim();
					unit = unit == null ? "" : unit.trim();
					qtyStduom = qtyStduom == null ? "" : qtyStduom.trim();
					locCode = locCode == null ? "" : locCode.trim();
					sscc_18 = sscc_18 == null ? "" : sscc_18.trim();
					packQty = packQty == null ? "" : packQty.trim();

					System.out.println("generateTrackInfo S-DSP:invoiceId @@"+ cnt + " " + invoiceId);
					System.out.println("generateTrackInfo S-DSP:itemCode @@"+ cnt + " " + itemCode);
					System.out.println("generateTrackInfo S-DSP:lotNo @@" + cnt	+ " " + lotNo);
					System.out.println("generateTrackInfo S-DSP:lotSl @@" + cnt	+ " " + lotSl);
					System.out.println("generateTrackInfo S-DSP:quantity @@"+ cnt + " " + quantity);
					System.out.println("generateTrackInfo S-DSP:quantityOrd @@"	+ cnt + " " + quantityOrd);
					System.out.println("generateTrackInfo S-DSP:convQtyStduom @@ "+ cnt + " " + convQtyStduom);
					System.out.println("generateTrackInfo S-DSP:unitStd @@ "+ cnt + " " + unitStd);
					System.out.println("generateTrackInfo S-DSP:unit @@" + cnt+ " " + unit);

					tempMap.put("invoice_id" + cnt, invoiceId);
					tempMap.put("item_code" + cnt, itemCode);
					tempMap.put("lot_no" + cnt, lotNo);
					tempMap.put("lot_sl" + cnt, lotSl);
					tempMap.put("quantity" + cnt, quantity);
					tempMap.put("quantity_ord" + cnt, quantityOrd);
					tempMap.put("conv_qty_std" + cnt, convQtyStduom);
					tempMap.put("unit_std" + cnt, unitStd);
					tempMap.put("unit" + cnt, unit);
					tempMap.put("qty_stduom" + cnt, qtyStduom);
					tempMap.put("loc_code" + cnt, locCode);
					tempMap.put("pack_qty" + cnt, packQty);
					tempMap.put("gross_wt" + cnt, String.valueOf(grossWtDT));
					tempMap.put("tare_wt" + cnt, String.valueOf(tareWtDT));
					tempMap.put("net_wt" + cnt, String.valueOf(netWtDT));

				}

				rs.close();
				rs = null;
				pstmt.close();
				pstmt = null;

				System.out.println("value of cnt :" + cnt);


			sql = "select descr,add1,add2,city,pin from site where site_code=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, siteCode);
			rs = pstmt.executeQuery();
			if (rs.next()) 
			{
				siteDescr = rs.getString("descr");
				siteAdd1 = rs.getString("add1");
				siteAdd2 = rs.getString("add2");
				siteCity = rs.getString("city");
				sitePin = rs.getString("pin");
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;
			
			System.out.println("generateTrackInfo :siteDescr:" + siteDescr);
			System.out.println("generateTrackInfo :siteAdd1:" + siteAdd1);
			System.out.println("generateTrackInfo :siteAdd2:" + siteAdd2);
			System.out.println("generateTrackInfo :siteCity:" + siteCity);
			System.out.println("generateTrackInfo :sitePin:" + sitePin);

			sql = "select DRUG_LIC_NO from customer where cust_code=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, custCodeDlv);
			rs = pstmt.executeQuery();
			if (rs.next())
			{
				drugLicNo = rs.getString("DRUG_LIC_NO");
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;
			System.out.println("drugLicNo:" + drugLicNo);

			/*sql = "select plan_end,plan_start  from shipment_route where shipment_id=?  ";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, tranId);
			rs = pstmt.executeQuery();
			if (rs.next())
			{
				planStart = rs.getDate("plan_start");
				planEnd = rs.getDate("plan_end");
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;
			System.out.println("generateTrackInfo :planStart:" + planStart);
			System.out.println("generateTrackInfo :planEnd:" + planEnd);*/

			sql = "select CUST_PORD,PORD_DATE from sorder where sale_order = (select sord_no from despatch where desp_id=?)";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, desptchId);
			rs = pstmt.executeQuery();
			if (rs.next())
			{
				custProd = rs.getString("CUST_PORD");
				prodDate = rs.getDate("PORD_DATE");

			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;
			System.out.println("generateTrackInfo :custProd: " + custProd);
			System.out.println("generateTrackInfo :prodDate: " + prodDate);
			
			
			sql = "select SCAC_CODE  from transporter where tran_code=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, tranCode);
			rs = pstmt.executeQuery();
			if (rs.next())
			{
				scac = rs.getString("SCAC_CODE");
				
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;
			System.out.println("scac  "+scac);
			/*-----------------storing asn data---------------------*/
			
			
			
			System.out.println("storing asn records!!!");
			System.out.println("siteCode "+siteCode);
			System.out.println("custCode "+custCode);
			
			sql = "select site_code__ch from site_customer where site_code=? and cust_code=? and CASE WHEN channel_partner IS NULL THEN 'N' ELSE channel_partner END='N'  ";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, siteCode);
			pstmt.setString(2, custCode);
			rs = pstmt.executeQuery();
			if (rs.next())
			{
				siteCode_ch = rs.getString("site_code__ch");
				
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;
			
			System.out.println("siteCode_ch !!!! "+siteCode_ch);
			if(siteCode_ch == "" || siteCode_ch == null)
			{
				sql = "select site_code from customer where cust_code=? and asn_reqd ='Y' and CASE WHEN channel_partner IS NULL THEN 'N' ELSE channel_partner END='N' ";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, custCode);
				rs = pstmt.executeQuery();
				if (rs.next())
				{
					siteCode_ch = rs.getString("site_code");
					
				}
				rs.close();
				rs = null;
				pstmt.close();
				pstmt = null;
				
				
			}
			System.out.println("siteCode_ch @@@@ "+siteCode_ch);
			
			
			
			//sql = "select supp_code from site_supplier where site_code__ch=? and site_code=? and channel_partner = 'N' ";
			sql = "select supp_code from site_supplier where site_code__ch=? and site_code=? and CASE WHEN channel_partner IS NULL THEN 'N' ELSE channel_partner END='N' ";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, siteCode_ch);
			pstmt.setString(2, siteCode);
			rs = pstmt.executeQuery();
			if (rs.next())
			{
				suppCode = rs.getString("supp_code");
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;
			
			if(suppCode != "")
			{
			
				sql = "select count(*) from supplier where ASN_REQD ='Y' and channel_partner ='N' and suppCode=?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, suppCode);
				
				rs = pstmt.executeQuery();
				if (rs.next())
				{
					count = rs.getInt(1);
				}
				rs.close();
				rs = null;
				pstmt.close();
				pstmt = null;
				
				if(count == 0)
				{
					suppCode="";
				}
				
				
			}
			
			
			
			
			System.out.println("suppCode  !!!!! "+suppCode);
			if(suppCode == "" || suppCode == null)
			{
				sql = "select supp_code from supplier where site_code=? and ASN_REQD ='Y' and channel_partner ='N' ";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, siteCode_ch);
				rs = pstmt.executeQuery();
				if (rs.next())
				{
					suppCode = rs.getString("supp_code");
				}
				rs.close();
				rs = null;
				pstmt.close();
				pstmt = null;
				
			}
			
			System.out.println("supp_code : "+suppCode);
			if(suppCode != "" && suppCode != null)
			{
				asnRequired="Y";
				asnCreated="N";
				
			}
			System.out.println("@asnRequired :"+asnRequired+"@asnCreated "+asnCreated);	
			
				
          /*---------------------------------------------------------*/
			
			
			siteCode = siteCode == null ? "" : siteCode.trim();
			siteDescr = siteDescr == null ? "" : siteDescr.trim();
			siteAdd1 = siteAdd1 == null ? "" : siteAdd1.trim();
			siteAdd2 = siteAdd2 == null ? "" : siteAdd2.trim();
			siteCity = siteCity == null ? "" : siteCity.trim();
			sitePin = sitePin == null ? "" : sitePin.trim();
			custCodeDlv = custCodeDlv == null ? "" : custCodeDlv.trim();
			drugLicNo = drugLicNo == null ? "" : drugLicNo.trim();
			dlvAdd1 = dlvAdd1 == null ? "" : dlvAdd1.trim();
			dlvAdd2 = dlvAdd2 == null ? "" : dlvAdd2.trim();
			dlvCity = dlvCity == null ? "" : dlvCity.trim();
			dlvPin = dlvPin == null ? "" : dlvPin.trim();
			dlvCountry = dlvCountry == null ? "" : dlvCountry.trim();
			tranCode = tranCode == null ? "" : tranCode.trim();
			// grossWtHD = grossWtHD==null?"":grossWt.trim();
			// tarewt = tarewt==null?"":tarewt.trim();
			// netwt = netwt==null?"":netwt.trim();
			unitShip = unitShip == null ? "" : unitShip.trim();
			bolNo = bolNo == null ? "" : bolNo.trim();
			scac = scac == null ? "" : scac.trim();
			noArt = noArt == null ? "" : noArt.trim();
			planStartDt = planStart == null ? "" : sdf.format(planStart);
			planEndDt = planEnd == null ? "" : sdf.format(planEnd);
			shipMtDt = shipmentDate == null ? "" : sdf.format(shipmentDate);
			custProd = custProd == null ? "" : custProd.trim();
			lrNo = lrNo == null ? "" : lrNo.trim();
			purcDate = prodDate == null ? "" : sdf.format(prodDate);
			suppCode = suppCode == null ? "" : suppCode.trim();
			siteCode_ch = siteCode_ch == null ? "" : siteCode_ch.trim();
			

			// create xml for track information
			xmlBuff.append("<?xml version='1.0' encoding='ISO-8859-1'?>\n");
			xmlBuff.append("<DocumentRoot>");
			xmlBuff.append("<description>").append("Datawindow Root").append("</description>");
			xmlBuff.append("<group0>");
			xmlBuff.append("<description>").append("Group0 description").append("</description>");
			xmlBuff.append("<Header0>");
			xmlBuff.append("<objName><![CDATA[").append("tnt_tran_info").append("]]></objName>");
			xmlBuff.append("<pageContext><![CDATA[").append("1").append("]]></pageContext>");
			xmlBuff.append("<objContext><![CDATA[").append("1").append("]]></objContext>");
			xmlBuff.append("<editFlag><![CDATA[").append("A").append("]]></editFlag>");
			xmlBuff.append("<focusedColumn><![CDATA[").append("").append("]]></focusedColumn>");
			xmlBuff.append("<action><![CDATA[").append("SAVE").append("]]></action>");
			xmlBuff.append("<elementName><![CDATA[").append("").append("]]></elementName>");
			xmlBuff.append("<keyValue><![CDATA[").append("1").append("]]></keyValue>");
			xmlBuff.append("<taxKeyValue><![CDATA[").append("").append("]]></taxKeyValue>");
			xmlBuff.append("<saveLevel><![CDATA[").append("1").append("]]></saveLevel>");
			xmlBuff.append("<forcedSave><![CDATA[").append(true).append("]]></forcedSave>");
			xmlBuff.append("<taxInFocus><![CDATA[").append(false).append("]]></taxInFocus>");
			xmlBuff.append("<callType><![CDATA[").append("SH").append("]]></callType>");
			xmlBuff.append("<description>").append("Header0 members").append("</description>");

			System.out.println("details 1 start!!!!!");
			/*
			 * deatails 1 start
			 */
			

			xmlBuff.append("<Detail1 dbID=\"\" domID=\"1\" objName=\"tnt_tran_info\" objContext=\"1\">");
			xmlBuff.append("<attribute selected=\"N\" updateFlag=\"A\" status=\"N\" pkNames=\"\"/>");
			xmlBuff.append("<tran_id/>");
			xmlBuff.append("<tran_date><![CDATA["+ sdf.format(currentDate).toString() + "]]></tran_date>");
			xmlBuff.append("<site_code><![CDATA[" + siteCode.trim()+ "]]></site_code>");
			xmlBuff.append("<site_descr><![CDATA[" + siteDescr.trim()+ "]]></site_descr>");
			xmlBuff.append("<site_add1><![CDATA[" + siteAdd1.trim()+ "]]></site_add1>");
			xmlBuff.append("<site_add2><![CDATA[" + siteAdd2+ "]]></site_add2>");
			xmlBuff.append("<site_city><![CDATA[" + siteCity.trim()+ "]]></site_city>");
			xmlBuff.append("<site_pin><![CDATA[" + sitePin.trim() + "]]></site_pin>");
			xmlBuff.append("<count_code__site><![CDATA[]]></count_code__site>");
			xmlBuff.append("<shipment_id><![CDATA[]]></shipment_id>");
			xmlBuff.append("<shipment_date><![CDATA[" + shipMtDt+ "]]></shipment_date>");
			xmlBuff.append("<no_art><![CDATA[" + noArt + "]]></no_art>");
			xmlBuff.append("<gross_weight><![CDATA[" + grossWtHD+ "]]></gross_weight>");
			xmlBuff.append("<net_weight><![CDATA[" + netWtHD+ "]]></net_weight>");
			xmlBuff.append("<tare_weight><![CDATA[" + tareWtHD+ "]]></tare_weight>");
			xmlBuff.append("<unit__ship><![CDATA[" + unitShip+ "]]></unit__ship>");
			xmlBuff.append("<tran_code><![CDATA[" + tranCode.trim()+ "]]></tran_code>");
			xmlBuff.append("<bol_no><![CDATA[" + bolNo.trim() + "]]></bol_no>");
			xmlBuff.append("<cust_code__dlv><![CDATA[" + custCodeDlv.trim()+ "]]></cust_code__dlv>");
			xmlBuff.append("<site_code__dlv><![CDATA[" + siteCode_dlv.trim()+ "]]></site_code__dlv>");
			xmlBuff.append("<scac><![CDATA["+scac.trim()+"]]></scac>");
			xmlBuff.append("<drug_lic_no><![CDATA[" + drugLicNo.trim()+ "]]></drug_lic_no>");
			xmlBuff.append("<dlv_add1><![CDATA[" + dlvAdd1 + "]]></dlv_add1>");
			xmlBuff.append("<dlv_add2><![CDATA[" + dlvAdd2 + "]]></dlv_add2>");
			xmlBuff.append("<dlv_city><![CDATA[" + dlvCity.trim() + "]]></dlv_city>");
			xmlBuff.append("<dlv_pin><![CDATA[" + dlvPin.trim() + "]]></dlv_pin>");
			xmlBuff.append("<count_code__dlv><![CDATA[" + dlvCountry+ "]]></count_code__dlv>");
			xmlBuff.append("<plan_start><![CDATA[" + planStartDt+ "]]></plan_start>");
			xmlBuff.append("<plan_end><![CDATA[" + planEndDt + "]]></plan_end>"); 
			xmlBuff.append("<site_code__asn><![CDATA[" + siteCode_ch.trim() + "]]></site_code__asn>");
			xmlBuff.append("<supp_code__asn><![CDATA[" + suppCode.trim() + "]]></supp_code__asn>");
			xmlBuff.append("<asn_required><![CDATA[" + asnRequired.trim() + "]]></asn_required>"); 
			xmlBuff.append("<asn_created><![CDATA[" + asnCreated.trim() + "]]></asn_created>");
		
			// xmlBuff.append("<remarks><![CDATA["+ tranId +"]]></remarks>");
			xmlBuff.append("</Detail1>");
			lineNo = 0;

			/*
			 * deatils1 end
			 */

			System.out.println("end of details 1");

			for (int itemCtr = 1; itemCtr <= cnt; itemCtr++)
			{
				lineNo++;
				System.out.println("lineNo is " + lineNo);
				// tempMap = (HashMap)tempList.get(itemCtr);

				/*
				 * start of Details 2
				 */
				System.out.println("start of details 2");

				xmlBuff.append("<Detail2 dbID='' domID=\"1\" objName=\"tnt_tran_info\" objContext=\"2\">");
				xmlBuff.append("<attribute pkNames=\"\" selected=\"N\" updateFlag=\"A\" status=\"N\" />");
				xmlBuff.append("<tran_id/>");
				lineNoStr = "   " + lineNo;
				lineNoStr = lineNoStr.substring(lineNoStr.length() - 3);
				System.out.println("lineNoStr " + lineNoStr);
				// xmlBuff.append("<line_no>" + new
				// Integer(lineNo).toString().length() + "</line_no>");
				xmlBuff.append("<line_no>" + lineNoStr + "</line_no>");

				xmlBuff.append("<ref_ser><![CDATA[" + refSer + "]]></ref_ser>");
				xmlBuff.append("<ref_id><![CDATA[" + desptchId + "]]></ref_id>");
				xmlBuff.append("<ref_date><![CDATA[" + sdf.format(despDate).toString() + "]]></ref_date>");
				System.out.println("val of invoice_id "+ (String) tempMap.get("invoice_id" + itemCtr));
				System.out.println("val of item_code "+ (String) tempMap.get("item_code" + itemCtr));
				xmlBuff.append("<invoice_id><![CDATA["+ (String) tempMap.get("invoice_id" + itemCtr)+ "]]></invoice_id>");
				xmlBuff.append("<item_code><![CDATA["+ (String) tempMap.get("item_code" + itemCtr)+ "]]></item_code>");

				sql = "select DESCR,ITEM_CODE__UPC,ITEM_CODE__NDC,strength_unit,strength_value from item where item_code=?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, tempMap.get("item_code" + itemCtr));
				rs = pstmt.executeQuery();
				if (rs.next()) 
				{
					itmDescr = rs.getString("DESCR");
					itmCodeUPC = rs.getString("ITEM_CODE__UPC");
					itmCodeNDC = rs.getString("ITEM_CODE__NDC");
					strengthValue = rs.getString("strength_unit");
					strengthUnit = rs.getString("strength_value");
				}
				rs.close();
				rs = null;
				pstmt.close();
				pstmt = null;
				System.out.println("generateTrackInfo :itmDescr: " + itmDescr);
				System.out.println("generateTrackInfo :itmCodeUPC: "+ itmCodeUPC);
				System.out.println("generateTrackInfo :itmCodeNDC: "+ itmCodeNDC);
				System.out.println("generateTrackInfo :strengthValue: "	+ strengthValue);
				System.out.println("generateTrackInfo :strengthUnit: "+ strengthUnit);

				itmDescr = itmDescr == null ? "" : itmDescr.trim();
				itmCodeUPC = itmCodeUPC == null ? "" : itmCodeUPC.trim();
				itmCodeNDC = itmCodeNDC == null ? "" : itmCodeNDC.trim();

				if (strengthUnit != null && strengthValue != null)
				{
					sql = " select descr from uom where unit=? ";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, strengthUnit);
					rs = pstmt.executeQuery();
					if (rs.next())
					{
						unitDescr = rs.getString("descr");
					}
					rs.close();
					rs = null;
					pstmt.close();
					pstmt = null;

					itemStrength = strengthValue + "-" + unitDescr;
					System.out.println("generateTrackInfo :itemStrength : "	+ itemStrength);

				}

				sql = "select exp_date from ITEM_LOT_INFO where item_code=? and lot_no=? ";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, tempMap.get("item_code" + itemCtr));
				pstmt.setString(2, tempMap.get("lot_no" + itemCtr));
				rs = pstmt.executeQuery();
				if (rs.next())
				{
					expDate = rs.getDate("exp_date");
				}
				expDt = expDate == null ? "" : sdf.format(expDate);
				rs.close();
				rs = null;
				pstmt.close();
				pstmt = null;
				System.out.println("itmDescr: " + itmDescr);
				System.out.println("itmCodeUPC: " + itmCodeUPC);
				System.out.println("itmCodeNDC: " + itmCodeNDC);

				itmDescr = itmDescr == null ? "" : itmDescr.trim();
				itmCodeUPC = itmCodeUPC == null ? "" : itmCodeUPC.trim();
				itmCodeNDC = itmCodeNDC == null ? "" : itmCodeNDC.trim();

				System.out.println("lot_no  !!!"+ tempMap.get("lot_sl" + itemCtr));
				System.out.println("conv_qty_std  !!!"+ tempMap.get("conv_qty_std" + itemCtr));
				System.out.println("pack_qty  !!!"+ tempMap.get("pack_qty" + itemCtr));
				System.out.println("itmDescr  !!!"+ tempMap.get("itmDescr" + itemCtr));
				
				
				
				xmlBuff.append("<item_descr><![CDATA[" + itmDescr+ "]]></item_descr>");
				xmlBuff.append("<item_code__ndc><![CDATA[" + itmCodeNDC	+ "]]></item_code__ndc>");
				xmlBuff.append("<item_code__upc><![CDATA[" + itmCodeUPC	+ "]]></item_code__upc>");
				xmlBuff.append("<item_strength><![CDATA[" + itemStrength + "]]></item_strength>");
				xmlBuff.append("<lot_no><![CDATA["+ (String) tempMap.get("lot_no" + itemCtr)+ "]]></lot_no>");
				xmlBuff.append("<exp_date><![CDATA[" + expDt + "]]></exp_date>");
				xmlBuff.append("<lot_sl><![CDATA["+ (String) tempMap.get("lot_sl" + itemCtr)+ "]]></lot_sl>");
				xmlBuff.append("<quantity__ord><![CDATA["+ (String) tempMap.get("quantity_ord" + itemCtr)+ "]]></quantity__ord>");
				xmlBuff.append("<quantity><![CDATA["+ (String) tempMap.get("quantity" + itemCtr)+ "]]></quantity>");
				xmlBuff.append("<loc_code><![CDATA["+ (String) tempMap.get("loc_code" + itemCtr)+ "]]></loc_code>");
				xmlBuff.append("<conv__qty_stduom><![CDATA["+ (String) tempMap.get("conv_qty_std" + itemCtr)+ "]]></conv__qty_stduom>");
				xmlBuff.append("<unit__std><![CDATA["+ (String) tempMap.get("unit_std" + itemCtr)+ "]]></unit__std>");
				xmlBuff.append("<unit><![CDATA["+ (String) tempMap.get("unit" + itemCtr) + "]]></unit>");
				xmlBuff.append("<quantity__stduom><![CDATA["+ (String) tempMap.get("qty_stduom" + itemCtr)+ "]]></quantity__stduom>");
				xmlBuff.append("<pack_qty><![CDATA["+ (String) tempMap.get("pack_qty" + itemCtr)+ "]]></pack_qty>");
				xmlBuff.append("<gross_weight><![CDATA["+ (String) tempMap.get("gross_wt" + itemCtr)+ "]]></gross_weight>");
				xmlBuff.append("<net_weight><![CDATA["+ (String) tempMap.get("net_wt" + itemCtr)+ "]]></net_weight>");
				xmlBuff.append("<tare_weight><![CDATA["+ (String) tempMap.get("tare_wt" + itemCtr)+ "]]></tare_weight>");
				xmlBuff.append("<cust_pord><![CDATA[" + custProd+ "]]></cust_pord>");
				xmlBuff.append("<pord_date><![CDATA[" + purcDate+ "]]></pord_date>");
				xmlBuff.append("<sscc_18><![CDATA[" + sscc_18 + "]]></sscc_18>");
		
				
				
				
			    		
				xmlBuff.append("</Detail2>");

			}// end of for loop

			/*
			 * end of details 2
			 */

			System.out.println("end of details 2");

			xmlBuff.append("</Header0>");
			xmlBuff.append("</group0>");
			xmlBuff.append("</DocumentRoot>");
			xmlString = xmlBuff.toString();
			System.out.println("@@@@@2: retString:" + xmlString);
		

		} catch (Exception e)
		{

			e.printStackTrace();
			System.out.println("@@@@@@@@Exception@@@@@@@@@@@");
			System.out.println("Exception ::" + e.getMessage());
			errString = itmDBAccessLocal.getErrorString("", "VTTRINERR", "");
			return errString;

		}
		finally
		{
			try
			{
				if(rs != null)
				{
					rs.close();
				}
				if(pstmt != null)
				{
					pstmt.close();
				}
				if(conn != null)
				{
					conn.close();
				}
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
			
		}

		return xmlString;
	}


	
	
	public String trackInfoServlet(String xmlString,String ipAddress)throws ITMException, IOException,UnsupportedEncodingException {
		String msg="";
		String urlAddress="";
		URL url;
	    HttpURLConnection connection = null; 
	    String urlParameters = "fName=" + URLEncoder.encode("mahendra", "UTF-8") +"&lName=" + URLEncoder.encode("jadhav", "UTF-8");
	
	    
		try
		{ 
			  //Create connection
			  System.out.println("call trackInfoServlet==####@@@@@@@====");
			  System.out.println("xmlString  !!"+xmlString);
			  ipAddress=ipAddress.trim();
			  System.out.println("ipAddress  ::: "+ipAddress);
			  urlAddress="http://"+ipAddress+"/ibase/TrackInfoServlet";
			  System.out.println("urlAddress is :::"+urlAddress);
		      url = new URL(urlAddress);
		      connection = (HttpURLConnection)url.openConnection();
		      connection.setRequestMethod("POST");
		      connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
		      connection.setUseCaches (false);
		      connection.setDoInput(true);
		      connection.setDoOutput(true);
		  
		      
		      //send request
		      OutputStreamWriter out = new OutputStreamWriter(
              connection.getOutputStream());
		      out.write("xmlData=" + xmlString);
		      out.close();
		    
		    
		      
		    //Get Response	
		      InputStream is = connection.getInputStream();
		      BufferedReader rd = new BufferedReader(new InputStreamReader(is));
		      String line;
		      StringBuffer response = new StringBuffer(); 
		      while((line = rd.readLine()) != null) 
		      {
		    	System.out.println("line  !!!! "+line);
		        response.append(line);
		        response.append('\r');
		      }
		      rd.close();
		      return response.toString();
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
			msg="failed :"+e.getMessage();
			return msg;
		}
		finally
		{
			 if(connection != null) 
			 {
			        connection.disconnect(); 
			 }
		}
		
		//return msg;
	}
	

	

}
