package ibase.webitm.ejb.sys;




import ibase.system.config.ConnDriver;
import ibase.webitm.ejb.ValidatorEJB;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.sys.UtilMethods;
import java.util.*;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
//import java.util.Date; 
//import javax.ejb.CreateException;
//import javax.ejb.SessionBean;
import java.text.SimpleDateFormat;
//import java.text.DateFormat;
//import java.text.DecimalFormat;
import java.sql.Timestamp;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.ejb.Stateless; // added for ejb3
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//import com.sun.org.apache.xalan.internal.xsltc.compiler.Pattern;
//import com.sun.org.apache.xerces.internal.impl.xs.identity.Selector.Matcher;


@Stateless // added for ejb3

public class RoleMaster extends ValidatorEJB implements RoleMasterLocal, RoleMasterRemote
{
	GenericUtility genericUtility = GenericUtility.getInstance();
	String winName = null;
	UtilMethods utilMethods = UtilMethods.getInstance();
	/*public void ejbCreate() throws RemoteException, CreateException
	{
		//System.out.println("<======= SOrderForm DISPLAY IS IN PROCESS ! \n Welcome!========>");
	}
	public void ejbRemove()
	{
	}
	public void ejbActivate()
	{
	}
	public void ejbPassivate()
	{
	}*/
	
	//
	
	public String wfValData(String xmlString, String xmlString1, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		System.out.println("[wfValData 6 Param] We can Not Expect Program Call this Function [Check System Entry]");
		return "";
	}
	public String itemChanged(String xmlString, String xmlString1,String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		System.out.println("[wfValData 6 Param ]We can Not Expect Program Call this Function [Check System Entry]");
		return "";
	}
	public String wfValData(Document dom, Document dom1, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		return "";
	}
	
	
	
	
		//
	
	
	public String wfValData(String xmlString, String xmlString1,String xmlString2, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		String errString = null;
		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 );
			//long startTime = System.currentTimeMillis();
			dom = parseString(xmlString);
			dom1 = parseString(xmlString1);
			dom2 = parseString(xmlString2);
					
