package ibase.webitm.ejb.adm;

import ibase.system.config.ConnDriver;
import ibase.webitm.ejb.ValidatorEJB;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;
import java.util.*;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.sql.Timestamp;

import org.jboss.ejb3.statistics.InvocationStatistics.TimeStatistic;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.ejb.Stateless;
import ibase.webitm.ejb.adm.AdmCommon;

@Stateless

public class BondRecovery extends ValidatorEJB implements BondRecoveryLocal, BondRecoveryRemote
{	
	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
	{
		String errString = null;
		Document dom = null;
		Document dom1 = null;
		Document dom2 = null;
		NodeList parentNodeList = null;
		NodeList childNodeList = null;
		Node parentNode = null;
		Node childNode = null;
		String childNodeName = "";
	
		System.out.println("\n\n xmlString in wfValData \n:" + xmlString + "\n");
		System.out.println("\n\n xmlString1 in wfValData \n :" + xmlString1 +"\n");
		System.out.println("\n\n xmlString2 in wfValData \n :" + xmlString2 +"\n");
	
		try
		{
			dom = parseString(xmlString);
			dom1 = parseString(xmlString1);
			if (xmlString2.trim().length() > 0)
			{
				dom2 = parseString("<Root>" + xmlString2+ "</Root>");
			}
			//errString = wfValData(dom,dom1,dom2,objContext,editFlag,xtraParams);
	
			//ADDED FOR INVOIKING VALIDATION IN WORKFLOW
			if( objContext != null && Integer.parseInt(objContext) == 1 )
			{
				parentNodeList = dom2.getElementsByTagName("Header0");
				parentNode = parentNodeList.item(1);
				childNodeList = parentNode.getChildNodes();						
				for(int x=0;x<childNodeList.getLength();x++)
				{				
					childNode = childNodeList.item(x);
					childNodeName = childNode.getNodeName();
					if(childNodeName.equalsIgnoreCase("Detail1"))
					{
						errString = wfValData(dom,dom1,dom2,"1",editFlag,xtraParams);
						if(errString != null && errString.trim().length() > 0)
							break;
					}
					else if(childNodeName.equalsIgnoreCase("Detail2"))
					{						
						errString = wfValData(dom,dom1,dom2,"2",editFlag,xtraParams);
						if(errString != null && errString.trim().length() > 0)
							break;
					}	
					else if(childNodeName.equalsIgnoreCase("Detail3"))
					{
						errString = wfValData(dom,dom1,dom2,"3",editFlag,xtraParams);
						if(errString != null && errString.trim().length() > 0)
							break;
					}
				}
			}
			else
			{
				errString = wfValData(dom,dom1,dom2,objContext,editFlag,xtraParams);
			}
		}
		catch(Exception e)
		{
			System.out.println("Exception : ChangecontrolEJB : wfValData(String xmlString) : ==>\n"+e.getMessage());
		}
		return (errString);
	}
	public String wfValData(Document dom, Document dom1, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		return "";
	}

	public String wfValData(String xmlString, String xmlString1,String xmlString2, String objContext, String editFlag) throws RemoteException,ITMException
	{
		return "";
	}
	public String itemChanged(Document dom, Document dom1,String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		return "";
	}
	public String itemChanged(String xmlString, String xmlString1,String xmlString2, String objContext, String currentColumn, String editFlag) throws RemoteException,ITMException
	{
		return "";
	}
		
