/*
Window Name : w_acctanal
Button name : Process
Action		: Insert details into ACCOUNTS_ANALYSIS Table.
*/

package ibase.webitm.ejb.fin.adv;

import java.rmi.RemoteException;
import java.util.*;
import java.sql.*;
import org.w3c.dom.*;
import javax.ejb.*;

import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.*;
import ibase.webitm.utility.GenericUtility;
import ibase.system.config.*;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3

public class AcctAnalPrc extends ProcessEJB implements AcctAnalPrcLocal ,AcctAnalPrcRemote //SessionBean
{
	/*public void ejbCreate() throws RemoteException, CreateException 
	{
	}
	public void ejbRemove()
	{
	}
	public void ejbActivate() 
	{
	}
	public void ejbPassivate() 
	{
	}*/
	public String process() throws RemoteException,ITMException
	{
		return "";
	}

	public String process(String xmlString, String xmlString2, String windowName, String xtraParams) throws RemoteException,ITMException
	{
		System.out.println("AcctAnalEJB called");
		Document dom = null,dom1 = null;
		String  resString = null;
		try
		{	
			if(xmlString != null && xmlString.trim().length() > 0)
			{
				System.out.println("XML String :"+xmlString);
				GenericUtility genericUtility = GenericUtility.getInstance();
				dom = genericUtility.parseString(xmlString);				
			}		
			resString = process(dom,dom1,windowName,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception :AcctAnalEJB : process(String xmlString):" + e.getMessage() + ":");
			throw new ITMException(e);
		}
		System.out.println("returning from Process "+resString);
	    return resString;
	}

	public String process(Document dom,Document dom1,String windowName,String xtraParams) throws RemoteException,ITMException
	{
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		PreparedStatement pstmt = null;
		PreparedStatement pstmt1 = null;
		String sql = "";
		String insertSql = "", insertSql1 = "";
		String acctCode ="";
		String analCodeFr= "",acctCodetgt = "",errCode = "",retString = "";
		String analCodeTo= "";
		String chgTerm = "";
		String chgUser = "";
		int insertCount = 0,cnt = 0, upd = 0, count = 0;
		ArrayList analCode = new ArrayList();
		ConnDriver connDriver = new ConnDriver(); 
		GenericUtility genericUtility = GenericUtility.getInstance();
		try
		{	
			conn = connDriver.getConnectDB("DriverITM");
			acctCode = genericUtility.getColumnValue("acct_code__src",dom);//Get Source Account
			if(acctCode != null)
			{
				sql="SELECT ANAL_CODE FROM ACCOUNTS_ANALYSIS WHERE ACCT_CODE ='"+acctCode+"'" ;
			}
			else
			{
				analCodeFr = genericUtility.getColumnValue("anal_code__fr",dom);
				analCodeTo = genericUtility.getColumnValue("anal_code__to",dom);
				sql=" SELECT ANAL_CODE FROM ANALYSIS "+
					" WHERE ANAL_CODE >= '"+analCodeFr+"'"+ 
					" AND ANAL_CODE <= '"+analCodeTo+"'"; 

			}
			System.out.println("AcctAnalEJB :actionCopy:sql:"+sql);
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);  //
			while(rs.next())
			{
				analCode.add(rs.getString(1)); 
			}
			acctCodetgt = genericUtility.getColumnValue("acct_code__tgt",dom); // Get The Target Account 
			
			chgUser = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginCode");
			chgTerm = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"termId");
			
			insertSql = "INSERT INTO ACCOUNTS_ANALYSIS(ACCT_CODE,ANAL_CODE,CHG_DATE,CHG_USER,CHG_TERM) VALUES (?,?,?,?,?)";
					
			pstmt = conn.prepareStatement(insertSql);
			
			System.out.println("insertSql:"+insertSql);
			
			for(cnt=0;cnt < analCode.size();cnt++)
			{				
				sql = "SELECT COUNT(*) FROM ACCOUNTS_ANALYSIS WHERE ACCT_CODE = '"+acctCodetgt+"' AND ANAL_CODE = '"+analCode.get(cnt).toString()+"'";
				rs = stmt.executeQuery(sql);
				if (rs.next())
				{
					count = rs.getInt(1);
					if (count > 0)
					{
						continue;
					}
				}				
				pstmt.setString(1,acctCodetgt);
				pstmt.setString(2,analCode.get(cnt).toString());
				pstmt.setDate(3,new java.sql.Date(System.currentTimeMillis()));
				pstmt.setString(4,chgUser);
				pstmt.setString(5,chgTerm);
				
				upd = pstmt.executeUpdate();
				if (upd > 0)
				{
					insertCount++;
				}				
				pstmt.clearParameters();				
			}
			
			System.out.println("Rows Inserted :: "+insertCount);
			if (insertCount > 0)
			{
				System.out.println("Process Completed Successfully......");
				errCode = "VTSUCC";
			}
			else if (insertCount == 0)
			{
				System.out.println("No Record Inserted....");
				errCode = "VTNCOPY";
			}
			ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
			retString = itmDBAccessEJB.getErrorString("",errCode,"","",conn);			
		}
		catch(SQLException e)
		{
			System.out.println("Exception : AcctAnalEJB : process()" +e.getMessage());
			throw new ITMException(e);
		}
		catch(Exception e)
		{
			System.out.println("Exception :AcctAnalEJB :process():"+e.getMessage());
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				System.out.println("Closing Database Connection......");
				conn.close();
				conn = null;
			}catch(Exception se){}
		}
		return retString;  	
	}		
}