package ibase.webitm.ejb.fin;

import java.rmi.RemoteException;
import java.text.*;
import java.sql.*;
import org.w3c.dom.*;
import javax.ejb.*;
import ibase.webitm.utility.ITMException;
import ibase.webitm.utility.*;
import ibase.webitm.ejb.*;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3
public class GlOpen extends ValidatorEJB implements  GlOpenLocal,GlOpenRemote //SessionBean
{
	/*public void ejbCreate() throws RemoteException, CreateException 
	{
		System.out.println("GlOpenEJB is in Process..........");
	}
	public void ejbRemove()
	{
	}
	public void ejbActivate() 
	{
	}
	public void ejbPassivate() 
	{
	}*/
	public String wfValData() throws RemoteException,ITMException
	{
		return "";
	}

	public String itemChanged() throws RemoteException,ITMException
	{
		return "";
	}

	public String wfValData(String xmlString, String xmlString1,String xmlString2, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		Document dom = null;
		Document dom1 = null;
		Document dom2 = null;
		String  errString = null;
		System.out.println("Validation Start..........");
		try
		{
			System.out.println("xmlString:::"+xmlString);
			dom = parseString(xmlString);
			dom1 = parseString(xmlString1); 
			if (xmlString2.trim().length() > 0 )
			{
				dom2 = parseString("<Root>" + xmlString2+ "</Root>");
			}
			errString = wfValData(dom,dom1,dom2,objContext,editFlag,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception : Sordform_attEJB : wfValData(String xmlString) : ==>\n"+e.getMessage());
		}
		return (errString);
	}

	public String wfValData(Document dom, Document dom1,Document dom2, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		String errString = "";
		String errCode = "";
		Connection conn = null;
		Statement stmt = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "";
		String userId = ""; 
		String loginSite = "";
		String siteCode = "";
		String currCode = "";
		String itemSer = "";
		NodeList parentNodeList = null;
		NodeList childNodeList = null;
		Node parentNode = null;
		Node childNode = null;
		String childNodeName = ""; 
		int ctr,currentFormNo=0;
		int childNodeListLength;
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB(); 
		try
		{
			conn = getConnection(); 
			stmt = conn.createStatement();
			userId = getValueFromXTRA_PARAMS(xtraParams,"userId");
			loginSite = getValueFromXTRA_PARAMS(xtraParams, "loginSiteCode");
			GenericUtility genericUtility = GenericUtility.getInstance(); 
			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++)
					{
						int cnt;
						childNode = childNodeList.item(ctr);
						childNodeName = childNode.getNodeName();
						if (childNodeName.equals("site_code"))
						{
							siteCode = getColumnValue("site_code",dom);
							if (childNode.getFirstChild() == null)
							{
								errCode = "VMSITECD1";
								errString = getErrorString("site_code",errCode,userId);
								break;
							}
							System.out.println("siteCode::"+siteCode);
							System.out.println("call to site code function");
							errCode = itmDBAccessEJB.isSiteCode(siteCode,"fin",conn); 
							System.out.println("errcode="+errCode);
							if(errCode != "")
							{
								errString = getErrorString("site_code",errCode,userId);
								break;
							}
						}
						else if (childNodeName.equals("curr_code"))
						{
							currCode = getColumnValue("curr_code",dom);
							if (childNode.getFirstChild() == null)
							{
								errCode = "VMSITECD2";
								errString = getErrorString("curr_code",errCode,userId);
								break;
							}
							System.out.println("curr_code:::"+currCode);
							sql = "SELECT COUNT(*) AS COUNT FROM CURRENCY WHERE curr_code = '"+currCode+"'";
							rs = stmt.executeQuery(sql);
							if (rs.next())
							{
								cnt = rs.getInt("COUNT");
								if (cnt == 0)
								{
									errCode = "VMSITE2";
									errString = getErrorString("curr_code",errCode,userId);
									break;
								}
							}
						}
					}//END FOR
				
					case 2:
					String acctCode = "";
					String cctrCode = "";
					String crAmt = "";
					String drAmt = "";
					double d_crAmt= 0;
					double d_drAmt= 0;
					
					
					System.out.println("Validation for Detail..........");
					parentNodeList = dom.getElementsByTagName("Detail2");
					parentNode = parentNodeList.item(0);
					childNodeList = parentNode.getChildNodes();
					childNodeListLength = childNodeList.getLength();
					for(ctr = 0; ctr < childNodeListLength; ctr++)
					{
						int cnt;
						childNode = childNodeList.item(ctr);
						childNodeName = childNode.getNodeName();
						if (childNodeName.equals("acct_code"))
						{
							acctCode = getColumnValue("acct_code",dom,"2");
							if (childNode.getFirstChild() == null)	
							{
								errCode = "VMSITECD1";
								errString = getErrorString("acct_code",errCode,userId);
								break;
							}
							System.out.println("acct_code:::"+acctCode);
							Connection c = null;
							//errCode = itmDBAccessEJB.isAcctCode(getColumnValue("site_code",dom1),acctCode,"fin",c);
							errCode = isAcctCode(getColumnValue("site_code",dom1),acctCode,"fin",c);
							if(errCode != "")
							{
								errString = getErrorString("acct_code",errCode,userId);
								break;
							}
													
						}
						if (childNodeName.equals("cr_amt"))
						{
							
							drAmt = getColumnValue("dr_amt",dom,"2");
							crAmt = getColumnValue("cr_amt",dom,"2");
							d_drAmt = Double.parseDouble(drAmt);
							d_crAmt = Double.parseDouble(crAmt);
							if(d_drAmt > 0 && d_crAmt > 0)
							{
								errCode = "VTCRAMT";
								errString = getErrorString("cr_amt",errCode,userId);
								break;
							}
							
						}
						if (childNodeName.equals("dr_amt"))
						{
							
							drAmt = getColumnValue("dr_amt",dom,"2");
							crAmt = getColumnValue("cr_amt",dom,"2");
							d_drAmt = Double.parseDouble(drAmt);
							d_crAmt = Double.parseDouble(crAmt);
							if(d_drAmt > 0 && d_crAmt>0)
							{
								errCode = "VTCRAMT";
								errString = getErrorString("cr_amt",errCode,userId);
								break;
							}
							
						}
						
						if (childNodeName.equals("cctr_code"))
						{
							cctrCode = getColumnValue("cctr_code",dom,"2");
							if (childNode.getFirstChild() == null)
							{
								errCode = "VMSITECD1";
								errString = getErrorString("cctr_code",errCode,userId);
								break;
							}
							System.out.println("cctr_code:::"+cctrCode);
/*						sql = "SELECT COUNT(*) AS COUNT FROM COSTCTR WHERE cctr_code = '"+cctrCode+"'";
							rs = stmt.executeQuery(sql);
							if (rs.next())
							{
								cnt = rs.getInt("COUNT");
								if (cnt == 0)
								{
									errCode = "VMSITECD1";
									errString = getErrorString("cctr_code",errCode,userId);
									break;
								}
							}*/
							errCode = itmDBAccessEJB.isCctrCode(getColumnValue("acct_code",dom,"2"),cctrCode,"fin",conn);
							System.out.println("acct_code="+getColumnValue("acct_code",dom,"2"));
							if(errCode != "")
							{
								errString = getErrorString("cctr_code",errCode,userId);
								break;
							}
						}
					
				}//END FOR
				
			}//END SWITCH
		}//END TRY
		catch(Exception e)
		{
			System.out.println("Exception ::"+e);
			e.printStackTrace();
		}
		System.out.println("ErrString ::"+errString);
		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;
		Document dom2 = null;
		String valueXmlString = "";
		try
		{
			dom = parseString(xmlString); 
			System.out.println("xmlString" + xmlString);
			dom1 = parseString(xmlString1); 
			if (xmlString2.trim().length() > 0 )
			{
				dom2 = parseString("<Root>" + xmlString2+ "</Root>");
			}
			valueXmlString = itemChanged(dom,dom1,dom2,objContext,currentColumn,editFlag,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception : [Gl_openEJB][itemChanged(String,String)] :==>\n"+e.getMessage());
		}
        return valueXmlString; 
	}
	public String itemChanged(Document dom, Document dom1,Document dom2, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		int n;
		int currentFormNo = 0;
		String errCode = "";
		Connection connGl = null;
		Statement stmtGl = null;
		ResultSet rsGl = null;
		String errString = "";
		StringBuffer valueXmlString = new StringBuffer();
		String sql = "";
		String columnValue = "";
		String loginSite = "";
		String format = "";
		NodeList parentNodeList = null;
		Node parentNode = null;
		Node childNode = null;
		NodeList childNodeList = null;
		String childNodeName = "";
		int ctr = 0;
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		GenericUtility genericUtility = GenericUtility.getInstance();
		java.util.Date DateX = new java.util.Date();
		java.text.SimpleDateFormat dtf= new SimpleDateFormat(format);
		String userId = getValueFromXTRA_PARAMS(xtraParams,"userId");
		String empCode = getValueFromXTRA_PARAMS(xtraParams,"empCode");
		loginSite = getValueFromXTRA_PARAMS(xtraParams,"loginSiteCode");
		StringBuffer retString = new StringBuffer();

		try
		{
			format= getApplDateFormat();
		}
		catch(Exception e)
		{
			System.out.println("Exception :[Gl_openEJB]While Getting Date Format"+e.getMessage());		
		}
		
		
		try
		{
			connGl = getConnection(); 
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			System.out.println("[Gl_openEJB] [itemChanged] :currentFormNo ....." +currentFormNo);
			valueXmlString = new StringBuffer("<?xml version=\"1.0\"?><Root><header><editFlag>");
			valueXmlString.append(editFlag).append("</editFlag></header>");
			valueXmlString.append("<Detail>");	
			System.out.println("FORM NO:::"+currentFormNo);							
			switch (currentFormNo)
			{
				case 1:
					
//				SEARCHING THE DOM FOR THE INCOMING COLUMN VALUE START
					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();
							}
						}
						ctr++;
					}while(ctr < childNodeListLength  && !childNodeName.equals(currentColumn));
					