	public String wfValData(Document dom, Document dom1,Document dom2, String objContext, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		NodeList parentNodeList = null;
		NodeList childNodeList = null;
		Node parentNode = null;
		Node childNode = null;
		int ctr = 0,cnt = 0;
		String childNodeName = null;
		String errString = "";
		String errCode = "";
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "";
		String userId = "";
		int currentFormNo=0;
		int childNodeListLength;				
		String empCode="",siteCode = "",recAmtAct="",bondAmt="",recAmtDone="",acctcode="",cctrcode="",analCode="";
		GenericUtility genericUtility = GenericUtility.getInstance();
		try
		{
			ConnDriver connDriver = new ConnDriver();
			conn = connDriver.getConnectDB("DriverITM");
			conn.setAutoCommit(false);
			connDriver = null;
			userId = getValueFromXTRA_PARAMS(xtraParams,"loginCode");
						
			if(objContext != null && objContext.trim().length() > 0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			
			switch(currentFormNo)
			{
				case 1 :
					parentNodeList = dom.getElementsByTagName("Detail1");
					parentNode = parentNodeList.item(0);
					childNodeList = parentNode.getChildNodes();
					childNodeListLength = childNodeList.getLength();
					for(ctr = 0; ctr < childNodeListLength; ctr++)
					{
						childNode = childNodeList.item(ctr);
						childNodeName = childNode.getNodeName();
						
						if (childNodeName.equalsIgnoreCase("emp_code"))
						{
							empCode = genericUtility.getColumnValue("emp_code",dom);							
							if (empCode == null || empCode.trim().length() == 0 )
							{															
								errCode = "VNULEMPC";
								errString = getErrorString("emp_code",errCode,userId);
								break;								
							}

							else 
							{								
								sql = "select count(1) from employee where  emp_code = ?";
								pstmt=conn.prepareStatement(sql);
								pstmt.setString(1,empCode);
								rs = pstmt.executeQuery();
								if(rs.next())
								{
									cnt = rs.getInt(1);
								}
								pstmt.close();
								rs.close();
								pstmt = null;
								rs = null;
								if(cnt == 0)
								{
									errCode = "VMEMPCD1";
									errString = getErrorString("emp_code",errCode,userId);
									
									break;
								}
							}
						}
						
						else if (childNodeName.equalsIgnoreCase("site_code"))
						{
							siteCode = genericUtility.getColumnValue("site_code",dom);							

							if (siteCode == null || siteCode.trim().length() == 0 )
							{															
								errCode = "VMSITENULL";
								errString = getErrorString("site_code",errCode,userId);
								break;							
							}
							else
							{
								sql = "select count(1) from site where  site_code = ? ";
								pstmt=conn.prepareStatement(sql);
								pstmt.setString(1,siteCode);
								rs = pstmt.executeQuery();
								if(rs.next())
								{
									cnt = rs.getInt(1);
								}
								pstmt.close();
								rs.close();
								pstmt = null;
								rs = null;
								if(cnt == 0)
								{
									errCode = "VMSITENVAL";
									errString = getErrorString("site_code",errCode,userId);
									break;
								}
							}
						}
						else if (childNodeName.equalsIgnoreCase("recovery_amt__act"))
						{
							
							recAmtAct = genericUtility.getColumnValue("recovery_amt__act",dom);		
							String recAmtDue = genericUtility.getColumnValue("amt__due",dom);
							bondAmt = genericUtility.getColumnValue("bond_amt",dom);
							recAmtDone= genericUtility.getColumnValue("recovery_amt__done",dom);
							
							System.out.println("Actual amt :"+recAmtAct+" recAmtDue :"+recAmtDue);
							
							double recAmountAct=0.0,recAmountDue=0.0,bondAmount=0.0,recAmountDone=0.0;
							if (recAmtAct == null || recAmtAct.trim().length() == 0 )
							{		
								System.out.println(" recAmtAct Null value........");
								errCode = "VBACTAMT";
								errString = getErrorString("recovery_amt__act",errCode,userId);
								break;							
							}
							if( bondAmt !=null && bondAmt.trim().length() > 0 )
							{
								
								bondAmount=Double.parseDouble(bondAmt);
								recAmountDone=Double.parseDouble(recAmtDone);
								System.out.println("bondAmount :-->>"+bondAmount+" recAmountDone : "+recAmountDone);
								if(bondAmount == recAmountDone)
								{
									System.out.println("equality condition true -------");
									errCode="VRECAMTGRT";
									errString = getErrorString("recovery_amt__done",errCode,userId);
									break;
								}
								
							}			
							if (recAmtAct != null && recAmtAct.trim().length() > 0 )
							{
								recAmountAct=Double.parseDouble(recAmtAct);
								recAmountDue=Double.parseDouble(recAmtDue);
								if(recAmountAct > recAmountDue)
								{
									System.out.println("greater condition-------");
									errCode="VBACTAMTGR";
									errString = getErrorString("recovery_amt__done",errCode,userId);
									break;
								}
							}
							
											
							
						}
						else if (childNodeName.equalsIgnoreCase("acct_code__bond")) 
						{
							acctcode = genericUtility.getColumnValue("acct_code__bond",dom);
							if (acctcode == null || acctcode.trim().length() == 0 )
							{															
								errCode = "VACTCODNUL";
								errString = getErrorString("acct_code__bond",errCode,userId);
								break;							
							}
							else
							{
								sql = "select count(1) from accounts where acct_code = ? ";
								pstmt=conn.prepareStatement(sql);
								pstmt.setString(1,acctcode);
								rs = pstmt.executeQuery();
								if(rs.next())
								{
									cnt = rs.getInt(1);
								}
								pstmt.close();
								rs.close();
								pstmt = null;
								rs = null;
								if(cnt == 0)
								{
									errCode = "VACTNEXIST";
									errString = getErrorString("acct_code__bond",errCode,userId);
									break;
								}
							}
						}
						else if (childNodeName.equalsIgnoreCase("cctr_code__bond")) 
						{
							cctrcode = genericUtility.getColumnValue("cctr_code__bond",dom);
							if (cctrcode == null || cctrcode.trim().length() == 0 )
							{															
								errCode = "VCTRCODNUL";
								errString = getErrorString("cctr_code__bond",errCode,userId);
								break;							
							}
							else
							{
								sql = "select count(1) from costctr where cctr_code = ? ";
								pstmt=conn.prepareStatement(sql);
								pstmt.setString(1,cctrcode);
								rs = pstmt.executeQuery();
								if(rs.next())
								{
									cnt = rs.getInt(1);
								}
								pstmt.close();
								rs.close();
								pstmt = null;
								rs = null;
								if(cnt == 0)
								{
									errCode = "VCCTNEXIST";
									errString = getErrorString("cctr_code__bond",errCode,userId);
									break;
								}
							}
						}
						else if (childNodeName.equalsIgnoreCase("anal_code")) 
						{
							analCode=genericUtility.getColumnValue("anal_code",dom);
							if (analCode == null || analCode.trim().length() == 0 )
							{															
								errCode = "VANACODN";
								errString = getErrorString("anal_code",errCode,userId);
								break;							
							}
							else
							{
								sql = "select count(1) from analysis where anal_code = ? ";
								pstmt=conn.prepareStatement(sql);
								pstmt.setString(1,analCode);
								rs = pstmt.executeQuery();
								if(rs.next())
								{
									cnt = rs.getInt(1);
								}
								pstmt.close();
								rs.close();
								pstmt = null;
								rs = null;
								if(cnt == 0)
								{
									errCode = "VANALNEXT";
									errString = getErrorString("cctr_code__bond",errCode,userId);
									break;
								}
							}
						}
						
						/*else if (childNodeName.equalsIgnoreCase("bond_amt"))
						{
							bondAmt = genericUtility.getColumnValue("bond_amt",dom);
							if (bondAmt == null || bondAmt.trim().length() == 0 )
							{															
								errCode = "VBAMTNOTF";
								errString = getErrorString("bond_amt",errCode,userId);
								break;							
							}
						}*/
					}// for loop end
			} //end switch
			
		}//end try
		catch(Exception e)
		{			
			e.printStackTrace();
			errString = e.getMessage();
		}
		finally
		{
			try
			{
				if(conn!=null)
				{
					conn.close();
					conn = null;
				}				
				if(pstmt != null )
				{
					pstmt.close();
					pstmt = null;
				}
				if(rs != null )
				{
					rs.close();
					rs = null;
				}
			}
			catch(Exception d)
			{
				d.printStackTrace();
			}
		}		
		return errString;
	}
	
	public String itemChanged(String xmlString, String xmlString1,String xmlString2, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		System.out.println("in item changed --------->>>");
		Document dom = null;
		Document dom1 = null;
		Document dom2 = null;
		String valueXmlString = "";
		
		//System.out.println("\n\n xmlString in ItemChanged \n:" + xmlString + "\n");
		//System.out.println("\n\n xmlString1 In ItemChanged \n :" + xmlString1 +"\n");

		try
		{
			dom = parseString(xmlString);			
			dom1 = parseString(xmlString1);
			if (xmlString2.trim().length() > 0 )
			{
				dom2 = parseString(xmlString2);
			}
			valueXmlString = itemChanged(dom,dom1,dom2,objContext,currentColumn,editFlag,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception : [ChangeControlEJB][itemChanged(String,String)] :==>\n"+e.getMessage());
		}
		System.out.println("@@@@@ valueXmlString [ "+valueXmlString+" ]");
		return valueXmlString;
	}
	
	public String itemChanged(Document dom, Document dom1,Document dom2, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException,ITMException
	{
		StringBuffer valueXmlString = new StringBuffer();
		NodeList parentNodeList = null;
		AdmCommon objAdmCommon=null;
		NodeList childNodeList = null;
		Node parentNode = null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null ;
				
		int currentFormNo = 0;
		String deptCode="", empCode="",BondAcctCode="",BondCctrCode="",BondAnalCode="";
		String dateRelieve="",dateJoin="",loginEmpCode = null;
		String sql = "",descr = "",empfname = "", empmname = "", emplname = "",analcode="";
		ConnDriver connDriver = new ConnDriver();
		String acct_code="", cctr_code="";
		int bondPrd=0;
		double bondAmt=0.0,bondAmtDone=0.0,bondAmtDue=0.0;
		
		String tranDate="";
		String [] datearrayJ=new String[3];
		String [] datearrayR=new String[3];
		String splitdateJ="",splitmonthJ="",splityearJ="",splitdateR="",splitmonthR="",splityearR="";
		Timestamp timestampDateJ = null,timestampDateR=null;
		
		try
		{
			GenericUtility genericUtility = GenericUtility.getInstance();
			loginEmpCode = GenericUtility.getInstance().getValueFromXTRA_PARAMS(xtraParams, "loginEmpCode");
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(genericUtility.getApplDateFormat());
			Timestamp timestamp = new Timestamp(System.currentTimeMillis());
			conn = connDriver.getConnectDB("DriverITM");
			conn.setAutoCommit(false);
			connDriver=null;
			if(objContext != null && objContext.trim().length()>0)
			{
				currentFormNo = Integer.parseInt(objContext);
			}
			valueXmlString = new StringBuffer("<?xml version=\"1.0\"?><Root><header><editFlag>");
			valueXmlString.append(editFlag).append("</editFlag></header>");

			switch(currentFormNo)
			{
			case 1 :
				valueXmlString.append("<Detail1>");
				parentNodeList = dom.getElementsByTagName("Detail1");
				parentNode = parentNodeList.item(0);
				childNodeList = parentNode.getChildNodes();
								
				if(currentColumn.trim().equalsIgnoreCase("itm_default"))
				{
					String confirmed="N";
					tranDate = simpleDateFormat.format(timestamp).toString();
					System.out.println("Tran Date 111: "+tranDate.toString());
					valueXmlString.append("<tran_date>").append("<![CDATA["+tranDate.trim()+"]]>").append("</tran_date>");  
					valueXmlString.append("<confirmed>").append("<![CDATA["+confirmed+"]]>").append("</confirmed>");
										
				}//end item_default
				else if(currentColumn.trim().equalsIgnoreCase("site_code"))
				{
					System.out.println("site code change...");
					String siteCode =genericUtility.getColumnValue("site_code",dom);
					sql = "SELECT DESCR FROM SITE WHERE SITE_CODE = ? ";
					pstmt= conn.prepareStatement(sql);
					pstmt.setString(1,siteCode);
					rs = pstmt.executeQuery();
					while(rs.next())
					{
						descr = rs.getString(1) == null ? "" : rs.getString(1);
					}

					pstmt.close();
					rs.close();
					pstmt = null;
					rs = null;
					valueXmlString.append("<site_descr>").append("<![CDATA["+descr.trim()+"]]>").append("</site_descr>");
				}
				else if(currentColumn.trim().equalsIgnoreCase("emp_code"))
				{
					System.out.println("IN EMP CODE OF ITEM CHANGE -->>");	
					objAdmCommon=new AdmCommon();
					 
					empCode = genericUtility.getColumnValue("emp_code",dom);
														
					sql="Select e.emp_fname,e.emp_mname,e.emp_lname,e.date_join,e.RELIEVE_DATE from employee e  "+ 
					"where  e.emp_code = ?";
					
					pstmt= conn.prepareStatement(sql);
					pstmt.setString(1,empCode);
					rs = pstmt.executeQuery();
					if(rs.next())
					{
							empfname = rs.getString(1) == null ? "" : rs.getString(1);
							empmname = rs.getString(2) == null ? "" : rs.getString(2);
							emplname = rs.getString(3) == null ? "" : rs.getString(3);							 
							timestampDateJ =  rs.getTimestamp(4); 
							timestampDateR = rs.getTimestamp(5);
							
							dateJoin= timestampDateJ==null? "":simpleDateFormat.format(timestampDateJ).toString();
							dateRelieve= timestampDateR==null? "":simpleDateFormat.format(timestampDateR).toString();
							
							BondAcctCode =objAdmCommon.getEnv("999999", "Bond_Acct_code", conn);
							BondCctrCode =objAdmCommon.getEnv("999999", "Bond_Cctr_Code", conn);
							BondAnalCode =objAdmCommon.getEnv("999999", "Bond_Anal_Code", conn);
						
							valueXmlString.append("<emp_fname>").append("<![CDATA["+empfname+"]]>").append("</emp_fname>");
							valueXmlString.append("<emp_mname>").append("<![CDATA["+empmname+"]]>").append("</emp_mname>");
							valueXmlString.append("<emp_lname>").append("<![CDATA["+emplname+"]]>").append("</emp_lname>");				
							
							
							acct_code  = BondAcctCode == "NULLFOUND" ? getAcctCodeFromDept(conn,pstmt,rs,empCode) : BondAcctCode;
							cctr_code = BondCctrCode == "NULLFOUND" ? getCctrCodeFromDept(conn,pstmt,rs,empCode) :  BondCctrCode;
							analcode  = BondAnalCode == "NULLFOUND" ? "" : BondAnalCode;
							valueXmlString.append("<acct_code__bond>").append("<![CDATA["+acct_code+"]]>").append("</acct_code__bond>");
							valueXmlString.append("<cctr_code__bond>").append("<![CDATA["+cctr_code+"]]>").append("</cctr_code__bond>");
							valueXmlString.append("<anal_code>").append("<![CDATA["+analcode+"]]>").append("</anal_code>");
							
							valueXmlString.append("<date_join>").append("<![CDATA["+dateJoin+"]]>").append("</date_join>");
							valueXmlString.append("<relieve_date>").append("<![CDATA["+dateRelieve+"]]>").append("</relieve_date>");
							
							System.out.println("First Name :"+empfname+" Middle Name :"+empmname+" Last Name :"+emplname);
							System.out.println("Date Join -->>> "+dateJoin+" Relieve Date : "+dateRelieve);
							
							System.out.println("in item change---->>>acct_code : "+acct_code+"  cctr_code :"+cctr_code+" analcode : "+analcode);
							pstmt.close();
							rs.close();
							pstmt = null;
							rs = null;
											
							 if (dateRelieve !=null && dateJoin !=null && dateRelieve.trim().length() > 0 && dateJoin.trim().length() > 0)
						     {				     
								StringTokenizer  st = new StringTokenizer(dateJoin, "/");
								StringTokenizer  st1 = new StringTokenizer(dateRelieve, "/");
								int i=0;
						        while (st.hasMoreElements())
						        {
						        	datearrayJ[i]=st.nextToken();
						        	i++;
						        }
						        i=0;
						        splitdateJ=datearrayJ[0];
						        splitmonthJ=datearrayJ[1];		
						        int join=Integer.parseInt(datearrayJ[2]);
						       
						        if (join>=80 && join<=99)
						        	splityearJ="19"+datearrayJ[2];
						        else
						        	splityearJ="20"+datearrayJ[2];
						       
						        
						        System.out.println("splitdate :--> "+splitdateJ+" splitmonth :--> "+splitmonthJ+" splityear :--> "+splityearJ);
						      					
						        while (st1.hasMoreElements())
						        {
						        	datearrayR[i]=st1.nextToken();
						        	i++;
						        }
						        i=0;
						        splitdateR=datearrayR[0];
						        splitmonthR=datearrayR[1];		
						        int relieve=Integer.parseInt(datearrayR[2]);
						       
						        if (relieve>=80 && relieve<=99)
						        	splityearR="19"+datearrayR[2];
						        else
						        	splityearR="20"+datearrayR[2];
						       
						        
						        System.out.println("splitdate :--> "+splitdateR+" splitmonth :--> "+splitmonthR+" splityear :--> "+splityearR);
						        Calendar dateJ = new GregorianCalendar(Integer.parseInt(splityearR), Integer.parseInt(splitmonthR), Integer.parseInt(splitdateR));
						      
						        Calendar dateR = new GregorianCalendar(Integer.parseInt(splityearJ),Integer.parseInt(splitmonthJ), Integer.parseInt(splitdateJ));
						        int monthsBetween = (int) monthsBetween(dateJ, dateR);
						       
						        System.out.println("Difference between months ------->>>>"+monthsBetween);
					     
					      
						        sql="select BOND_AMT,BOND_PRD from EMPLOYEE_BOND WHERE EMP_CODE = ? and BOND_PRD >= ?  order by BOND_PRD";
								pstmt= conn.prepareStatement(sql);
								pstmt.setString(1,empCode);
								pstmt.setInt(2,monthsBetween);
								rs = pstmt.executeQuery();
								if(rs.next())
								{
									bondAmt = rs.getDouble(1) < 1.0 ? 0.0 : rs.getDouble(1);
									bondPrd = rs.getInt(2)< 1 ? 0 : rs.getInt(2);					
								}
								valueXmlString.append("<bond_amt>").append("<![CDATA["+bondAmt+"]]>").append("</bond_amt>");
								valueXmlString.append("<bond_prd>").append("<![CDATA["+bondPrd+"]]>").append("</bond_prd>");
								System.out.println("@@@------>> BOND AMT : "+bondAmt+" Bond Period : "+bondPrd);		
								pstmt.close();
								rs.close();
								pstmt = null;
								rs = null;
								
							   sql="select BOND_AMT__RECO from EMPLOYEE where emp_code = ?";
							   pstmt= conn.prepareStatement(sql);
							   pstmt.setString(1,empCode);
							   rs = pstmt.executeQuery();
							   if(rs.next())
							   {
								   bondAmtDue= bondAmtDue+rs.getDouble(1);
								   
								   System.out.println("@@ Total bond amt due------->>>>"+bondAmtDue);
								   bondAmtDone=bondAmtDue;
								   bondAmtDue=bondAmt-bondAmtDue;
								  
							   }
							  pstmt.close();
							  rs.close();
							  pstmt = null;
							  rs = null;
							  valueXmlString.append("<amt__due>").append("<![CDATA["+bondAmtDue+"]]>").append("</amt__due>");
							  valueXmlString.append("<recovery_amt__done>").append("<![CDATA["+bondAmtDone+"]]>").append("</recovery_amt__done>");
							
				          }
						 else
						 {	
							/*valueXmlString.append("<date_join>").append("<![CDATA[DD/MM/YY]]>").append("</date_join>");;
							valueXmlString.append("<relieve_date>").append("<![CDATA[DD/MM/YY]]>").append("</relieve_date>");*/
							valueXmlString.append("<bond_amt>").append("<![CDATA["+bondAmt+"]]>").append("</bond_amt>");
							valueXmlString.append("<bond_prd>").append("<![CDATA["+bondPrd+"]]>").append("</bond_prd>");
							 valueXmlString.append("<amt__due>").append("<![CDATA["+bondAmtDue+"]]>").append("</amt__due>");
							 valueXmlString.append("<recovery_amt__done>").append("<![CDATA["+bondAmtDone+"]]>").append("</recovery_amt__done>");
							System.out.println("Null date found -->> Date Join -->>"+dateJoin+" Relieve Date : "+dateRelieve);
							System.out.println("Null amount found -->> Bond Amount -->>"+bondAmt+" Bond period : "+bondPrd);
						 }
					}
					else{
						System.out.println("--------@@ rs is null @@ @----------------------> ");
						
						valueXmlString.append("<acct_code__bond>").append("<![CDATA["+acct_code+"]]>").append("</acct_code__bond>");
						valueXmlString.append("<cctr_code__bond>").append("<![CDATA["+cctr_code+"]]>").append("</cctr_code__bond>");
						valueXmlString.append("<anal_code>").append("<![CDATA["+analcode+"]]>").append("</anal_code>");
						
						valueXmlString.append("<bond_amt>").append("<![CDATA["+bondAmt+"]]>").append("</bond_amt>");
						valueXmlString.append("<bond_prd>").append("<![CDATA["+bondPrd+"]]>").append("</bond_prd>");
						
						valueXmlString.append("<date_join>").append("<![CDATA["+dateJoin+"]]>").append("</date_join>");
						valueXmlString.append("<relieve_date>").append("<![CDATA["+dateRelieve+"]]>").append("</relieve_date>");
						

						valueXmlString.append("<emp_fname>").append("<![CDATA["+empfname+"]]>").append("</emp_fname>");
						valueXmlString.append("<emp_mname>").append("<![CDATA["+empmname+"]]>").append("</emp_mname>");
						valueXmlString.append("<emp_lname>").append("<![CDATA["+emplname+"]]>").append("</emp_lname>");		
						
						  valueXmlString.append("<amt__due>").append("<![CDATA["+bondAmtDue+"]]>").append("</amt__due>");
						 valueXmlString.append("<recovery_amt__done>").append("<![CDATA["+bondAmtDone+"]]>").append("</recovery_amt__done>");
					}
					 
				}	
				valueXmlString.append("</Detail1>");
				break;
			}// end switch
			valueXmlString.append("</Root>");
			
		}
		catch(Exception e)
		{
			e.printStackTrace();			
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				if(conn!=null)
				{	
					conn.close();
					conn = null;
				}
				if(pstmt != null)
				{
					pstmt.close();
					pstmt = null;
				}
				if(rs != null)
				{
					rs.close();
					rs = null;
				}
			}
			catch(Exception d)
			{
				d.printStackTrace();
			}			
		}
		System.out.println("Return xml ---->>>>"+valueXmlString.toString());
		return valueXmlString.toString();
	}
	public String getAcctCodeFromDept(Connection conn,PreparedStatement pstmt,ResultSet rs,String loginEmpCode)
    {	
		String acct_code="";
		try
		{
			String sql1 = "SELECT D.ACCT_CODE,E.EMP_CODE FROM EMPLOYEE E,DEPARTMENT D WHERE E.DEPT_CODE=D.DEPT_CODE AND E.EMP_CODE=?";
		    	
		    	 pstmt = conn.prepareStatement(sql1);
				pstmt.setString(1,loginEmpCode);
				rs = pstmt.executeQuery();
				if(rs.next())
					acct_code  = rs.getString(1) ;					
		}
		catch(Exception e)
		{
			e.printStackTrace();			
			
		}
		return acct_code;
		
    }
	public String getCctrCodeFromDept(Connection conn,PreparedStatement pstmt,ResultSet rs,String loginEmpCode)
    {
		String cctr_code="";
		try
		{
			String sql1 = "SELECT D.CCTR_CODE,E.EMP_CODE FROM EMPLOYEE E,DEPARTMENT D WHERE E.DEPT_CODE=D.DEPT_CODE AND E.EMP_CODE=?";
		    pstmt = conn.prepareStatement(sql1);
			pstmt.setString(1,loginEmpCode);
			rs = pstmt.executeQuery();
			if(rs.next())
				cctr_code  = rs.getString(1) ;			
		}
		catch(Exception e)
		{
			e.printStackTrace();			
			
		}
		return cctr_code;
		
    }
	public static double monthsBetween(Calendar date1, Calendar date2)
	{
		 //new GregorianCalendar(yyyy, mm, dd) here month 0 is for January
        double monthsBetween = 0;
        monthsBetween = (date1.get(Calendar.YEAR)-date2.get(Calendar.YEAR))*12;
        monthsBetween += date1.get(Calendar.MONTH)-date2.get(Calendar.MONTH);
      
        if(date1.get(Calendar.DAY_OF_MONTH)!=date1.getActualMaximum(Calendar.DAY_OF_MONTH)
                && date1.get(Calendar.DAY_OF_MONTH)!=date1.getActualMaximum(Calendar.DAY_OF_MONTH) ){
            monthsBetween += ((date1.get(Calendar.DAY_OF_MONTH)-date2.get(Calendar.DAY_OF_MONTH))/31d);
        }
       
        return monthsBetween;
    }
  }
