package ibase.webitm.ejb.adm;
import ibase.webitm.utility.*;
import ibase.utility.CommonConstants;
import ibase.webitm.ejb.*;
import ibase.system.config.*;
import java.rmi.RemoteException;
import java.util.*;
import java.sql.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.ejb.*;
import javax.naming.InitialContext;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import ibase.webitm.utility.TransIDGenerator;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3

public class EmpInductionDefaultPrc extends ProcessEJB implements EmpInductionDefaultPrcLocal,EmpInductionDefaultPrcRemote // SessionBean
{
	Connection conn = null;
	String errorString = null;
	GenericUtility genericUtility = GenericUtility.getInstance();
	ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
	/*
	public void ejbCreate() throws RemoteException, CreateException 
	{
		System.out.println("Create Method Called....");
	}
	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
	{
		Document detailDom = null;
		Document headerDom = null;
		GenericUtility genericUtility = GenericUtility.getInstance();
		String retStr = "";
		try
		{				
				System.out.println("xmlString[process]::::::::::;;;"+xmlString);
				System.out.println("xmlString2[process]::::::::::;;;"+xmlString2);
				System.out.println("windowName[process]::::::::::;;;"+windowName);
				System.out.println("xtraParams[process]:::::::::;;;"+xtraParams);
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		try
		{	
			if(xmlString != null && xmlString.trim().length()!=0)
			{
				headerDom = genericUtility.parseString(xmlString); 
				System.out.println("headerDom" + headerDom);
			}
			if(xmlString2 != null && xmlString2.trim().length()!=0)
			{
				detailDom = genericUtility.parseString(xmlString2); 
				System.out.println("detailDom" + detailDom);
			}
			retStr = process(headerDom, detailDom, windowName, xtraParams);
		}
		catch (Exception e)
		{			
			System.out.println("Exception :EmpInductionDefaultPrcEJB :process(String xmlString, String xmlString2, String windowName, String xtraParams):" + e.getMessage() + ":");
			e.printStackTrace();
			retStr = e.getMessage();
		}
		return retStr;
	}//END OF PROCESS (1)

	public String process(Document headerDom, Document detailDom, String windowName, String xtraParams) throws RemoteException,ITMException
	{		
		PreparedStatement pstmt = null,pstmt1 = null;
		ResultSet rs = null;		
		ConnDriver connDriver = new ConnDriver();
		String loginSiteCode="",chgUser="",chgTerm="",loginCode="",empCode="",SiteCode="",grade="";
		String 	inductTblno="",inductCode="",tranId="",errString="",sql="",errCode="";
		int upd=0,count=0;
		try
		{	
			System.out.println("xtraParams :::::::::::::::::::::::::::::::::: "+xtraParams);
			loginSiteCode = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginSiteCode");
			chgUser = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"userId");
			chgTerm = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"termId");
			loginCode = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginCode");
			System.out.println("Login Site Code............."+loginSiteCode);
			System.out.println("userId............."+chgUser);
			System.out.println("termId............."+chgTerm);
			conn = connDriver.getConnectDB("DriverITM");
			conn.setAutoCommit(false);
			connDriver = null;
			empCode  = genericUtility.getColumnValue("emp_code",headerDom);			
			System.out.println("empCode............."+empCode);
			if (empCode == null || empCode.trim().length() == 0 )
			{
				System.out.println("emp code is null..........");
				errString = itmDBAccessEJB.getErrorString("","VNEMPNULL","");
				return errString;
			}
			else
			{	
				sql =" select count(*) from empinduction where emp_code = '"+empCode+"' ";
				System.out.println("sql............."+sql);
				pstmt = conn.prepareStatement(sql);			
				rs = pstmt.executeQuery();
				if(rs.next())
				{
				   count = rs.getInt(1);
				}
				rs.close();
				rs = null;
				pstmt.close();
				pstmt = null;
				System.out.println("count.........."+count);
				if(count == 0)
				{
					sql =" select emp_site, grade  from employee where emp_code = '"+empCode+"' ";
					System.out.println("sql............."+sql);
					pstmt = conn.prepareStatement(sql);			
					rs = pstmt.executeQuery();
					if(rs.next())
					{
					   SiteCode = rs.getString("emp_site");
					   grade = rs.getString("grade");
					}
					rs.close();
					rs = null;
					pstmt.close();
					pstmt = null;
					sql =" select induct_tblno  from grade where grade_code = '"+grade+"' ";
					System.out.println("sql............."+sql);
					pstmt = conn.prepareStatement(sql);			
					rs = pstmt.executeQuery();
					if(rs.next())
					{
					   inductTblno = rs.getString("induct_tblno");
					}
					rs.close();
					rs = null;
					pstmt.close();
					pstmt = null;
					sql =" select induct_code  from induction where induct_tblno = '"+inductTblno+"' order by induct_code ";
					System.out.println("sql............."+sql);
					pstmt = conn.prepareStatement(sql);			
					rs = pstmt.executeQuery();
					sql = "insert into empinduction (EMP_CODE,INDUCT_CODE,CHG_DATE,CHG_USER,CHG_TERM,TRAN_ID )"
						  + "values (?,?,?,?,?,?) ";
					System.out.println("sql.................. :"+sql);
					pstmt1 = conn.prepareStatement(sql);
					while(rs.next())
					{
					    inductCode = rs.getString("induct_code");
					    tranId = generateTranId("w_emp_induction",SiteCode,conn);//function to generate NEW transaction id
						System.out.println("tranId......"+tranId);
						if(tranId == null || tranId.trim().length() ==0)
						{
							errCode = "VTTRANID";
							System.out.println("errcode......"+errCode);
							errString = itmDBAccessEJB.getErrorString("","VTTRANID","BASE","",conn);
							return errString;
						}
						pstmt1.setString(1,empCode);
						pstmt1.setString(2,inductCode);
						pstmt1.setTimestamp(3,new Timestamp(System.currentTimeMillis()));
						pstmt1.setString(4,chgUser);
						pstmt1.setString(5,chgTerm);
						pstmt1.setString(6,tranId);
						pstmt1.addBatch();
					}
					pstmt1.executeBatch();
					pstmt1.close();
					pstmt1 =null;	
				}				
			}
		}
	   	catch(Exception e)
		{
			   System.out.println("Exception in EmpInductionDefaultPrcEJB..."+e.getMessage());
			   e.printStackTrace();
			   errString = e.getMessage();

		}
		finally
		{
			try
			{		
				if(errString.trim().length() > 0)
				{
					conn.rollback();
					System.out.println(" Rollback connection.............");
					errString = itmDBAccessEJB.getErrorString("","VTPROUNSUC","");
				}
				else
				{
					conn.commit();
					System.out.println(" commiting connection.............");
					errString = itmDBAccessEJB.getErrorString("","VTCONF","");
				}
				if (pstmt != null)
				{
					pstmt.close();
					pstmt =null;
				}
				if(conn != null)
				{
					conn.close();
					conn = null;
				}	
			}
			catch(Exception e)
			{
			   System.out.println("Error In closing connection::==> "+e);
		       e.printStackTrace();
			}
		}
		System.out.println("returning from  EmpInductionDefaultPrcEJB   "+errString);
	    return (errString);
	}

	private String generateTranId(String windowName, String siteCode,Connection conn)
	{
		Statement stmt = null;
		ResultSet rs = null;
		String sql = "";
		String tranId = "";
	//	String newKeystring = "";
	//	String srType = "RS";
		 try
	     {
	    	sql = "SELECT KEY_STRING, TRAN_ID_COL, REF_SER FROM TRANSETUP WHERE TRAN_WINDOW= '"+windowName+"'";
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			System.out.println("sql ..........:"+sql);
			String tranSer1 = "";
			String keyString = "";
			String keyCol = "";
			if (rs.next())
			{
				keyString = rs.getString(1);
				keyCol = rs.getString(2);
				tranSer1 = rs.getString(3);				
			}			
			System.out.println("keyString :"+keyString);
			System.out.println("keyCol :"+keyCol);
			System.out.println("tranSer1 :"+tranSer1);
			String xmlValues = "";
			xmlValues ="<?xml version=\"1.0\" encoding=\"utf-8\"?><Root>";
			xmlValues = xmlValues + "<Header></Header>";
			xmlValues = xmlValues + "<Detail1>";
			xmlValues = xmlValues +	"<tran_id></tran_id>";
			xmlValues = xmlValues + "<site_code>" + siteCode + "</site_code>";
			xmlValues = xmlValues + "</Detail1></Root>";
			System.out.println("xmlValues  :["+xmlValues+"]");
			TransIDGenerator tg = new TransIDGenerator(xmlValues, "BASE", CommonConstants.DB_NAME);
			tranId = tg.generateTranSeqID(tranSer1, keyCol, keyString, conn);
			System.out.println("tranId :"+tranId);
		}
		catch (SQLException ex)
		{
			System.out.println("Exception ::" +sql+ ex.getMessage() + ":");			
			ex.printStackTrace();			
		}
		catch (Exception e)
		{			
			System.out.println("Exception ::" + e.getMessage() + ":");		
			e.printStackTrace();
		}
		return tranId;
	}//generateTranTd()
}