					if (currentColumn.trim().equals("itm_default"))
					{
						String keyStringQuery = null;
						String tranId = null;
						PreparedStatement pstmt = null;
						ResultSet rSet = null ;
						Statement stmt = null;
						String accPrd = "";
						String siteDescr = "";
						String finEnt = "";
						String finDescr = "";
						String currDescr = "";
						String currCode = "";
						System.out.println("Login site "+loginSite);
						sql = "SELECT acct_prd FROM parameter";
						stmtGl = connGl.createStatement();
						rsGl = stmtGl.executeQuery(sql);
						if (rsGl.next())
						{
							accPrd = rsGl.getString(1);
						}						
						sql = "SELECT A.DESCR,A.FIN_ENTITY, B.DESCR, B.CURR_CODE, C.DESCR "
								+" FROM SITE A, FINENT B, CURRENCY C "
								+ "WHERE A.FIN_ENTITY = B.FIN_ENTITY "
								+ "AND B.CURR_CODE = C.CURR_CODE "
								+ "AND A.SITE_CODE ='" +loginSite+ "'";
						stmtGl = connGl.createStatement();
						rsGl = stmtGl.executeQuery(sql);
						while (rsGl.next())
						{
							siteDescr = rsGl.getString(1);
							finEnt  = rsGl.getString(2);
							finDescr = rsGl.getString(3);
							currCode = rsGl.getString(4);
							currDescr = rsGl.getString(5);
						}
						stmtGl.close();
						System.out.println("SiteCode::::"+loginSite);
						System.out.println("sitSiteCode Desc::::"+siteDescr);
						System.out.println("FinEntity.:::"+finEnt);
						System.out.println("FinEntityDesc.:::"+finDescr);
						System.out.println("CurrCode.:::"+currCode);
						System.out.println("CurrDescr.:::"+currDescr);
						System.out.println("AaccountPeriod.:::"+accPrd);
						
								
						valueXmlString.append("<site_code>").append(loginSite).append("</site_code>");
						valueXmlString.append("<acct_prd>").append(accPrd).append("</acct_prd>");
						//valueXmlString.append("<site_descr>").append(siteDescr).append("</site_descr>"); //Gulzar 29/12/06
						valueXmlString.append("<site_descr>").append("<![CDATA["+siteDescr+"]]>").append("</site_descr>"); //Gulzar 29/12/06
						valueXmlString.append("<fin_entity>").append(finEnt).append("</fin_entity>");
						//valueXmlString.append("<finent_descr>").append(finDescr).append("</finent_descr>"); //Gulzar 29/12/06
						valueXmlString.append("<finent_descr>").append("<![CDATA["+finDescr+"]]>").append("</finent_descr>"); //Gulzar 29/12/06
						valueXmlString.append("<curr_code>").append(currCode).append("</curr_code>");
						//valueXmlString.append("<currency_descr>").append(currDescr).append("</currency_descr>"); //Gulzar 29/12/06
						valueXmlString.append("<currency_descr>").append("<![CDATA["+currDescr+"]]>").append("</currency_descr>"); //Gulzar 29/12/06
						valueXmlString.append("<tran_type protect = '1'>").append("OPN").append("</tran_type>"); //Gulzar 06-12-06				
					}
					else if (currentColumn.trim().equals("curr_code"))
					{
						String currDescr = "";
						if (columnValue != null)
						{
							try					 
							{
								sql = "SELECT DESCR FROM CURRENCY WHERE CURR_CODE ='" +columnValue+ "'";
								stmtGl = connGl.createStatement();
								rsGl = stmtGl.executeQuery(sql);
								if (rsGl.next())
								{
									currDescr = rsGl.getString(1);
								}
								rsGl.close();
								stmtGl.close();
							}
							catch(Exception e)
							{
								System.out.println("Exception :[Gl_openEJB][itemChanged::case 1::curr_code] :==>\n"+e.getMessage());
								throw new ITMException(e);
							}
						}
						//valueXmlString.append("<currency_descr>").append(currDescr).append("</currency_descr>"); //Gulzar 29/12/06
						valueXmlString.append("<currency_descr>").append("<![CDATA["+currDescr+"]]>").append("</currency_descr>");//Gulzar 29/12/06
					}
									
