package ibase.webitm.ejb.adm;

import ibase.system.config.ConnDriver;
import ibase.webitm.ejb.ValidatorEJB;
import ibase.webitm.utility.ITMException;
import ibase.utility.E12GenericUtility;// Modified By Hemlata on[20/11/2014] [As per changes in framework to use genericUtility ]
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;  
import javax.ejb.Stateless;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

@Stateless
public class EmpAddressInfo extends ValidatorEJB implements EmpAddressInfoLocal ,EmpAddressInfoRemote
{
	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 errString = null;
		
		//Modified By Hemlata on[19/11/2014] [As per changes in framework to use genericUtility ][Start]
	    E12GenericUtility genericUtility = new E12GenericUtility();
		//GenericUtility genericUtility = GenericUtility.getInstance();
	    //Modified By Hemlata on[19/11/2014] [As per changes in framework to use genericUtility ][End]
	    
		try
		{
			System.out.println("EmpAddressInfo xmlString in itemChanged..::["+xmlString+"]");
			System.out.println("EmpAddressInfo xmlString1 in itemChanged..:: ["+xmlString1+"]");
			System.out.println("EmpAddressInfo xmlString2 in itemChanged..:: ["+xmlString2+"]");
			
			if(xmlString != null && xmlString.length() > 0)
			{
				dom = genericUtility.parseString(xmlString);				
			}
			if(xmlString1 != null  &&  xmlString1.length() > 0)
			{
				dom1 = genericUtility.parseString(xmlString1);
			}
			if(xmlString2 != null && xmlString2.length() > 0)
			{
				dom2 = genericUtility.parseString(xmlString2);
			}
			errString = itemChanged( dom, dom1, dom2, objContext, currentColumn,  editFlag,  xtraParams);
			System.out.println("EmpAddressInfo errString Returing at Stage 1["+errString+"]");
		}
		catch(Exception e)
		{
			e.printStackTrace();
			System.out.println("Exception :EmpAddressInfo [:" + e.getMessage() + ":]" );
			errString = genericUtility.createErrorString(e);
		}
		System.out.println("EmpAddressInfo errString returing at Stage 2[" + errString+"]");
		return errString;
	}
		
	//Start of itemChanged block
	public String itemChanged(Document dom, Document dom1,Document dom2, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		Connection conn = null;
		ConnDriver connDriver = new ConnDriver();
		String sql = "";
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		String objNameDefault = "";
		
		//Variable defined . Start
		
		String empCode = "";
		String currentAdd = "";
		String permentAdd = "";
		String contactCode = "";
		String curState ="";
		String addressOne = "";
		String addressTwo = "";
		String addressThree = "";
		String city = "";
		String stateCode = "";
		String pin = "";
		String teleOne = "";
		String teleTwo = "";
		String teleThree = "";
		String currentAddTwo = "";
		String currentAddThree = "";
		String curCity = "";
		String curPin = "";
		String perTel = "";
		String curTel1 = "";
		String perCity = "";
		String perState = "";
		
		//Variable defined  . End

		int currentFormNo = 0;
		
		// Modified By Hemlata on[19/11/2014] [As per changes in framework to use genericUtility ][Start]
		//GenericUtility genericUtility = GenericUtility.getInstance();
		 E12GenericUtility genericUtility = new E12GenericUtility();
		 //Modified By Hemlata on[19/11/2014] [As per changes in framework to use genericUtility ][End]
		 
		StringBuffer valueXmlString = new StringBuffer();
		
		System.out.println("objContext ..:: ["+objContext+"]");
		System.out.println("currentColumn ..:: ["+currentColumn+"]");
		System.out.println("editFlag ..:: ["+editFlag+"]");
		System.out.println("xtraParams ..:: ["+xtraParams+"]");
		

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

		try
		{		
			conn = connDriver.getConnectDB("Driver");
			conn.setAutoCommit(false);

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

			objNameDefault  = checkNull(getObjName(dom ,"1"));
			System.out.println("objNameDefault ..:: ["+objNameDefault+"]");
			
			switch (currentFormNo)
			{
				case 1:
						{
							valueXmlString.append("<Detail1 domID='1' dbID='' objContext='1' objName='"+objNameDefault+"' selected='Y'>\r\n");
							
							empCode = genericUtility.getColumnValue("emp_code",dom2);
							System.out.println("empCode ..:: ["+empCode+"]");
							
							sql = "SELECT  PER_ADD1  FROM EMPLOYEE WHERE EMP_CODE = ?";
							
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1,empCode);
							rs = pstmt.executeQuery();
							
							if(rs.next())
							{
								permentAdd = checkNull(rs.getString("PER_ADD1"));
							}
							if( rs != null )
							{
								rs.close();
								rs = null;
							}
							if( pstmt != null )
							{
								pstmt.close();
								pstmt = null;
							}
							System.out.println("permentAdd ..:: ["+permentAdd+"]");
							
							if(currentColumn.equalsIgnoreCase("itm_defaultedit"))
							{
								sql = "SELECT CUR_ADD1 , PER_ADD1 , CONTACT_CODE , CUR_STATE , PER_STATE FROM EMPLOYEE WHERE EMP_CODE = ?";
								
								pstmt = conn.prepareStatement(sql);
								pstmt.setString(1,empCode);
								rs = pstmt.executeQuery();
								
								if(rs.next())
								{
									currentAdd = checkNull(rs.getString("CUR_ADD1"));
									permentAdd = checkNull(rs.getString("PER_ADD1"));
									contactCode = checkNull(rs.getString("CONTACT_CODE"));
									curState = checkNull(rs.getString("CUR_STATE"));
									perState = checkNull(rs.getString("PER_STATE"));
								}
								if( rs != null )
								{
									rs.close();
									rs = null;
								}
								if( pstmt != null )
								{
									pstmt.close();
									pstmt = null;
								}
								System.out.println("currentAdd ..:: ["+currentAdd+"]");
								System.out.println("permentAdd ..:: ["+permentAdd+"]");
								System.out.println("contactCode ..:: ["+contactCode+"]");
								System.out.println("curState ..:: ["+curState+"]");
								System.out.println("perState ..:: ["+perState+"]");
								
								if(currentAdd == null || currentAdd.length() == 0)
								{
									if(contactCode != null && contactCode.length() > 0)
									{
										sql = "SELECT ADDR1, ADDR2, ADDR3, CITY, STATE_CODE, PIN, TELE1, TELE2, TELE3 FROM CONTACT WHERE CONTACT_CODE = ?";
										
										pstmt = conn.prepareStatement(sql);
										pstmt.setString(1,contactCode);
										rs = pstmt.executeQuery();
										
										if(rs.next())
										{
											addressOne = checkNull(rs.getString("ADDR1"));
											addressTwo = checkNull(rs.getString("ADDR2"));
											addressThree = checkNull(rs.getString("ADDR3"));
											city = checkNull(rs.getString("CITY"));
											stateCode = checkNull(rs.getString("STATE_CODE"));
											pin = checkNull(rs.getString("PIN"));
											teleOne = checkNull(rs.getString("TELE1"));
											teleTwo = checkNull(rs.getString("TELE2"));
											teleThree = checkNull(rs.getString("TELE3"));
										}
										if( rs != null )
										{
											rs.close();
											rs = null;
										}
										if( pstmt != null )
										{
											pstmt.close();
											pstmt = null;
										}
										System.out.println("addressOne ..:: ["+addressOne+"]");
										System.out.println("addressTwo ..:: ["+addressTwo+"]");
										System.out.println("addressThree ..:: ["+addressThree+"]");
										System.out.println("city ..:: ["+city+"]");
										System.out.println("stateCode ..:: ["+stateCode+"]");
										System.out.println("pin ..:: ["+pin+"]");
										System.out.println("teleOne ..:: ["+teleOne+"]");
										System.out.println("teleTwo ..:: ["+teleTwo+"]");
										System.out.println("teleThree ..:: ["+teleThree+"]");
										
										valueXmlString.append("<cur_add1><![CDATA[").append(addressOne).append("]]></cur_add1>\r\n");
										valueXmlString.append("<cur_add2><![CDATA[").append(addressTwo).append("]]></cur_add2>\r\n");
										valueXmlString.append("<cur_add3><![CDATA[").append(addressThree).append("]]></cur_add3>\r\n");
										valueXmlString.append("<cur_city><![CDATA[").append(city).append("]]></cur_city>\r\n");
										valueXmlString.append("<cur_state><![CDATA[").append(stateCode).append("]]></cur_state>\r\n");
										valueXmlString.append("<cur_pin><![CDATA[").append(pin).append("]]></cur_pin>\r\n");
										valueXmlString.append("<cur_tel1><![CDATA[").append(teleOne).append("]]></cur_tel1>\r\n");
										valueXmlString.append("<cur_tel2><![CDATA[").append(teleTwo).append("]]></cur_tel2>\r\n");
										valueXmlString.append("<cur_tel3><![CDATA[").append(teleThree).append("]]></cur_tel3>\r\n");
										
										if(permentAdd == null || permentAdd.length() == 0)
										{
											valueXmlString.append("<per_add1><![CDATA[").append(addressOne).append("]]></per_add1>\r\n");
											valueXmlString.append("<per_add2><![CDATA[").append(addressTwo).append("]]></per_add2>\r\n");
											valueXmlString.append("<per_add3><![CDATA[").append(addressThree).append("]]></per_add3>\r\n");
											valueXmlString.append("<per_city><![CDATA[").append(city).append("]]></per_city>\r\n");
											valueXmlString.append("<per_state><![CDATA[").append(stateCode).append("]]></per_state>\r\n");
											valueXmlString.append("<per_pin><![CDATA[").append(pin).append("]]></per_pin>\r\n");
											valueXmlString.append("<per_tel><![CDATA[").append(teleOne).append("]]></per_tel>\r\n");
										}
									}

									if(stateCode != null && stateCode.length() > 0 )
									{
										valueXmlString.append("<cur_state protect=\"1\"><![CDATA[").append(stateCode).append("]]></cur_state>\r\n");
									}
									else
									{
										valueXmlString.append("<cur_state protect=\"0\"><![CDATA[").append(stateCode).append("]]></cur_state>\r\n");
									}
									/*
									if(perState != null && perState.length() > 0 )
									{
										valueXmlString.append("<per_state protect=\"1\"><![CDATA[").append(perState).append("]]></per_state>\r\n");
									}
									else
									{
										valueXmlString.append("<per_state protect=\"0\"><![CDATA[").append(perState).append("]]></per_state>\r\n");
									}*/
								}
							}
							else if (currentColumn.equalsIgnoreCase("cur_add1"))
							{
								currentAdd = checkNull(genericUtility.getColumnValue("cur_add1",dom,"1"));
								System.out.println("currentAdd ..:: ["+currentAdd+"]");
								
								if(permentAdd == null || permentAdd.length() == 0)
								{
									valueXmlString.append("<per_add1><![CDATA[").append(currentAdd).append("]]></per_add1>\r\n");
								}
							}
							else if (currentColumn.equalsIgnoreCase("cur_add2"))
							{
								currentAddTwo = checkNull(genericUtility.getColumnValue("cur_add2",dom,"1"));
								System.out.println("currentAddTwo ..:: ["+currentAddTwo+"]");
								
								if(permentAdd == null || permentAdd.length() == 0)
								{
									valueXmlString.append("<per_add2><![CDATA[").append(currentAddTwo).append("]]></per_add2>\r\n");
								}
							}
							else if (currentColumn.equalsIgnoreCase("cur_add3"))
							{
								currentAddThree = checkNull(genericUtility.getColumnValue("cur_add3",dom,"1"));
								System.out.println("currentAddThree ..:: ["+currentAddThree+"]");
								
								if(permentAdd == null || permentAdd.length() == 0)
								{
									valueXmlString.append("<per_add3><![CDATA[").append(currentAddThree).append("]]></per_add3>\r\n");
								}
							}
							else if (currentColumn.equalsIgnoreCase("cur_city"))
							{
								curCity = checkNull(genericUtility.getColumnValue("cur_city",dom,"1"));
								System.out.println("curCity ..:: ["+curCity+"]");
								
								sql = "SELECT STATE_CODE  FROM STATION WHERE DESCR = ?";
								
								pstmt = conn.prepareStatement(sql);
								pstmt.setString(1,curCity);
								rs = pstmt.executeQuery();
								
								if(rs.next())
								{
									stateCode = checkNull(rs.getString("STATE_CODE"));
								}
								if( rs != null )
								{
									rs.close();
									rs = null;
								}
								if( pstmt != null )
								{
									pstmt.close();
									pstmt = null;
								}
								System.out.println("stateCode ..:: ["+stateCode+"]");
								
								if(permentAdd == null || permentAdd.length() == 0)
								{
									valueXmlString.append("<per_city><![CDATA[").append(curCity).append("]]></per_city>\r\n");
									valueXmlString.append("<per_state><![CDATA[").append(stateCode).append("]]></per_state>\r\n");
								}
								
								valueXmlString.append("<cur_state><![CDATA[").append(stateCode).append("]]></cur_state>\r\n");
								
								if(stateCode != null && stateCode.length() > 0)
								{
									valueXmlString.append("<cur_state protect=\"1\"><![CDATA[").append(stateCode).append("]]></cur_state>\r\n");
									//valueXmlString.append("<per_state protect=\"1\"><![CDATA[").append(stateCode).append("]]></per_state>\r\n");
								}
								else
								{
									valueXmlString.append("<cur_state protect=\"0\"><![CDATA[").append(stateCode).append("]]></cur_state>\r\n");
									//valueXmlString.append("<per_state protect=\"0\"><![CDATA[").append(stateCode).append("]]></per_state>\r\n");
								}
							}
							else if (currentColumn.equalsIgnoreCase("cur_state"))
							{
								curState = checkNull(genericUtility.getColumnValue("cur_state",dom,"1"));
								System.out.println("curState ..:: ["+curState+"]");
								
								if(permentAdd == null || permentAdd.length() == 0)
								{
									valueXmlString.append("<per_state><![CDATA[").append(curState).append("]]></per_state>\r\n");
								}
							}
							else if (currentColumn.equalsIgnoreCase("cur_pin"))
							{
								curPin = checkNull(genericUtility.getColumnValue("cur_pin",dom,"1"));
								System.out.println("curPin ..:: ["+curPin+"]");
								
								if(permentAdd == null || permentAdd.length() == 0)
								{
									valueXmlString.append("<per_pin><![CDATA[").append(curPin).append("]]></per_pin>\r\n");
								}
							}
							else if (currentColumn.equalsIgnoreCase("cur_tel1"))
							{
								curTel1 = checkNull(genericUtility.getColumnValue("cur_tel1",dom,"1"));
								System.out.println("curTel1 ..:: ["+curTel1+"]");
								perTel = checkNull(genericUtility.getColumnValue("per_tel",dom,"1"));
								System.out.println("perTel ..:: ["+perTel+"]");
								
								if(perTel == null || perTel.length() == 0)
								{
									valueXmlString.append("<per_tel><![CDATA[").append(curTel1).append("]]></per_tel>\r\n");
								}
							}
							else if (currentColumn.equalsIgnoreCase("per_city"))
							{
								perCity = checkNull(genericUtility.getColumnValue("per_city",dom,"1"));
								System.out.println("perCity ..:: ["+perCity+"]");
																
								sql = "SELECT STATE_CODE  FROM STATION WHERE DESCR = ?";
								
								pstmt = conn.prepareStatement(sql);
								pstmt.setString(1,perCity);
								rs = pstmt.executeQuery();
								
								if(rs.next())
								{
									stateCode = checkNull(rs.getString("STATE_CODE"));
								}
								if( rs != null )
								{
									rs.close();
									rs = null;
								}
								if( pstmt != null )
								{
									pstmt.close();
									pstmt = null;
								}
								System.out.println("stateCode ..:: ["+stateCode+"]");
								valueXmlString.append("<per_state><![CDATA[").append(stateCode).append("]]></per_state>\r\n");
							
								if(stateCode != null && stateCode.length() > 0)
								{
									valueXmlString.append("<per_state protect=\"1\"><![CDATA[").append(stateCode).append("]]></per_state>\r\n");
								}
								else
								{
									valueXmlString.append("<per_state protect=\"0\"><![CDATA[").append(stateCode).append("]]></per_state>\r\n");
								}
								
							}
							
							valueXmlString.append("</Detail1>\r\n");
						
						}//End of Case 1
						break;
			}//END OF SWITCH
			
		}//End of try block		
		catch(Exception e)
		{
			e.printStackTrace();
			System.out.println( "Exception :EmpAddressInfo [itemChanged(String xmlString)] : ==>\n"+e.getMessage());
			throw new ITMException(e);
		}//End of cacth block
		finally
		{
			try
			{
				if (rs != null)
				{
					rs.close();
					rs = null;
				}
				if (pstmt != null )
				{
					pstmt.close();
					pstmt = null;
				}
				if ( conn != null )
				{
					conn.close();
					conn = null;
				}
				
			}
			catch(Exception e)
			{
				System.out.println("Exception ::"+e);
				e.printStackTrace();
			}			
		}//End of finally block
		
		valueXmlString.append("</Root>\r\n");
		System.out.println("valueXmlString....["+valueXmlString.toString());
		return valueXmlString.toString();
	}//End of itemChanged block


	//Start of wfValData Start
	public String wfValData() throws RemoteException,ITMException
	{
		return "";
	}

	public String wfValData(String xmlString, String xmlString1,String xmlString2, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		System.out.println("Came Inside 1 wfValData:::");
		
		//Modified By Hemlata on[19/11/2014] [As per changes in framework to use genericUtility ][Start]
		//GenericUtility genericUtility = GenericUtility.getInstance();
		 E12GenericUtility genericUtility = new E12GenericUtility();
		 //Modified By Hemlata on[19/11/2014] [As per changes in framework to use genericUtility ][End]
		 
		Document dom = null;
		Document dom1 = null;
		Document dom2 = null;
		String errString = "";
		System.out.println("xmlString ["+xmlString+"]");
		System.out.println("xmlString1["+xmlString1+"]");
		System.out.println("xmlString2["+xmlString2+"]");
		try
		{
			if(xmlString != null && xmlString.trim().length()!=0)
			{
				dom = parseString(xmlString); 
			}
			else
			{
				System.out.println("xmlstring is null");
			}
			if(xmlString1 != null && xmlString1.trim().length()!=0)
			{
				dom1 = parseString(xmlString1); 
			}
			else
			{
				System.out.println("xmlstring1 is null");
			}
			if(xmlString2 != null && xmlString2.trim().length()!=0)
			{
				dom2 = parseString(xmlString2);
			}
			else
			{
				System.out.println("xmlstring2 is null");
			}
			System.out.println("Before call valdata");
			errString = wfValData(dom,dom1,dom2,objContext,editFlag,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception : [EmpAddressInfo][wfValData(String xmlString)] : ==>\n"+e.getMessage());
			e.printStackTrace();
			errString = genericUtility.createErrorString(e);
			
		}
		return (errString); 
	}

	public String wfValData(Document dom, Document dom1,Document dom2, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
	
		Node parentNode = null;
		Node childNode = null;

		NodeList parentNodeList = null;
		NodeList childNodeList = null;

		String errString = "";
		String childNodeName = "";	

		int currentFormNo = 0;		
		int childNodeListLength = 0;

		 
		String msgType = "";
		String errCode = "";
		String errFldName = "";
		StringBuffer errStringXml = new StringBuffer("<?xml version=\"1.0\"?>\r\n<Root><Errors>");

		ArrayList<String> errList = new ArrayList<String>();
		ArrayList<String> errFields = new ArrayList<String>();

		int cnt = 0;
		String sql = "";
		PreparedStatement pstmt = null;
		ResultSet rs = null;


		//Varibles used in case 1 Start
		
		String userId = "";
		String curState = "";
		String perState = "";
		
		//Varibles used in  case 1 End

		int ctr = 0;		
		int count = 0;	
		
		//Modified By Hemlata on[19/11/2014] [As per changes in framework to use genericUtility ][Start]
		//GenericUtility genericUtility = GenericUtility.getInstance();
		 E12GenericUtility genericUtility = new E12GenericUtility();
		 //Modified By Hemlata on[19/11/2014] [As per changes in framework to use genericUtility ][End]
		 
		Connection conn = null;
		try
		{
			
			ConnDriver connDriver = new ConnDriver();
			conn = connDriver.getConnectDB("Driver");
			conn.setAutoCommit(false);
			
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			
			switch(currentFormNo)
			{
				case 1:
				{
					parentNodeList = dom.getElementsByTagName("Detail"+currentFormNo);
					parentNode = parentNodeList.item(0);
					childNodeList = parentNode.getChildNodes();
					childNodeListLength = childNodeList.getLength();

					for(ctr = 0; ctr < childNodeListLength; ctr++)
					{
						childNode = childNodeList.item(ctr);
						
						System.out.println("childNode"+childNode);
						if( childNode.getNodeType() != Node.ELEMENT_NODE )
						{
							continue;
						}

						childNodeName = childNode.getNodeName();
						
						if(childNodeName.equalsIgnoreCase("cur_state"))
						{ 
							curState = genericUtility.getColumnValue("cur_state",dom,objContext);
							System.out.println("curState got in the validation ..::["+curState+"]");
							
							sql = "SELECT COUNT(*) AS STATE_CNT FROM STATE WHERE STATE_CODE = ?";
							
							pstmt = conn.prepareStatement(sql);		
							pstmt.setString(1,curState);	
							
							rs = pstmt.executeQuery();		
							
							if( rs.next())
							{
								count  = rs.getInt("STATE_CNT");
							}
							System.out.println("count:::: "+count);
							if ( rs != null )
							{
								rs.close();
								rs = null;
							}
							if ( pstmt != null )
							{
								pstmt.close();
								pstmt = null;
							}
							
							if(count == 0)
							{
								System.out.println("Invalid state (current), not defined in master::::");
								
								errList.add( "VESTATCD2" );
								errFields.add( childNodeName.toLowerCase() );
								
								msgType =  errorType( conn , "VESTATCD2" );
								if ( msgType.equalsIgnoreCase("E"))
								{
									break;
								}
							}
						}
						else if(childNodeName.equalsIgnoreCase("per_state"))
						{
							perState = genericUtility.getColumnValue("per_state",dom,objContext);
							System.out.println("perState got in the validation ..::["+perState+"]");
							
							sql = "SELECT COUNT(*) AS STATE_CNT FROM STATE WHERE STATE_CODE = ?";
							
							pstmt = conn.prepareStatement(sql);		
							pstmt.setString(1,perState);	
							
							rs = pstmt.executeQuery();		
							
							if( rs.next())
							{
								count  = rs.getInt("STATE_CNT");
							}
							System.out.println("count:::: "+count);
							if ( rs != null )
							{
								rs.close();
								rs = null;
							}
							if ( pstmt != null )
							{
								pstmt.close();
								pstmt = null;
							}
							
							if(count == 0)
							{
								System.out.println("Invalid state (permanent), not defined in master::::");
								
								errList.add( "VESTATCD2" );
								errFields.add( childNodeName.toLowerCase() );
								
								msgType =  errorType( conn , "VESTATCD2" );
								if ( msgType.equalsIgnoreCase("E"))
								{
									break;
								}
							}
						}
				}
			}//End of case
			break;		
		}//End of Switch
			int errListSize = errList.size();
				if ( errList != null && errListSize > 0 )
				{
					for (cnt = 0; cnt < errListSize; cnt++ )
					{
						errCode = (String)errList.get(cnt);
						errFldName = (String)errFields.get(cnt);
						System.out.println("errCode .........."+errCode);
						
						errString = getErrorString( errFldName, errCode, userId );
						System.out.println("errString is : ::::>>>> " + errString);
						msgType =  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);
							System.out.println("errStringXml .........."+errStringXml);
							errString = "";
						}
						if ( msgType.equalsIgnoreCase("E"))
						{
							break;
						}
					}
					errList.clear();
					errList = null;
					errFields.clear();
					errFields = null;
					
					errStringXml.append("</Errors></Root>\r\n");
				}
				else
				{
					errStringXml = new StringBuffer( "" );
				}
				errString = errStringXml.toString();

		}
		catch(Exception e)
		{
			System.out.println( "Exception : [EmpAddressInfo][wfValData(String xmlString)] : ==>\n"+e.getMessage());
			e.printStackTrace();
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				if ( rs != null )
				{
					rs.close();
					rs = null;
				}
				if ( pstmt != null )
				{
					pstmt.close();
					pstmt = null;
				}
				
				if ( conn != null )
				{
					conn.close();
					conn = null;
				}
			}
			catch(Exception e1)
			{
				System.out.println( "Exception : [EmpAddressInfo][wfValData] : ==>\n"+e1.getMessage());
				throw new ITMException(e1);
			}
		}

	
		return errString;
	}
	//Start of wfValData End


	private String checkNull( String input )
	{
		if (input == null)
		{
			input = "";
		}
		else
		{
			input = input.trim();
		}
		return input;
	}
	private String getObjName(Document dom,String objContext)
	{	
		Node elementName = null;
		NodeList elementList = null;
		String objName = "";
		elementList = dom.getElementsByTagName("Detail"+objContext);
		elementName = elementList.item(0);
		if (elementName!=null && ("Detail"+objContext).equalsIgnoreCase(elementName.getNodeName()))
		{
			NamedNodeMap etlAttributes = elementName.getAttributes();
			if (etlAttributes!=null)
			{
				if (etlAttributes.getNamedItem("objName")!=null)
				{
					objName = etlAttributes.getNamedItem("objName").getNodeValue();
				}
			}
		}
		return objName;
	}

	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();
			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;
	}

}