

/********************************************************
	Title : MilestoneIC (DI2LDOS002)
	Date  : 16/04/13
	Developer: Kunal Mandhre

 ********************************************************/

package ibase.webitm.ejb.dos;
import ibase.system.config.ConnDriver;
import ibase.webitm.ejb.ValidatorEJB;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;
import java.util.*;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
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 MilestoneIC extends ValidatorEJB implements MilestoneICLocal, MilestoneICRemote
{
	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);
			}
			if (xmlString1 != null && xmlString1.trim().length() > 0 )
			{
				dom1 = parseString(xmlString1);
			}
			if (xmlString2 != null && xmlString2.trim().length() > 0 )
			{
				dom2 = parseString(xmlString2);
			}
			errString = wfValData(dom, dom1, dom2, objContext, editFlag, xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception : [MilestoneIC][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 siteCode = "" ,projectCode = "", wingCode = "",milestoneType = "";
		String startDateStr = "" ,endDateStr = "";
		String childNodeName = null;
		String errString = "";
		String errCode = "";
		String userId = "";
		String sql = "";
		String errorType = "";
		//double amountPerc = 0, totAmountPerc = 0;// prvSequence = 0;
		//double sequence = 0;
		//double sequenceArry [] = null; ;
		int count = 0,counter = 0;
		int ctr=0;
		int currentFormNo = 0;
		int childNodeListLength;
		Timestamp startDate = null, endDate = null;
		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>");
		System.out.println("editFlag="+editFlag);
		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			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("proj_code"))
					{
						projectCode = checkNull(genericUtility.getColumnValue("proj_code", dom));
						System.out.println("projectCode="+projectCode);
						if(projectCode == null || projectCode.trim().length() == 0)
						{
							errCode = "VMPROJCD";
							errList.add(errCode);
							errFields.add(childNodeName.toLowerCase());
						}
						else
						{
							if(!(isExist(conn, "project", "proj_code" ,projectCode)))
							{
								errCode = "VMPROJCDX";
								errList.add(errCode);
								errFields.add(childNodeName.toLowerCase());   
							}
						}

					}
					else if(childNodeName.equalsIgnoreCase("site_code"))
					{
						siteCode = checkNull(genericUtility.getColumnValue("site_code", dom));
						if(siteCode == null || siteCode.trim().length() == 0)
						{
							errCode = "VMSITECD";
							errList.add(errCode);
							errFields.add(childNodeName.toLowerCase());
						}
						else
						{
							if(!(isExist(conn, "site", "site_code" ,siteCode)))
							{
								errCode = "VMSITE";
								errList.add(errCode);
								errFields.add(childNodeName.toLowerCase());   
							}
						}

					}
					
					else if(childNodeName.equalsIgnoreCase("wing_code"))
					{
						wingCode = checkNull(genericUtility.getColumnValue("wing_code", dom));
						projectCode = checkNull(genericUtility.getColumnValue("proj_code", dom));
						siteCode = checkNull(genericUtility.getColumnValue("site_code", dom));
						if(wingCode == null || wingCode.trim().length() == 0)
						{
							errCode = "VMWINGCNUL";
							errList.add(errCode);
							errFields.add(childNodeName.toLowerCase());
						}
						else
						{
							if(!(isExist(conn, "wing_mst", "wing_code" ,wingCode)))
							{
								errCode = "VMWING1";
								errList.add(errCode);
								errFields.add(childNodeName.toLowerCase());   
							}
							else
							{
								sql = " select count(*) from wing_mst where proj_code =  ? and site_code = ?  and wing_code =  ? ";
								pstmt = conn.prepareStatement(sql);
								pstmt.setString(1,projectCode);
								pstmt.setString(2,siteCode);
								pstmt.setString(3,wingCode);
								rs = pstmt.executeQuery();
								if(rs.next())
								{					
									counter = rs.getInt(1);
								}
								rs.close();
								rs = null;
								pstmt.close();
								pstmt = null;
								if(counter == 0)
								{
									errCode = "VMWING2";
									errList.add(errCode);
									errFields.add(childNodeName.toLowerCase());
								}

								if("A".equalsIgnoreCase(editFlag))
								{
									sql = "select count(*) from  milestone where proj_code =  ? and site_code = ?  and wing_code =  ?";
									pstmt = conn.prepareStatement(sql);
									pstmt.setString(1,projectCode);
									pstmt.setString(2,siteCode);
									pstmt.setString(3,wingCode);
									rs = pstmt.executeQuery();
									if(rs.next())
									{					
										counter = rs.getInt(1);
									}
									rs.close();
									rs = null;
									pstmt.close();
									pstmt = null;
									if(counter > 0)
									{
										errCode = "VMSOM";
										errList.add(errCode);
										errFields.add(childNodeName.toLowerCase());
									}
								}

							}
						}

					}else if(childNodeName.equalsIgnoreCase("milestone_type"))
					{
						milestoneType = checkNull(genericUtility.getColumnValue("milestone_type", dom));
						System.out.println("milestoneType="+milestoneType);
						if("P".equalsIgnoreCase(milestoneType))
						{
							sql="select count(1) from proj_milestone where proj_code=? and site_code=? and wing_code=?";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1, projectCode);
							pstmt.setString(2, siteCode);
							pstmt.setString(3, wingCode);
							rs = pstmt.executeQuery();

							if (rs.next()) {
								counter = rs.getInt(1);
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;
							if (counter > 0) {
								errCode = "VMWING3";
								errList.add(errCode);
								errFields.add(childNodeName.toLowerCase());
							}
						}
						}
								
							
							
							else if (childNodeName.equalsIgnoreCase("amount")) {
						String amtType = checkNull(genericUtility.getColumnValue("amt_type", dom));
						if (("P").equalsIgnoreCase(amtType.trim())) {
							String amount = genericUtility.getColumnValue("amount", dom);
						    	if(amount!=null && amount.trim().length()>0)
						    	{
								    Double amtPer=Double.parseDouble(amount);
								    if(amtPer>100){
							    	errCode = "VMAMTNGHP";
									errList.add(errCode);
									errFields.add(childNodeName.toLowerCase());	
							   }
						    }
						}
					    }
						

					else if(childNodeName.equalsIgnoreCase("exp_end_date"))
					{
						startDateStr = genericUtility.getColumnValue("exp_start_date", dom);
						endDateStr = genericUtility.getColumnValue("exp_end_date", dom);
						/*if( endDateStr == null || endDateStr.trim().length() == 0)
						{
							errCode = "VEDAT1";
							errList.add(errCode);
							errFields.add(childNodeName.toLowerCase());
						}*/
						if(startDateStr != null && startDateStr.trim().length() > 0 && endDateStr != null && endDateStr.trim().length() > 0 )
						{
							startDate = Timestamp.valueOf(genericUtility.getValidDateString(startDateStr, genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat()) + " 00:00:00.0");
							endDate = Timestamp.valueOf(genericUtility.getValidDateString(endDateStr, genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat()) + " 00:00:00.0");
							if(endDate != null && endDate.before(startDate))
							{
								errCode = "VTDATE1";
								errList.add(errCode);
								errFields.add(childNodeName.toLowerCase());
							}
						}
					}
					else if(childNodeName.equalsIgnoreCase("act_end_date"))
					{
						startDateStr = genericUtility.getColumnValue("act_start_date", dom);
						endDateStr = genericUtility.getColumnValue("act_end_date", dom);
						/*if( endDateStr == null || endDateStr.trim().length() == 0)
						{
							errCode = "VEDAT1";
							errList.add(errCode);
							errFields.add(childNodeName.toLowerCase());
						}*/
						if(startDateStr != null && startDateStr.trim().length() > 0 && endDateStr != null && endDateStr.trim().length() > 0 )
						{
							startDate = Timestamp.valueOf(genericUtility.getValidDateString(startDateStr, genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat()) + " 00:00:00.0");
							endDate = Timestamp.valueOf(genericUtility.getValidDateString(endDateStr, genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat()) + " 00:00:00.0");
							if(endDate != null && endDate.before(startDate))
							{
								errCode = "VTDATE1";
								errList.add(errCode);
								errFields.add(childNodeName.toLowerCase());
							}
						}
					}

				}
				break;


			/*case 2 :
				parentNodeList = dom.getElementsByTagName("Detail2");
				parentNode = parentNodeList.item(0);
				childNodeList = parentNode.getChildNodes();
				childNodeListLength = childNodeList.getLength();
				for(ctr = 0; ctr < childNodeListLength; ctr ++)
				{
					childNode = childNodeList.item(ctr);
					childNodeName = childNode.getNodeName();

					else if(childNodeName.equalsIgnoreCase("start_date"))
					{
						startDateStr = genericUtility.getColumnValue("start_date", dom);
						if( startDateStr == null || startDateStr.trim().length() == 0)
						{
							errCode = "VEDAT1";
							errList.add(errCode);
							errFields.add(childNodeName.toLowerCase());
						}
					}
					if(childNodeName.equalsIgnoreCase("exp_end_date"))
					{
						startDateStr = genericUtility.getColumnValue("exp_start_date", dom);
						endDateStr = genericUtility.getColumnValue("exp_end_date", dom);
						if( endDateStr == null || endDateStr.trim().length() == 0)
						{
							errCode = "VEDAT1";
							errList.add(errCode);
							errFields.add(childNodeName.toLowerCase());
						}
						if(startDateStr != null && startDateStr.trim().length() > 0 && endDateStr != null && endDateStr.trim().length() > 0 )
						{
							startDate = Timestamp.valueOf(genericUtility.getValidDateString(startDateStr, genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat()) + " 00:00:00.0");
							endDate = Timestamp.valueOf(genericUtility.getValidDateString(endDateStr, genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat()) + " 00:00:00.0");
							if(endDate != null && endDate.before(startDate))
							{
								errCode = "VTDATE1";
								errList.add(errCode);
								errFields.add(childNodeName.toLowerCase());
							}
						}
					}
					else if(childNodeName.equalsIgnoreCase("act_end_date"))
					{
						startDateStr = genericUtility.getColumnValue("act_start_date", dom);
						endDateStr = genericUtility.getColumnValue("act_end_date", dom);
						if( endDateStr == null || endDateStr.trim().length() == 0)
						{
							errCode = "VEDAT1";
							errList.add(errCode);
							errFields.add(childNodeName.toLowerCase());
						}
						if(startDateStr != null && startDateStr.trim().length() > 0 && endDateStr != null && endDateStr.trim().length() > 0 )
						{
							startDate = Timestamp.valueOf(genericUtility.getValidDateString(startDateStr, genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat()) + " 00:00:00.0");
							endDate = Timestamp.valueOf(genericUtility.getValidDateString(endDateStr, genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat()) + " 00:00:00.0");
							if(endDate != null && endDate.before(startDate))
							{
								errCode = "VTDATE1";
								errList.add(errCode);
								errFields.add(childNodeName.toLowerCase());
							}
						}
					}
					else if(childNodeName.equalsIgnoreCase("amount_perc"))
					{
						amountPerc = Double.parseDouble(checkNull(genericUtility.getColumnValue("amount_perc", dom)).trim().length() == 0 ?"0": genericUtility.getColumnValue("amount_perc", dom).trim());
						System.out.println("amountPerc="+amountPerc);
						if(amountPerc > 100 || amountPerc <= 0 )
						{
							errCode = "VMAMTPER3";
							errList.add(errCode);
							errFields.add(childNodeName.toLowerCase());
						}
						else
						{
							
							amountPerc = 0;
							totAmountPerc = 0;
							for(int i =0; i< dom2.getElementsByTagName("Detail2").getLength();i++)
							{
								amountPerc = Double.parseDouble( checkNull(genericUtility.getColumnValueFromNode("amount_perc",dom2.getElementsByTagName("Detail2").item(i))).trim().length() == 0 ?"0":genericUtility.getColumnValueFromNode("amount_perc",dom2.getElementsByTagName("Detail2").item(i)).trim()) ;
								System.out.println("AmountPerc = "+amountPerc);
								totAmountPerc  = totAmountPerc + amountPerc;
								System.out.println("ToTAmountPerc = "+totAmountPerc);
							}
							if(totAmountPerc > 100)
							{
								errCode = "VMAMTPER1";
								errList.add(errCode);
								errFields.add(childNodeName.toLowerCase());
							}
						}
					}
					else if(childNodeName.equalsIgnoreCase("milestone_type"))
					{
						milestoneType = checkNull(genericUtility.getColumnValue("milestone_type", dom));
						System.out.println("milestoneType="+milestoneType);

						if("P".equalsIgnoreCase(milestoneType))
						{
							amountPerc = 0;
							totAmountPerc = 0;
							for(int i =0; i< dom2.getElementsByTagName("Detail2").getLength();i++)
							{
								amountPerc = Double.parseDouble( checkNull(genericUtility.getColumnValueFromNode("amount_perc",dom2.getElementsByTagName("Detail2").item(i))).trim().length() == 0 ?"0":genericUtility.getColumnValueFromNode("amount_perc",dom2.getElementsByTagName("Detail2").item(i)).trim()) ;
								System.out.println("AmountPerc = "+amountPerc);
								totAmountPerc  = totAmountPerc + amountPerc;
							}
							System.out.println("ToTAmountPerc FOR TYPE P = "+totAmountPerc);
							if(totAmountPerc != 100)
							{
								errCode = "VMAMTPER2";
								errList.add(errCode);
								errFields.add(childNodeName.toLowerCase());
							}
						}
					}
					else if(childNodeName.equalsIgnoreCase("appl_order"))
					{
						sequence = Double.parseDouble((checkNull(genericUtility.getColumnValue("appl_order", dom)).trim().length() == 0 ?"0": genericUtility.getColumnValue("appl_order", dom).trim()));
						System.out.println("sequence="+sequence);
						boolean isDuplicate = false;
						double maxSequence = 0;
						if(sequence < 1)
						{
							errCode = "VMMSSEQ1";
							errList.add(errCode);
							errFields.add(childNodeName.toLowerCase());
						}
						else
						{
							int length = dom2.getElementsByTagName("Detail2").getLength();
							System.out.println("length="+length);
							sequenceArry = new double [length];
							for(int i =0; i< dom2.getElementsByTagName("Detail2").getLength();i++)
							{
								//prvSequence = Integer.parseInt( checkNull(genericUtility.getColumnValueFromNode("appl_order",dom2.getElementsByTagName("Detail2").item(0))).trim().length() == 0 ?"0":genericUtility.getColumnValueFromNode("appl_order",dom2.getElementsByTagName("Detail2").item(0)).trim()) ;
								
									sequence = Double.parseDouble( checkNull(genericUtility.getColumnValueFromNode("appl_order",dom2.getElementsByTagName("Detail2").item(i))).trim().length() == 0 ?"0":genericUtility.getColumnValueFromNode("appl_order",dom2.getElementsByTagName("Detail2").item(i)).trim()) ;
									milestoneType = checkNull(genericUtility.getColumnValueFromNode("milestone_type",dom2.getElementsByTagName("Detail2").item(i)));
									sequenceArry [i] = sequence;
									
									System.out.println("prvSequence = "+prvSequence+"   Sequence"+sequence);
									if("P".equalsIgnoreCase(milestoneType))
									{
										if(sequence <= maxSequence)
										{
											errCode = "VMMSSEQ3";
											errList.add(errCode);
											errFields.add(childNodeName.toLowerCase());
										}
									}
									if(sequence > maxSequence)
									{
										maxSequence = sequence ; 
									}
								
							}
							Arrays.sort(sequenceArry);
							for(int i = 1; i < sequenceArry.length; i++) 
							{
							    if(sequenceArry[i] == sequenceArry[i - 1]) 
							    {
							        System.out.println("Duplicate: " + sequenceArry[i]);
							        isDuplicate = true;
							    }
							}
							if(isDuplicate)
							{
								errCode = "VMMSSEQ2";
								errList.add(errCode);
								errFields.add(childNodeName.toLowerCase());
							}
						}

					}


				}
				break;*/


			}
			int errListSize = errList.size();
			count = 0;
			String errFldName = null;
			if(errList != null && errListSize > 0)
			{
				for(count = 0; count < errListSize; count ++)
				{
					errCode = errList.get(count);
					errFldName = errFields.get(count);
					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 : [MilestoneIC][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 descr = "",siteCode = "", projCode = "",wingCode = "";
		String childNodeName = null;
		//String sql = "";
		StringBuffer valueXmlString = new StringBuffer();
		int ctr = 0;
		int currentFormNo = 0;
		int childNodeListLength = 0;
		//double sequence = 0, maxSequence = 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();
		try
		{   
			conn = connDriver.getConnectDB("DriverITM");
			conn.setAutoCommit(false);
			connDriver = null;
			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();
					ctr ++;
				}while(ctr < childNodeListLength  && ! childNodeName.equals(currentColumn));

				if(currentColumn.trim().equalsIgnoreCase("proj_code"))
				{
					projCode  = checkNull(genericUtility.getColumnValue("proj_code", dom));
					descr = "";
					descr = findValue(conn, "descr", "project", "proj_code",projCode );
					valueXmlString.append("<project_descr>").append("<![CDATA[" + descr +"]]>").append("</project_descr>");
				}
				else if(currentColumn.trim().equalsIgnoreCase("site_code"))
				{
					siteCode  = checkNull(genericUtility.getColumnValue("site_code", dom));
					descr = "";
					descr = findValue(conn, "descr", "site", "site_code",siteCode );
					valueXmlString.append("<site_descr>").append("<![CDATA[" + descr +"]]>").append("</site_descr>");
				}
				else if(currentColumn.trim().equalsIgnoreCase("wing_code"))
				{
					wingCode  = checkNull(genericUtility.getColumnValue("wing_code", dom));
					descr = "";
					descr = findValue(conn, "wing_descr", "wing_mst", "wing_code",wingCode );
					valueXmlString.append("<wing_mst_wing_descr>").append("<![CDATA[" + descr +"]]>").append("</wing_mst_wing_descr>");
				}
				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(rs != null) 
					{
						rs.close();
						rs = null;
					}
					if(pstmt != null) 
					{
						pstmt.close();
						pstmt = null;

					}
					conn.close();
				}
				conn = null;	
			}
			catch(Exception e)
			{
				e.printStackTrace();
				throw new ITMException(e);
			}			
		}
		return valueXmlString.toString();
	}
	private String checkNull(String input) 
	{
		if(input == null)
		{
			input = "";
		}
		return input;
	}
	private boolean isExist(Connection conn, String tableName, String columnName, String value) throws  ITMException, RemoteException
	{
		PreparedStatement pstmt = null ;
		ResultSet rs = null ; 
		String sql = "";
		boolean status = false;
		try
		{			
			sql = "SELECT count(*) from " + tableName + " where " + columnName +"  = ? ";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,value);
			rs = pstmt.executeQuery();

			if(rs.next())
			{					 
				if(rs.getBoolean(1))
				{					
					status = true;
				}
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;	        
		}
		catch(Exception e)
		{
			System.out.println("Exception in isExist ");
			e.printStackTrace();
			throw new ITMException(e); 
		}
		System.out.println("returning String from isExist ");
		return status;
	}
	private String findValue(Connection conn, String columnName ,String tableName, String columnName2, String value) throws  ITMException, RemoteException
	{

		PreparedStatement pstmt = null ;
		ResultSet rs = null ; 
		String sql = "";
		String findValue = "";

		try
		{			
			sql = "SELECT " + columnName + " from " + tableName + " where " + columnName2 +"  = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,value);
			rs = pstmt.executeQuery();

			if(rs.next())
			{					
				findValue = rs.getString(columnName);
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;	        
		}
		catch(Exception e)
		{
			System.out.println("Exception in findValue ");
			e.printStackTrace();
			throw new ITMException(e); 
		}
		System.out.println("returning String from findValue ");
		return findValue;
	}	
	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;
	}
}	
