/********************************************************
        Title : BomAltAmdIC
        Date  : 29/01/14
        Developer: Sagar Mane
        Request id:MF3JSUN002

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

import ibase.system.config.ConnDriver;
import ibase.webitm.ejb.ValidatorEJB;
import ibase.webitm.ejb.dis.DistCommon;
//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 BomAltAmdIC extends ValidatorEJB implements BomAltAmdICLocal, BomAltAmdICRemote
{
	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 supMfgCode=""; 
		String loginCode="";
		String loginSiteCode="";
		String bomCode="";
     	String itemCode = "";
        String itemRef="";
        String itemCodeAlt="";
     	System.out.println("editFlag ---->>>["+editFlag+"]");
		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");
			loginSiteCode = checkNull(genericUtility.getValueFromXTRA_PARAMS(xtraParams, "loginSiteCode"));
			if ((objContext != null) && (objContext.trim().length() > 0))
			{
				currentFormNo = Integer.parseInt(objContext);
				System.out.println(">>>currentFormNo:"+currentFormNo);
			}
			System.out.println("**************For Validation*********************:"+currentFormNo);
			System.out.println(">>>loginSiteCode:"+loginSiteCode);
			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();
					if (childNodeName.equalsIgnoreCase("bom_code"))
					{
						System.out.println("***************Validation for bom code************:"+bomCode);
						bomCode=checkNull(this.genericUtility.getColumnValue("bom_code", dom));
						
						if(bomCode == null || (bomCode.trim().length() == 0))
						{
							System.out.println(">>>>>>>>>>bomCode null or space:"+bomCode);
							errList.add("VTINVBOM1");
							errFields.add(childNodeName.toLowerCase());
						}
						else
						{
							System.out.println(">>>>>>>>>bomCode not null:"+bomCode);
							sql = "select count(*) from bom_alt where bom_code =?";
							pstmt =  conn.prepareStatement(sql);
							pstmt.setString(1,bomCode);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								cnt =  rs.getInt(1);
								if(cnt == 0) 
								{
									System.out.println("Bom code not exit in bom_alt:"+bomCode);
									errCode = "VTALTBOMCD"; 
									errList.add(errCode);
									errFields.add(childNodeName.toLowerCase());	
								}									
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;
						}
						
					}
					if (childNodeName.equalsIgnoreCase("item_code"))
					{
						bomCode=checkNull(this.genericUtility.getColumnValue("bom_code", dom));
						itemCode=checkNull(this.genericUtility.getColumnValue("item_code", dom));
						System.out.println(">>>>bomCode:"+bomCode);
						System.out.println(">>>>itemCode:"+itemCode);
						if(itemCode == null || (itemCode.trim().length() == 0))
						{
							System.out.println(">>>>>>>>>>itemCode null or space:"+itemCode);
							errList.add("VTBOMITEM");
							errFields.add(childNodeName.toLowerCase());
							
						} 
						else 
						{
							System.out.println(">>>>>>>>>itemCode not null:"+itemCode);
							sql = "SELECT count(*) FROM bom_alt WHERE confirmed='Y' AND bom_code =? AND item_code=? ";
							pstmt =  conn.prepareStatement(sql);
							pstmt.setString(1,bomCode);
							pstmt.setString(2,itemCode);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								cnt =  rs.getInt(1);
								if(cnt == 0) 
								{
									System.out.println("Item code not exit in bom_alt:"+itemCode);
									errCode = "VTBOMITEM";
									errList.add(errCode);
								    errFields.add(childNodeName.toLowerCase());	
								}									
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;
							
						}
						
					 }
						
					
					if (childNodeName.equalsIgnoreCase("item_ref"))
					{
						bomCode=checkNull(this.genericUtility.getColumnValue("bom_code", dom));
						itemCode=checkNull(this.genericUtility.getColumnValue("item_code", dom));
						itemRef=checkNull(this.genericUtility.getColumnValue("item_ref", dom));
						System.out.println(">>>>bomCode:"+bomCode);
						System.out.println(">>>>itemCode:"+itemCode);
						System.out.println(">>>>itemRef:"+itemRef);
						if(itemRef == null || (itemRef.trim().length() == 0))
						{
							System.out.println(">>>>>>>>>>itemRef null or space:"+itemRef);
							errList.add("VTITEMREF");
							errFields.add(childNodeName.toLowerCase());
							
						} 
						else 
						{
							System.out.println(">>>>>>>>>itemRef not null:"+itemRef);
							sql = "SELECT count(*) FROM bom_alt WHERE bom_code =? AND item_code =? AND item_ref=? AND confirmed='Y'";
							pstmt =  conn.prepareStatement(sql);
							pstmt.setString(1,bomCode);
							pstmt.setString(2,itemCode);
							pstmt.setString(3,itemRef);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								cnt =  rs.getInt(1);
								if(cnt == 0) 
								{
									System.out.println("itemRef not exit in bom_alt:"+itemRef);
									errCode = "VTITEMREF";
									errList.add(errCode);
									errFields.add(childNodeName.toLowerCase());	
								}									
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;
							
							
						}
						
					}
					if (childNodeName.equalsIgnoreCase("item_code__alt"))
					{
						bomCode=checkNull(this.genericUtility.getColumnValue("bom_code", dom));
						itemCode=checkNull(this.genericUtility.getColumnValue("item_code", dom));
						itemRef=checkNull(this.genericUtility.getColumnValue("item_ref", dom));
						itemCodeAlt=checkNull(this.genericUtility.getColumnValue("item_code__alt", dom));
						System.out.println(">>>>bomCode:"+bomCode);
						System.out.println(">>>>itemCode:"+itemCode);
						System.out.println(">>>>itemRef:"+itemRef);
						System.out.println(">>>>itemCodeAlt:"+itemCodeAlt);
						if(itemCodeAlt == null || (itemCodeAlt.trim().length() == 0))
						{
							System.out.println(">>>>>>>>>>itemCodeAlt null or space:"+itemCodeAlt);
							errList.add("VTALTITMCD");
						    errFields.add(childNodeName.toLowerCase());
							
						} 
						else 
						{
							if(itemCodeAlt.trim().equalsIgnoreCase(itemCode.trim()))
							{
								System.out.println(">>>>>>>>>>Item Code Alt and Item code are same:"+itemCodeAlt);
								errList.add("VTITEM1A");
								errFields.add(childNodeName.toLowerCase());
								
							}
							else
							{
								System.out.println(">>>>>>>>>itemCodeAlt not null:"+itemCodeAlt);
								sql = "SELECT count(*) FROM bom_alt WHERE bom_code =? AND item_code =? AND item_ref=? AND item_code__alt=? AND confirmed='Y' ";
								pstmt =  conn.prepareStatement(sql);
								pstmt.setString(1,bomCode);
								pstmt.setString(2,itemCode);
								pstmt.setString(3,itemRef);
								pstmt.setString(4,itemCodeAlt);
								rs = pstmt.executeQuery();
								if(rs.next())
								{
									cnt =  rs.getInt(1);
									if(cnt == 0) 
									{
										System.out.println("itemCodeAlt not exit in bom_alt:"+itemCodeAlt);
										errCode = "VTALTITMCD";
										errList.add(errCode);
										errFields.add(childNodeName.toLowerCase());	
									}									
								}
								rs.close();
								rs = null;
								pstmt.close();
								pstmt = null;
								
							}
							
							
							
						}
						
					}
					
					if (childNodeName.equalsIgnoreCase("supp_code__mfg"))
					{
						supMfgCode=checkNull(this.genericUtility.getColumnValue("supp_code__mfg", dom));
						itemCode=checkNull(this.genericUtility.getColumnValue("item_code", dom));
						System.out.println(">>>>>>>>>>>>>>supMfgCode for validation:"+supMfgCode);
						System.out.println(">>>>>>>>>>>>>>itemCode for validation:"+itemCode);
						if (supMfgCode == null || supMfgCode.trim().length()==0)
						{
							sql = "SELECT count(*) AS cnt FROM siteitem WHERE site_code=? AND item_code=? AND CASE WHEN appr_supp IS NULL THEN 'N' ELSE appr_supp END ='Y' ";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1, loginSiteCode);
							pstmt.setString(2, itemCode);
							
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								cnt = rs.getInt("cnt");
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;	
							System.out.println(">>>>>>>>>>>>>>cnt validation:"+cnt);
							if(cnt > 0)
							{
								errCode = "VTAPRSUPP";
								errList.add(errCode);
								errFields.add(childNodeName.toLowerCase());
								
							}
							
						}
					}
					

					
				} //for loop

				valueXmlString.append("</Detail1>");
				break;
			
			

			}
			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
			{
		System.out.println("***********************IN item changed called**********************");
		Document dom = null;
		Document dom1 = null;
		Document dom2 = null;
		String valueXmlString = "";
		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(">>>>>>>>>>>>>>>>>>>>>>>>>>>>In actual method of itemchanged>>>>>>>>>>>>>>>>>>>>>>>");
		NodeList parentNodeList = null;
		NodeList childNodeList = null;
		Node parentNode = null;
		Node childNode = null;
		String childNodeName = null;
		String sql = "";
		
		String itemCode = ""; //start var
		String bomCode = "";
		String itemDescr = "";
		String itemUnit = "";
		double bomQty=0.0;
		String itemRef="";
		String itemCodeAlt="";
		String itemAltDecr="";
		String itemAltUnit="";
		String supCodeMfgOld="";
		int ctr = 0;
		int currentFormNo = 0;
		int childNodeListLength = 0;
		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();
	
		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("item_code"))
				{
					//for item_code
					bomCode=checkNull(this.genericUtility.getColumnValue("bom_code", dom));
					itemCode=checkNull(this.genericUtility.getColumnValue("item_code", dom));
					
					if(bomCode !=null && itemCode !=null )
					{
						sql = "SELECT item.descr,item.unit,bomdet.qty_per FROM bomdet bomdet,item item WHERE  bomdet.item_code=item.item_code AND bomdet.bom_code=? and bomdet.item_code=?";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, bomCode);
						pstmt.setString(2, itemCode);
						rs = pstmt.executeQuery();
						if (rs.next())
						{
							itemDescr = rs.getString("descr");
							itemUnit = rs.getString("unit");
							bomQty = rs.getDouble("qty_per");
						}
						rs.close();
						rs = null;
						pstmt.close();
						pstmt = null;
                       
						valueXmlString.append("<descr>").append("<![CDATA["+itemDescr+"]]>").append("</descr>");
						valueXmlString.append("<unit>").append("<![CDATA["+itemUnit+"]]>").append("</unit>");
						valueXmlString.append("<qty_per>").append("<![CDATA["+bomQty+"]]>").append("</qty_per>");
						
					}
					
				}
				else if (currentColumn.trim().equalsIgnoreCase("item_ref"))
				{
					//for item_ref
					bomCode=checkNull(this.genericUtility.getColumnValue("bom_code", dom));
					itemCode=checkNull(this.genericUtility.getColumnValue("item_code", dom));
					itemRef=checkNull(this.genericUtility.getColumnValue("item_ref", dom));
					if(bomCode !=null && itemCode !=null && itemRef !=null)
					{
						sql = "SELECT qty_per FROM bomdet WHERE bom_code=? and item_code=? and item_ref=? ";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, bomCode);
						pstmt.setString(2, itemCode);
						pstmt.setString(3, itemRef);
						rs = pstmt.executeQuery();
						if (rs.next())
						{
							bomQty = rs.getDouble("qty_per");
							System.out.println(">>>>>>>>>>>>>>>>>>>> set bomQty in item ref:"+bomQty);
						}
						rs.close();
						rs = null;
						pstmt.close();
						pstmt = null;
						valueXmlString.append("<qty_per>").append("<![CDATA["+bomQty+"]]>").append("</qty_per>");
										
					}
					
				}
				else if (currentColumn.trim().equalsIgnoreCase("item_code__alt"))
				{
					//for item_code__alt 
					bomCode=checkNull(this.genericUtility.getColumnValue("bom_code", dom));
					itemCode=checkNull(this.genericUtility.getColumnValue("item_code", dom));
					itemRef=checkNull(this.genericUtility.getColumnValue("item_ref", dom));
					itemCodeAlt=checkNull(this.genericUtility.getColumnValue("item_code__alt", dom));
					if(itemCodeAlt !=null )
					{
						sql = "select descr,unit from item where item_code=? ";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, itemCodeAlt);
						rs = pstmt.executeQuery();
						if (rs.next())
						{
							itemAltDecr = rs.getString("descr");
							itemAltUnit = rs.getString("unit");
							System.out.println(">>>>>>>>>>>>>>>>>>>> set bomQty in item_code__alt:"+itemAltDecr);
						}
						rs.close();
						rs = null;
						pstmt.close();
						pstmt = null;
						valueXmlString.append("<descr_1>").append("<![CDATA["+itemAltDecr+"]]>").append("</descr_1>");
						valueXmlString.append("<unit_1>").append("<![CDATA["+itemAltUnit+"]]>").append("</unit_1>");				
					}
					
					if(bomCode !=null && itemCode !=null && itemRef !=null && itemCodeAlt !=null )
					{
						sql = "SELECT supp_code__mfg FROM bom_alt WHERE bom_code=? and item_code=? and item_ref=? and  item_code__alt=? and confirmed='Y'";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, bomCode);
						pstmt.setString(2, itemCode);
						pstmt.setString(3, itemRef);
						pstmt.setString(4, itemCodeAlt);
						rs = pstmt.executeQuery();
						if (rs.next())
						{
							supCodeMfgOld = rs.getString("supp_code__mfg");
							System.out.println(">>>>>>>>>>>>>>>>>>>> set supp_code__mfg in item_code__alt:"+supCodeMfgOld);
						}
						rs.close();
						rs = null;
						pstmt.close();
						pstmt = null;
						if(supCodeMfgOld==null)
						{
							supCodeMfgOld=" ";	
						}
						valueXmlString.append("<supp_code__mfg__o>").append("<![CDATA["+supCodeMfgOld+"]]>").append("</supp_code__mfg__o>");
										
					}
					
				}

				else if (currentColumn.trim().equalsIgnoreCase("itm_default"))
				{
					System.out.println(">>>>>>>>>>>>>>>>>>>>itm_default called>>>:");
					String currAppdate ="";
					java.sql.Timestamp currDate = null;
					currDate = new java.sql.Timestamp(System.currentTimeMillis()) ;
					currAppdate = new SimpleDateFormat(genericUtility.getApplDateFormat()).format(currDate);
					valueXmlString.append("<amd_date>").append("<![CDATA["+currAppdate+"]]>").append("</amd_date>");
					System.out.println(">>>>>>>>>>>>>>>>>>>>itm_default currAppdate:"+currAppdate);
				}

				valueXmlString.append("</Detail1>");
				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 (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;
	}

}