					else if (currentColumn.trim().equals("site_code"))
					{
						String siteDescr = "";
						String finEnt = "";
						String finDescr = "";
						String currDescr = "";
						String currCode = "";
						if(columnValue != null)
						{
							try
							{   
								sql = "SELECT A.DESCR,A.FIN_ENTITY, B.DESCR, B.CURR_CODE, C.DESCR "
									+" FROM SITE A, FINENT B, CURRENCY C "
									+ "WHERE A.FIN_ENTITY = B.FIN_ENTITY "
									+ "AND B.CURR_CODE = C.CURR_CODE "
									+ "AND A.SITE_CODE ='" +columnValue+ "'";
									stmtGl = connGl.createStatement();
									rsGl = stmtGl.executeQuery(sql);
									while (rsGl.next())
									{
										siteDescr = rsGl.getString(1);
										finEnt  = rsGl.getString(2);
										finDescr = rsGl.getString(3);
										currCode = rsGl.getString(4);
										currDescr = rsGl.getString(5);
									}
									stmtGl.close();
									System.out.println("SiteCode::::"+columnValue);
									System.out.println("sitSiteCode Desc::::"+siteDescr);
									System.out.println("FinEntity.:::"+finEnt);
									System.out.println("FinEntityDesc.:::"+finDescr);
									System.out.println("CurrCode.:::"+currCode);
									System.out.println("CurrDescr.:::"+currDescr);
								
									valueXmlString.append("<site_code>").append(loginSite).append("</site_code>");
									//valueXmlString.append("<site_descr>").append(siteDescr).append("</site_descr>");//Gulzar 29/12/06
									valueXmlString.append("<site_descr>").append("<![CDATA["+siteDescr+"]]>").append("</site_descr>");//Gulzar 29/12/06
									valueXmlString.append("<fin_entity>").append(finEnt).append("</fin_entity>");
									//valueXmlString.append("<finent_descr>").append(finDescr).append("</finent_descr>");//Gulzar 29/12/06
									valueXmlString.append("<finent_descr>").append("<![CDATA["+finDescr+"]]>").append("</finent_descr>");//Gulzar 29/12/06
									valueXmlString.append("<curr_code>").append(currCode).append("</curr_code>");
									//valueXmlString.append("<currency_descr>").append(currDescr).append("</currency_descr>");//Gulzar 29/12/06
									valueXmlString.append("<currency_descr>").append("<![CDATA["+currDescr+"]]>").append("</currency_descr>");//Gulzar 29/12/06
							}
							catch(Exception e)
							{
								System.out.println("Exception :[Gl_openEJB][itemChanged::case 1::order_type] :==>\n"+e.getMessage());
								throw new ITMException(e);
							}
						}
					}
					valueXmlString.append("</Detail>");
					break;
					
