/********************************************************
        Title : BillofQuantityIC
        Date  : 20/09/12
        Developer: Akhilesh Sikarwar

 ********************************************************/
package ibase.webitm.ejb.dis;

import ibase.system.config.ConnDriver;
import ibase.webitm.ejb.ValidatorEJB;
import ibase.webitm.ejb.fin.FinCommon;
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.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.ejb.Stateless;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
@Stateless
public class BillofQuantityIC extends ValidatorEJB

implements BillofQuantityICLocal, BillofQuantityICRemote
{
	GenericUtility genericUtility = GenericUtility.getInstance();
	String winName = null;
	FinCommon finCommon = null;
	ValidatorEJB validator = null;

	public String wfValData(String xmlString, String xmlString1, String xmlString2, String objContext, String editFlag, String xtraParams) throws RemoteException, ITMException
	{
		String errString = "";
		Document dom = null;
		Document dom1 = null;
		Document dom2 = null;
		try
		{
			System.out.println("Val xmlString :: " + xmlString);
			System.out.println("Val xmlString1 :: " + xmlString1);
			System.out.println("Val xmlString2 :: " + xmlString2);
			if (xmlString != null && xmlString.trim().length() > 0)
			{
				dom = parseString(xmlString);
			}
			if (xmlString1 != null && xmlString1.trim().length() > 0)
			{
				dom1 = parseString(xmlString1);
			}
			if (xmlString2 != null && xmlString2.trim().length() > 0)
			{
				dom2 = parseString("<Root>" + xmlString2 + "</Root>");
			}
			errString = wfValData(dom, dom1, dom2, objContext, editFlag, xtraParams);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			throw new ITMException(e);
		}
		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;
		String childNodeName = null;
		StringBuffer valueXmlString = new StringBuffer();
		String userId = "";
		String sql = "";
		String errCode = "";
		String errorType = "";
		String errString = "";
		String currCode = "";
		String suppCodePref = "";
		String projCode = "";
		String itemCode = "";
		String packCode = "";
		String hitemCode = "";
		String indentNo = "";
		String tranDateS = "";
		String unitRate="";
		String unit = "";
		int ctr = 0;
		int currentFormNo = 0;
		int cnt = 0;
		ArrayList errList = new ArrayList();
		ArrayList errFields = new ArrayList();
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		ConnDriver connDriver = new ConnDriver();
		StringBuffer errStringXml = new StringBuffer("<?xml version = \"1.0\"?> \r\n <Root> <Errors>");
		try
		{ 
			this.finCommon = new FinCommon();
			this.validator = new ValidatorEJB();
			conn = connDriver.getConnectDB("DriverITM");
			userId = getValueFromXTRA_PARAMS(xtraParams, "loginCode");
			if ((objContext != null) && (objContext.trim().length() > 0))
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			valueXmlString = new StringBuffer("<?xml version = \"1.0\"?> <Root> <header> <editFlag>");
			valueXmlString.append(editFlag).append("</editFlag> </header>");
			switch (currentFormNo)
			{
			case 1:
				parentNodeList = dom.getElementsByTagName("Detail1");
				parentNode = parentNodeList.item(0);
				childNodeList = parentNode.getChildNodes();
				valueXmlString.append("<Detail1>");
				int childNodeListLength = childNodeList.getLength();
				for (ctr = 0; ctr < childNodeListLength; ctr++)
				{
					childNode = childNodeList.item(ctr);
					childNodeName = childNode.getNodeName();

					System.out.println("CURRENT COLUMN IN  VALIDATION ["+childNodeName+"]");
					if (childNodeName.equalsIgnoreCase("tran_date"))
					{
						tranDateS=this.genericUtility.getColumnValue("tran_date", dom);
						if (tranDateS == null || tranDateS.equals("DD/MM/YY"))
						{
							errCode = "VMTRANDT";
							errList.add(errCode);
							errFields.add(childNodeName.toLowerCase());
						}
					}

					else if (childNodeName.trim().equalsIgnoreCase("indent_no"))
					{
						indentNo=this.genericUtility.getColumnValue("indent_no", dom);
						if (indentNo != null && indentNo.trim().length() > 0 )
						{
							itemCode=checkNull(this.genericUtility.getColumnValue("item_code", dom));
							sql = "select count(*) from indent where item_code = ?";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1, itemCode);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								cnt = rs.getInt(1);
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;	

							if(cnt == 0 ) 
							{
								errCode = "VTINDNO";
								errList.add(errCode);
								errFields.add(childNodeName.toLowerCase());
							}
						}
					}

					else if (childNodeName.equalsIgnoreCase("item_code"))
					{
						itemCode=this.genericUtility.getColumnValue("item_code", dom);
						if (itemCode != null && itemCode.trim().length() > 0 )
						{	
							sql = "select count(*) from item where item_code = ?";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1, itemCode);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								cnt = rs.getInt(1);
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;	
							if(cnt == 0 ) 
							{
								errCode = "VMITEM1";
								errList.add(errCode);
								errFields.add(childNodeName.toLowerCase());
							}
						}
						else
						{
							errCode = "VMITEMBLK";
							errList.add(errCode);
							errFields.add(childNodeName.toLowerCase());
						}
					}
					else if (childNodeName.trim().equalsIgnoreCase("unit__rate"))
					{
						unitRate=checkNull(this.genericUtility.getColumnValue("unit__rate", dom));		
						unit=checkNull(this.genericUtility.getColumnValue("unit", dom));
						itemCode=checkNull(this.genericUtility.getColumnValue("item_code", dom));
						if(!unit.trim().equalsIgnoreCase(unitRate.trim()))
						{
							sql =	"SELECT COUNT(*) FROM UOMCONV WHERE UNIT__FR = ? AND UNIT__TO = ? AND ITEM_CODE = ?"; 
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1, unit);
							pstmt.setString(2, unitRate);
							pstmt.setString(3, itemCode);
							rs = pstmt.executeQuery();
							if (rs.next())
							{
								cnt = rs.getInt(1);
								System.out.println("Count........1 "+cnt);
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;
							if(cnt == 0)
							{
								sql =	"SELECT COUNT(*) FROM UOMCONV WHERE UNIT__FR = ? AND UNIT__TO = ? AND ITEM_CODE = ?"; 
								pstmt = conn.prepareStatement(sql);
								pstmt.setString(1, unit);
								pstmt.setString(2, unitRate);
								pstmt.setString(3, "X");
								rs = pstmt.executeQuery();
								if (rs.next())
								{
									cnt = rs.getInt(1);
									System.out.println("Count........2 "+cnt);
								}
								rs.close();
								rs = null;
								pstmt.close();
								pstmt = null;

								if(cnt == 0)
								{
									sql =	"SELECT COUNT(*) FROM UOMCONV WHERE UNIT__FR = ? AND UNIT__TO = ? AND ITEM_CODE = ?"; 
									pstmt = conn.prepareStatement(sql);
									pstmt.setString(1, unitRate);
									pstmt.setString(2, unit);
									pstmt.setString(3, itemCode);
									rs = pstmt.executeQuery();
									if (rs.next())
									{
										cnt = rs.getInt(1);
									}
									rs.close();
									rs = null;
									pstmt.close();
									pstmt = null;

									if(cnt == 0)
									{
										sql =	"SELECT COUNT(*) FROM UOMCONV WHERE UNIT__FR = ? AND UNIT__TO = ? AND ITEM_CODE = ?"; 
										pstmt = conn.prepareStatement(sql);
										pstmt.setString(1, unitRate);
										pstmt.setString(2, unit);
										pstmt.setString(3, "X");
										rs = pstmt.executeQuery();
										if (rs.next())
										{
											cnt = rs.getInt(1);
										}
										rs.close();
										rs = null;
										pstmt.close();
										pstmt = null;
										if(cnt == 0)
										{

											errCode = "VTUNIT3";
											errList.add(errCode);
											errFields.add(childNodeName.toLowerCase());
										}
									}
								}
							}
						}
					}
					else if (childNodeName.equalsIgnoreCase("curr_code"))
					{
						currCode=this.genericUtility.getColumnValue("curr_code", dom);
						if(currCode != null && currCode.trim().length() > 0)
						{
							sql = "select count(*) from currency where curr_code= ?";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1, currCode);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								cnt = rs.getInt(1);									
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;

							if(cnt == 0)
							{
								errCode = "VTCURRCD1";
								errList.add(errCode);
								errFields.add(childNodeName.toLowerCase());
							}

						}
						else
						{
							errCode = "VECUR2";
							errList.add(errCode);
							errFields.add(childNodeName.toLowerCase());
						}
					}
					else if (childNodeName.equalsIgnoreCase("supp_code__pref"))
					{
						suppCodePref=this.genericUtility.getColumnValue("supp_code__pref", dom);
						if(suppCodePref != null && suppCodePref.trim().length() > 0)
						{
							sql = "select count(*) from supplier where supp_code = ?";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1, suppCodePref);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								cnt = rs.getInt(1);									
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;

							if(cnt == 0)
							{
								errCode = "VMSUPP1";
								errList.add(errCode);
								errFields.add(childNodeName.toLowerCase());
							}

						}
					}

					else if (childNodeName.equalsIgnoreCase("proj_code"))
					{
						projCode=this.genericUtility.getColumnValue("proj_code", dom);	
						if (projCode != null && projCode.trim().length() > 0 )
						{

							sql = "select count(*) from project where proj_code = ?";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1, projCode);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								cnt = rs.getInt(1);									
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;

							if(cnt == 0)
							{
								errCode = "VMPROJ1";
								errList.add(errCode);
								errFields.add(childNodeName.toLowerCase());
							}
						}						
					}	
					else if (childNodeName.trim().equalsIgnoreCase("pack_code"))
					{
						packCode=checkNull(this.genericUtility.getColumnValue("pack_code", dom));
						if(packCode != null && packCode.trim().length() > 0)
						{
							sql = "select count(*) from packing where pack_code = ?";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1, packCode);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								cnt = rs.getInt(1);
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;	
							if(cnt == 0 ) 
							{
								errCode = "VMPACKCD";
								errList.add(errCode);
								errFields.add(childNodeName.toLowerCase());
							}					
						}
					}
				}

				valueXmlString.append("</Detail1>");
				break;
			case 2:
				parentNodeList = dom.getElementsByTagName("Detail2");
				parentNode = parentNodeList.item(0);
				childNodeList = parentNode.getChildNodes();
				valueXmlString.append("<Detail2>");
				childNodeListLength = childNodeList.getLength();
				for (ctr = 0; ctr < childNodeListLength; ctr++)
				{
					childNode = childNodeList.item(ctr);
					childNodeName = childNode.getNodeName();
					if (childNodeName.trim().equalsIgnoreCase("item_code"))
					{
						itemCode=checkNull(this.genericUtility.getColumnValue("item_code", dom));
						hitemCode=checkNull(this.genericUtility.getColumnValue("item_code", dom1));
						if (itemCode != null && itemCode.trim().length() > 0 )
						{
							sql = "select count(*) from item where item_code = ?";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1, itemCode);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								cnt = rs.getInt(1);
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;	
							if(cnt == 0 ) 
							{
								errCode = "VMITEM1";
								errList.add(errCode);
								errFields.add(childNodeName.toLowerCase());
							}
							else if(itemCode.equalsIgnoreCase(hitemCode))
							{
								errCode = "VMITMSM";
								errList.add(errCode);
								errFields.add(childNodeName.toLowerCase());
							}

						}else
						{
							errCode = "VMITEMBLK";
							errList.add(errCode);
							errFields.add(childNodeName.toLowerCase());
						}
					}
					else if (childNodeName.trim().equalsIgnoreCase("unit__rate"))
					{
						unitRate=checkNull(this.genericUtility.getColumnValue("unit__rate", dom));		
						unit=checkNull(this.genericUtility.getColumnValue("unit", dom));
						itemCode=checkNull(this.genericUtility.getColumnValue("item_code", dom));
						if(!unit.trim().equalsIgnoreCase(unitRate.trim()))
						{
							sql =	"SELECT COUNT(*) FROM UOMCONV WHERE UNIT__FR = ? AND UNIT__TO = ? AND ITEM_CODE = ?"; 
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1, unit);
							pstmt.setString(2, unitRate);
							pstmt.setString(3, itemCode);
							rs = pstmt.executeQuery();
							if (rs.next())
							{
								cnt = rs.getInt(1);
								System.out.println("Count........1 "+cnt);
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;
							if(cnt == 0)
							{
								sql =	"SELECT COUNT(*) FROM UOMCONV WHERE UNIT__FR = ? AND UNIT__TO = ? AND ITEM_CODE = ?"; 
								pstmt = conn.prepareStatement(sql);
								pstmt.setString(1, unit);
								pstmt.setString(2, unitRate);
								pstmt.setString(3, "X");
								rs = pstmt.executeQuery();
								if (rs.next())
								{
									cnt = rs.getInt(1);
									System.out.println("Count........2 "+cnt);
								}
								rs.close();
								rs = null;
								pstmt.close();
								pstmt = null;

								if(cnt == 0)
								{
									sql =	"SELECT COUNT(*) FROM UOMCONV WHERE UNIT__FR = ? AND UNIT__TO = ? AND ITEM_CODE = ?"; 
									pstmt = conn.prepareStatement(sql);
									pstmt.setString(1, unitRate);
									pstmt.setString(2, unit);
									pstmt.setString(3, itemCode);
									rs = pstmt.executeQuery();
									if (rs.next())
									{
										cnt = rs.getInt(1);
										System.out.println("Count........3 "+cnt);
									}
									rs.close();
									rs = null;
									pstmt.close();
									pstmt = null;

									if(cnt == 0)
									{
										sql =	"SELECT COUNT(*) FROM UOMCONV WHERE UNIT__FR = ? AND UNIT__TO = ? AND ITEM_CODE = ?"; 
										pstmt = conn.prepareStatement(sql);
										pstmt.setString(1, unitRate);
										pstmt.setString(2, unit);
										pstmt.setString(3, "X");
										rs = pstmt.executeQuery();
										if (rs.next())
										{
											cnt = rs.getInt(1);
											System.out.println("Count........4 "+cnt);
										}
										rs.close();
										rs = null;
										pstmt.close();
										pstmt = null;
										if(cnt == 0)
										{

											errCode = "VTUNIT3";
											errList.add(errCode);
											errFields.add(childNodeName.toLowerCase());
										}
									}
								}
							}
						}
					}
					else if (childNodeName.trim().equalsIgnoreCase("pack_code"))
					{
						packCode=checkNull(this.genericUtility.getColumnValue("pack_code", dom));
						if(packCode != null && packCode.trim().length() > 0)
						{
							sql = "select count(*) from packing where pack_code = ?";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1, packCode);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								cnt = rs.getInt(1);
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;	
							if(cnt == 0 ) 
							{
								errCode = "VMPACKCD";
								errList.add(errCode);
								errFields.add(childNodeName.toLowerCase());
							}	
						}
					}
				}
				valueXmlString.append("</Detail1>");

			}
			int errListSize = errList.size();
			cnt = 0;
			String errFldName = null;
			if ((errList != null) && (errListSize > 0))
			{
				for (cnt = 0; cnt < errListSize; cnt++)
				{
					errCode = (String)errList.get(cnt);
					errFldName = (String)errFields.get(cnt);
					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;
					}
				}

				errStringXml.append("</Errors> </Root> \r\n");
			}
			else
			{
				errStringXml = new StringBuffer("");
			}

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

					conn.close();
				}
				conn = null;
			}
			catch (Exception d)
			{
				d.printStackTrace();
				throw new ITMException(d);
			}
		}
		errString = errStringXml.toString();
		return errString;
	}


	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 = "";
		System.out.println("hELLO PRINT");
		try
		{
			dom = parseString(xmlString);
			dom1 = parseString(xmlString1);
			if (xmlString2.trim().length() > 0)
			{
				dom2 = parseString(xmlString2);
			}
			System.out.println("HELLO1 PRINT");
			valueXmlString = itemChanged(dom, dom1, dom2, objContext, currentColumn, editFlag, xtraParams);
			System.out.println("VALUE HELLO PRINT["+valueXmlString+"]");
		}
		catch (Exception e)
		{
			System.out.println("Exception : [MiscVal][itemChanged( String, String )] :==>\n" + 
					e.getMessage());
			throw new ITMException(e);
		}
		System.out.println("VALUE HELLO PRINTA["+valueXmlString+"]");
		return valueXmlString;
	}

	public String itemChanged(Document dom, Document dom1, Document dom2, String objContext, String currentColumn, String editFlag, String xtraParams)
	throws RemoteException, ITMException
	{
		System.out.println("sTART PRINT ");
		NodeList parentNodeList = null;
		NodeList childNodeList = null;
		Node parentNode = null;
		Node childNode = null;
		String childNodeName = null;
		String sql = "";
		String currCode = "";
		String shName = "";
		String salesPers = "";
		String indentNo = "";
		String descrItem = "";
		String itemCode = "";
		String packCode = "";
		String projCode = "";
		String suppCodePref = "";
		String descrCurr = "";
		String descrProjCode = "";
		String itemSer = "";
		String unit = "";
		String unitRate = "";
		String unitPur ="";
		String descrPackCode = "";
		String status = "";
		String loginSite = "";
		String unitStd="";
		String descr = "";
		String siteCode = "";
		int ctr = 0;
		int currentFormNo = 0;
		double  quantity = 0.0;
		double stdExrt = 0.0;
		double amount = 0.0;
		double amountBc = 0.0;
		double rate = 0.0;
		double quantityStduom = 0.0 ;
		double rateStduom = 0.0 ;
		double convQtuomStduom = 0.0;

		int childNodeListLength = 0;
		java.util.Date statusDate = null;
		ArrayList errList = new ArrayList();
		ArrayList errFields = new ArrayList();
		SimpleDateFormat sdf;
		StringBuffer valueXmlString = new StringBuffer();
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		GenericUtility genericUtility = GenericUtility.getInstance();
		ConnDriver connDriver = new ConnDriver();
		DistCommon distCommon = new DistCommon();
		try
		{
			sdf = new SimpleDateFormat(genericUtility.getApplDateFormat());

			conn = connDriver.getConnectDB("DriverITM");
			conn.setAutoCommit(false);
			connDriver = null;
			this.finCommon = new FinCommon();
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(genericUtility.getApplDateFormat());
			if ((objContext != null) && (objContext.trim().length() > 0))
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			valueXmlString = new StringBuffer("<?xml version = \"1.0\"?> <Root> <header> <editFlag>");
			valueXmlString.append(editFlag).append("</editFlag> </header>");
			switch (currentFormNo)
			{
			case 1:
				parentNodeList = dom.getElementsByTagName("Detail1");
				parentNode = parentNodeList.item(0);
				childNodeList = parentNode.getChildNodes();
				valueXmlString.append("<Detail1>");
				childNodeListLength = childNodeList.getLength();
				do
				{
					childNode = childNodeList.item(ctr);
					childNodeName = childNode.getNodeName();
					if (childNodeName.equals(currentColumn))
					{
						childNode.getFirstChild();
					}

					ctr++;
				}
				while ((ctr < childNodeListLength) && (!childNodeName.equals(currentColumn)));
				System.out.println("CURRENT COLUMN ["+currentColumn+"]");

				if (currentColumn.trim().equalsIgnoreCase("itm_defaultedit"))
				{
				}
				else if (currentColumn.trim().equalsIgnoreCase("itm_default"))
				{
					loginSite = genericUtility.getValueFromXTRA_PARAMS(xtraParams, "loginSiteCode");
					valueXmlString.append("<site_code>").append("<![CDATA["+loginSite+"]]>").append("</site_code>");
					sql = "select descr from site where site_code = ?";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, loginSite);
					rs = pstmt.executeQuery();
					if (rs.next())
					{
						descr = checkNull(rs.getString(1));
					}
					rs.close();
					rs = null;
					pstmt.close();
					pstmt = null;


					valueXmlString.append("<site_descr>").append("<![CDATA["+descr+"]]>").append("</site_descr>");

					String currAppdate ="";
					java.sql.Timestamp currDate = null;
					currDate = new java.sql.Timestamp(System.currentTimeMillis()) ;
					currAppdate = new SimpleDateFormat(genericUtility.getApplDateFormat()).format(currDate);
					valueXmlString.append("<tran_date>").append("<![CDATA["+currAppdate+"]]>").append("</tran_date>");

				}

				else if (currentColumn.trim().equalsIgnoreCase("indent_no"))
				{
					indentNo=checkNull(this.genericUtility.getColumnValue("indent_no", dom));
					sql = "select item_code ,quantity ,supp_code__pref,proj_code ,status,status_date  from indent where ind_no = ?";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, indentNo);
					rs = pstmt.executeQuery();
					if(rs.next())
					{
						itemCode = checkNull(rs.getString(1));
						quantity = rs.getDouble(2);
						suppCodePref = checkNull(rs.getString(3));
						projCode = checkNull(rs.getString(4));
						status = checkNull(rs.getString(5));
						statusDate = rs.getDate(6);
					}
					rs.close();
					rs = null;
					pstmt.close();
					pstmt = null;

					valueXmlString.append("<item_code >").append("<![CDATA["+itemCode+"]]>").append("</item_code>");
					valueXmlString.append("<quantity >").append("<![CDATA["+quantity+"]]>").append("</quantity>");
					valueXmlString.append("<supp_code__pref >").append("<![CDATA["+suppCodePref+"]]>").append("</supp_code__pref>");
					valueXmlString.append("<proj_code >").append("<![CDATA["+projCode+"]]>").append("</proj_code>");
					valueXmlString.append("<status >").append("<![CDATA["+status+"]]>").append("</status>");
					if(statusDate == null)
					{
						valueXmlString.append("<status_date >").append("<![CDATA["+""+"]]>").append("</status_date>");
					}
					else
					{
						valueXmlString.append("<status_date >").append("<![CDATA["+sdf.format(statusDate)+"]]>").append("</status_date>");
					}
					sql = "select descr ,item_ser ,unit__rate from item where item_code = ?";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, itemCode);
					rs = pstmt.executeQuery();
					if(rs.next())
					{
						descrItem = checkNull(rs.getString(1));
						itemSer = checkNull(rs.getString(2));
						//unit = checkNull(rs.getString(3));
						unitRate = checkNull(rs.getString(3));
					}
					rs.close();
					rs = null;
					pstmt.close();
					pstmt = null;

					valueXmlString.append("<item_descr >").append("<![CDATA["+descrItem+"]]>").append("</item_descr>");
					valueXmlString.append("<item_ser>").append("<![CDATA["+itemSer+"]]>").append("</item_ser>");
					//valueXmlString.append("<unit>").append("<![CDATA["+unit+"]]>").append("</unit>");
					valueXmlString.append("<unit__rate>").append("<![CDATA["+unitRate+"]]>").append("</unit__rate>");
				}
				else if (currentColumn.trim().equalsIgnoreCase("item_code"))
				{
					itemCode=checkNull(this.genericUtility.getColumnValue("item_code", dom));					
					sql = "select descr ,item_ser ,unit ,unit__pur ,unit__rate from item where item_code = ?";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, itemCode);
					rs = pstmt.executeQuery();
					if(rs.next())
					{
						descrItem = checkNull(rs.getString(1));
						itemSer = checkNull(rs.getString(2));
						unit = checkNull(rs.getString(3));
						unitPur = rs.getString(4)== null?"":rs.getString(3);
						unitRate = rs.getString(5)== null?"":rs.getString(3);
					}
					rs.close();
					rs = null;
					pstmt.close();
					pstmt = null;

					valueXmlString.append("<item_descr >").append("<![CDATA["+descrItem+"]]>").append("</item_descr>");
					valueXmlString.append("<item_ser>").append("<![CDATA["+itemSer+"]]>").append("</item_ser>");

					valueXmlString.append("<unit>").append("<![CDATA["+unitPur+"]]>").append("</unit>");
					valueXmlString.append("<unit__std >").append("<![CDATA["+unit+"]]>").append("</unit__std>");
					valueXmlString.append("<unit__rate>").append("<![CDATA["+unitRate+"]]>").append("</unit__rate>");

					/*sql = "select unit from indent_det where item_code = ?";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, itemCode);
					rs = pstmt.executeQuery();
					if(rs.next())
					{
						unit = checkNull(rs.getString(1));
					}
					rs.close();
					rs = null;
					pstmt.close();
					pstmt = null;	
					valueXmlString.append("<unit >").append("<![CDATA["+unit+"]]>").append("</unit>");*/
				}	
				else if (currentColumn.trim().equalsIgnoreCase("site_code"))
				{
					siteCode = checkNull(this.genericUtility.getColumnValue("site_code",dom));
					sql = "select descr from site where site_code = ?";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, siteCode);
					rs = pstmt.executeQuery();
					if (rs.next())
					{
						descr = checkNull(rs.getString(1));
					}
					rs.close();
					rs = null;
					pstmt.close();
					pstmt = null;


					valueXmlString.append("<site_code>").append("<![CDATA["+siteCode+"]]>").append("</site_code>");	
					valueXmlString.append("<site_descr>").append("<![CDATA["+descr+"]]>").append("</site_descr>");	
				}

				else if (childNodeName.trim().equalsIgnoreCase("unit__rate"))
				{
					unitRate=checkNull(this.genericUtility.getColumnValue("unit__rate", dom));
					quantity=checkDoubleNull(this.genericUtility.getColumnValue("quantity", dom));
					unit=checkNull(this.genericUtility.getColumnValue("unit", dom));
					itemCode=checkNull(this.genericUtility.getColumnValue("item_code", dom));
					rate=checkDoubleNull(this.genericUtility.getColumnValue("rate", dom));
					if(unitRate.equalsIgnoreCase(unit))
					{
						valueXmlString.append("<conv__qty_stduom >").append("<![CDATA["+1+"]]>").append("</conv__qty_stduom>");
						valueXmlString.append("<conv__rate_stduom >").append("<![CDATA["+1+"]]>").append("</conv__rate_stduom>");
						valueXmlString.append("<rate__stduom >").append("<![CDATA["+quantity+"]]>").append("</rate__stduom>");
						valueXmlString.append("<quantity__stduom >").append("<![CDATA["+rate+"]]>").append("</quantity__stduom>");
					}
					else
					{

						System.out.print("unit unitStd itemCode quantity"+unit+""+unitStd+""+quantity);
						ArrayList convQtyList  = distCommon.getConvQuantityFact(unit, unitRate, itemCode, quantity, 0, conn);					
						System.out.println("convQtyList = "+convQtyList);

						convQtuomStduom = Double.parseDouble(convQtyList.get(0).toString());
						System.out.println("meffQty ["+convQtuomStduom+"]");

						quantityStduom = Double.parseDouble(convQtyList.get(1).toString());
						System.out.println("meffQty ["+quantityStduom+"]");

						valueXmlString.append("<conv__qty_stduom >").append("<![CDATA["+convQtuomStduom+"]]>").append("</conv__qty_stduom>");
						valueXmlString.append("<rate__stduom >").append("<![CDATA["+quantityStduom+"]]>").append("</rate__stduom>");

						ArrayList convRateList  = distCommon.getConvQuantityFact(unit, unitRate, itemCode, rate, 0, conn);					
						System.out.println("convRateList = "+convRateList);

						convQtuomStduom = Double.parseDouble(convRateList.get(0).toString());
						System.out.println("meffQty ["+convQtuomStduom+"]");

						rateStduom = Double.parseDouble(convRateList.get(1).toString());
						System.out.println("meffQty ["+quantityStduom+"]");

						valueXmlString.append("<conv__rate_stduom >").append("<![CDATA["+convQtuomStduom+"]]>").append("</conv__rate_stduom>");
						valueXmlString.append("<quantity__stduom >").append("<![CDATA["+rateStduom+"]]>").append("</quantity__stduom>");

					}
				}

				else if (currentColumn.trim().equalsIgnoreCase("curr_code"))
				{
					currCode = checkNull(genericUtility.getColumnValue("curr_code", dom));
					quantity=checkDoubleNull(this.genericUtility.getColumnValue("quantity", dom));
					rate=checkDoubleNull(this.genericUtility.getColumnValue("rate", dom));
					sql = "select descr,std_exrt from currency where curr_code = ?";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, currCode);
					rs = pstmt.executeQuery();
					if (rs.next())
					{
						descrCurr = checkNull(rs.getString(1));
						stdExrt = rs.getDouble(2);
					}
					rs.close();
					rs = null;
					pstmt.close();
					pstmt = null;

					amount = quantity*rate;
					amountBc = amount*stdExrt;

					System.out.println("amountsssssssss"+quantity+""+rate+""+stdExrt+""+amount+""+amountBc);
					valueXmlString.append("<amount>").append("<![CDATA["+amount+"]]>").append("</amount>");
					valueXmlString.append("<amount__bc>").append("<![CDATA["+amountBc+"]]>").append("</amount__bc>");	

					valueXmlString.append("<currency_descr>").append("<![CDATA["+descrCurr+"]]>").append("</currency_descr>");
					valueXmlString.append("<exch_rate>").append("<![CDATA["+stdExrt+"]]>").append("</exch_rate>");
					valueXmlString.append("<rate__stduom >").append("<![CDATA["+quantity+"]]>").append("</rate__stduom>");
					valueXmlString.append("<quantity__stduom >").append("<![CDATA["+rate+"]]>").append("</quantity__stduom>");

				}	
				else if(currentColumn.trim().equalsIgnoreCase("quantity") || currentColumn.trim().equalsIgnoreCase("rate"))
				{
					System.out.println(" 333333333333amountsssssssss"+quantity+""+rate+""+stdExrt+""+amount+""+amountBc);
					quantity=checkDoubleNull(this.genericUtility.getColumnValue("quantity", dom));
					rate=checkDoubleNull(this.genericUtility.getColumnValue("rate", dom));
					stdExrt=checkDoubleNull(this.genericUtility.getColumnValue("exch_rate", dom));
					amount = quantity*rate;
					amountBc = amount*stdExrt;

					System.out.println("amountsssssssss"+quantity+""+rate+""+stdExrt+""+amount+""+amountBc);
					valueXmlString.append("<amount>").append("<![CDATA["+amount+"]]>").append("</amount>");
					valueXmlString.append("<amount__bc>").append("<![CDATA["+amountBc+"]]>").append("</amount__bc>");	




					unitRate=checkNull(this.genericUtility.getColumnValue("unit__rate", dom));
					unit=checkNull(this.genericUtility.getColumnValue("unit", dom));
					itemCode=checkNull(this.genericUtility.getColumnValue("item_code", dom));
					if(unitRate.equalsIgnoreCase(unit))
					{
						valueXmlString.append("<conv__qty_stduom >").append("<![CDATA["+1+"]]>").append("</conv__qty_stduom>");
						valueXmlString.append("<conv__rate_stduom >").append("<![CDATA["+1+"]]>").append("</conv__rate_stduom>");
					}
					else
					{

						System.out.print("unit unitStd itemCode quantity"+unit+""+unitStd+""+quantity);
						ArrayList convQtyList  = distCommon.getConvQuantityFact(unit, unitRate, itemCode, quantity, 0, conn);					
						System.out.println("convQtyList = "+convQtyList);

						convQtuomStduom = Double.parseDouble(convQtyList.get(0).toString());
						System.out.println("meffQty ["+convQtuomStduom+"]");

						quantityStduom = Double.parseDouble(convQtyList.get(1).toString());
						System.out.println("meffQty ["+quantityStduom+"]");

						valueXmlString.append("<conv__qty_stduom >").append("<![CDATA["+convQtuomStduom+"]]>").append("</conv__qty_stduom>");
						valueXmlString.append("<rate__stduom >").append("<![CDATA["+quantityStduom+"]]>").append("</rate__stduom>");

						ArrayList convRateList  = distCommon.getConvQuantityFact(unit, unitRate, itemCode, rate, 0, conn);					
						System.out.println("convRateList = "+convRateList);

						convQtuomStduom = Double.parseDouble(convRateList.get(0).toString());
						System.out.println("meffQty ["+convQtuomStduom+"]");

						rateStduom = Double.parseDouble(convRateList.get(1).toString());
						System.out.println("meffQty ["+quantityStduom+"]");

						valueXmlString.append("<conv__rate_stduom >").append("<![CDATA["+convQtuomStduom+"]]>").append("</conv__rate_stduom>");
						valueXmlString.append("<quantity__stduom >").append("<![CDATA["+rateStduom+"]]>").append("</quantity__stduom>");

					}

				}

				else if (currentColumn.trim().equalsIgnoreCase("supp_code__pref"))
				{

					salesPers = checkNull(genericUtility.getColumnValue("supp_code__pref", dom));
					sql = "select sh_name from supplier where supp_code = ?";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, salesPers);
					rs = pstmt.executeQuery();
					if(rs.next())
					{
						shName = checkNull(rs.getString(1));									
					}
					rs.close();
					rs = null;
					pstmt.close();
					pstmt = null;

					valueXmlString.append("<supplier_sh_name>").append("<![CDATA["+shName+"]]>").append("</supplier_sh_name>");

				}
				else if (childNodeName.equalsIgnoreCase("proj_code"))
				{
					projCode=checkNull(this.genericUtility.getColumnValue("proj_code", dom));					
					sql = "select descr from project where proj_code = ?";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, projCode);
					rs = pstmt.executeQuery();
					if(rs.next())
					{
						descrProjCode = checkNull(rs.getString(1));									
					}
					rs.close();
					rs = null;
					pstmt.close();
					pstmt = null;

					valueXmlString.append("<project_descr>").append("<![CDATA["+descrProjCode+"]]>").append("</project_descr>");				
				}
				else if (currentColumn.trim().equalsIgnoreCase("pack_code"))
				{
					packCode=checkNull(this.genericUtility.getColumnValue("pack_code", dom));					
					sql = "select descr  from packing where pack_code = ?";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, packCode);
					rs = pstmt.executeQuery();
					if(rs.next())
					{
						descrPackCode = checkNull(rs.getString(1));
					}
					rs.close();
					rs = null;
					pstmt.close();
					pstmt = null;

					valueXmlString.append("<packing_descr >").append("<![CDATA["+descrPackCode+"]]>").append("</packing_descr>");
				}
				valueXmlString.append("</Detail1>");
				break;
			case 2:
				parentNodeList = dom.getElementsByTagName("Detail2");
				parentNode = parentNodeList.item(0);
				childNodeList = parentNode.getChildNodes();
				valueXmlString.append("<Detail2>");
				childNodeListLength = childNodeList.getLength();
				for (ctr = 0; ctr < childNodeListLength; ctr++)
				{
					childNode = childNodeList.item(ctr);
					childNodeName = childNode.getNodeName();
					if (childNodeName.trim().equalsIgnoreCase("itm_default"))
					{

					}
					else if (currentColumn.trim().equalsIgnoreCase("item_code"))
					{
						itemCode=checkNull(this.genericUtility.getColumnValue("item_code", dom));					
						sql = "select descr ,item_ser ,unit ,unit__rate from item where item_code = ?";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, itemCode);
						rs = pstmt.executeQuery();
						if(rs.next())
						{
							descrItem = checkNull(rs.getString(1));
							itemSer = checkNull(rs.getString(2));
							unit = checkNull(rs.getString(3));
							unitRate = checkNull(rs.getString(4));
						}
						rs.close();
						rs = null;
						pstmt.close();
						pstmt = null;

						valueXmlString.append("<item_descr>").append("<![CDATA["+descrItem+"]]>").append("</item_descr>");
						valueXmlString.append("<item_ser>").append("<![CDATA["+itemSer+"]]>").append("</item_ser>");
						valueXmlString.append("<unit>").append("<![CDATA["+unit+"]]>").append("</unit>");
						valueXmlString.append("<unit__rate>").append("<![CDATA["+unitRate+"]]>").append("</unit__rate>");

					}
					else if (childNodeName.trim().equalsIgnoreCase("unit__rate"))
					{
						unitRate=checkNull(this.genericUtility.getColumnValue("unit__rate", dom));
						quantity=checkDoubleNull(this.genericUtility.getColumnValue("quantity", dom));
						unit=checkNull(this.genericUtility.getColumnValue("unit", dom));
						itemCode=checkNull(this.genericUtility.getColumnValue("item_code", dom));
						rate=checkDoubleNull(this.genericUtility.getColumnValue("rate", dom));
						if(unitRate.equalsIgnoreCase(unit))
						{
							valueXmlString.append("<conv__qty_stduom >").append("<![CDATA["+1+"]]>").append("</conv__qty_stduom>");
							valueXmlString.append("<conv__rate_stduom >").append("<![CDATA["+1+"]]>").append("</conv__rate_stduom>");
						}
						else
						{

							System.out.print("unit unitStd itemCode quantity"+unit+""+unitStd+""+quantity);
							ArrayList convQtyList  = distCommon.getConvQuantityFact(unit, unitRate, itemCode, quantity, 0, conn);					
							System.out.println("convQtyList = "+convQtyList);

							convQtuomStduom = Double.parseDouble(convQtyList.get(0).toString());
							System.out.println("meffQty ["+convQtuomStduom+"]");

							quantityStduom = Double.parseDouble(convQtyList.get(1).toString());
							System.out.println("meffQty ["+quantityStduom+"]");

							valueXmlString.append("<conv__qty_stduom >").append("<![CDATA["+convQtuomStduom+"]]>").append("</conv__qty_stduom>");
							valueXmlString.append("<rate__stduom >").append("<![CDATA["+quantityStduom+"]]>").append("</rate__stduom>");

							ArrayList convRateList  = distCommon.getConvQuantityFact(unit, unitRate, itemCode, rate, 0, conn);					
							System.out.println("convRateList = "+convRateList);

							convQtuomStduom = Double.parseDouble(convRateList.get(0).toString());
							System.out.println("meffQty ["+convQtuomStduom+"]");

							rateStduom = Double.parseDouble(convRateList.get(1).toString());
							System.out.println("meffQty ["+quantityStduom+"]");

							valueXmlString.append("<conv__rate_stduom >").append("<![CDATA["+convQtuomStduom+"]]>").append("</conv__rate_stduom>");
							valueXmlString.append("<quantity__stduom >").append("<![CDATA["+rateStduom+"]]>").append("</quantity__stduom>");

						}
					}

					else if (currentColumn.trim().equalsIgnoreCase("quantity") || currentColumn.trim().equalsIgnoreCase("rate") )
					{
						quantity=checkDoubleNull(this.genericUtility.getColumnValue("quantity", dom));
						rate=checkDoubleNull(this.genericUtility.getColumnValue("rate", dom));
						amount = quantity*rate;
						System.out.println("amountsssssssss"+quantity+""+rate+""+stdExrt+""+amount+""+amountBc);
						valueXmlString.append("<amount>").append("<![CDATA["+amount+"]]>").append("</amount>");	
					}

					else if (currentColumn.trim().equalsIgnoreCase("pack_code"))
					{
						packCode=checkNull(this.genericUtility.getColumnValue("pack_code", dom));					
						sql = "select descr  from packing where pack_code = ?";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, packCode);
						rs = pstmt.executeQuery();
						if(rs.next())
						{
							descrPackCode = checkNull(rs.getString(1));
						}
						rs.close();
						rs = null;
						pstmt.close();
						pstmt = null;

						valueXmlString.append("<packing_descr >").append("<![CDATA["+descrPackCode+"]]>").append("</packing_descr>");
					}
				}


				valueXmlString.append("</Detail2>");

			}
			valueXmlString.append("</Root>");
		}
		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();
				throw new ITMException(d);
			}
		}
		return valueXmlString.toString();
	}



	private String checkNull(String str)
	{
		if(str == null)
		{
			return "";
		}
		else
		{
			return str ;
		}

	}

	private double checkDoubleNull(String str)
	{
		if(str == null || str.trim().length() == 0)
		{
			return 0.0;
		}
		else
		{
			return Double.parseDouble(str) ;
		}

	}

	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();
			while (rs.next())
			{
				msgType = rs.getString("MSG_TYPE");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			try
			{
				if (rs != null)
				{
					rs.close();
					rs = null;
				}
				if (pstmt != null)
				{
					pstmt.close();
					pstmt = null;
				}
			}
			catch (Exception e)
			{
				e.printStackTrace();
				throw new ITMException(e);
			}
		}
		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;
	}

}

