/********************************************************
	Title : BomAmdIC[MF3GSUN024]
	Date  : 06/01/14
	Developer: Chandrashekar

 ********************************************************/
package ibase.webitm.ejb.mfg;
import ibase.system.config.ConnDriver;
import ibase.webitm.ejb.ValidatorEJB;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;

import java.text.SimpleDateFormat;
import java.util.*;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


import javax.ejb.Stateless; 
@Stateless 

public class BomAmdIC extends ValidatorEJB implements BomAmdICLocal, BomAmdICRemote
{
	GenericUtility genericUtility = GenericUtility.getInstance();

	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
		{
			if (xmlString != null && xmlString.trim().length() > 0 )
			{
				dom = parseString(xmlString);
				System.out.println("xmlString["+xmlString+"]");
			}
			if (xmlString1 != null && xmlString1.trim().length() > 0 )
			{
				dom1 = parseString(xmlString1);
				System.out.println("xmlString1["+xmlString1+"]");
			}
			if (xmlString2 != null && xmlString2.trim().length() > 0 )
			{
				dom2 = parseString(xmlString2);
				System.out.println("xmlString2["+xmlString2+"]");
				System.out.println("dom2["+dom2+"]");
			}
			
			errString = wfValData(dom, dom1, dom2, objContext, editFlag, xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception : [BOMAMD][wfValData( String, String )] :==>\n" + e.getMessage());
			throw new ITMException(e);
		}
		return(errString);
	}
	public String wfValData(Document dom, Document dom1, Document dom2, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
		{
			String childNodeName = null;
			String errString = "";
			String errCode = "";
			String userId = "";
			String sql = "";
			String errorType = "";
			int cnt = 0,cnt1= 0;
			int ctr=0;
			int childNodeListLength;
			NodeList parentNodeList = null;
			NodeList childNodeList = null;	
			Node parentNode = null;
			Node childNode = null;
			ArrayList<String> errList = new ArrayList<String>();
			ArrayList<String> errFields = new ArrayList<String>();
			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>");
			
			int currentFormNo =0,recCnt=0;
			
			String bomcode="",amdNo="",lineNo="";
			String active = "",confirmed = "";
			String bomcodeHdr = "",lineNoBOM = "",updateStatus="";
			try
			{
				System.out.println("@@@@@@@@ wfvaldata called");
				conn = connDriver.getConnectDB("DriverITM");
				connDriver = null;
				userId = getValueFromXTRA_PARAMS(xtraParams,"loginCode");
				if(objContext != null && objContext.trim().length()>0)
				{
					currentFormNo = Integer.parseInt(objContext);
				}
				switch(currentFormNo)
				{
				case 1 :
					parentNodeList = dom.getElementsByTagName("Detail1");
					parentNode = parentNodeList.item(0);
					childNodeList = parentNode.getChildNodes();
					childNodeListLength = childNodeList.getLength();
	
					for(ctr = 0; ctr < childNodeListLength; ctr++)
					{
						childNode = childNodeList.item(ctr);
						childNodeName = childNode.getNodeName();
						
						
						if(childNodeName.equalsIgnoreCase("bom_code"))
						{    
							bomcode = checkNull(genericUtility.getColumnValue("bom_code", dom));
							System.out.println("@@@@ bom_code["+bomcode+"]");
							if(bomcode == null || bomcode.trim().length() == 0)
							{
								errCode = "VTINVBOM1";
								errList.add(errCode);
								errFields.add(childNodeName.toLowerCase());
							}
							else
							{	
								errCode = isExist("bom","bom_code",bomcode,conn);
								if( "FALSE".equalsIgnoreCase(errCode ))		
								{
									errCode = "VTBOMCD3";
									errList.add(errCode);
									errFields.add(childNodeName.toLowerCase());
								}
									sql = "	select active,confirmed from bom where bom_code = ? ";
									pstmt =  conn.prepareStatement(sql);
									pstmt.setString(1,bomcode);
									rs = pstmt.executeQuery();
									if(rs.next())
									{   
										recCnt++;
										active = rs.getString("active");
										confirmed = rs.getString("confirmed");
									}
									rs.close();
									rs = null;
									pstmt.close(); 
									pstmt = null;
	
									if( recCnt == 0 )
									{
										errCode = "VTBOMCD3";
										errList.add(errCode);
										errFields.add(childNodeName.toLowerCase());
									} 
									else if(!("Y".equalsIgnoreCase(active)))
									{
										errCode = "VTNOTACT";
										errList.add(errCode);
										errFields.add(childNodeName.toLowerCase());
									}
								
									else if (!("Y".equalsIgnoreCase(confirmed)))
									{
										errCode = "VTNOTCONF1";
										errList.add(errCode);
										errFields.add(childNodeName.toLowerCase());
	
									}	
								
							}
						}
						
					}// end for
					break;  // case 1 end
	
				case 2:
					parentNodeList = dom.getElementsByTagName("Detail2");
					parentNode = parentNodeList.item(0);
					childNodeList = parentNode.getChildNodes();
					childNodeListLength = childNodeList.getLength();
					System.out.println("@@@@@@@@@@@@childNodeListLength["+childNodeListLength+"]");
					for(ctr = 0; ctr < childNodeListLength; ctr++)
					{
						childNode = childNodeList.item(ctr);
						childNodeName = childNode.getNodeName();
						if(childNodeName.equalsIgnoreCase("bom_code"))
						{
							bomcode = checkNull(genericUtility.getColumnValue("bom_code", dom));
							bomcodeHdr = checkNull(genericUtility.getColumnValue("bom_code", dom1));
							System.out.println("@@@@ bomcode["+bomcode+"]::::bomcodeHdr["+bomcodeHdr+"]");
							
						}
						if ( childNodeName.equalsIgnoreCase("line_no__bom"))
						{
								lineNoBOM = checkNull(genericUtility.getColumnValue("line_no__bom", dom));
								bomcode = checkNull(genericUtility.getColumnValue("bom_code", dom));
								lineNo = checkNull(genericUtility.getColumnValue("line_no", dom));
								System.out.println("@@@@@@lineNoBOM["+lineNoBOM+"]"+"bomcode["+bomcode+"]");	

								if(lineNoBOM == null || lineNoBOM.trim().length() == 0)
								{
									errCode = "VTLINEBLK";
									errList.add(errCode);
									errFields.add(childNodeName.toLowerCase());
								}
								else if( lineNoBOM != null && lineNoBOM.length() > 0 )	
								{
								sql = "select count(1) from bomdet where bom_code   = ? and line_no = ? ";
								pstmt =  conn.prepareStatement(sql);
								pstmt.setString(1,bomcode);
								pstmt.setString(2,lineNoBOM);
								rs = pstmt.executeQuery();
								if(rs.next())
								{
									cnt = rs.getInt(1);
								}
								rs.close();
								rs = null;
								pstmt.close();
								pstmt = null;

								if( cnt == 0 )
								{
									errCode = "VTBOMDTLN2";										
									errList.add(errCode);
									errFields.add(childNodeName.toLowerCase());
								}
								else
								{ 		
											java.util.HashSet<String> bomData=new java.util.HashSet<String>();
											for(int i =0; i< dom2.getElementsByTagName("Detail2").getLength();i++)
											{
												updateStatus = checkNull(getCurrentUpdateFlag(dom2.getElementsByTagName("Detail2").item(i)));
												System.out.println("lineno ="+genericUtility.getColumnValueFromNode("line_no",dom2.getElementsByTagName("Detail2").item(i)));
												bomcode =  checkNull(genericUtility.getColumnValueFromNode("bom_code",dom2.getElementsByTagName("Detail2").item(i)));
												lineNoBOM =  checkNull(genericUtility.getColumnValueFromNode("line_no__bom",dom2.getElementsByTagName("Detail2").item(i)));
												System.out.println("bomcode = ["+bomcode+"]"+"Update Status =[ "+updateStatus+"]"+"lineNoBOM["+lineNoBOM+"]");
																											
												if(!updateStatus.equalsIgnoreCase("D"))
												{								
													if ( ! bomData.contains(bomcode.trim()+"@"+lineNoBOM.trim()))
													{
														bomData.add(bomcode.trim()+"@"+lineNoBOM.trim());										
													}
													else
													{	errCode = "VTDUPBOMLN";
													errList.add(errCode);
													errFields.add(childNodeName.toLowerCase());
													}										
													System.out.println("bomData LIST["+bomData+"]");
												}
											}
										
									}
								}
						}
							
						
					} // end for
					break;  // case 1 end
	
				
	
				}
	
				int errListSize = errList.size();
				cnt = 0;
				String errFldName = null;
				if(errList != null && errListSize > 0)
				{
					for(cnt = 0; cnt < errListSize; cnt ++)
					{
						errCode = errList.get(cnt);
						errFldName = errFields.get(cnt);
						System.out.println("errCode .........." + errCode);
						errString = getErrorString(errFldName, errCode, userId);
						errorType = errorType(conn , errCode);
						if(errString.length() > 0)
						{
							String bifurErrString = errString.substring(errString.indexOf("<Errors>") + 8, errString.indexOf("<trace>"));
							bifurErrString = bifurErrString + errString.substring(errString.indexOf("</trace>") + 8, errString.indexOf("</Errors>"));
							errStringXml.append(bifurErrString);
							errString = "";
						}
						if(errorType.equalsIgnoreCase("E"))
						{
							break;
						}
					}
					errList.clear();
					errList = null;
					errFields.clear();
					errFields = null;
					errStringXml.append("</Errors> </Root> \r\n");
				}
				else
				{
					errStringXml = new StringBuffer("");
				}
			}
			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;
		}

	//end of validation

	// method for item change
	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("xmlString............."+xmlString);
		System.out.println("xmlString1............"+xmlString);
		System.out.println("xmlString2............"+xmlString);
		try
		{   
			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(xmlString2);
			}
			valueXmlString = itemChanged(dom, dom1, dom2, objContext, currentColumn, editFlag, xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception : [RoadPermitIC][itemChanged( String, String )] :==>\n" + e.getMessage());
			throw new ITMException(e);
		}
		return valueXmlString;
	}


