/*
    Window Name : w_acctcostcntr
	Button Name : Process
	Action		: It insert the values into ACCOUNTS_CCTR 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 AcctCctrPrc extends ProcessEJB implements AcctCctrPrcLocal,AcctCctrPrcRemote //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("AcctCctrCpyEJB called");
		Document dom = null,dom1 = null;
		String  retString = null;
		try
		{	
			if(xmlString != null && xmlString.trim().length()!=0)
			{
				System.out.println("XML String :"+xmlString);
				GenericUtility genericUtility = GenericUtility.getInstance();   
				dom = genericUtility.parseString(xmlString);				
			}
			retString = process(dom,dom1,windowName,xtraParams);
		}
		catch(Exception e)
		{
			System.out.println("Exception :AcctCctrCpyEJB :Process (String xmlString):" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
		System.out.println("Returning from Process :: "+retString);
	    return retString;
	}

	public String process(Document dom,Document dom1,String windowName,String xtraParams) throws RemoteException,ITMException
	{	
		Connection conn = null;
		Statement stmt = null;
		PreparedStatement pstmt = null;
		PreparedStatement pstmt1 = null;
		ResultSet rs = null,rs1 = null;
		String sql = "";
		String insertSql = "", insertSql1 = "";
		String actCode = "",cctrCode = "";
		String actCodeTgt = "",retString = "",errCode = "";
		String ccrtCodeFr = "";
		String ccrtCodeTo = "";
		String chgTerm = "";
		String chgUser = "";
		int insertCount = 0,cnt=0,upd = 0,count = 0;
		ArrayList actCctrCode = new ArrayList();
		ConnDriver connDriver = new ConnDriver();
		GenericUtility genericUtility = GenericUtility.getInstance();
		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			actCode = genericUtility.getColumnValue("acct_code__src",dom);
			actCodeTgt = genericUtility.getColumnValue("acct_code__tgt",dom);
			System.out.println("AcctCctrCpyEJB :actCode:"+actCode+":");
			if(actCode != null)
			{
				sql="SELECT ACCOUNTS_CCTR.CCTR_CODE FROM ACCOUNTS_CCTR WHERE ACCOUNTS_CCTR.ACCT_CODE ='"+actCode+"'";
			}
			else
			{
				ccrtCodeFr = genericUtility.getColumnValue("cctr_code__fr",dom);
				ccrtCodeTo = genericUtility.getColumnValue("cctr_code__to",dom);
				sql="SELECT COSTCTR.CCTR_CODE FROM COSTCTR "+
					" WHERE COSTCTR.CCTR_CODE >= '"+ccrtCodeFr+"'" +
					" AND COSTCTR.CCTR_CODE <= '"+ccrtCodeTo+"'";
			}
			System.out.println("AcctCctrCpyEJB :sql :"+sql);
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while(rs.next())
			{
				actCctrCode.add(rs.getString(1)); 
			}
			System.out.println("AcctCctrCpyEJB:actCctrCode:"+actCctrCode);

			chgUser = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginCode");
			chgTerm = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"termId");
			
			insertSql = "INSERT INTO ACCOUNTS_CCTR(ACCT_CODE,CCTR_CODE ,CHG_DATE,CHG_TERM ,CHG_USER) VALUES (?,?,?,?,?)";
						
			pstmt = conn.prepareStatement(insertSql);
			
			System.out.println("insertSql:"+insertSql);
			if (rs != null)
			{
				rs.close();
			}			   			
			for(cnt=0;cnt < actCctrCode.size();cnt++)
			{			
				System.out.println("actCodeTgt ::"+actCodeTgt+ " :: Cctr Code :: "+actCctrCode.get(cnt).toString());
				sql = "SELECT COUNT(*) AS COUNT FROM ACCOUNTS_CCTR WHERE ACCT_CODE = '"+actCodeTgt+"' AND CCTR_CODE = '"+actCctrCode.get(cnt).toString()+"' ";
				rs1 = stmt.executeQuery(sql);
				if (rs1.next())
				{
					count = rs1.getInt("COUNT");
					if (count > 0)
					{
						continue;
					}
				}
				System.out.println("Count :: "+count+"\n"+"actCodeTgt ::"+actCodeTgt+"\n"+"CctrCode :"+actCctrCode.get(cnt).toString());
				pstmt.setString(1,actCodeTgt);
				pstmt.setString(2,actCctrCode.get(cnt).toString());
				pstmt.setDate(3,new java.sql.Date(System.currentTimeMillis()));
				pstmt.setString(4,chgTerm);
				pstmt.setString(5,chgUser);				

				upd = pstmt.executeUpdate();								
				if (upd > 0)
				{
					insertCount++;	
				}
				pstmt.clearParameters();
				//pstmt1.clearParameters();
			}
			System.out.println("Rows Inserted :: "+insertCount);
			if (insertCount > 0)
			{
				System.out.println("Process Completed Successfully......");
				errCode = "VTSUCC";
				conn.commit();
			}
			else if (insertCount == 0)
			{
				System.out.println("No Record Inserted....");
				errCode = "VTNCOPY";
				conn.rollback();
			}
			ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
			retString = itmDBAccessEJB.getErrorString("",errCode,"","",conn);			
		}
		catch(SQLException e)
		{
			System.out.println("Exception : AcctCctrCpyEJB : actionCopy()" +e.getMessage());
			throw new ITMException(e);
		}
		catch(Exception e)
		{
			System.out.println("Exception :AcctCctrCpyEJB :actionCopy():"+e.getMessage());
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				System.out.println("Closing Database Connection......");
				conn.close();
				conn = null;
			}catch(Exception se){}
		}
		return retString;
  	}
}