			errString = wfValData(dom,dom1,dom2,objContext,editFlag,xtraParams);
		}
		catch(Exception e)
		{
			//System.out.println("Exception : SOrderFormEJB : wfValData(String xmlString) : ==>\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
	{
	  	NodeList parentNodeList = null,pNodeList=null;
		NodeList childNodeList = null,cNodeList=null;
		Node parentNode = null,pNode=null;
		Node childNode = null,cNode=null;
		int ctr=0;
		String childNodeName = null,cNodeName="";
		String errString = "";
		String errCode = "";
		Connection conn = null;
		PreparedStatement pstmt = null ;
		ResultSet rs = null , rs1 = null;
		String sql = "" ,sql1 = "";
		String userId = "";
		String effFromStr = null;
		String validUptoStr = null;
		int cnt = 0,cnt1 = 0;
		int currentFormNo=0;
		int childNodeListLength=0;
		int parentNodeListLength=0;
        ConnDriver connDriver = new ConnDriver();
		StringBuffer errStringXml = new StringBuffer("<?xml version=\"1.0\"?>\r\n<Root><Errors>"); 
		String errorType = "";
		String objName = null;
		ArrayList errList = new ArrayList();
		ArrayList errFields = new ArrayList();
		String email_addr = null;
		String role_code = "";
		String chg_date = "";
		String chg_user = "";
		String chg_term = "";
		String userCode="";
		String roleCode="";
		String editMode="";
		String subStr="";
		ArrayList<String> userIdList=new ArrayList<String>();
		Set<String> userIdSet= new LinkedHashSet<String>();;
		//StringBuffer valueXmlString = new StringBuffer();
		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			connDriver = null;
			
			userId = getValueFromXTRA_PARAMS(xtraParams,"loginCode");
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
				System.out.println("currentFormNo ::"+currentFormNo);
				System.out.println("objContext ::"+objContext);
				System.out.println("dom ::"+dom);
				System.out.println("dom1 ::"+dom1);
				
			}
			switch(currentFormNo)
			{
				case 1 :
					parentNodeList = dom.getElementsByTagName("Detail1");
					parentNode = parentNodeList.item(0);
					
					System.out.println("parentNode ::"+parentNode);
					
					objName = getObjName(parentNode);
					
					System.out.println("objName ::"+objName);
					
					childNodeList = parentNode.getChildNodes();
					childNodeListLength = childNodeList.getLength();
					System.out.println("childNodeListLength ::"+childNodeListLength);
					
					for(ctr = 0; ctr < childNodeListLength; ctr++)
					{
					    childNode = childNodeList.item(ctr);
						
						childNodeName = childNode.getNodeName();
					System.out.println("childNodeName ::"+childNodeName);
					
					
					if(childNodeName.equalsIgnoreCase("role_code"))
					{
					roleCode=genericUtility.getColumnValue("role_code",dom);
					System.out.println("role_code entered [" +roleCode + "]");
					
					if(roleCode==null || roleCode.trim().length()==0)
					{
						System.out.println("Role_code Comming Null");
						errList.add("NLROLCODE");
						errFields.add("role_code");
					}
					else
					{
						pNodeList=dom.getElementsByTagName("Detail1");
						pNode=pNodeList.item(0);
						cNodeList=pNode.getChildNodes();
						for(int s=0;s<cNodeList.getLength();s++)
						{
							cNode=cNodeList.item(s);
							if(cNode.getNodeName().equalsIgnoreCase("attribute"))
							{
								editFlag=cNode.getAttributes().getNamedItem("updateFlag").getNodeValue();
								System.out.println("Edit Flag is ["+editFlag+"]");
								s=cNodeList.getLength()-1;
							}
							
						}
						if("A".equalsIgnoreCase(editFlag))
						{
							sql="SELECT COUNT(1) AS CNT FROM WF_ROLE WHERE TRIM(ROLE_CODE)=? ";
							
							try
							{
								pstmt=conn.prepareStatement(sql);
								pstmt.setString(1,roleCode.trim());
								rs=pstmt.executeQuery();	
								if(rs.next())
								{
									System.out.println("User Alrady Exist ["+ rs.getInt("CNT")+"]" );
									if(rs.getInt("CNT") !=0)
									{
										System.out.println("User already Exist");
										errList.add("ROLAE");
										errFields.add("role_code");
										
									}
								}
							}
							catch(Exception e)
							{
								System.out.println("Exception in validating roleCode PK_value"+e.getMessage());
								
							}
							finally
							{
								
							}
							
						}
						
					}
						
					}
					
					else if( childNodeName.equalsIgnoreCase( "email_addr" ) )
						{ 
							email_addr = genericUtility.getColumnValue("email_addr",dom); 
							if ( email_addr == null || email_addr.trim().length() == 0 )
							{
									errList.add("EIDIV");
									errFields.add("email_addr");
							}
							else
							{
								   boolean check;
								   String  expression="^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
								   CharSequence inputStr = email_addr;
								   Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);
								   Matcher matcher = pattern.matcher(inputStr);
								   check = matcher.matches();
								   
								   if(!check)
								   {
									   errList.add("EIDIV");
									   errFields.add("email_addr");   
								   }
								   if(check)
								   {
									   System.out.println("In if(check)");
									   
									   subStr= email_addr.substring(email_addr.indexOf("@"), email_addr.length()-1);
									   System.out.println("subString is :["+subStr+"] and length is ["+ subStr.length()+"]");
									   
									   if(subStr.indexOf(".")==0 || subStr.indexOf(".")<2)
									   {
										   System.out.println("Domain name is not specified and index is ["+subStr.indexOf(".") +"]");
										   errList.add("IVDMN");
										   errFields.add("email_addr");
										   
									   }
									   
									   //subStr=subStr.substring(subStr.indexOf("."),subStr.length()-1);
									   
									   
								   }
							}
						}
						else if(childNodeName.equalsIgnoreCase("user_id"))
						{
							userCode=genericUtility.getColumnValue("user_id",dom);
							if(userCode ==null || userCode.trim().length()==0)
							{
								errList.add("NLUSERID");
								errFields.add("user_id");
								
							}
							else
							{
								sql="select count(1) AS CNT from users where TRIM(code)=?";
								pstmt=conn.prepareStatement(sql);
								pstmt.setString(1,userCode.trim());
								rs=pstmt.executeQuery();
								if(rs.next())
								{
									System.out.println("Result count for code ["+userCode +"] is" +rs.getInt("CNT"));
									if(rs.getInt("CNT")==0)
									{
										errList.add("IVUSRID");
										errFields.add("user_id");
										
									}
								}
								
							}
							
							
						}
							
					
						/*
						else if( childNodeName.equalsIgnoreCase( "chg_date" ) )
						{
							chg_date = getColumnValue("chg_date",dom);
						}
						else if( childNodeName.equalsIgnoreCase( "chg_user" ) )
						{
							chg_user = getColumnValue("chg_user",dom);
						}
						else if( childNodeName.equalsIgnoreCase( "chg_term" ) )
						{
							chg_term = getColumnValue("chg_term",dom);
						}
						*/
                        						
					} // end for
			
					break;	
				case 2:
					parentNodeList=dom.getElementsByTagName("Detail2");
					System.out.println("ParentNodeList Length in case 2 is [" + parentNodeList.getLength() + "]");
					
					parentNode=parentNodeList.item(0);
					
					pNodeList = dom2.getElementsByTagName("Detail2");
					System.out.println("pNodeList Length in case 2 is [" + pNodeList.getLength() + "]");
					
					parentNodeListLength=pNodeList.getLength();
					
					
					childNodeList=parentNode.getChildNodes();
					
					for(int i=0;i<childNodeList.getLength();i++)
					{
						childNode=childNodeList.item(i);
						childNodeName=childNode.getNodeName();
						if("userid".equalsIgnoreCase(childNodeName))
						{
							userId=genericUtility.getColumnValue("userid", dom);
							
							if(userId ==null || userId.trim().length()==0)
							{
								errList.add("NLUSERID");
								errFields.add("user_id");
								
							}
							else
							{
								sql="select count(1) AS CNT from users where TRIM(code)=?";
								pstmt=conn.prepareStatement(sql);
								pstmt.setString(1,userId.trim());
								rs=pstmt.executeQuery();
								if(rs.next())
								{
									System.out.println("Result count for code ["+userId +"] is" +rs.getInt("CNT"));
									if(rs.getInt("CNT")==0)
									{
										errList.add("IVUSRID");
										errFields.add("user_id");
										
									}
									else if(parentNodeListLength>1)
									{
										System.out.println("Duplicate User Id Checking Start and parentNodeListLength is["+ parentNodeListLength+"]");
										pNodeList=dom2.getElementsByTagName("Detail2");
										
										for(int s=0;s<pNodeList.getLength();s++)
										{
											pNode=pNodeList.item(s);
											cNodeList=pNode.getChildNodes();
											for(int p=0;p<cNodeList.getLength();p++)
											{
												cNode=cNodeList.item(p);
												if("userid".equalsIgnoreCase(cNode.getNodeName()))
												{
													//userIdList.add(cNode.getNodeValue());
													System.out.println("Try to add node Value in Set and cNode Index ["+ p +"]  and pnodeList counter is["+s+"]" );
													userIdSet.add(cNode.getFirstChild().getNodeValue().trim());
													p= cNodeList.getLength()-1;
												}
											}
											
										}
										
										System.out.println("UserIdSet Contains ["+ userIdSet.size()+"]");
										System.out.println("ParentNodeList contains ["+parentNodeListLength +"]");
										
										if(parentNodeListLength - userIdSet.size() !=0)
										{
											System.out.println("Duplicate User Id found !");
											errList.add("UIDAE");
											errFields.add("userid");
											
											
										}
										
										
										
										
										
									}
								}
								
							}
						
							i=childNodeList.getLength()-1;
						}
						
						
						
					}
					
					
					break;


									
			} //END switch
			
			
			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);
					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;
						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( "" );
			}
		}//END TRY
		catch(Exception e)
		{
			e.printStackTrace();
			errString=e.getMessage();
			throw new ITMException( e );
		}
		finally
		{
			try
			{
				if(conn!=null)
				{
					if(rs != null )rs.close();
					if(rs1 != null )rs.close();
					rs = null;
					rs1 = 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

 	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;
		String valueXmlString = "";
		try
		{   
			dom = parseString(xmlString);
			dom1 = parseString(xmlString1);
			valueXmlString = itemChanged(dom,dom1,objContext,currentColumn,editFlag,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception : [TrainingEJB][itemChanged(String,String)] :==>\n"+e.getMessage());
		}
        return valueXmlString;
	}
	public String itemChanged(Document dom, Document dom1, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		//System.out.println("---------------------\nItem chang is called\n -----------------------");
		StringBuffer valueXmlString = new StringBuffer();
		NodeList parentNodeList = null;
		NodeList childNodeList = null;
		Node parentNode = null;
		Node childNode = null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null ;
		int ctr=0;
		String childNodeName = null;
		String columnValue = null;
		String Col_name = "";
		int currentFormNo = 0 ,cnt = 0;
		Timestamp timestamp=null;
		String item_ser = "";
		String userId = "";
		String roleCode = "";
		String emp_lname = "";
		String item_descr = "";
		String userName="";
		GenericUtility genericUtility = GenericUtility.getInstance();
		String sql = "";
		Double minAmount = 0.000; 
		String roleType="";
		ConnDriver connDriver = new ConnDriver();
		try
		{   
			conn = connDriver.getConnectDB("DriverITM");
			conn.setAutoCommit(false);
			connDriver=null;
			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 :
					valueXmlString.append("<Detail1>");
					parentNodeList = dom.getElementsByTagName("Detail1");
					parentNode = parentNodeList.item(0);
					childNodeList = parentNode.getChildNodes();
					ctr = 0;
					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();
							// Code By Amit D. on 24-Mar-2011
							// Start.	
								System.out.println("In Role Code ItemChange case 1 and column name is ["+childNodeName +"] and value is ["+columnValue+"]");
								try
								{
								sql="SELECT NAME  FROM USERS WHERE CODE= ? ";
								pstmt=conn.prepareStatement(sql);
								pstmt.setString(1,columnValue);
								rs=pstmt.executeQuery();
								while(rs.next())
								{
									userName=rs.getString(1);
									System.out.println("In rs.next() and name is"+userName);
									//valueXmlString.append("<users_name>").append("<![CDATA["+ rs.getString(1)==null?" ":rs.getString(1) +"]]>").append("</users_name>");
									//valueXmlString.append("<users_name>").append("<![CDATA["+rs.getString(1)+"]]>").append("</users_name>");
								}
								valueXmlString.append("<user_name>").append("<![CDATA["+userName+"]]>").append("</user_name>");
								}catch(Exception e)
								{
									System.out.println("Exception in Item Change of "+childNodeName+ "\n "+e.getMessage());
								}
								
								finally
								{
									if(pstmt !=null)
									{
										pstmt=null;
									}
									if(rs !=null)
									{
									    rs.close();
										rs=null;		
									}
									
								}
						//end of Code of Amit D.		
							}
						}
						
						
						if(childNodeName.equalsIgnoreCase(currentColumn))
						{
							if(childNode.getFirstChild() !=null)
							{
								columnValue=childNode.getFirstChild().getNodeValue().trim();
								System.out.println("Current cloumn ["+currentColumn +"] and value ["+ columnValue+"]");
								
								if("S".equalsIgnoreCase(columnValue))
								{
									  valueXmlString.append("<role_entity_sql protect =\"1\">").append("</role_entity_sql>");		
								}
								else
								{
									 valueXmlString.append("<role_entity_sql protect =\"0\">").append("</role_entity_sql>");
								}
							}
							
						}
						if("itm_default".equalsIgnoreCase(currentColumn))
						{
							 
							valueXmlString.append("<role_entity_sql protect =\"1\">").append("</role_entity_sql>");
							
						}
						if("itm_defaultedit".equalsIgnoreCase(currentColumn))
						{
							
                            roleType = genericUtility.getColumnValue("role_type", dom);
							if("S".equalsIgnoreCase(roleType))
							{
							valueXmlString.append("<role_entity_sql protect =\"1\">").append("</role_entity_sql>");	
							}
							
							
						}
						
						
						ctr++;
					}while(ctr < childNodeListLength  && !childNodeName.equals(currentColumn));
				  valueXmlString.append("</Detail1>"); 
                  break;
				  
				  case 2:
					
					parentNodeList = dom1.getElementsByTagName("Detail1");
					parentNode = parentNodeList.item(0);
					winName = getObjName(parentNode);
					System.out.println( "winName :: " + winName );
					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.trim()))
						{
							if (childNode.getFirstChild() != null)
							{
								columnValue = childNode.getFirstChild().getNodeValue();
							}
						}
						ctr++;
					}
				   while(ctr < childNodeListLength && !childNodeName.equals(currentColumn)); 
						   
					if(currentColumn.trim().equalsIgnoreCase("itm_default"))
					{
						roleCode = genericUtility.getColumnValue("role_code", dom1);
						//userId = genericUtility.getColumnValue("user_id",dom1);
											
						System.out.println("roleCode ::"+roleCode);
						//System.out.println("userId ::"+userId);
						/* 
						  if(userId !=null )
						{
							sql="SELECT NAME  FROM USERS WHERE CODE= ? ";
							pstmt=conn.prepareStatement(sql);
							pstmt.setString(1,userId);
							rs=pstmt.executeQuery();
							while(rs.next())
							{
								userName=rs.getString(1);
								System.out.println("In rs.next() and name is"+userName);
								//valueXmlString.append("<users_name>").append("<![CDATA["+ rs.getString(1)==null?" ":rs.getString(1) +"]]>").append("</users_name>");
								//valueXmlString.append("<users_name>").append("<![CDATA["+rs.getString(1)+"]]>").append("</users_name>");
							}
							//valueXmlString.append("<user_name>").append("<![CDATA["+userName+"]]>").append("</user_name>");
							
							
						}
						*/
						
						if (roleCode == null || roleCode.trim().length() == 0 || "null".equalsIgnoreCase( roleCode.trim() ) )
						{
							valueXmlString.append("<role_code protect =\"1\">").append("<![CDATA["+""+"]]>").append("</role_code>");
						}
						else
						{
							valueXmlString.append("<role_code protect =\"1\">").append("<![CDATA["+roleCode+"]]>").append("</role_code>");
						}
						//valueXmlString.append("<userid protect =\"1\">").append("<![CDATA["+userId+"]]>").append("</userid>");
						
						//valueXmlString.append("<users_name protect=\"1\">").append("<![CDATA["+userName +"]]>").append("</users_name>");
					} // end itm_default
					else if(currentColumn.trim().equalsIgnoreCase("itm_defaultedit"))
					{
					} // end itm_defaultedit
					
					else if(currentColumn.equalsIgnoreCase("userid"))
					{
						userId=genericUtility.getColumnValue("userid",dom);
						if(userId !=null || userId.trim().length()!=0)
						{
							sql="select emp_fname,emp_mname,emp_lname from employee where"
								+" emp_code = (Select emp_code from users where code = ?)";
							pstmt=conn.prepareStatement(sql);
							pstmt.setString(1,userId);
							rs=pstmt.executeQuery();
							if(rs.next())
							{
							  valueXmlString.append("<emp_fname>").append("<![CDATA["+rs.getString("emp_fname")+"]]>").append("</emp_fname>");
							  valueXmlString.append("<emp_mname>").append("<![CDATA["+rs.getString("emp_mname")+"]]>").append("</emp_mname>");
							  valueXmlString.append("<emp_lname>").append("<![CDATA["+rs.getString("emp_lname")+"]]>").append("</emp_lname>");
							  valueXmlString.append("<users_code protect =\"1\">").append("<![CDATA["+userId+"]]>").append("</users_code>");  
								
							}
							

							
						}
						
					}
					
				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(pstmt != null)
					pstmt.close();
					if(rs != null)
					rs.close();
					rs = null;
					pstmt = null;
					conn.close();
					conn = null;
				}
				
			}catch(Exception d)
			{
			  d.printStackTrace();
			}			
		}
		return valueXmlString.toString();
	}	 


	private String errorType( Connection conn , String errorCode )
	{
		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();
		}		
		finally
		{
			try
			{
				if ( rs != null )
				{
					rs.close();
					rs = null;
				}
				if ( pstmt != null )
				{
					pstmt.close();
					pstmt = null;
				}
			}
			catch ( Exception e )
			{
				e.printStackTrace();
			}
		}		
		return msgType;
	}
	 
	private String getObjName(Node node) throws Exception
	{
		String objName = null;
		NodeList nodeList = null;
		Node detaulNode = null;
		Node detailNode = null;
		nodeList = node.getChildNodes();
		NamedNodeMap attrMap = node.getAttributes();
		objName = attrMap.getNamedItem( "objName" ).getNodeValue();
		return "w_" + objName;
	}			
}// END OF MAIN CLASS
		
		