	// method for item change
	public String itemChanged(Document dom, Document dom1, Document dom2, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException, ITMException
	{
	String childNodeName = null;
	String sql = "",amdNo="";
	StringBuffer valueXmlString = new StringBuffer();
	int ctr = 0;
	NodeList parentNodeList = null;
	NodeList childNodeList = null;
	Node parentNode = null;
	Node childNode = null;
	Connection conn = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null ;
	GenericUtility genericUtility = GenericUtility.getInstance();
	ConnDriver connDriver = new ConnDriver();
	
	int currentFormNo =0;
	String columnValue="",bomcode="",type="",descr="",sitecode="",sitedescr="",itemcode="",itemdescr="";
	String unit="",unitpotency="";
	
	String linenobom="",itemref="",unitalt="",matchpoten="",adjpotency="",qtyperunit="";
	String criticalitem="",supcodemfg="",mfgref="",issvariencetype="",issvarvaltype="",locusagetype="";
	double minpotency =0,minqty=0,qtyper=0,qtyperalt=0,leadtime=0,conqty=0,maxbatchqty=0,minbatchqty=0,batchqty=0;
	double issVarience = 0;
	
	Timestamp validupto=null;
	
	
	try
	{
		Calendar currentDate = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(genericUtility.getApplDateFormat());
		String sysDate = sdf.format(currentDate.getTime());
		System.out.println("Now the date is :=>  " + sysDate);

		conn = connDriver.getConnectDB("DriverITM");
		conn.setAutoCommit(false);
		connDriver = null;

		if(objContext != null && objContext.trim().length()>0)
		{
			currentFormNo = Integer.parseInt(objContext.trim());
		}

		valueXmlString = new StringBuffer("<?xml version = \"1.0\"?> <Root> <header> <editFlag>");
		valueXmlString.append(editFlag).append("</editFlag> </header>");

		System.out.println("**********ITEMCHANGE FOR CASE"+currentFormNo+"**************");
		switch(currentFormNo)
		{
		case 1 :
			parentNodeList = dom.getElementsByTagName("Detail1");
			parentNode = parentNodeList.item(0);
			childNodeList = parentNode.getChildNodes();
			ctr = 0; 
			valueXmlString.append("<Detail1>");
			int childNodeListLength = childNodeList.getLength();
			do
			{ 
				childNode = childNodeList.item(ctr);
				childNodeName = childNode.getNodeName();
				if(childNodeName.equals(currentColumn))
				{
					if (childNode.getFirstChild()!= null)
					{
						columnValue = childNode.getFirstChild().getNodeValue().trim();
					}
				}
				ctr++;
			}
			while(ctr < childNodeListLength  && !childNodeName.equals(currentColumn));
			System.out.println("[" + currentColumn + "] ==> '" + columnValue + "'");
			if(currentColumn.trim().equalsIgnoreCase("itm_defaultedit"))
			{	
				bomcode = checkNull(genericUtility.getColumnValue("bom_code", dom));
				System.out.println("@@@@@@@@ itm_defaultedit called @@@@@@@@");
				
				valueXmlString.append("<bom_code protect = \"1\">").append("<![CDATA["+bomcode+"]]>").append("</bom_code>");
								
			}
			else if(currentColumn.trim().equalsIgnoreCase("itm_default"))
			{
				System.out.println("@@@@@@@@ itm_default called @@@@@@@@");
				valueXmlString.append("<amd_date>").append("<![CDATA["+sysDate+"]]>").append("</amd_date>");
								
			}
			else if(currentColumn.trim().equalsIgnoreCase("bom_code"))
			{	
				bomcode = checkNull(genericUtility.getColumnValue("bom_code", dom));
				System.out.println("@@@@Bom code called["+bomcode+"]");
				amdNo = checkNull(genericUtility.getColumnValue("amd_no", dom));
				sql = " select bom_code,type,descr,site_code,item_code," +
						"batch_qty,unit,unit__potency,min_batch_qty,max_batch_qty," +
						"valid_upto from bom where bom_code = ? ";
				pstmt =  conn.prepareStatement(sql);
				pstmt.setString(1,bomcode);
				rs = pstmt.executeQuery();
				if(rs.next())
				{
					bomcode =  rs.getString("bom_code");
					type =  rs.getString("type");
					descr =  rs.getString("descr");
					sitecode =  rs.getString("site_code");					
					itemcode =  rs.getString("item_code");					
					batchqty =  rs.getDouble("batch_qty");
					unit =  rs.getString("unit");
					unitpotency =  checkNull(rs.getString("unit__potency"));
					minbatchqty =  rs.getDouble("min_batch_qty");
					maxbatchqty =  rs.getDouble("max_batch_qty");
					validupto = rs.getTimestamp("valid_upto");
					
					
					
				}
				rs.close();
				rs = null;
				pstmt.close(); 
				pstmt = null;
				valueXmlString.append("<type>").append("<![CDATA["+type+"]]>").append("</type>");
				valueXmlString.append("<descr>").append("<![CDATA["+descr+"]]>").append("</descr>");
				valueXmlString.append("<site_code>").append("<![CDATA["+sitecode+"]]>").append("</site_code>");
				valueXmlString.append("<item_code>").append("<![CDATA["+itemcode+"]]>").append("</item_code>");
				valueXmlString.append("<batch_qty>").append("<![CDATA["+batchqty+"]]>").append("</batch_qty>");
				valueXmlString.append("<unit>").append("<![CDATA["+unit+"]]>").append("</unit>");
				valueXmlString.append("<unit__potency>").append("<![CDATA["+unitpotency+"]]>").append("</unit__potency>");
				valueXmlString.append("<min_batch_qty>").append("<![CDATA["+minbatchqty+"]]>").append("</min_batch_qty>");
				valueXmlString.append("<max_batch_qty>").append("<![CDATA["+maxbatchqty+"]]>").append("</max_batch_qty>");
										
				if(validupto!=null)	
				{	String validuptodt = sdf.format(validupto.getTime());
					valueXmlString.append("<valid_upto>").append("<![CDATA["+validuptodt+"]]>").append("</valid_upto>");
					valueXmlString.append("<valid_upto__o>").append("<![CDATA["+validuptodt+"]]>").append("</valid_upto__o>");
					
				}else
				{					
					
					valueXmlString.append("<valid_upto__o protect = \"1\">").append("<![CDATA[]]>").append("</valid_upto__o>");
				}
				if ( sitecode!= null && sitecode.length() > 0  )
				{
					sql = "select descr from site where site_code = ? ";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, sitecode);
					rs = pstmt.executeQuery();
					if(rs.next())
					{
						sitedescr = checkNull( rs.getString(1));									
						
					}
					rs.close();
					rs = null;
					pstmt.close(); 
					pstmt = null;
					valueXmlString.append("<site_descr>").append("<![CDATA["+checkNull(sitedescr)+"]]>").append("</site_descr>");
				}else
				{
					valueXmlString.append("<site_descr protect = \"1\">").append("<![CDATA[]]>").append("</site_descr>");
				}
				if ( itemcode!= null && itemcode.length() > 0  )
				{
					sql = "select descr from item where item_code = ? ";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, itemcode);
					rs = pstmt.executeQuery();
					if(rs.next())
					{
						itemdescr = checkNull( rs.getString(1));									
						
					}
					rs.close();
					rs = null;
					pstmt.close(); 
					pstmt = null;
					valueXmlString.append("<item_descr>").append("<![CDATA["+checkNull(itemdescr)+"]]>").append("</item_descr>");
				}else
				{
					valueXmlString.append("<item_descr protect = \"1\">").append("<![CDATA[]]>").append("</item_descr>");
				}
				
			}
			
			

			valueXmlString.append("</Detail1>");
			break;
			// case 2 start
		case 2 :
			parentNodeList = dom.getElementsByTagName("Detail2");
			parentNode = parentNodeList.item(0);
			childNodeList = parentNode.getChildNodes();
			ctr = 0; 
			valueXmlString.append("<Detail2>");
			childNodeListLength = childNodeList.getLength();
			do
			{ 
				childNode = childNodeList.item(ctr);
				childNodeName = childNode.getNodeName();
				if(childNodeName.equals(currentColumn))
				{
					if (childNode.getFirstChild()!= null)
					{
						columnValue = childNode.getFirstChild().getNodeValue().trim();
						System.out.println("[" + currentColumn + "] ==> '" + columnValue + "'");
					}
				}
				ctr++;
			}
			while(ctr < childNodeListLength  && !childNodeName.equals(currentColumn));
			System.out.println("[" + currentColumn + "] ==> '" + columnValue + "'");
			if ( currentColumn.trim().equalsIgnoreCase("itm_default") )
			{
				amdNo = checkNull(genericUtility.getColumnValue("amd_no", dom1));
				System.out.println("@@@@@@@@@@@@@***** dom 1 *******[amdNo]::::::["+amdNo+"]");
				valueXmlString.append("<amd_no >").append("<![CDATA["+amdNo+"]]>").append("</amd_no>");
				bomcode = checkNull(genericUtility.getColumnValue("bom_code", dom1));
				valueXmlString.append("<bom_code >").append("<![CDATA["+bomcode+"]]>").append("</bom_code>");
				
			}
			else if ( currentColumn.trim().equalsIgnoreCase("line_no__bom") )
			{
				linenobom = checkNull(genericUtility.getColumnValue("line_no__bom", dom));
				bomcode = checkNull(genericUtility.getColumnValue("bom_code", dom));
				System.out.println("@@@bomcode["+bomcode+"]");			 
									
					sql = "select item_code,unit,item_ref,unit__alt,match_potency,min_potency_perc,adj_potency,qty_per__alt,qty_per," +
							"qty_per__unit,conv__qty_stduom,min_qty,req_lt,crit_item,supp_code__mfg,mfg_ref,iss_varience_type," +
							"iss_var_valtype,loc_usage_type ,iss_varience " +			
							" from  bomdet where bom_code = ? and line_no = ? ";					
							
					pstmt =  conn.prepareStatement(sql);
					pstmt.setString(1,bomcode );
					pstmt.setString(2,linenobom );
					rs = pstmt.executeQuery();
					if(rs.next())
					{
						itemcode = checkNull( rs.getString("item_code")); 
						unit = checkNull( rs.getString("unit")); 
						itemref = rs.getString("item_ref");
						unitalt = rs.getString("unit__alt");
						matchpoten = rs.getString("match_potency");
						minpotency = rs.getDouble("min_potency_perc");
						adjpotency = rs.getString("adj_potency");
						qtyperalt = rs.getDouble("qty_per__alt");
						qtyper = rs.getDouble("qty_per");
						qtyperunit = rs.getString("qty_per__unit");
						conqty = rs.getDouble("conv__qty_stduom");
						minqty = rs.getDouble("min_qty");
						leadtime = rs.getDouble("req_lt");
						criticalitem = rs.getString("crit_item");
						supcodemfg = rs.getString("supp_code__mfg");
						mfgref = checkNull(rs.getString("mfg_ref"));
						issvariencetype = rs.getString("iss_varience_type");
						issvarvaltype = rs.getString("iss_var_valtype");
						locusagetype = rs.getString("loc_usage_type");
						issVarience = rs.getDouble("iss_varience");//added by kunal on 05/feb/14
						
						
					}
					rs.close();
					rs = null;
					pstmt.close(); 
					pstmt = null;	
					System.out.println("issvariencetype["+issvariencetype+"]");
					valueXmlString.append("<item_code>").append("<![CDATA["+checkNull(itemcode)+"]]>").append("</item_code>");
					valueXmlString.append("<unit>").append("<![CDATA["+checkNull(unit)+"]]>").append("</unit>");
					valueXmlString.append("<item_ref>").append("<![CDATA["+checkNull(itemref)+"]]>").append("</item_ref>");
					valueXmlString.append("<unit__alt>").append("<![CDATA["+checkNull(unitalt)+"]]>").append("</unit__alt>");
					valueXmlString.append("<match_potency>").append("<![CDATA["+checkNull(matchpoten)+"]]>").append("</match_potency>");
					valueXmlString.append("<min_potency_perc>").append("<![CDATA["+minpotency+"]]>").append("</min_potency_perc>");
					valueXmlString.append("<adj_potency>").append("<![CDATA["+checkNull(adjpotency)+"]]>").append("</adj_potency>");
					valueXmlString.append("<qty_per__alt>").append("<![CDATA["+qtyperalt+"]]>").append("</qty_per__alt>");
					valueXmlString.append("<qty_per>").append("<![CDATA["+qtyper+"]]>").append("</qty_per>");
					valueXmlString.append("<qty_per__unit>").append("<![CDATA["+checkNull(qtyperunit)+"]]>").append("</qty_per__unit>");
					valueXmlString.append("<conv__qty_stduom>").append("<![CDATA["+conqty+"]]>").append("</conv__qty_stduom>");
					valueXmlString.append("<min_qty>").append("<![CDATA["+minqty+"]]>").append("</min_qty>");
					valueXmlString.append("<req_lt>").append("<![CDATA["+leadtime+"]]>").append("</req_lt>");
					valueXmlString.append("<crit_item>").append("<![CDATA["+checkNull(criticalitem)+"]]>").append("</crit_item>");
					valueXmlString.append("<supp_code__mfg>").append("<![CDATA["+checkNull(supcodemfg)+"]]>").append("</supp_code__mfg>");
					valueXmlString.append("<supp_code__mfg__o>").append("<![CDATA["+checkNull(supcodemfg)+"]]>").append("</supp_code__mfg__o>");
					valueXmlString.append("<mfg_ref>").append("<![CDATA["+checkNull(mfgref)+"]]>").append("</mfg_ref>");
					valueXmlString.append("<mfg_ref__o>").append("<![CDATA["+checkNull(mfgref)+"]]>").append("</mfg_ref__o>");
					valueXmlString.append("<iss_varience_type>").append("<![CDATA["+checkNull(issvariencetype)+"]]>").append("</iss_varience_type>");
					valueXmlString.append("<iss_varience_type__o>").append("<![CDATA["+checkNull(issvariencetype)+"]]>").append("</iss_varience_type__o>");
					valueXmlString.append("<iss_var_valtype>").append("<![CDATA["+checkNull(issvarvaltype)+"]]>").append("</iss_var_valtype>");
					valueXmlString.append("<iss_var_valtype__o>").append("<![CDATA["+checkNull(issvarvaltype)+"]]>").append("</iss_var_valtype__o>");
					valueXmlString.append("<loc_usage_type>").append("<![CDATA["+checkNull(locusagetype)+"]]>").append("</loc_usage_type>");
					valueXmlString.append("<loc_usage_type__o>").append("<![CDATA["+checkNull(locusagetype)+"]]>").append("</loc_usage_type__o>");
					valueXmlString.append("<iss_varience>").append("<![CDATA["+issVarience+"]]>").append("</iss_varience>");
					valueXmlString.append("<iss_varience__o>").append("<![CDATA["+issVarience+"]]>").append("</iss_varience__o>");
				
			}	
				
			valueXmlString.append("</Detail2>");
			break;

				}	
		valueXmlString.append("</Root>");
	}
	catch(Exception e)
	{
		e.printStackTrace();
		System.out.println("Exception ::"+ 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();
		}			
	}
	return valueXmlString.toString();
}
	 private String getCurrentUpdateFlag(Node currDetail)
		{
			NodeList currDetailList = null;
			String updateStatus = "",nodeName = "";
			int currDetailListLength = 0;

			currDetailList = currDetail.getChildNodes();
			currDetailListLength = currDetailList.getLength();
			for (int i=0;i< currDetailListLength;i++)
			{
				nodeName = currDetailList.item(i).getNodeName();
				if (nodeName.equalsIgnoreCase("Attribute"))
				{
					updateStatus =currDetailList.item(i).getAttributes().getNamedItem("updateFlag").getNodeValue();
					break;
				}
			}
			return updateStatus;
		}
	private String checkNull(String input) 
	{
		if(input == null)
		{
			input = "";
		}
		return input;
	}
	
	private String isExist(String table, String field, String value,Connection conn) throws SQLException
	{
		String sql = "",retStr="";
		PreparedStatement pstmt = null;
		ResultSet rs = null ;
		int cnt=0;

		sql = " SELECT COUNT(1) FROM "+ table + " WHERE " + field + " = ? ";
		pstmt =  conn.prepareStatement(sql);
		pstmt.setString(1,value);
		rs = pstmt.executeQuery();
		if(rs.next())
		{
			cnt = rs.getInt(1);
		}
		rs.close();
		rs = null;
		pstmt.close(); 
		pstmt = null;
		if( cnt > 0)
		{
			retStr = "TRUE";
		}
		if( cnt == 0 )
		{
			retStr = "FALSE";
		}
		System.out.println("@@@@ isexist["+value+"]:::["+retStr+"]:::["+cnt+"]");
		return retStr;
	}

	
	private String errorType(Connection conn , String errorCode) throws ITMException
	{
		String msgType = "";
		PreparedStatement pstmt = null ; 
		ResultSet rs = null;
		try
		{			
			String  sql = "SELECT MSG_TYPE FROM MESSAGES WHERE MSG_NO = ?";
			pstmt = conn.prepareStatement(sql);			
			pstmt.setString(1,errorCode);			
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				msgType = rs.getString("MSG_TYPE");
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			throw new ITMException(ex);
		}		
		finally
		{
			try
			{
				if(rs != null)
				{
					rs.close();
					rs = null;
				}
				if(pstmt != null)
				{
					pstmt.close();
					pstmt = null;
				}
			}
			catch(Exception e)
			{
				e.printStackTrace();
				throw new ITMException(e);
			}
		}		
		return msgType;
	}



}	
