

/********************************************************
	Title : MiscDrCrIC
	Date  : 
	Developer: Dipak Chattar

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

package ibase.webitm.ejb.fin.adv;
import ibase.system.config.ConnDriver;
import ibase.webitm.ejb.ITMDBAccessEJB;
import ibase.webitm.ejb.ValidatorEJB;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import javax.ejb.Stateless;

import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
// added for ejb3


@Stateless // added for ejb3

public class DrCrRcpMstIC extends ValidatorEJB implements DrCrRcpMstICLocal, DrCrRcpMstICRemote
{
	GenericUtility genericUtility = GenericUtility.getInstance();
	String winName = null;
	FinCommon finCommon = null;
	ValidatorEJB validator = null;
    
  
	//method for validation
	public String wfValData(String xmlString, String xmlString1, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		String errString = "";
	    Document dom = null;
		Document dom1 = null;
		try
		{
			System.out.println("Val xmlString :: " + xmlString);
			System.out.println("Val xmlString1 :: " + xmlString1);
			dom = parseString(xmlString);
			dom1 = parseString(xmlString1);
			errString = wfValData(dom, dom1, objContext, editFlag, xtraParams);
	    }
		catch(Exception e)
		{
			throw new ITMException(e);
		}
		return(errString);
	}

    //method for validation
	public String wfValData(Document dom, Document dom1, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
    {
		NodeList parentNodeList = null;
		NodeList childNodeList = null;	
		Node parentNode = null;
		Node childNode = null;
		String errorType = "";
		String childNodeName = null;
		String refNo = "";
		String errString = "";
		String errCode = "";
		String userId = "";
		String sql = "";
		String analCode = "";
		String empCode = "";
		String siteCodeDrcr = "";
		String itemSer = "";
		String tranSer = "";
		String adjMiscCrn = "";
		String currCode = "";
		String dueDate = "";
		String finEntity = "";
		String acctCode = "";
		String custRefNo = "";
		String custRefDate = "";
		String tranId = "";
		String siteCode = "";
		String cctrCode = "";	
		String drcrFlag = "";
		String effDate = "";
		String sundryCode = "";
		String sundryType = "";
		String tranDate = "";
		String itemCode = "";
		String adjAmt = "";
		String refSer = "";
		String tranType = "";
		String refType = "";
		String modName = "";
		String amount1 = "";
		int ctr=0;
		int cnt = 0;
		int lineNo = 0;
		int currentFormNo = 0;
		int childNodeListLength;
	    Double balAmt = 0.0;
		Double sumAdj = 0.0;
		Double amount = 0.0;
		java.util.Date tranPdate = 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>"); 
		try							
		{
			finCommon = new FinCommon();
			validator = new ValidatorEJB();
			ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
			conn = connDriver.getConnectDB("DriverITM");
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(genericUtility.getApplDateFormat());
			SimpleDateFormat dbDateFormat = new SimpleDateFormat(genericUtility.getInstance().getDBDateFormat());
			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);
						winName = getObjName(parentNode);
						System.out.println( "winName :: " + winName );
						childNodeList = parentNode.getChildNodes();
						childNodeListLength = childNodeList.getLength();
						for(ctr = 0; ctr < childNodeListLength; ctr ++)
						{
							childNode = childNodeList.item(ctr);
							childNodeName = childNode.getNodeName();
							if(childNodeName.equalsIgnoreCase("site_code"))
					    	{
								siteCode = genericUtility.getColumnValue("site_code", dom);
								if(siteCode == null || (siteCode.length() == 0))
								{
									errList.add("VMSITECOD");
									errFields.add(childNodeName.toLowerCase());
								}
								if(siteCode != null && siteCode.trim().length() > 0)
								{
									sql = " select count(*) from site where site_code = ? ";
									pstmt =  conn.prepareStatement(sql);
									pstmt.setString(1,siteCode);
									rs = pstmt.executeQuery();
									if(rs.next())
									{
										cnt =  rs.getInt(1);
										if(cnt == 0) 
										{
											errCode = "VMSITE1";
											errList.add(errCode);
											errFields.add(childNodeName.toLowerCase());	
										}									
									}
									rs.close();
									rs = null;
									pstmt.close();
									pstmt = null;
								}
								if(errCode == null || errCode.trim().length() == 0)
								{
									tranDate = genericUtility.getColumnValue("tran_date", dom);
									try
									{
										tranPdate = (Date)simpleDateFormat.parse(tranDate);
										errCode = validator.nfCheckPeriod("FIN",tranPdate,siteCode);
									}
									
									catch (ParseException e)
									{
										System.out.println("Exception in transaction date [tran_date_validation]"+e);
										e.printStackTrace();
										throw new ITMException(e);
									}
									if(errCode != null && errCode.trim().length() > 0)
									{
										errList.add(errCode);
										errFields.add(childNodeName.toLowerCase());
									}
								}
							}
							
							else if(childNodeName.equalsIgnoreCase("tran_ser"))
							{ 
								tranSer = genericUtility.getColumnValue("tran_ser", dom); 
								drcrFlag = genericUtility.getColumnValue("drcr_flag", dom); 
								if(tranSer != null && drcrFlag != null && tranSer.trim().length() > 0 && drcrFlag.trim().length() > 0)
								{
									if("MDRCRD".equalsIgnoreCase(tranSer) && "C".equalsIgnoreCase(drcrFlag))
									{
										errCode = "VTMATCH";
										errList.add(errCode);
										errFields.add(childNodeName.toLowerCase());	
									}
									if("MDRCRC".equalsIgnoreCase(tranSer) && "D".equalsIgnoreCase(drcrFlag))
									{
										errCode = "VTMATCH";
										errList.add(errCode);
										errFields.add(childNodeName.toLowerCase());	
									}
								}
							}
							
							else if(childNodeName.equalsIgnoreCase("sundry_code"))
							{    
								sundryCode = genericUtility.getColumnValue("sundry_code",dom); 
								siteCode = genericUtility.getColumnValue("site_code",dom);
								sundryType = genericUtility.getColumnValue("sundry_type",dom);
								tranSer = genericUtility.getColumnValue("tran_ser",dom);
								errCode = finCommon.isSundryCode(siteCode,sundryType,sundryCode,tranSer, conn);
								if(errCode != null && (errCode.trim().length() > 0))
								{
									errList.add(errCode);
									errFields.add(childNodeName.toLowerCase());
								}
							}
							
							else if(childNodeName.equalsIgnoreCase("fin_entity"))
							{ 
								finEntity = genericUtility.getColumnValue("fin_entity", dom); 
								if(finEntity != null && finEntity.trim().length() > 0)
								{
									sql = "SELECT COUNT(*) FROM FINENT WHERE FIN_ENTITY = ?";
									pstmt =  conn.prepareStatement(sql);
									pstmt.setString(1,finEntity);
									rs = pstmt.executeQuery();
									if(rs.next())
									{
										cnt =  rs.getInt(1);
										if(cnt == 0) 
										{
											errCode = "VMFENTY1";
											errList.add(errCode);
											errFields.add(childNodeName.toLowerCase());   
										}									
									}
									rs.close();
									rs = null;
									pstmt.close();
									pstmt = null;
								}
							}
							
							else if(childNodeName.equalsIgnoreCase("site_code__drcr"))
							{ 
								siteCodeDrcr = genericUtility.getColumnValue("site_code__drcr", dom); 
								if(siteCodeDrcr == null || siteCodeDrcr.length() == 0)
								{
									errList.add("VMSITECOD");
									errFields.add(childNodeName.toLowerCase());
								}
								if(siteCodeDrcr != null && siteCodeDrcr.trim().length() > 0)
								{
									sql = " select count(*) from site where site_code = ? ";
									pstmt =  conn.prepareStatement(sql);
									pstmt.setString(1,siteCodeDrcr);
									rs = pstmt.executeQuery();
									if(rs.next())
									{
										cnt =  rs.getInt(1);
										if(cnt == 0) 
										{
											errCode = "VMSITE1";
											errList.add(errCode);
											errFields.add(childNodeName.toLowerCase());	
										}									
									}
									rs.close();
									rs = null;
									pstmt.close();
									pstmt = null;
								}
							}else if(childNodeName.equalsIgnoreCase("site_code__drcr"))
							{ 
								siteCodeDrcr = genericUtility.getColumnValue("site_code__drcr", dom); 
								if(siteCodeDrcr == null || siteCodeDrcr.length() == 0)
								{
									errList.add("VMSITECOD");
									errFields.add(childNodeName.toLowerCase());
								}
								if(siteCodeDrcr != null && siteCodeDrcr.trim().length() > 0)
								{
									sql = " select count(*) from site where site_code = ? ";
									pstmt =  conn.prepareStatement(sql);
									pstmt.setString(1,siteCodeDrcr);
									rs = pstmt.executeQuery();
									if(rs.next())
									{
										cnt =  rs.getInt(1);
										if(cnt == 0) 
										{
											errCode = "VMSITE1";
											errList.add(errCode);
											errFields.add(childNodeName.toLowerCase());	
										}									
									}
									rs.close();
									rs = null;
									pstmt.close();
									pstmt = null;
								}
							}else if(childNodeName.equalsIgnoreCase("sale_order"))
							{ 
								String saleOrder = genericUtility.getColumnValue("sale_order", dom); 
								if(saleOrder == null || saleOrder.length() == 0)
								{
									errList.add("VMSODNL");
									errFields.add(childNodeName.toLowerCase());
								}
							}
							
							else if(childNodeName.equalsIgnoreCase("milestone_id"))
							{ 
								String milestoneId = genericUtility.getColumnValue("milestone_id", dom);
								if(milestoneId == null || milestoneId.length() == 0)
								{
									errList.add("VTMSTIDNL");
									errFields.add(childNodeName.toLowerCase());
								}
								if(milestoneId != null && milestoneId.trim().length() > 0)
								{
									sql = "select milestone_type, act_start_date from proj_milestone where tran_id=?";
									pstmt =  conn.prepareStatement(sql);
									pstmt.setString(1,milestoneId);
									rs = pstmt.executeQuery();
									if(rs.next())
									{
										String mstType =  rs.getString(1);
										String actStartDt = rs.getString(2);
										if("U".equalsIgnoreCase(mstType)&& actStartDt==null) 
										{
											errCode = "VMACSDUNL";
											errList.add(errCode);
											errFields.add(childNodeName.toLowerCase());	
										}
										if("P".equalsIgnoreCase(mstType)&&actStartDt==null)
										{
											errCode = "VMACSDPNL";
											errList.add(errCode);
											errFields.add(childNodeName.toLowerCase());		
										}
									}
									rs.close();
									rs = null;
									pstmt.close();
									pstmt = null;
								}
								
							}
							
							else if(childNodeName.equalsIgnoreCase("acct_code"))
							{ 
								acctCode = genericUtility.getColumnValue("acct_code", dom); 
								siteCode = genericUtility.getColumnValue("site_code", dom); 
								if(acctCode != null && acctCode.trim().length() > 0 )
								{
									errCode = finCommon.isAcctCode(siteCode, acctCode, "", conn);
								}
								else
								{
									errCode = "VMACCTCD1";
								}
								if(errCode != null && errCode.trim().length() > 0)
								{
									errList.add(errCode);
									errFields.add(childNodeName.toLowerCase());
								}
								if(errCode == null || errCode.trim().length() == 0)
								{
									sundryCode = genericUtility.getColumnValue("sundry_code", dom);
									sundryType = genericUtility.getColumnValue("sundry_type", dom);
									if(acctCode != null && sundryCode != null && sundryType != null && acctCode.trim().length() > 0 && sundryCode.trim().length() > 0 && sundryType.trim().length() > 0)
									{
										errCode = finCommon.isAcctType(acctCode, sundryCode, sundryType, conn);
									}
									if(errCode != null && errCode.trim().length() > 0)
									{
										errList.add(errCode);
										errFields.add(childNodeName.toLowerCase());
									}
								}
							}	
							
							else if(childNodeName.equalsIgnoreCase("cctr_code"))
							{ 
								cctrCode = genericUtility.getColumnValue("cctr_code", dom); 
								acctCode = genericUtility.getColumnValue("acct_code", dom); 
								if(cctrCode != null  && cctrCode.trim().length() > 0)
								{
									errCode = finCommon.isCctrCode(acctCode, cctrCode, modName, conn);
								}
								else
								{
									errCode = "VMCCTRCD1";
								}
								if(errCode != null && errCode.trim().length() > 0)
								{
									errList.add(errCode);
									errFields.add(childNodeName.toLowerCase());
								}
							}	
							
							else if(childNodeName.equalsIgnoreCase("due_date"))
							{ 
								dueDate = genericUtility.getColumnValue("due_date", dom); 
								effDate = genericUtility.getColumnValue("eff_date", dom); 
								if(simpleDateFormat.parse(dueDate).before(simpleDateFormat.parse(effDate)))
								{
									errCode = "VTDATE4";
									errList.add(errCode);
									errFields.add(childNodeName.toLowerCase());
								}
							}	
                     
							else if(childNodeName.equalsIgnoreCase("curr_code"))
							{ 
								currCode = genericUtility.getColumnValue("curr_code", dom);
								sql = " select count(*) from currency where curr_code = ? ";
							    pstmt =  conn.prepareStatement(sql);
							    pstmt.setString(1,currCode);
							    rs = pstmt.executeQuery();
							    if(rs.next())
							    {
									cnt =  rs.getInt(1);
									if(cnt == 0) 
									{
										errCode = "VMCUR1";
										errList.add(errCode);
										errFields.add(childNodeName.toLowerCase());	
									}									
							    }
							    rs.close();
							    rs = null;
							    pstmt.close();
							    pstmt = null;
							}	
							
							else if(childNodeName.equalsIgnoreCase("adj_misc_crn"))
							{ 
								adjMiscCrn = genericUtility.getColumnValue("adj_misc_crn", dom); 
								if("AI".equalsIgnoreCase(adjMiscCrn) || "MI".equalsIgnoreCase(adjMiscCrn))
								{
									itemSer = genericUtility.getColumnValue("item_ser", dom);
									if(itemSer == null || itemSer.trim().length() == 0)
									{
										errCode = "VTITEMSER7";
										errList.add(errCode);
										errFields.add(childNodeName.toLowerCase());	
									}
								}
							}

							else if(childNodeName.equalsIgnoreCase("cust_ref_no"))
							{ 
								custRefNo = genericUtility.getColumnValue("cust_ref_no", dom); 
								custRefDate = genericUtility.getColumnValue("cust_ref_date", dom); 
								tranId = genericUtility.getColumnValue("tran_id", dom); 
								if(custRefNo != null && custRefDate != null && custRefNo.trim().length() > 0 && custRefDate.trim().length() > 0)
								{
									sql = "select count(1) from misc_drcr_rcp where cust_ref_no = ? and cust_ref_date = ?";
									pstmt =  conn.prepareStatement(sql);
									pstmt.setString(1,custRefNo);
									pstmt.setTimestamp(2,Timestamp.valueOf(dbDateFormat.format(simpleDateFormat.parse(custRefDate)).toString()+ " 00:00:00.0"));
									rs = pstmt.executeQuery();
									if(rs.next())
									{
										cnt =  rs.getInt(1);
										if(cnt > 0) 
										{
											errCode = "VTCUSTREF";
											errList.add(errCode);
											errFields.add(childNodeName.toLowerCase());	
										}									
									}
									rs.close();
									rs = null;
									pstmt.close();
									pstmt = null;
								}
							}
							
							else if(childNodeName.equalsIgnoreCase("cust_ref_date"))
							{ 
								custRefNo = genericUtility.getColumnValue("cust_ref_no", dom); 
								custRefDate = genericUtility.getColumnValue("cust_ref_date", dom); 
								tranId = genericUtility.getColumnValue("tran_id", dom); 
								if(custRefDate != null && custRefDate.trim().length() > 0)
								{
									sql = "select count(1) from misc_drcr_rcp where tran_id != ?  and cust_ref_no = ? and cust_ref_date = ?";
									pstmt =  conn.prepareStatement(sql);
									pstmt.setString(1,tranId);
									pstmt.setString(2,custRefNo);
									pstmt.setTimestamp(3,Timestamp.valueOf(dbDateFormat.format(simpleDateFormat.parse(custRefDate)).toString()+ " 00:00:00.0"));
									rs = pstmt.executeQuery();
									if(rs.next())
									{
										cnt =  rs.getInt(1);
										if(cnt > 0) 
										{
											errCode = "VTCUSTREF";
											errList.add(errCode);
											errFields.add(childNodeName.toLowerCase());	
										}									
									}
									rs.close();
									rs = null;
									pstmt.close();
									pstmt = null;
								}
							}
						}
						break;
                   
				case 2 : 
						parentNodeList = dom1.getElementsByTagName("Detail1");
						parentNode = parentNodeList.item(0);
						parentNodeList = dom.getElementsByTagName("Detail2");
						parentNode = parentNodeList.item(0);
						winName = getObjName(parentNode);
						System.out.println( "winName :: " + winName );
						childNodeList = parentNode.getChildNodes();
						childNodeListLength = childNodeList.getLength();
						for(ctr = 0; ctr < childNodeListLength; ctr ++)
						{
							childNode = childNodeList.item(ctr);
							childNodeName = childNode.getNodeName();
							if(childNodeName.equalsIgnoreCase("amount"))
							{ 
								amount1 = genericUtility.getColumnValue("amount", dom);
								if(amount1 == null || amount1.trim().length() == 0)
								{
									errCode = "VMAMTB";
									errList.add(errCode);
									errFields.add(childNodeName.toLowerCase());
								}
							}  
							
							else if(childNodeName.equalsIgnoreCase("acct_code"))
							{ 
								acctCode = genericUtility.getColumnValue("acct_code", dom); 
								siteCode = genericUtility.getColumnValue("site_code", dom1); 
								if(acctCode != null && acctCode.trim().length() > 0 )
								{
									errCode = finCommon.isAcctCode(siteCode, acctCode, "", conn);
								}
								else
								{
									errCode = "VMACCTCD1";
								}
								if(errCode != null && errCode.trim().length() > 0)
								{
									errList.add(errCode);
									errFields.add(childNodeName.toLowerCase());
								}
								if(errCode == null || errCode.trim().length() == 0)
								{
									if(acctCode != null && acctCode.trim().length() > 0 )
									{
										errCode = finCommon.isAcctType(acctCode, "", "O", conn);
									}
									if(errCode != null && errCode.trim().length() > 0)
									{
										errList.add(errCode);
										errFields.add(childNodeName.toLowerCase());
									}
								}
							}  

							else if(childNodeName.equalsIgnoreCase("site_code__drcr"))
							{ 
								siteCodeDrcr = genericUtility.getColumnValue("site_code__drcr", dom);
								siteCode = genericUtility.getColumnValue("site_code", dom1);
								if(siteCodeDrcr == null || (siteCodeDrcr.length() == 0))
								{
									errList.add("VMSITECOD");
									errFields.add(childNodeName.toLowerCase());
								}
								if(siteCodeDrcr != null && siteCodeDrcr.trim().length() > 0)
								{
									sql = " select count(*) from site where site_code = ? ";
									pstmt =  conn.prepareStatement(sql);
									pstmt.setString(1,siteCodeDrcr);
									rs = pstmt.executeQuery();
									if(rs.next())
									{
										cnt =  rs.getInt(1);
										if(cnt == 0) 
										{
											errCode = "VMSITE1";
											errList.add(errCode);
											errFields.add(childNodeName.toLowerCase());	
										}									
									}
									rs.close();
									rs = null;
									pstmt.close();
									pstmt = null;
								}
								if(errCode == null || errCode.trim().length() == 0)
								{
									if(siteCodeDrcr != null && !siteCodeDrcr.equalsIgnoreCase(siteCode))
									{
										errCode = "VTSITEWRNG";
										errList.add(errCode);
										errFields.add(childNodeName.toLowerCase());	
									}
								}
							}  			
							
							
							else if(childNodeName.equalsIgnoreCase("cctr_code"))
							{ 
								cctrCode = genericUtility.getColumnValue("cctr_code", dom);
								acctCode = genericUtility.getColumnValue("acct_code", dom);
								if(cctrCode != null && cctrCode.trim().length() > 0)
								{
									errCode = finCommon.isCctrCode(acctCode, cctrCode, modName, conn);
								}
								else
								{
									errCode = "VMCCTRCD1";
								}
								if(errCode != null && errCode.trim().length() > 0)
								{
									errList.add(errCode);
									errFields.add(childNodeName.toLowerCase());
								}
							} 

							else if(childNodeName.equalsIgnoreCase("anal_code"))
							{ 
								analCode = genericUtility.getColumnValue("anal_code", dom);
								tranSer = genericUtility.getColumnValue("tran_ser", dom1);
								acctCode = genericUtility.getColumnValue("acct_code", dom);
								if(acctCode != null && analCode != null && tranSer != null && acctCode.trim().length() > 0 && analCode.trim().length() > 0 && tranSer.trim().length() > 0)
								{	
									errCode = finCommon.isAnalysis(acctCode, analCode, tranSer, conn);
								}
								if(errCode != null && errCode.trim().length() > 0)
								{
									errList.add(errCode);
									errFields.add(childNodeName.toLowerCase());
								}
							} 

							else if(childNodeName.equalsIgnoreCase("emp_code"))
							{ 
								empCode = genericUtility.getColumnValue("emp_code", dom);
								siteCode = genericUtility.getColumnValue("site_code", dom1);
								tranSer = genericUtility.getColumnValue("tran_ser", dom1);
								if(empCode != null && empCode.trim().length() > 0)
								{
									if(tranSer != null && tranSer.trim().length() > 0)
									{	
										errCode = finCommon.isEmployee(siteCode, empCode, tranSer, conn);
									}
									if(errCode != null && errCode.trim().length() > 0)
									{
										errList.add(errCode);
										errFields.add(childNodeName.toLowerCase());
									}
								}
							}  
							
							
							else if(childNodeName.equalsIgnoreCase("item_code"))
							{ 
								itemCode = genericUtility.getColumnValue("item_code", dom);
								siteCode = genericUtility.getColumnValue("site_code", dom1);
								if(winName != null)
								{
									modName = winName.trim().toUpperCase();
								}
								if(itemCode != null && itemCode.trim().length() > 0)
								{
									errCode = itmDBAccessEJB.isItem(siteCode, itemCode, modName, conn);
									if(errCode != null && errCode.trim().length() > 0)
									{
										errList.add(errCode);
										errFields.add(childNodeName.toLowerCase());
									}
								}
							}  
							
						}
						break;

				case 3 : 
						parentNodeList = dom1.getElementsByTagName("Detail1");
						parentNode = parentNodeList.item(0);
						parentNodeList = dom.getElementsByTagName("Detail3");
						parentNode = parentNodeList.item(0);
						winName = getObjName(parentNode);
						System.out.println( "winName :: " + winName );
						childNodeList = parentNode.getChildNodes();
						childNodeListLength = childNodeList.getLength();
						for(ctr = 0; ctr < childNodeListLength; ctr ++)
						{
							childNode = childNodeList.item(ctr);
							childNodeName = childNode.getNodeName();
							if(childNodeName.equalsIgnoreCase("adj_amt"))
							{ 
								adjAmt = genericUtility.getColumnValue("adj_amt", dom); 
								refSer = genericUtility.getColumnValue("ref_ser", dom);
								refNo = genericUtility.getColumnValue("ref_no", dom);
								itemSer = genericUtility.getColumnValue("item_ser", dom1);
								sundryCode = genericUtility.getColumnValue("sundry_code", dom1);
								adjMiscCrn = genericUtility.getColumnValue("adj_misc_crn", dom1);
								if("MC".equalsIgnoreCase(adjMiscCrn) || "AC".equalsIgnoreCase(adjMiscCrn))
								{
									sql = "select case when tot_amt is null then 0 else tot_amt end - case when adj_amt is null then 0 else adj_amt end,ref_type from receivables where tran_ser = ? and ref_no = ? and cust_code = ?";
									pstmt =  conn.prepareStatement(sql);
									pstmt.setString(1,refSer);
									pstmt.setString(2,refNo);
									pstmt.setString(3,sundryCode);
									rs = pstmt.executeQuery();
									if(rs.next())
									{
										balAmt =  rs.getDouble(1);
										refType = rs.getString(2);
									}
									else
									{
										if("MI".equalsIgnoreCase(adjMiscCrn))
										{
											errCode = "VTNSUCHRNI";
											errList.add(errCode);
											errFields.add(childNodeName.toLowerCase());		
										}
										else
										{
											errCode = "VTNSUCHRNA";
											errList.add(errCode);
											errFields.add(childNodeName.toLowerCase());	
										}
									
									}
									rs.close();
									rs = null;
									pstmt.close();
									pstmt = null;
								}
								else if("MI".equalsIgnoreCase(adjMiscCrn) || "AI".equalsIgnoreCase(adjMiscCrn))
								{
									sql = "select case when tot_amt is null then 0 else tot_amt end - case when adj_amt is null then 0 else adj_amt end from receivables where tran_ser = ? and ref_no = ? and item_ser = ? and cust_code = ?";
									pstmt =  conn.prepareStatement(sql);
									pstmt.setString(1,refSer);
									pstmt.setString(2,refNo);
									pstmt.setString(3,itemSer);
									pstmt.setString(4,sundryCode);
									rs = pstmt.executeQuery();
									if(rs.next())
									{
										balAmt =  rs.getDouble(1);
									}
									else
									{
										if("MI".equalsIgnoreCase(adjMiscCrn))
										{
											errCode = "VTNSUCHRNI";
											errList.add(errCode);
											errFields.add(childNodeName.toLowerCase());		
										}
										else
										{
											errCode = "VTNSUCHRNA";
											errList.add(errCode);
											errFields.add(childNodeName.toLowerCase());	
										}
									
									}
									rs.close();
									rs = null;
									pstmt.close();
									pstmt = null;
								}
								else
								{
								
									tranType = validator.getEnvDis("999999", "SRET_TYPE_FRS", conn);
									if(tranType == null || "NULLFOUND".equalsIgnoreCase(tranType))
									{
										tranType = "";
									}
									if(refType == null || refType.trim().length() == 0)
									{
										refType = "";
									}
									if(adjAmt != null && balAmt != null && balAmt < Double.parseDouble(adjAmt))
									{
										errCode = "VTBAL1";
										errList.add(errCode);
										errFields.add(childNodeName.toLowerCase());							
									}
								}
								if(errCode == null || errCode.trim().length() == 0)
								{
									Double totAmt = 0.0;
									tranId = genericUtility.getColumnValue("tran_id", dom); 
									amount = Double.parseDouble(genericUtility.getColumnValue("amount", dom1)); 
									lineNo = Integer.parseInt(genericUtility.getColumnValue("line_no", dom));
									sql = "select sum(adj_amt) from misc_drcr_rcpinv where tran_id = ? and line_no != ? ";
									pstmt =  conn.prepareStatement(sql);
									pstmt.setString(1,tranId);
									pstmt.setInt(2,lineNo);
									rs = pstmt.executeQuery();
									if(rs.next())
									{
										sumAdj =  rs.getDouble(1);
									}
									rs.close();
									rs = null;
									pstmt.close();
									pstmt = null;
									totAmt = sumAdj + Double.parseDouble(adjAmt);
									if(totAmt > amount)
									{
										errCode = "VTHAILTSDA";
										errList.add(errCode);
										errFields.add(childNodeName.toLowerCase());							
									}
								}
							}
							
							
						}
						break;
			}//end switch
			int errListSize = errList.size();
			cnt = 0;
			String errFldName = null;
			if(errList != null && errListSize > 0)
			{
				for(cnt = 0; cnt < errListSize; cnt ++)
				{
					errCode = errList.get(cnt);
					errFldName = 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 + 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();
						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 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 : [MiscDnVal][itemChanged( String, String )] :==>\n" + e.getMessage());
		}
        return valueXmlString;
	}

	// method for item change
	public String itemChanged(Document dom, Document dom1, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException, ITMException
	{
		StringBuffer valueXmlString = new StringBuffer();
		NodeList parentNodeList = null;
		NodeList childNodeList = null;
		Node parentNode = null;
		Node childNode = null;
		String childNodeName = null;
		String siteCode = "";
		String drcrFlag = "D";
		String dateNow = "";
		String tranSer = "";
		String rndStr = "";
		String rndOff = "";
		String currCode1 = "";
		String finEntity = "";
		String itemSer = "";
		String descr = "";
		String modName = "";
		String udfStr1 = "";
		String tranType = "";
		String acctCode1 = "";
		String descr2 = "";
		String sundryCode = "";
		String fName = "";
		String mName = "";
		String lName = "";
		String descr1 = "";
		String cctrCode = "";
		String acctCode = "";
		String refNo = "";
		String reasCode = "";
		String tranId = "";
		String currCode = "";
		String sundryType = "";
		String lineNo = "";
		String refSer = "";
		String sql = "";
		String tranDate = "";
		String getAcctDet = "";
		int quantity = 0;
		int ctr = 0;
		int cnt = 0;
		int currentFormNo = 0;
		Double balAmt = 0.0;
		Double totAmt = 0.0;
		Double adjAmt = 0.0;
		Double exchRate = 0.0;
		Double amount = 0.0;
		Double rate = 0.0;
		String itemCode="", unitValue="", unit="";
		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;
			finCommon = new FinCommon();
			Calendar currentDate = Calendar.getInstance();
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(genericUtility.getApplDateFormat());
			if(objContext != null && objContext.trim().length() > 0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			valueXmlString = new StringBuffer("<?xml version = \"1.0\"?> <Root> <header> <editFlag>");
			valueXmlString.append(editFlag).append("</editFlag> </header>");
			switch(currentFormNo)
			{
			
				case 1 : 
						parentNodeList = dom.getElementsByTagName("Detail1");
						parentNode = parentNodeList.item(0);
						childNodeList = parentNode.getChildNodes();
						valueXmlString.append("<Detail1>");
						int childNodeListLength = childNodeList.getLength();
						do
						{   
							childNode = childNodeList.item(ctr);
							childNodeName = childNode.getNodeName();
							if(childNodeName.equals(currentColumn))
							{
								if(childNode.getFirstChild() != null)
								{
								}
							}
							ctr ++;
					    }
						
						while(ctr < childNodeListLength  && ! childNodeName.equals(currentColumn));
						System.out.println("currentColumn----"+currentColumn);
						if(currentColumn.trim().equalsIgnoreCase("itm_default"))
					    {
							siteCode =(genericUtility.getValueFromXTRA_PARAMS(xtraParams, "loginSiteCode"));
							valueXmlString.append("<site_code>").append("<![CDATA[" +  siteCode + "]]>").append("</site_code>");
							dateNow = simpleDateFormat.format(currentDate.getTime());
							System.out.println("dateNow-----"+dateNow);
							valueXmlString.append("<tran_date>").append("<![CDATA[" +  dateNow + "]]>").append("</tran_date>");
							valueXmlString.append("<eff_date>").append("<![CDATA[" +  dateNow + "]]>").append("</eff_date>");
							valueXmlString.append("<due_date>").append("<![CDATA[" +  dateNow + "]]>").append("</due_date>");
							valueXmlString.append("<drcr_flag>").append("<![CDATA[" +  drcrFlag + "]]>").append("</drcr_flag>");
							if("D".equalsIgnoreCase(drcrFlag))
							{
								tranSer = "MDRCRD";
								valueXmlString.append("<adj_misc_crn protect = \"1\">").append("<![CDATA[" + "NA" + "]]>").append("</adj_misc_crn>");
							}
							else if("C".equalsIgnoreCase(drcrFlag))
							{
								tranSer = "MDRCRC";
							}
							valueXmlString.append("<tran_ser>").append("<![CDATA[" + tranSer + "]]>").append("</tran_ser>");
							rndStr = tranSer + "-RND";
							if(rndStr != null && rndStr.trim().length() > 0)
							{
								rndOff = finCommon.getFinparams("999999",rndStr,conn);
							}
							if(!"NULLFOUND".equalsIgnoreCase(rndOff))
							{
								valueXmlString.append("<rnd_off>").append("<![CDATA[" + rndOff + "]]>").append("</rnd_off>");
								rndStr = tranSer + "-RNDTO";
								if(rndStr != null && rndStr.trim().length() > 0)
								{
									rndOff = finCommon.getFinparams("999999",rndStr,conn);
								}
								if(!"NULLFOUND".equalsIgnoreCase(rndOff))
								{
									valueXmlString.append("<rnd_to>").append("<![CDATA[" + Integer.parseInt(rndOff) + "]]>").append("</rnd_to>");
								}
							}
							setNodeValue(dom, "site_code", getAbsString(siteCode));
							
						}//end of if

                       else if(currentColumn.trim().equalsIgnoreCase("itm_defaultedit"))
					    {
                    	   	currCode = genericUtility.getColumnValue("curr_code", dom);
                    	   	exchRate = Double.parseDouble(genericUtility.getColumnValue("exch_rate", dom));
							tranId = genericUtility.getColumnValue("tran_id", dom);
							sundryType = genericUtility.getColumnValue("sundry_type", dom);
							sundryCode = genericUtility.getColumnValue("sundry_code", dom);
							if(sundryType != null && sundryCode != null && sundryType.trim().length() > 0 && sundryCode.trim().length() > 0)
							{	
								descr = finCommon.getChequeName(sundryType, sundryCode, "N", conn);
							}
							valueXmlString.append("<sundry_name>").append("<![CDATA[" + descr + "]]>").append("</sundry_name>");
							sql = " select count(*) from misc_drcr_rcpinv where tran_id = ? ";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1,tranId);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								cnt = rs.getInt(1);
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;
							if(cnt > 0)
											{
								valueXmlString.append("<adj_misc_crn protect = \"1\">").append("</adj_misc_crn>");
							}
							else
							{
								valueXmlString.append("<adj_misc_crn protect = \"0\">").append("</adj_misc_crn>");
							}
							sql = " select count(tran_id) from misc_drcr_rdet where tran_id = ? ";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1,tranId);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								cnt = rs.getInt(1);
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;
							if(cnt > 0)
							{
								valueXmlString.append("<curr_code protect = \"1\">").append("<![CDATA[" + currCode + "]]>").append("</curr_code>");
								valueXmlString.append("<exch_rate protect = \"1\">").append("<![CDATA[" + exchRate + "]]>").append("</exch_rate>");
							}
							else
							{
								valueXmlString.append("<curr_code protect = \"0\">").append("<![CDATA[" + currCode + "]]>").append("</curr_code>");
								valueXmlString.append("<exch_rate protect = \"0\">").append("<![CDATA[" + exchRate + "]]>").append("</exch_rate>");
							}
							

						}

						else if(currentColumn.trim().equalsIgnoreCase("sundry_code"))
					    {
							sundryCode = genericUtility.getColumnValue("sundry_code", dom);
							sundryType = genericUtility.getColumnValue("sundry_type", dom);
							siteCode = genericUtility.getColumnValue("site_code", dom);
							if(sundryType != null && sundryCode != null && sundryType.trim().length() > 0 && sundryCode.trim().length() > 0)
							{
								descr = finCommon.getChequeName(sundryType, sundryCode, "N", conn);
							}
							valueXmlString.append("<sundry_name>").append("<![CDATA[" + descr + "]]>").append("</sundry_name>");
							if("B".equalsIgnoreCase(sundryType))
							{
								descr = findValue(conn,"bank_name", "bank", "bank_code", sundryCode);
								currCode = findValue(conn,"curr_code", "bank", "bank_code", sundryCode);
								siteCode = findValue(conn,"site_code", "bank", "bank_code", sundryCode);
								acctCode = findValue(conn,"acct_code__bal", "bank", "bank_code", sundryCode);
								cctrCode = findValue(conn,"cctr_code__bal", "bank", "bank_code", sundryCode);
							}
							else if("E".equalsIgnoreCase(sundryType))
							{
								fName = findValue(conn,"emp_fname", "employee", "emp_code", sundryCode);
								if(fName != null)
													{
									fName = fName.trim();
								}
								lName = findValue(conn,"emp_lname", "employee", "emp_code", sundryCode);
								if(lName != null)
								{
									lName = lName.trim();
								}
								siteCode = findValue(conn,"pay_site", "employee", "emp_code", sundryCode);
								descr = fName + "" + lName;
							}
							else if("C".equalsIgnoreCase(sundryType))
							{
								descr = findValue(conn,"cust_name", "customer", "cust_code", sundryCode);
								currCode = findValue(conn,"curr_code", "customer", "cust_code", sundryCode);
								acctCode = findValue(conn,"acct_code__ar", "customer", "cust_code", sundryCode);
								cctrCode = findValue(conn,"cctr_code__ar", "customer", "cust_code", sundryCode);
								if(acctCode == null || acctCode.trim().length() == 0)
								{
									itemSer = genericUtility.getColumnValue("item_ser", dom);
									tranType = genericUtility.getColumnValue("tran_type", dom);
									siteCode = genericUtility.getColumnValue("site_code", dom);
									acctCode = findValue(conn,"acct_code__ar", "itemser", "item_ser", itemSer);
									cctrCode = findValue(conn,"cctr_code__ar", "itemser", "item_ser", itemSer);
									if(acctCode == null || cctrCode == null || acctCode.trim().length() == 0 || cctrCode.trim().length() == 0)
									{
											if(itemSer != null && tranType != null && itemSer.trim().length() > 0 && tranType.trim().length() > 0)
											{
												getAcctDet = finCommon.getAcctDetrTtype("", itemSer, "AR", tranType, conn);
											}
											String [] acctToken = getAcctDet.split(",");
											if(acctToken.length > 0)
											{
												if(acctToken.length == 1)
												{
													acctCode = acctToken[0];
												}
												else
												{
													acctCode = acctToken[0];
													cctrCode = acctToken[1];
												}
											}
											
									}
								}
							}
							else if("R".equalsIgnoreCase(sundryType))
							{
								fName = findValue(conn, "case when first_name is null then '' else first_name end" , "strg_customer b, customer a", "b.cust_code = a.cust_code and b.sc_code", sundryCode);
								lName = findValue(conn, "case when last_name is null then '' else last_name end" , "strg_customer b, customer a", "b.cust_code = a.cust_code and b.sc_code", sundryCode);
								mName =	findValue(conn, "case when middle_name is null then '' else middle_name end" , "strg_customer b, customer a", "b.cust_code = a.cust_code and b.sc_code", sundryCode);
								if(fName != null)
								{
									fName = fName.trim();
								}
								if(lName != null)
								{
									lName = lName.trim();
								}
								if(mName != null)
								{
									mName = mName.trim();
								}
								currCode =	findValue(conn, "a.curr_code" , "strg_customer b, customer a", "b.cust_code = a.cust_code and b.sc_code", sundryCode);
								acctCode =	findValue(conn, "a.acct_code__ar" , "strg_customer b, customer a", "b.cust_code = a.cust_code and b.sc_code", sundryCode);
								cctrCode =	findValue(conn, "a.cctr_code__ar" , "strg_customer b, customer a", "b.cust_code = a.cust_code and b.sc_code", sundryCode);
								descr = fName + "" + mName + "" + lName;
							}
										else if("P".equalsIgnoreCase(sundryType))
							{
								descr = findValue(conn, "sp_name" , "sales_pers", "sales_pers", sundryCode);
								currCode = findValue(conn, "curr_code" , "sales_pers", "sales_pers", sundryCode);
								acctCode = findValue(conn, "acct_code__ap" , "sales_pers", "sales_pers", sundryCode);
								cctrCode = findValue(conn, "cctr_code__ap" , "sales_pers", "sales_pers", sundryCode);
							}
							else if("T".equalsIgnoreCase(sundryType))
							{
								descr = findValue(conn, "tran_name" , "transporter", "tran_code", sundryCode);
								currCode = findValue(conn, "curr_code" , "transporter", "tran_code", sundryCode);
								acctCode = findValue(conn, "acct_code__fp" , "transporter", "tran_code", sundryCode);
								cctrCode = findValue(conn, "cctr_code__fp" , "transporter", "tran_code", sundryCode);
							}
							else if("O".equalsIgnoreCase(sundryType))
							{
								descr = findValue(conn, "descr" , "accounts", "acct_code", sundryCode);
								currCode = findValue(conn, "curr_code" , "accounts", "acct_code", sundryCode);
							}
							if(acctCode == null)
							{
								acctCode = "";
							}
							if(cctrCode == null)
							{
								cctrCode = "";
							}
							if(descr == null)
							{
								descr = "";
							}
							if(currCode == null)
							{
								currCode = "";
							}
							valueXmlString.append("<acct_code>").append("<![CDATA[" + acctCode + "]]>").append("</acct_code>");
							descr1 = findValue(conn, "descr" , "accounts", "acct_code", acctCode);//added by dipak
							valueXmlString.append("<accounts_descr>").append("<![CDATA[" + descr1 + "]]>").append("</accounts_descr>");//added extra by dipak
							valueXmlString.append("<cctr_code>").append("<![CDATA[" + cctrCode + "]]>").append("</cctr_code>");
							valueXmlString.append("<sundry_name>").append("<![CDATA[" + descr + "]]>").append("</sundry_name>");
							valueXmlString.append("<curr_code>").append("<![CDATA[" + currCode + "]]>").append("</curr_code>");
							tranDate = genericUtility.getColumnValue("tran_date", dom);
							if(currCode != null && tranDate != null && currCode.trim().length() > 0 && tranDate.trim().length() > 0)
							{	
								exchRate = finCommon.getDailyExchRateSellBuy(currCode, "", siteCode, tranDate, "B", conn);
							}
							valueXmlString.append("<exch_rate protect = \"1\">").append("<![CDATA[" + exchRate + "]]>").append("</exch_rate>");
							if(siteCode != null && siteCode.trim().length() > 0)
							{
								valueXmlString.append("<site_code>").append("<![CDATA[" + siteCode + "]]>").append("</site_code>");
								descr = findValue(conn, "descr","site","site_code",siteCode);
								finEntity = findValue(conn, "fin_entity","site","site_code", siteCode);
								valueXmlString.append("<site_descr>").append("<![CDATA[" + descr + "]]>").append("</site_descr>");
								valueXmlString.append("<fin_entity>").append("<![CDATA[" + finEntity + "]]>").append("</fin_entity>");
								if(currCode == null || currCode.trim().length() == 0)
								{
									currCode = findValue(conn, "curr_code" , "finent", "fin_entity", finEntity);
									valueXmlString.append("<curr_code>").append("<![CDATA[" + currCode + "]]>").append("</curr_code>");
									tranDate = genericUtility.getColumnValue("tran_date", dom);
									if(currCode != null && tranDate != null && currCode.trim().length() > 0 && tranDate.trim().length() > 0)
									{
										exchRate = finCommon.getDailyExchRateSellBuy(currCode, "", siteCode, tranDate, "B", conn);
									}
									valueXmlString.append("<exch_rate protect = \"1\">").append("<![CDATA[" + exchRate + "]]>").append("</exch_rate>");
								}
							}
							if(acctCode != null && acctCode.trim().length() > 0)
							{
								valueXmlString.append("<acct_code>").append("<![CDATA[" + acctCode + "]]>").append("</acct_code>");
								descr1 = findValue(conn, "descr" , "accounts", "acct_code", acctCode);
								valueXmlString.append("<accounts_descr>").append("<![CDATA[" + descr1 + "]]>").append("</accounts_descr>");
								valueXmlString.append("<cctr_code>").append("<![CDATA[" + cctrCode + "]]>").append("</cctr_code>");
							}
					    }

						else if(currentColumn.trim().equalsIgnoreCase("site_code"))
						{
							siteCode = genericUtility.getColumnValue("site_code", dom);
							descr = findValue(conn, "descr","site","site_code", siteCode);
							finEntity = findValue(conn, "fin_entity","site", "site_code", siteCode);
							valueXmlString.append("<site_descr>").append("<![CDATA[" + descr + "]]>").append("</site_descr>");
							valueXmlString.append("<fin_entity>").append("<![CDATA[" + finEntity + "]]>").append("</fin_entity>");
						}
						
						else if(currentColumn.trim().equalsIgnoreCase("acct_code"))
						{							
							acctCode = genericUtility.getColumnValue("acct_code", dom);
							descr = findValue(conn, "descr" , "accounts", "acct_code", acctCode);
							valueXmlString.append("<accounts_descr>").append("<![CDATA[" + descr + "]]>").append("</accounts_descr>");
						}
						
						else if(currentColumn.trim().equalsIgnoreCase("curr_code"))
						{
							currCode1 = genericUtility.getColumnValue("curr_code", dom);
							siteCode = genericUtility.getColumnValue("site_code", dom);
							tranDate = genericUtility.getColumnValue("tran_date", dom);
							if(currCode1 != null && tranDate != null && currCode1.trim().length() > 0 && tranDate.trim().length() > 0)
							{
								exchRate = finCommon.getDailyExchRateSellBuy(currCode1, "", siteCode, tranDate, "B", conn);
							}
							valueXmlString.append("<exch_rate>").append("<![CDATA[" + exchRate + "]]>").append("</exch_rate>");
							finEntity = genericUtility.getColumnValue("fin_entity", dom);
							currCode = findValue(conn, "curr_code" , "finent", "fin_entity", finEntity);
							if(currCode1 != null && currCode != null && currCode1.trim().equalsIgnoreCase(currCode.trim()))
							{
								valueXmlString.append("<exch_rate protect = \"1\">").append(1).append("</exch_rate>");
							}
							else
							{
								valueXmlString.append("<exch_rate protect = \"0\">").append("</exch_rate>");
							}
							amount = Double.parseDouble(genericUtility.getColumnValue("amount", dom));
							if(amount > 0)
							{
								amount = amount * exchRate;
								valueXmlString.append("<amount__bc>").append("<![CDATA[" + amount + "]]>").append("</amount__bc>");
							}
						}
						
						else if(currentColumn.trim().equalsIgnoreCase("exch_rate"))
						{
							exchRate = Double.parseDouble(genericUtility.getColumnValue("exch_rate", dom));
							if(exchRate == null)
							{
								exchRate = 0.0;
							}
							amount = Double.parseDouble(genericUtility.getColumnValue("amount", dom));
							if(amount > 0)
							{
								amount = amount * exchRate;
								valueXmlString.append("<amount__bc>").append("<![CDATA[" + amount + "]]>").append("</amount__bc>");
							}
						}
						
						else if(currentColumn.trim().equalsIgnoreCase("tran_date"))
						{
							tranDate = genericUtility.getColumnValue("tran_date", dom);
							valueXmlString.append("<eff_date>").append("<![CDATA[" + tranDate + "]]>").append("</eff_date>");
							valueXmlString.append("<due_date>").append("<![CDATA[" + tranDate + "]]>").append("</due_date>");
						}
						
						else if(currentColumn.trim().equalsIgnoreCase("item_ser"))
						{
							itemSer = genericUtility.getColumnValue("item_ser", dom);
							String acctCodeTot="",cctrCodeTot="",actCodeDesc=""; 
							itemSer = itemSer == null ? "" : itemSer.trim();
							if(itemSer.length()>0){
							sql="select s.acct_code__tot_ar,ac.descr,s.cctr_code__tot_ar from accounts ac, item_acct_detr s, itemser i where ac.acct_code=s.acct_code__tot_ar and i.item_ser=s.item_ser and s.item_ser=?";	
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1, itemSer);
							rs = pstmt.executeQuery();
							if (rs.next()) {
								acctCodeTot = rs.getString(1);
								actCodeDesc = rs.getString(2);
								cctrCodeTot = rs.getString(3);
								
								
							   }
							acctCodeTot = (acctCodeTot == null) ? "" : acctCodeTot.trim();
							actCodeDesc = (actCodeDesc == null) ? "" : actCodeDesc.trim();
							cctrCodeTot = (cctrCodeTot == null) ? "" : cctrCodeTot.trim();
							
							rs.close();
							pstmt.close();
							rs = null;
							pstmt = null;
							}
							valueXmlString.append("<acct_code__tot_ar>").append("<![CDATA[" + acctCodeTot + "]]>").append("</acct_code__tot_ar>");
							valueXmlString.append("<accounts_descr_tot>").append("<![CDATA[" + actCodeDesc + "]]>").append("</accounts_descr_tot>");
							valueXmlString.append("<cctr_code__tot_ar>").append("<![CDATA[" + cctrCodeTot + "]]>").append("</cctr_code__tot_ar>");
							
							
							
							/*itemSer = genericUtility.getColumnValue("item_ser", dom);
							acctCode = findValue(conn, "acct_code__ar" , "itemser", "item_ser", itemSer);
							if(acctCode == null)
							{
								acctCode = "";
							}
							cctrCode = findValue(conn, "cctr_code__ar" , "itemser", "item_ser", itemSer);
							if(cctrCode == null)
							{
								cctrCode = "";
							}
							acctCode1 = genericUtility.getColumnValue("acct_code", dom);
							if(acctCode1 == null || acctCode1.trim().length() == 0)
							{
								valueXmlString.append("<acct_code>").append("<![CDATA[" + acctCode + "]]>").append("</acct_code>");
								valueXmlString.append("<cctr_code>").append("<![CDATA[" + cctrCode + "]]>").append("</cctr_code>");
								descr2 = findValue(conn, "descr" , "accounts", "acct_code", acctCode);
								valueXmlString.append("<accounts_descr>").append("<![CDATA[" + descr2 + "]]>").append("</accounts_descr>");
							}*/
						}else if(currentColumn.trim().equalsIgnoreCase("sale_order"))
				        {
							String salOrder = genericUtility.getColumnValue("sale_order", dom);
							String projCode="", projDescr="";
							salOrder = salOrder == null ? "" : salOrder.trim();
							if(salOrder.length()>0){
							sql="select s.proj_code,p.proj_code, p.descr from sorder s, project p where s.proj_code=p.proj_code and s.sale_order=?";	
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1, salOrder);
							rs = pstmt.executeQuery();
							if (rs.next()) {
								projCode = rs.getString(1);
								projDescr = rs.getString(2);
								
							   }
							projCode = (projCode == null) ? "" : projCode.trim();
							projDescr = (projDescr == null) ? "" : projDescr.trim();	
							rs.close();
							pstmt.close();
							rs = null;
							pstmt = null;
							}
							valueXmlString.append("<proj_code>").append("<![CDATA[" + projCode + "]]>").append("</proj_code>");
							valueXmlString.append("<project_descr>").append("<![CDATA[" + projDescr + "]]>").append("</project_descr>");
				        }
						
						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))
							{
								if(childNode.getFirstChild() != null)
								{
								}
							}
							ctr ++;
					    }while(ctr < childNodeListLength  && ! childNodeName.equals(currentColumn));
                       
						if(currentColumn.trim().equalsIgnoreCase("itm_default"))
				        {   
							acctCode = genericUtility.getColumnValue("acct_code", dom);
							lineNo = genericUtility.getValueFromXTRA_PARAMS(xtraParams, "focusRow");
							if (lineNo != null)
							{
								if(Integer.parseInt(lineNo) > 0)
								{
									valueXmlString.append("<line_no>").append("<![CDATA[" + lineNo + "]]>").append("</line_no>");
								}
							}
							tranId = genericUtility.getColumnValue("tran_id", dom1);
							valueXmlString.append("<tran_id>").append("<![CDATA[" + tranId + "]]>").append("</tran_id>");
							valueXmlString.append("<acct_code protect = \"0\">").append("<![CDATA[" + acctCode + "]]>").append("</acct_code>");
							tranType = genericUtility.getColumnValue("tran_type", dom1);
							if(winName != null)
							{
								modName = winName.trim().toUpperCase();
							}
							sql = " select udf_str1 from gencodes where fld_name = 'TRAN_TYPE' and mod_name = ? and fld_value = ?  ";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1,modName);
							pstmt.setString(2,tranType);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								udfStr1 = rs.getString(1);
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;
							if(udfStr1 != null && "I".equalsIgnoreCase(udfStr1.trim()))
							{
								valueXmlString.append("<item_code protect = \"0\">").append("</item_code>");
								valueXmlString.append("<quantity protect = \"0\">").append("</quantity>");
								valueXmlString.append("<rate protect = \"0\">").append("</rate>");
							}
							else
							{
								valueXmlString.append("<item_code protect = \"1\">").append("</item_code>");
								valueXmlString.append("<quantity protect = \"1\">").append("</quantity>");
								valueXmlString.append("<rate protect = \"1\">").append("</rate>");
							}
							String acctCodeTot="", cctrCodeTot="",actCodeDesc="";
							acctCodeTot=genericUtility.getColumnValue("acct_code__tot_ar", dom1);
							actCodeDesc=genericUtility.getColumnValue("accounts_descr_tot", dom1);
							cctrCodeTot=genericUtility.getColumnValue("cctr_code__tot_ar", dom1);
							valueXmlString.append("<acct_code>").append("<![CDATA[" + acctCodeTot + "]]>").append("</acct_code>");
							valueXmlString.append("<accounts_descr>").append("<![CDATA[" + actCodeDesc + "]]>").append("</accounts_descr>");
							valueXmlString.append("<cctr_code>").append("<![CDATA[" + cctrCodeTot + "]]>").append("</cctr_code>");
						}
						
						else if(currentColumn.trim().equalsIgnoreCase("itm_defaultedit"))
					    {
							acctCode = genericUtility.getColumnValue("acct_code", dom);
							reasCode = genericUtility.getColumnValue("reas_code", dom);
							if(reasCode == null || reasCode.trim().length() == 0)
							{
								valueXmlString.append("<acct_code protect = \"0\">").append("<![CDATA[" + acctCode + "]]>").append("</acct_code>");
							}
							else
							{
								valueXmlString.append("<acct_code protect = \"1\">").append("<![CDATA[" + acctCode + "]]>").append("</acct_code>");
							}
							tranType = genericUtility.getColumnValue("tran_type", dom1);
							if(tranType != null)
							{
								tranType = tranType.trim();
							}
							if(winName != null)
							{
								modName = winName.trim().toUpperCase();
							}
							sql = " select udf_str1 from gencodes where fld_name = 'TRAN_TYPE' and mod_name = ? and fld_value = ?  ";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1,modName);
							pstmt.setString(2,tranType);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								udfStr1 = rs.getString(1);
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;
							if(udfStr1 != null && "I".equalsIgnoreCase(udfStr1.trim()))
							{
								valueXmlString.append("<item_code protect = \"0\">").append("</item_code>");
								valueXmlString.append("<quantity protect = \"0\">").append("</quantity>");
								valueXmlString.append("<rate protect = \"0\">").append("</rate>");
							}
							else
							{
								valueXmlString.append("<item_code protect = \"1\">").append("</item_code>");
								valueXmlString.append("<quantity protect = \"1\">").append("</quantity>");
								valueXmlString.append("<rate protect = \"1\">").append("</rate>");
							}
						}

						else if(currentColumn.trim().equalsIgnoreCase("acct_code"))
				        {
							acctCode = genericUtility.getColumnValue("acct_code", dom);
							descr = findValue(conn, "descr" , "accounts", "acct_code", acctCode);
							valueXmlString.append("<accounts_descr>").append("<![CDATA[" + descr + "]]>").append("</accounts_descr>");
						}else if(currentColumn.trim().equalsIgnoreCase("item_code"))
				        {
							itemCode = genericUtility.getColumnValue("item_code", dom);
							String itemDescr="", unitVal="", unitDescr="";
							itemCode = itemCode == null ? "" : itemCode.trim();
							if(itemCode.length()>0){
							sql="select i.descr, u.unit, u.descr from item i, uom u where i.unit = u.unit and i.item_code=?";	
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1, itemCode);
							rs = pstmt.executeQuery();
							if (rs.next()) {
								itemDescr = rs.getString(1);
								unitVal = rs.getString(2);
								unitDescr = rs.getString(3);
							   }
							itemDescr = (itemDescr == null) ? "" : itemDescr.trim();
							unitVal = (unitVal == null) ? "" : unitVal.trim();	
							unitDescr=(unitDescr == null) ? "" : unitDescr.trim();
							rs.close();
							pstmt.close();
							rs = null;
							pstmt = null;
							}
							valueXmlString.append("<item_descr>").append("<![CDATA[" + itemDescr + "]]>").append("</item_descr>");
							valueXmlString.append("<unit>").append("<![CDATA[" + unitVal + "]]>").append("</unit>");
							valueXmlString.append("<uom_descr>").append("<![CDATA[" + unitDescr + "]]>").append("</uom_descr>");
				        }
							
							
						/*else if(currentColumn.trim().equalsIgnoreCase("reas_code"))
				        {
							reasCode = genericUtility.getColumnValue("reas_code", dom);
							if(winName != null)
							{
								modName = winName.trim().toUpperCase();
							}
							sql = " select udf_str1 from gencodes where fld_name = 'REAS_CODE' and mod_name = ? and fld_value = ?  ";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1,modName);
							pstmt.setString(2,reasCode);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								acctCode = rs.getString(1);
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;
							
							sql = " select udf_str2 from gencodes where fld_name = 'REAS_CODE' and mod_name = ? and fld_value = ?  ";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1,modName);
							pstmt.setString(2,reasCode);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								cctrCode = rs.getString(1);
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;
							if(acctCode != null && acctCode.trim().length() > 0)
							{
								valueXmlString.append("<acct_code>").append("<![CDATA[" + acctCode + "]]>").append("</acct_code>");
								descr2 = findValue(conn, "descr" , "accounts", "acct_code", acctCode);
								valueXmlString.append("<accounts_descr>").append("<![CDATA[" + descr2 + "]]>").append("</accounts_descr>");
								valueXmlString.append("<cctr_code>").append("<![CDATA[" + cctrCode + "]]>").append("</cctr_code>");
							}
						}*/
						
						else if(currentColumn.trim().equalsIgnoreCase("rate"))
				        {
							rate = Double.parseDouble(genericUtility.getColumnValue("rate", dom));
							quantity = Integer.parseInt(genericUtility.getColumnValue("quantity", dom));
							amount = rate * quantity;
							valueXmlString.append("<amount>").append("<![CDATA[" + amount + "]]>").append("</amount>");
							if(rate!= 0 || quantity != 0)
							{
								valueXmlString.append("<amount protect = \"1\">").append("<![CDATA[" + amount + "]]>").append("</amount>");
							}
							else
							{
								valueXmlString.append("<amount protect = \"0\">").append("<![CDATA[" + amount + "]]>").append("</amount>");
							}
						}

						valueXmlString.append("</Detail2>");
				        break;
			  
				case 3 : 
						parentNodeList = dom1.getElementsByTagName("Detail1");
						parentNode = parentNodeList.item(0);;
						parentNodeList = dom.getElementsByTagName("Detail3");
						parentNode = parentNodeList.item(0);
						winName = getObjName(parentNode);
						System.out.println( "winName :: " + winName );
						childNodeList = parentNode.getChildNodes();
						ctr = 0;
						valueXmlString.append("<Detail3>");
						childNodeListLength = childNodeList.getLength();
						do
						{   
							childNode = childNodeList.item(ctr);
							childNodeName = childNode.getNodeName();
							if(childNodeName.equals(currentColumn))
							{
								if(childNode.getFirstChild() != null)
								{
								}
							}
							ctr ++;
					    }while(ctr < childNodeListLength  && ! childNodeName.equals(currentColumn));
                      
						if(currentColumn.trim().equalsIgnoreCase("ref_no"))
				        {
							refNo = genericUtility.getColumnValue("ref_no", dom);
							System.out.println("@@@@Reference No.@@@@"+refNo);
							refSer = genericUtility.getColumnValue("ref_ser", dom);
							sql = " select tot_amt from receivables where tran_ser = ? and ref_no = ?";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1,refSer);
							pstmt.setString(2,refNo);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								totAmt = rs.getDouble(1);
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;
							
							sql = " select adj_amt from receivables where tran_ser = ? and ref_no = ?";
							pstmt = conn.prepareStatement(sql);
							pstmt.setString(1,refSer);
							pstmt.setString(2,refNo);
							rs = pstmt.executeQuery();
							if(rs.next())
							{
								adjAmt = rs.getDouble(1);
							}
							rs.close();
							rs = null;
							pstmt.close();
							pstmt = null;
							balAmt = totAmt - adjAmt;
							valueXmlString.append("<ref_bal_amt>").append("<![CDATA[" + balAmt + "]]>").append("</ref_bal_amt>");
							valueXmlString.append("<adj_amt>").append("<![CDATA[" + balAmt + "]]>").append("</adj_amt>");
					    }

						valueXmlString.append("</Detail3>");
				        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 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(1);
			}
			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 " + findValue);
		return findValue;
	}

	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;
		NamedNodeMap attrMap = node.getAttributes();
		objName = attrMap.getNamedItem( "objName" ).getNodeValue();
		return "w_" + objName;
	}

	private static void setNodeValue(Document dom, String nodeName, String nodeVal) throws Exception
	{
		Node tempNode = dom.getElementsByTagName(nodeName).item(0);
		if(tempNode != null)
		{
			if(tempNode.getFirstChild() == null)
			{
				CDATASection cDataSection = dom.createCDATASection(nodeVal);
				tempNode.appendChild(cDataSection);
			}
			else
			{
				tempNode.getFirstChild().setNodeValue(nodeVal);
			}
		}
		tempNode = null;
	}	

	private static String getAbsString(String str)
	{
		return(str == null || str.trim().length() == 0 || "null".equalsIgnoreCase(str.trim()) ? "" : str.trim() );
	}

}	
	