					case 2:
					String currCode = "";
					String 	currDescr = "";
					String 	tranId = "";
					String acctPrd = "";
					String cctrCode = "";
					String acctCode = "";
					String finEnt = "";
					String prdCode ="";
					String siteCode  = "";
					siteCode = getColumnValue("site_code",dom1);
					currCode = getColumnValue("curr_code",dom1);
					currDescr = getColumnValue("currency_descr",dom1);
					tranId = getColumnValue("tran_id",dom1);
					finEnt = getColumnValue("fin_entity",dom1);
					acctPrd = getColumnValue("acct_prd",dom1);
					prdCode = getColumnValue("prd_code",dom1);
						
//				 SEARCHING THE DOM FOR THE INCOMING COLUMN VALUE START
					parentNodeList = dom.getElementsByTagName("Detail2");
					parentNode = parentNodeList.item(0);
					childNodeList = parentNode.getChildNodes();
					ctr = 0;
					int childListLength = childNodeList.getLength();
					do
					{
						childNode = childNodeList.item(ctr);
						childNodeName = childNode.getNodeName();
						if(childNodeName.equals(currentColumn))
						{
							if (childNode.getFirstChild() != null)
							{
								columnValue=childNode.getFirstChild().getNodeValue();
							}
						}
						ctr++;
					}while(ctr < childListLength && !childNodeName.equals(currentColumn));
//					SEARCHING THE DOM FOR THE INCOMING COLUMN VALUE END
					if (currentColumn.trim().equals("itm_default"))
					{
						String sqlEx = "";
						String dbCurrCode = "";
						
					
						sqlEx = "SELECT CURR_CODE FROM FINENT WHERE FIN_ENTITY ='" +finEnt+ "'";
						stmtGl = connGl.createStatement();
						rsGl = stmtGl.executeQuery(sqlEx);
						while (rsGl.next())
						{
							dbCurrCode = rsGl.getString(1);
						}
						rsGl.close();
						stmtGl.close();
						System.out.println("Curr Code from DATA BASE:::"+dbCurrCode);
						System.out.println("Curr Code from HEADER:::"+dbCurrCode);
						
						if (currCode.trim().equalsIgnoreCase(dbCurrCode.trim()))
						{
							valueXmlString.append("<exch_rate protect = '1'>").append("1").append("</exch_rate>");								
						}
						else
						{
							valueXmlString.append("<exch_rate protect = '0'>").append("</exch_rate>");								
						}
						
						System.out.println("currCode(HEADER)"+currCode);
						System.out.println("currDescr(HEADER)"+currDescr);
						valueXmlString.append("<tran_id>").append(tranId).append("</tran_id>");
						valueXmlString.append("<curr_code>").append(currCode).append("</curr_code>");
						//valueXmlString.append("<currency_descr>").append(currDescr).append("</currency_descr>");
						valueXmlString.append("<currency_descr>").append("<![CDATA["+currDescr+"]]>").append("</currency_descr>");
					}
					else if (currentColumn.trim().equals("cr_amt")) 
					{
						 double exchRate = Double.parseDouble(getColumnValue("exch_rate",dom,"2"));
						 double crAmt = Double.parseDouble(getColumnValue("cr_amt",dom,"2"));
						 double result =  exchRate * crAmt;
						 String sResult =  Double.toString(result);
						valueXmlString.append("<cr_amt__base>").append(sResult).append("</cr_amt__base>");	
					}
					else if (currentColumn.trim().equals("dr_amt"))
					{
						 double exchRate = Double.parseDouble(getColumnValue("exch_rate",dom,"2"));
						 double crAmt = Double.parseDouble(getColumnValue("dr_amt",dom,"2"));
						 double result = exchRate * crAmt;
						 String sResult =  Double.toString(result);
						valueXmlString.append("<dr_amt__base>").append(sResult).append("</dr_amt__base>");	
					}
					else if (currentColumn.trim().equals("acct_code"))
					{
						String acctDescr = "";
						double crAmtOld = 0;
						double drAmtOld = 0;
						double crAmtBaseOld = 0;
						double drAmtBaseOld = 0;
						
						if (columnValue != null)
						{
							try					 
							{
								sql = "SELECT DESCR FROM ACCOUNTS WHERE ACCT_CODE ='" +columnValue+ "'";
								cctrCode = getColumnValue("cctr_code",dom);
								stmtGl = connGl.createStatement();
								rsGl = stmtGl.executeQuery(sql);
														
								if (rsGl.next())
								{
									acctDescr = rsGl.getString(1);
								}
								rsGl.close();
								stmtGl.close();
							}
							catch(Exception e)
							{
								System.out.println("Exception :[Gl_openEJB][itemChanged::case 1::curr_code] :==>\n"+e.getMessage());
								throw new ITMException(e);
							}
							//********************
							
							cctrCode = getColumnValue("cctr_code",dom,"2"); 
							System.out.println("acct_code="+columnValue);
							System.out.println("acctPrd="+acctPrd);
							System.out.println("prdCode="+prdCode);
							System.out.println("siteCode="+siteCode);
							System.out.println("cctr_code="+cctrCode);
							System.out.println("finEnt="+finEnt);
							
							sql = "SELECT CR_AMT, DR_AMT ,CR_AMT__BASE, DR_AMT__BASE FROM ACCTBAL "+
								  " WHERE ACCT_PRD = '"+ acctPrd +"' AND PRD_CODE = '"+ prdCode+"' AND FIN_ENTITY = '"+ finEnt +"'"+
								  " and SITE_CODE = '"+ siteCode +"' AND ACCT_CODE = '"+ columnValue +"' AND CCTR_CODE = '"+ cctrCode +"'";	
							 try					 
							{
								stmtGl = connGl.createStatement();
								rsGl = stmtGl.executeQuery(sql);
								if (rsGl.next())
								{
									crAmtOld = rsGl.getDouble(1);
									drAmtOld = rsGl.getDouble(2);
									crAmtBaseOld = rsGl.getDouble(3);
									drAmtBaseOld = rsGl.getDouble(4);
								}
								rsGl.close();
								stmtGl.close();
							}
							catch(Exception e)
							{
								System.out.println("Exception :[Gl_openEJB][itemChanged::case 1::cctr_code] :==>\n"+e.getMessage());
								throw new ITMException(e);
							}
							//***********************
						}
						//valueXmlString.append("<accounts_descr>").append(acctDescr).append("</accounts_descr>");//Gulzar 29/12/06
						valueXmlString.append("<accounts_descr>").append("<![CDATA["+acctDescr+"]]>").append("</accounts_descr>");//Gulzar 29/12/06
						valueXmlString.append("<cr_amt__old>").append(crAmtOld).append("</cr_amt__old>");
						valueXmlString.append("<dr_amt__old>").append(drAmtOld).append("</dr_amt__old>");
						valueXmlString.append("<cr_amt__base__old>").append(crAmtBaseOld).append("</cr_amt__base__old>");
						valueXmlString.append("<dr_amt__base__old>").append(drAmtBaseOld).append("</dr_amt__base__old>");
					}
					else if (currentColumn.trim().equals("cctr_code"))
					{
						String cctrDescr = "";
						int crAmtOld = 0;
						int drAmtOld = 0;
						int crAmtBaseOld = 0;
						int drAmtBaseOld = 0;
						if (columnValue != null)
						{
							try					 
							{
								sql = "SELECT DESCR FROM COSTCTR WHERE cctr_code ='" +columnValue+ "'";
								stmtGl = connGl.createStatement();
								rsGl = stmtGl.executeQuery(sql);
								if (rsGl.next())
								{
									cctrDescr = rsGl.getString(1);
								}
								rsGl.close();
								stmtGl.close();
							}
							catch(Exception e)
							{
								System.out.println("Exception :[Gl_openEJB][itemChanged::case 1::cctr_code] :==>\n"+e.getMessage());
								throw new ITMException(e);
							}
							//*******************************
							acctCode = getColumnValue("acct_code",dom,"2"); 
							System.out.println("acct_code="+columnValue);
							System.out.println("acctPrd="+acctPrd);
							System.out.println("prdCode="+prdCode);
							System.out.println("siteCode="+siteCode);
							System.out.println("cctr_code="+cctrCode);
							System.out.println("finEnt="+finEnt);
							
							sql = "SELECT CR_AMT, DR_AMT ,CR_AMT__BASE, DR_AMT__BASE FROM ACCTBAL "+
								  " WHERE ACCT_PRD = '"+ acctPrd +"' AND PRD_CODE = '"+ prdCode+"' AND FIN_ENTITY = '"+ finEnt +"'"+
								  " and SITE_CODE = '"+ siteCode +"' AND ACCT_CODE = '"+acctCode +"' AND CCTR_CODE = '"+ columnValue +"'";	
							 try					 
							{
								stmtGl = connGl.createStatement();
								rsGl = stmtGl.executeQuery(sql);
								if (rsGl.next())
								{
									crAmtOld = rsGl.getInt(1);
									drAmtOld = rsGl.getInt(2);
									crAmtBaseOld = rsGl.getInt(3);
									drAmtBaseOld = rsGl.getInt(4);
								}
								rsGl.close();
								stmtGl.close();
							}
							catch(Exception e)
							{
								System.out.println("Exception :[Gl_openEJB][itemChanged::case 1::cctr_code] :==>\n"+e.getMessage());
								throw new ITMException(e);
							}
							//*******************
						}
						valueXmlString.append("<cr_amt__old>").append(crAmtOld).append("</cr_amt__old>");
						valueXmlString.append("<dr_amt__old>").append(drAmtOld).append("</dr_amt__old>");
						valueXmlString.append("<cr_amt__base__old>").append(crAmtBaseOld).append("</cr_amt__base__old>");
						valueXmlString.append("<dr_amt__base__old>").append(drAmtBaseOld).append("</dr_amt__base__old>");
						//valueXmlString.append("<costctr_descr>").append(cctrDescr).append("</costctr_descr>");//Gulzar 29/12/06
						valueXmlString.append("<costctr_descr>").append("<![CDATA["+cctrDescr+"]]>").append("</costctr_descr>");//Gulzar 29/12/06
					}
					valueXmlString.append("</Detail>");
					break;
				}//END OF SWITCH
				valueXmlString.append("</Root>");				
			}//END OF TRY
		
		catch(Exception e)
		{
			System.out.println("Exception ::"+e.getMessage());
		}
		
		return valueXmlString.toString();
	
	}//END OF ITEMCHNGE
	
	public String isAcctCode(String siteCode,String acctCode,String modName,Connection con) throws RemoteException,ITMException
	{
		String errCode = "", siteSpec = "";
		long counter;
		String sql1 = "";
		Statement statement1 = null;
		ResultSet resultSet1 = null;

		String sql2 = "";
		Statement stmt2 = null;
		ResultSet resultSet2 = null;

		String sql3 = "";
		Statement stmt3 = null;
		ResultSet rs3 = null;

		StringBuffer sql4 = null;
		Statement stmt4 = null;
		ResultSet rs4 = null;
 		boolean connectionState = false;
		try
		{
			if(con == null)
			{
				con = getConnection();
				connectionState = true;
			}
			sql1 = "SELECT VAR_VALUE FROM FINPARM WHERE PRD_CODE = '999999' AND VAR_NAME = 'SITE_SPECIFIC_ACCT'";
			statement1 = con.createStatement();
			resultSet1 = statement1.executeQuery(sql1);
			if (resultSet1.next())
			{
				sql2 = "SELECT COUNT(*) FROM ACCOUNTS WHERE ACCT_CODE = '" + acctCode + "'";
				stmt2 = con.createStatement();
				resultSet2 = stmt2.executeQuery(sql2);
				if (resultSet2.next())
				{
					if (resultSet2.getLong(1) != 0)
					{
						sql3 = "SELECT ACTIVE FROM ACCOUNTS WHERE ACCT_CODE = '" + acctCode + "'";
						stmt3 = con.createStatement();
						rs3 = stmt3.executeQuery(sql3);
						if (rs3.next())
						{
							if (!rs3.getString(1).equals("Y"))
							{
								// Error : A/c code not active.
								errCode = "VMACCTA";
							}
							else
							{
								siteSpec = resultSet1.getString(1).trim();
								if(siteSpec.equals("Y") && siteSpec != null)
								{
									sql4 = new StringBuffer();
									sql4.append("SELECT COUNT(*) FROM SITE_ACCOUNT WHERE SITE_CODE = '");
									sql4.append(siteCode).append("' AND ACCT_CODE = '");
									sql4.append(acctCode).append("'");
									stmt4 = con.createStatement();
									rs4 = stmt4.executeQuery(sql4.toString());
									if (rs4.next())
									{
										if (rs4.getLong(1) == 0)
										{
											errCode = "VMACCT4";
										}
									}
								}
							}
						}
					}
					else
					{
						errCode = "VMACCT1";
					}
				}
			}
			if (resultSet1 != null)      //Rakesh
			{
				resultSet1.close();			
			}
			if (resultSet2 != null)     
			{
				resultSet2.close();			
			}           
			if (rs3 != null)   
			{
				rs3.close();
			}    
			if (rs4 != null)   
			{
				rs4.close();             //Rakesh
			}   
			if(connectionState == true)
			{
				con.close();
			}
			else
			{
				con = null;
			}
		}
		catch(ITMException itme)
		{
			System.out.println("ITMException :ITMDBAccessEJB :isAcctCode:" + itme.getMessage() + ":");
			throw itme;
		}
		catch(Exception e)
		{
			System.out.println("Exception :ITMDBAccessEJB :isAcctCode:" + e.getMessage() + ":");
			errCode = e.getMessage();
			throw new ITMException(e);
		}
		return errCode;
	}

}