/* Developed By Ahmed Mohammed Ali
 * Date : 20-JUL-2016
 * Component Name : EmpMaritalStatusConfPrc
 */
package ibase.webitm.ejb.adm;

import ibase.system.config.AppConnectParm;
import ibase.system.config.ConnDriver;
import ibase.utility.E12GenericUtility;
import ibase.webitm.ejb.ITMDBAccessLocal;
import ibase.webitm.ejb.ProcessEJB;
import ibase.webitm.ejb.adm.adv.EmpMaritalStatusConfLocal;
import ibase.webitm.utility.ITMException;

import java.rmi.RemoteException;
import java.sql.Connection;

import javax.ejb.Stateless;
import javax.naming.InitialContext;

import org.w3c.dom.Document;

/**
 * Session Bean implementation class EmpMaritalStatusConfPrc
 */
@Stateless
public class EmpMaritalStatusConfPrc extends ProcessEJB implements EmpMaritalStatusConfPrcRemote, EmpMaritalStatusConfPrcLocal 
{
	public String process() throws RemoteException,ITMException
	{
		return "";
	}
	
	public String process(String xmlString, String xmlString2, String windowName, String xtraParams) throws RemoteException, ITMException
	{
		String retString = "";
		Document detailDom = null;
		Document headerDom = null;
		E12GenericUtility genericUtility = new E12GenericUtility();
		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);
			}
			retString = process(headerDom, detailDom, windowName, xtraParams);
		}
		catch (Exception e)
		{
			System.out.println("Exception :EmpMaritalStatusConfPrc :process(String xmlString, String xmlString2, String windowName, String xtraParams):" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
		return retString;
	}
	
	public String process(Document headerDom, Document detailDom, String windowName, String xtraParams) throws RemoteException, ITMException
	{
		String returnString = "";
		boolean isError = false;
		Connection connection = null;
		ConnDriver connDriver = null;
		AppConnectParm appConnect = null;
		InitialContext initialCtx = null;
		EmpMaritalStatusConfLocal empMaritalStatusConf = null;
		ITMDBAccessLocal itmDBAccess = null;
		
		try
		{
			connDriver = new ConnDriver();
			connection = connDriver.getConnectDB("DriverITM");
			connection.setAutoCommit(false);
			appConnect = new AppConnectParm();
			initialCtx = new InitialContext(appConnect.getProperty());
			itmDBAccess = (ITMDBAccessLocal)initialCtx.lookup("ibase/ITMDBAccessEJB/local");
			
			empMaritalStatusConf = (EmpMaritalStatusConfLocal)initialCtx.lookup("ibase/EmpMaritalStatusConf/local");
			returnString = checkNull(empMaritalStatusConf.actionHandler("", headerDom, detailDom, xtraParams, "N", connection));
			System.out.println("EmpMaritalStatusConfPrc returnString ::[" + returnString + "]");
			if(returnString.length() > 0)
			{
				isError = true;
				System.out.println("EmpMaritalStatusConfPrc isError ::[" + isError + "]");
			}
			else
			{
				returnString = itmDBAccess.getErrorString("", "VTMARSCH03", "", "", connection);
			}
			
		}
		catch(Exception e)
		{
			isError = true;
			System.out.println("Exception :EmpMaritalStatusConfPrc :process(String xmlString2, String xmlString2, String windowName, String xtraParams):" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		} 
		finally
		{
			System.out.println("Closing Connection....");
			try
			{
				if(isError)
				{
					connection.rollback();
					System.out.println("EmpMaritalStatusConfPrc connection rollback");
				}
				else
				{
					connection.commit();
					System.out.println("EmpMaritalStatusConfPrc connection committed");
				}
				if(connection != null)
				{
					connection.close();
					connection = null;
				}
			}
			catch(Exception e)
			{
				returnString = e.getMessage();
				e.printStackTrace();
			}
		}
		System.out.println("errString...:: "+returnString);
		return returnString;
	}
	
	private String checkNull(String input)
	{
		if (input == null || ("null").equalsIgnoreCase(input.trim()))
		{
			input = "";
		}
		else
		{
			input = input.trim();
		}
		return input;
	}
}
