
package ibase.webitm.ejb.mfg;

//import java.rmi.RemoteException;
//import java.util.*;
//import java.util.Date;
import java.sql.*;
import org.w3c.dom.*;
//import javax.xml.parsers.*;
//import javax.ejb.*;
//import javax.naming.InitialContext;

//import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.*;
import ibase.webitm.utility.GenericUtility;
//import ibase.webitm.utility.TransIDGenerator;
//import ibase.ejb.*;
//import ibase.system.config.*;
import java.text.SimpleDateFormat;
//import ibase.utility.CommonConstants;
//import java.sql.*;
//import java.lang.*;
//import ibase.webitm.ejb.mfg.*;
import java.text.NumberFormat;
import java.text.DecimalFormat;

import ibase.system.config.*;
import ibase.webitm.ejb.sys.UtilMethods; // 10/12/07 manoharan

public class ExplodeBom
{
	//*************** change all decalaration properly
	// ************** null / empty string checking not proper

	String errString = "",matchPotency ="";
	String siteCode = "",adjPotency ="";
	String itemCode = "",usageType="";
	String itemCodeBomDet = "",allocateType ="";
	String lineType = "",locUsageType="";
	String bomCode = "",considerLotNoIssue ="";
	String bomCodeInner = "",item_type ="";
	String bomCodeItem = "",item_ser ="";
	String unit = "",unit_ref ="";
	String reqType = "";
	String itemRef = "";
	String suppSour = "";
	String suppSourInner = "";
	String suppSourItem = "";
	String lineTypeInner = "";
	String sDueDate = "";
	String isDueDate = "";
	String sQuantity = "";
	String critItem= "";
	String exp_lev = "";
	String xml_exp_lev ="";
	String itemActive = null;
	String siteItemActive = null;
	int pevoper = 0;
	SimpleDateFormat simpleDateFormat = null;
	double perQty = 0,minPotencyPerc =0,netWeight =0;
	double refQty = 0;
	double minQty = 0;
	double quantity = 0;
	double quantityUpdate = 0;
	double batchQty = 0;
	double roundTo = 0;
	char round;
	//int mfgLeadTime = 0; comment by rajendra  14/04/07
	//int qcLeadTime = 0;
	///int leadTime = 0;
	double 	 dpurLeadTime =0;
	double 	 dmfgLeadTime =0;
	double 	 dqcLeadTime =0;
	double 	 dleadTime =0;
	double 	 mfgLeadTime =0;
	double 	 qcLeadTime =0;
	double 	 leadTime =0;
	int count = 0;
	int operation = 0;
	int  ctr = 0;
	java.sql.Timestamp dueDate =  null;
	java.sql.Timestamp idueDate =  null;
	java.sql.Date tmpDate =  null;
	Document dom;
	UtilMethods utilMethods = null;

	transient GenericUtility genericUtility = null;
	ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();



//	public String explodeBom(String xmlString, Connection conn)//Commented by Jiten as per Kandarp Sir 13/11/06 - Connenction to be opened and to be closed here
	public String explodeBom(String xmlString) throws Exception //Added by Jiten 13/11/06 -
	{
		utilMethods = new UtilMethods(); // 10/12/07 manoharan
		genericUtility = GenericUtility.getInstance();
		StringBuffer valueXmlString = new StringBuffer("<Root>");
		PreparedStatement pstmt = null;
		PreparedStatement pstmtInner = null;
		PreparedStatement pstmtLead = null;
		ResultSet rs = null;
		ResultSet rsInner = null;
		ResultSet rsLead = null;
		Connection conn = null;//Added by Jiten 13/11/06 -
		ConnDriver connDriver = new ConnDriver();//Added by Jiten 13/11/06 -
		int iLeadTime = 0;
		int iiLeadTime = 0;
		String sqlLead = null, tempXML = null;
		String mfgLeadBasis = "F";
		double batchSizeLead = 0;

		try
		{
			conn = connDriver.getConnectDB("DriverITM");//Added by Jiten 13/11/06 -
			dom = genericUtility.parseString(xmlString);
			siteCode = genericUtility.getColumnValue("site_code",dom);
			itemCode = genericUtility.getColumnValue("item_code",dom);
			lineType = genericUtility.getColumnValue("line_type",dom);
			sQuantity = genericUtility.getColumnValue("quantity",dom);

			quantity = Double.parseDouble(sQuantity);
			bomCode = genericUtility.getColumnValue("bom_code",dom);
			sDueDate = genericUtility.getColumnValue("due_date",dom);
			exp_lev = genericUtility.getColumnValue("exp_lev",dom);
			//mfgLeadTime = Integer.parseInt( genericUtility.getColumnValue("mfg_lead_time",dom) );	   //comment by rajendra
			//qcLeadTime = Integer.parseInt( genericUtility.getColumnValue("qc_lead_time",dom) );	   //comment by rajendra
			mfgLeadTime = Double.parseDouble( genericUtility.getColumnValue("mfg_lead_time",dom) );
			qcLeadTime = Double.parseDouble( genericUtility.getColumnValue("qc_lead_time",dom) );
			dueDate =  java.sql.Timestamp.valueOf(sDueDate + " 00:00:00");

			leadTime = mfgLeadTime + qcLeadTime;
			iLeadTime = (int)leadTime;
			//System.out.println("Due Date Before.........."+dueDate) ;
			dueDate = utilMethods.RelativeDate(dueDate, iLeadTime * -1); // 10/12/07 manoharan
			//System.out.println("Due Date After..........."+dueDate) ;
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(genericUtility.getDBDateFormat());
			sDueDate = simpleDateFormat.format(dueDate);

			String sql = null;
			String sqlInner = null;

			/* 11-08-2007 manoharan siteitem no mandatory to be taken out
                       sql = "SELECT A.ITEM_CODE , A.ITEM_REF, C.UNIT, C.BATCH_QTY, A.QTY_PER, "
                               + "A.REQ_TYPE, A.MIN_QTY,A.OPERATION,A.CRIT_ITEM,"
                               + "A.ROUND,A.ROUND_TO, "
							   + " A.MATCH_POTENCY,A.MIN_POTENCY_PERC,A.ADJ_POTENCY,A.USAGE_TYPE,A.ALLOCATE_TYPE ,"
							   +" LOC_USAGE_TYPE,CONSIDER_LOT_NO__ISSUE	"
                               + "FROM  BOMDET A , SITEITEM B, BOM C "
                               + "WHERE A.ITEM_CODE  = B.ITEM_CODE "
                               + "AND A.BOM_CODE = C.BOM_CODE "
                               + "AND A.BOM_CODE = ? "
                               + "AND B.SITE_CODE  = ? "
                               + "AND CASE WHEN B.ACTIVE IS NULL THEN 'Y' ELSE B.ACTIVE END = 'Y' "
                               + "ORDER BY A.OPERATION, A.ITEM_REF, A.ITEM_CODE ";
			*/

		sql = " SELECT  BOMDET.ITEM_CODE , BOMDET.ITEM_REF , ITEM.UNIT , BOM.BATCH_QTY , BOMDET.QTY_PER , "
			+ " BOMDET.REQ_TYPE , BOMDET.MIN_QTY , BOMDET.OPERATION , BOMDET.CRIT_ITEM , BOMDET.ROUND , "
			+ " BOMDET.ROUND_TO , BOMDET.MATCH_POTENCY , BOMDET.MIN_POTENCY_PERC , BOMDET.ADJ_POTENCY , "
			+ " BOMDET.USAGE_TYPE , BOMDET.ALLOCATE_TYPE , BOMDET.LOC_USAGE_TYPE , BOMDET.CONSIDER_LOT_NO__ISSUE ,"
			+ " ITEM.ACTIVE  "
			+ " FROM BOMDET LEFT OUTER JOIN ITEM ON BOMDET.ITEM_CODE = ITEM.ITEM_CODE , BOM "
			+ " WHERE BOMDET.BOM_CODE = BOM.BOM_CODE "
			+ " AND BOM.BOM_CODE = ? "
			+ " ORDER BY BOMDET.OPERATION, BOMDET.ITEM_REF, BOMDET.ITEM_CODE ";
			// end 11-08-2007 manoharan
	     	pstmt = conn.prepareStatement(sql);
				pstmt.setString(1,bomCode);
				//pstmt.setString(2,siteCode);
				rs = pstmt.executeQuery();
				//System.out.println("Exploding " + siteCode + " " + itemCode + "  " + bomCode +" for " + sQuantity );

                       while (rs.next())
                       {

						       itemCodeBomDet = rs.getString(1);
                               itemRef = rs.getString(2);
                               unit = rs.getString(3);
                               batchQty = rs.getDouble(4);
                               perQty = rs.getDouble(5);
                               reqType = rs.getString(6);
                               minQty = rs.getDouble(7);
                               operation = rs.getInt(8);
                               critItem = rs.getString(9);
                               round = rs.getString(10).charAt(0);
                               roundTo = rs.getDouble(11);
							//added field by rajendra  on 14/06/07
							   matchPotency = rs.getString(12);
							   minPotencyPerc = rs.getDouble(13);
							   adjPotency =( rs.getString(14) ==null ? "" :rs.getString(14) );
							   usageType = rs.getString(15);
							   allocateType = rs.getString(16);
							   locUsageType = rs.getString(17);
							   considerLotNoIssue = rs.getString(18);
							   // 11-08-2007 manoharan
								itemActive = rs.getString("ACTIVE");
								System.out.println("12/06/14 itemActive before[ " +itemActive + "]");
								if (itemActive == null || "null".equals(itemActive))
								{
									itemActive = "Y";
								}
								System.out.println("12/06/14 itemActive after[ " +itemActive + "]");
								sqlInner = "SELECT SUPP_SOUR, BOM_CODE ,UNIT,NET_WEIGHT,item_type,item_ser "		 //added unit,NET_WEIGHT by rajendra
										 + "FROM  ITEM "
									 	 + "WHERE ITEM_CODE = ?";
								pstmtInner = conn.prepareStatement(sqlInner);
								pstmtInner.setString(1,itemCodeBomDet);
								rsInner = pstmtInner.executeQuery();
								if (rsInner.next())
								{
									suppSourItem = rsInner.getString(1);
									bomCodeItem = rsInner.getString(2);
									unit =	rsInner.getString(3);
									netWeight =	rsInner.getDouble(4);
									item_type =rsInner.getString(5);
									item_ser  =rsInner.getString(6);
									//System.out.println("unit.........."+unit) ;
									//System.out.println("netWeight.........."+netWeight)	;
								}
								rsInner.close();
								rsInner = null;//Added by JIten 13/11/06 -

								pstmtInner.close();
								pstmtInner = null;//Added by JIten 13/11/06 -
								sqlInner = "SELECT SUPP_SOUR, BOM_CODE, ACTIVE "
										 + "FROM SITEITEM "
									     + "WHERE SITE_CODE = ? "
										 + "AND ITEM_CODE = ? ";
								pstmtInner = conn.prepareStatement(sqlInner);
								pstmtInner.setString(1,siteCode);
								pstmtInner.setString(2,itemCodeBomDet);
								rsInner = pstmtInner.executeQuery();
								//rsInner = pstmtInner.executeQuery();//Commented by Jiten as this is repeated - 13/11/06
								if (rsInner.next())
								{
									suppSourInner = rsInner.getString(1);
									bomCodeInner = rsInner.getString(2);
									siteItemActive = rsInner.getString("ACTIVE");

									if( (suppSourInner == null) || (suppSourInner.trim().length() == 0) )
									{
										suppSourInner = suppSourItem;
									}
									if( (bomCodeInner == null) || (bomCodeInner.trim().length() == 0) )
									{
										bomCodeInner = bomCodeItem;
									}
								}
								suppSourInner= suppSourInner.trim();
								rsInner.close();
								rsInner = null;//Added by Jiten 13/11/06 -
								pstmtInner.close();
								pstmtInner = null;//Added by Jiten 13/11/06 -
								System.out.println("12/06/14 siteItemActive [ " +siteItemActive + "]");
								if (siteItemActive == null || "null".equals(siteItemActive))
								{
									siteItemActive = "Y";
								}
								System.out.println("12/06/14 siteItemActive after[ " +siteItemActive + "]");
								if ("N".equalsIgnoreCase(itemActive) || "N".equalsIgnoreCase(siteItemActive) )
								{
									continue;
								}
								// end 11-08-2007 manoharan
								//siteItemActive = null;


							 //added by rajendra 6/04/07
							   if(pevoper != operation){
								   ctr =1;
								   pevoper = operation;
								   //System.out.println("pevoper........." +pevoper);
							   }

							   if(exp_lev == null){
								  xml_exp_lev = operation +"."+ctr+".";
								  //System.out.println("xml_exp_lev...when exp_lev is null......" +xml_exp_lev);
							   }else{
								  xml_exp_lev = xml_exp_lev +"."+ctr+".";
								  //System.out.println("xml_exp_lev........." +xml_exp_lev);
							   }
							   ctr++;
							   //end by rajendra

							   //System.out.println("Item Code : " + itemCodeBomDet + "Item Ref : " + itemRef + " Line Type " + lineType);
                              if (lineType.equals("B"))
                              {
                                       if (reqType.equals("S"))
                                       {
                                               quantityUpdate = (quantity / batchQty) * perQty;
                                       }
                                       else if (reqType.equals("P"))
                                       {
                                               quantityUpdate = (perQty / batchQty) * quantity;
                                       }
                                       else if (reqType.equals("F"))
                                       {
                                               quantityUpdate = perQty;
                                       }
                                       //System.out.println("Qty :" + quantity + " Batch Qty :" + batchQty + " Item Ref : " + itemRef + " Qty Per " + perQty + " Req Type " + reqType + " Qty Update :" + quantityUpdate);

                                       quantityUpdate = itmDBAccessEJB.getRndamt(quantityUpdate, round, roundTo);

                                       if (quantityUpdate < minQty)
                                       {
                                               quantityUpdate = minQty;
                                       }
                               }
							else
								{
                                     quantityUpdate = quantity;
								}
				// check for bom or item if bom explode further
				/* 11-08-2007 manoharan commented and taken up as was required to check the active flag
				sqlInner = "SELECT SUPP_SOUR, BOM_CODE ,UNIT,NET_WEIGHT,item_type,item_ser "		 //added unit,NET_WEIGHT by rajendra
						 + "FROM  ITEM "
					 	 + "WHERE ITEM_CODE = ?";
				pstmtInner = conn.prepareStatement(sqlInner);
				pstmtInner.setString(1,itemCodeBomDet);
				rsInner = pstmtInner.executeQuery();
				if (rsInner.next())
				{
					suppSourItem = rsInner.getString(1);
					bomCodeItem = rsInner.getString(2);
					unit =	rsInner.getString(3);
					netWeight =	rsInner.getDouble(4);
					item_type =rsInner.getString(5);
					item_ser  =rsInner.getString(6);
					System.out.println("unit.........."+unit) ;
					System.out.println("netWeight.........."+netWeight)	;
				}
				rsInner.close();
				rsInner = null;//Added by JIten 13/11/06 -

				pstmtInner.close();

				pstmtInner = null;//Added by JIten 13/11/06 -
				sqlInner = "SELECT SUPP_SOUR, BOM_CODE "
						 + "FROM SITEITEM "
					     + "WHERE SITE_CODE = ? "
						 + "AND ITEM_CODE = ? ";
				pstmtInner = conn.prepareStatement(sqlInner);
				pstmtInner.setString(1,siteCode);
				pstmtInner.setString(2,itemCodeBomDet);
				rsInner = pstmtInner.executeQuery();
				//rsInner = pstmtInner.executeQuery();//Commented by Jiten as this is repeated - 13/11/06
				if (rsInner.next())
				{
					suppSourInner = rsInner.getString(1);
					bomCodeInner = rsInner.getString(2);

					if( (suppSourInner == null) || (suppSourInner.trim().length() == 0) )
					{
						suppSourInner = suppSourItem;
					}
					if( (bomCodeInner == null) || (bomCodeInner.trim().length() == 0) )
					{
						bomCodeInner = bomCodeItem;
					}
				}
				rsInner.close();
				rsInner = null;//Added by Jiten 13/11/06 -
				pstmtInner.close();
				pstmtInner = null;//Added by Jiten 13/11/06 -
				*/
				sqlInner = "SELECT COUNT(1) FROM ITEM WHERE ITEM_CODE = ? ";
				pstmtInner = conn.prepareStatement(sqlInner);
				pstmtInner.setString(1,itemCodeBomDet);
				rsInner = pstmtInner.executeQuery();

				if (rsInner.next())
				{
					count = rsInner.getInt(1);
					rsInner.close();
					rsInner = null;//Added by Jiten 13/11/06 -
					if (count == 0)
					{
						pstmtInner.close();
						pstmtInner = null;//Added by JIten 13/11/06 -
						sqlInner = "SELECT COUNT(1) FROM BOM WHERE BOM_CODE = ? ";
						pstmtInner = conn.prepareStatement(sqlInner);
						pstmtInner.setString(1,itemCodeBomDet);
						rsInner = pstmtInner.executeQuery();

						if (rsInner.next())
						{
							count = rsInner.getInt(1);

							if(count != 0)
							{
								lineTypeInner = "B";
							}
							else
							{
								errString = itmDBAccessEJB.getErrorString("","VTITEM1","","",conn);
								return errString;
							}
						}
						rsInner.close();
						rsInner = null;//Added by Jiten 13/11/06 -
						pstmtInner.close();
						pstmtInner = null;//Added by Jiten 13/11/06 -
					}
					else if (count > 0)
					{
						pstmtInner.close();
						pstmtInner = null;//Added by Jiten 13/11/06 -
						sqlInner = "SELECT case when ITEM_STRU is null then 'S' else ITEM_STRU end  FROM ITEM WHERE ITEM_CODE = ?";
						pstmtInner = conn.prepareStatement(sqlInner);
						pstmtInner.setString(1,itemCode);
						rsInner = pstmtInner.executeQuery();
						if (rsInner.next())
						{
							lineTypeInner = rsInner.getString(1);

								if (!lineTypeInner.equals("F"))
								{
									lineTypeInner = "I";
								}
						}
						rsInner.close();
						rsInner = null;//Added by Jiten 13/11/06 -
						pstmtInner.close();
						pstmtInner = null;//Added by Jiten 13/11/06 -
					}
				}
				else//Added by Jiten 13/11/06 -
				{
						rsInner.close();
						rsInner = null;
						pstmtInner.close();
						pstmtInner = null;
				}
                //System.out.println("Qty :" + quantity + " Batch Qty :" + batchQty + " Item Ref : " + itemRef + " Qty Per " + perQty + " Req Type " + reqType + " Qty Update :" + quantityUpdate);
				//System.out.println(" Item "  + itemCodeBomDet+ " Qty Per " + decFormat(perQty) + " Req Type " + reqType + " Qty Update :" + decFormat(quantityUpdate) );

				valueXmlString.append("<Detail>");
				valueXmlString.append("<site_code>").append("<![CDATA[").append(siteCode).append("]]>").append("</site_code>");
				valueXmlString.append("<bom_code>").append("<![CDATA[").append(bomCode).append("]]>").append("</bom_code>");
				valueXmlString.append("<item_code>").append("<![CDATA[").append(itemCodeBomDet).append("]]>").append("</item_code>");
				valueXmlString.append("<item_Ref>").append("<![CDATA[").append(itemRef).append("]]>").append("</item_Ref>");
				valueXmlString.append("<quantity_Ref>").append("<![CDATA[").append(sQuantity).append("]]>").append("</quantity_Ref>");	//add by rajendra
				valueXmlString.append("<quantity>").append("<![CDATA[").append(quantityUpdate).append("]]>").append("</quantity>");	   //add by rajendra
				// 24/12/07 manoharan for supp_sour = 'P'
				//if ( lineTypeInner.equals("B") )
				//{
				//	valueXmlString.append("<due_date>").append("<![CDATA[").append(sDueDate).append("]]>").append("</due_date>");		//add by rajendra
				//}
				//else
				//{
					// 22/06/10 manoharan to consider mfg lead time per batch
					sqlLead = "SELECT PUR_LEAD_TIME, QC_LEAD_TIME, MFG_LEAD_TIME, mfg_lead_basis, batch_size_lead "
							 + "FROM SITEITEM "
						     + "WHERE SITE_CODE = ? "
							 + "AND ITEM_CODE = ? ";
					pstmtLead = conn.prepareStatement(sqlLead);
					pstmtLead.setString(1,siteCode);
					pstmtLead.setString(2,itemCodeBomDet);
					rsLead = pstmtLead.executeQuery();
					if (rsLead.next())
					{

						dpurLeadTime = rsLead.getDouble(1);
						dqcLeadTime = rsLead.getDouble(2);
						dmfgLeadTime = rsLead.getDouble(3);
						mfgLeadBasis = rsLead.getString(4);
						batchSizeLead = rsLead.getDouble(5);
						
						pstmtLead.close();
						rsLead.close();
					}
					else
					{

						pstmtLead.close();
						rsLead.close();

						mfgLeadBasis = "F";
						batchSizeLead = 0;
						
						sqlLead = "SELECT PUR_LEAD_TIME, QC_LEAD_TIME, MFG_LEAD "
								 + "FROM ITEM "
							     + "WHERE ITEM_CODE = ? ";
						pstmtLead = conn.prepareStatement(sqlLead);
						pstmtLead.setString(1,itemCodeBomDet);
						rsLead = pstmtLead.executeQuery();
						if (rsLead.next())
						{

							dpurLeadTime = rsLead.getDouble(1);
							dqcLeadTime = rsLead.getDouble(2);
							dmfgLeadTime = rsLead.getDouble(3);
							pstmtLead.close();
							rsLead.close();
						}
						else
						{
							pstmtLead.close();
							rsLead.close();
						}
					}
					if ( "P".equals(suppSourInner) )
					{
						dleadTime = dpurLeadTime + dqcLeadTime;
					}
					else
					{
						dleadTime = dmfgLeadTime + dqcLeadTime;
					}
					/*if ( lineTypeInner.equals("B") )
					{
						dleadTime = dmfgLeadTime + dqcLeadTime;
					}
					else
					{
						dleadTime = dpurLeadTime + dqcLeadTime;
					}*/

					iiLeadTime = (int)dleadTime;
					//System.out.println("Due Date Before Inner.........."+dueDate) ;
					//idueDate =  java.sql.Timestamp.valueOf(sDueDate + " 00:00:00");
					idueDate = utilMethods.RelativeDate(dueDate, iiLeadTime * -1); // 10/12/07 manoharan
					//System.out.println("Item.........."+itemCodeBomDet + "  pur ..."+ dpurLeadTime + " qc " + dqcLeadTime + " mfg " +dmfgLeadTime + " lead " +dleadTime  ) ;
					//System.out.println("suppSourInner..................."+suppSourInner);
					//System.out.println("Due Date After Inner..........."+idueDate) ;
					simpleDateFormat = new SimpleDateFormat(genericUtility.getDBDateFormat());
					isDueDate = simpleDateFormat.format(idueDate);
					valueXmlString.append("<due_date>").append("<![CDATA[").append(isDueDate).append("]]>").append("</due_date>");		//add by rajendra
				//}
				// end 24/12/07 manoharan

				valueXmlString.append("<exp_lev>").append("<![CDATA[").append(xml_exp_lev).append("]]>").append("</exp_lev>");		  //add by rajendra
				valueXmlString.append("<operation>").append("<![CDATA[").append(operation).append("]]>").append("</operation>");	 //add by rajendra
				valueXmlString.append("<crit_Item>").append("<![CDATA[").append(critItem).append("]]>").append("</crit_Item>");		 //add by rajendra
				valueXmlString.append("<line_type>").append("<![CDATA[").append(lineTypeInner).append("]]>").append("</line_type>");
				valueXmlString.append("</Detail>");

				if ( lineTypeInner.equals("B") )
				{
				// 22/06/10 manoharan mfg_lead_time to be calculated as per batch size
				// 22/06/10 manoharan to calculate due_date based on batch size
				// if configured as lead time per batch
				if (mfgLeadBasis ==  null)
				{
					mfgLeadBasis = "F";
				}
				
				if ("B".equals(mfgLeadBasis) && quantityUpdate > batchSizeLead )
				{
					System.out.println("manohar dmfgLeadTime before [" + dmfgLeadTime + "]");
					dmfgLeadTime = quantityUpdate * dmfgLeadTime / batchSizeLead;
					System.out.println("manohar dmfgLeadTime after [" + dmfgLeadTime + "]");
				}
				// end 22/06/10 manoharan

					simpleDateFormat = new SimpleDateFormat(genericUtility.getDBDateFormat());
					sDueDate = simpleDateFormat.format(dueDate);
					StringBuffer tempXMLString = new StringBuffer("<Root>");
					tempXMLString.append("<Detail>");
					tempXMLString.append("<site_code>").append("<![CDATA[").append(siteCode).append("]]>").append("</site_code>");
					tempXMLString.append("<item_code>").append("<![CDATA[").append(itemCodeBomDet).append("]]>").append("</item_code>");
					tempXMLString.append("<quantity>").append("<![CDATA[").append(quantityUpdate).append("]]>").append("</quantity>");
					tempXMLString.append("<bom_code>").append("<![CDATA[").append(bomCodeInner).append("]]>").append("</bom_code>");		  //add by rajendra
					tempXMLString.append("<quantity_Ref>").append("<![CDATA[").append(sQuantity).append("]]>").append("</quantity_Ref>");	 //add by rajendra
					tempXMLString.append("<item_Ref>").append("<![CDATA[").append(itemRef).append("]]>").append("</item_Ref>");		   //add by rajendra
					tempXMLString.append("<line_type>").append("<![CDATA[").append(lineTypeInner).append("]]>").append("</line_type>");
					tempXMLString.append("<due_date>").append("<![CDATA[").append(isDueDate).append("]]>").append("</due_date>");
					tempXMLString.append("<operation>").append("<![CDATA[").append(operation).append("]]>").append("</operation>");	   //add by rajendra
					tempXMLString.append("<exp_lev>").append("<![CDATA[").append(xml_exp_lev).append("]]>").append("</exp_lev>");			 //add by rajendra
					tempXMLString.append("<crit_Item>").append("<![CDATA[").append(critItem).append("]]>").append("</crit_Item>");			 //add by rajendra
					tempXMLString.append("<mfg_lead_time>").append("<![CDATA[").append(dmfgLeadTime).append("]]>").append("</mfg_lead_time>");
					tempXMLString.append("<qc_lead_time>").append("<![CDATA[").append(dqcLeadTime).append("]]>").append("</qc_lead_time>");
					tempXMLString.append("</Detail>");
					tempXMLString.append("</Root>");
					//errString = explodeBom(tempXMLString.toString(), conn);//Commented by Jiten 13/11/06 -
					errString = explodeBom(tempXMLString.toString()); // Added by Jiten 13/11/06 -
					if ( errString != null && errString.trim().length() > 0 )
					{
						System.out.println("Error While Exploding Bom Further:..."+errString);
						//return errString;
						errString = errString.replaceAll("<Root>","");
						errString = errString.replaceAll("</Root>","");
						valueXmlString.append(errString);
						errString = "";
					}
				}
			}//loop end
			valueXmlString.append("</Root>");
			//rs.close(); //Below three lines commented by Jiten 31/10/06
			//pstmtInner.close();
			//pstmt.close();
		}
		catch(SQLException se)
		{
			System.out.println("SQLException : ExplodeBom :explodeBom()" +se.getMessage());
			se.printStackTrace();
			errString = se.getMessage();
			try {
				tempXML = valueXmlString.toString();
				tempXML = tempXML.replaceAll("<Root>","");
				tempXML = tempXML.replaceAll("</Root>","");
				valueXmlString  = new StringBuffer("<Root>" + tempXML + "</Detail></Root>");
			}
			catch (Exception x) {}
			throw (se);
		}
		catch(Exception e)
		{
			System.out.println("Exception :ExplodeBom :explodeBom():" + e.getMessage() + ":");
			this.errString = genericUtility.createErrorString(e);
			e.printStackTrace();
			errString = e.getMessage();
				try {
					tempXML = valueXmlString.toString();
					tempXML = tempXML.replaceAll("<Root>","");
					tempXML = tempXML.replaceAll("</Root>","");
					valueXmlString  = new StringBuffer("<Root>" + tempXML + "</Detail></Root>");
				}
				catch (Exception x) {}
			throw (e);
		}
		finally
		{
			try{
			if(rs != null){//Added by Jiten 13/11/06 -
				rs.close();
				rs = null;
			}
			if (rsLead != null) 
			{ 
				rsLead.close();
				rsLead = null; 
			}
						
			
			if (rsInner != null) 
			{ 
				rsInner.close();
				rsInner = null; 
			}
			if (pstmt != null) 
			{ 
				pstmt.close();
				pstmt = null; 
			}
			if (pstmtInner != null) 
			{ 
				pstmtInner.close();
				pstmtInner = null; 
			}
			if (pstmtLead != null) 
			{ 
				pstmtLead.close();
				pstmtLead = null; 
			}
			if(conn != null)
			{//Added by Jiten 13/11/06 -
				conn.close();
				conn = null;
			}
			}catch(Exception e){}
			/*if( (errString != null) && (errString.length() > 0) )
				return errString;
			else
				return valueXmlString.toString();*/
		}
		return valueXmlString.toString();
	}
	private String decFormat(double num)
	{
		NumberFormat nf = new DecimalFormat("##############0.000");
		nf.setMaximumFractionDigits(3);
		//nf.setMaximumIntegerDigits(10);
		return nf.format(num);
	}

}