/**
 DEVELOPED BY CHANDRASHEKAR ON 14/05/14 
 PURPOSE: W14BSUN003 (StarClub Employee details.)
 */
package ibase.webitm.ejb.dis.adv;

import ibase.system.config.ConnDriver;
import ibase.utility.EMail;
import ibase.webitm.ejb.ActionHandlerEJB;
import ibase.webitm.ejb.ITMDBAccessEJB;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;
import org.w3c.dom.*;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.ejb.Stateless;

import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import javax.mail.internet.MimeUtility;

import java.lang.reflect.*;
import javax.naming.InitialContext;
import javax.ejb.*;
import javax.rmi.*;


@Stateless 
public class EmpTravelInfoCancel extends ActionHandlerEJB implements EmpInfoSubmitRemote, EmpInfoSubmitLocal
{
	public String confirm(String tranID,String xtraParams, String forcedFlag) throws RemoteException,ITMException
	{

		String retString = "";
		Connection conn = null;
		boolean isConn= false;
		try
		{
			retString = this.confirm(tranID, xtraParams, forcedFlag, conn, isConn);
			System.out.println("retString:::::"+retString);
		}
		catch(Exception e)
		{
			System.out.println("Exception in [EmpTravelInfoCancel] confirm " + e.getMessage());
			throw new ITMException(e);
		}
		return retString;
	}
	public String confirm( String tranId, String xtraParams, String forcedFlag, Connection conn, boolean connStatus ) throws RemoteException,ITMException
	{
		String retString = "";
		String sql = "";
		String userType="",sendTo="", sendCC="",formatCode="",senBCC="";
		int count=0,upd=0;
		ResultSet rs = null;
		PreparedStatement pstmt = null;
		GenericUtility genericUtility = null;
		ITMDBAccessEJB itmDBAccess = null;
		StringBuffer commInfo = new StringBuffer();
		try
		{
			genericUtility = GenericUtility.getInstance();
			itmDBAccess = new ITMDBAccessEJB();
			ConnDriver connDriver = new ConnDriver();
			conn = connDriver.getConnectDB("DriverITM");
			connDriver = null;
			conn.setAutoCommit(false);
			connStatus = true;	
			
			/*sendTo="chandrashekar.aluka@baseinformation.com";
			sendCC="chandrashekar.aluka@baseinformation.com,cpatil@baseinformation.com";
			senBCC="chandrashekar.aluka@baseinformation.com";
			
			commInfo.append("<ROOT>");
			commInfo.append("<MAIL><EMAIL_TYPE>page</EMAIL_TYPE><ENTITY_CODE>BASE</ENTITY_CODE>");
			commInfo.append("<ENTITY_TYPE>"+userType+"</ENTITY_TYPE>");
			commInfo.append("<TO_ADD>"+sendTo+"</TO_ADD>");
			commInfo.append("<CC_ADD>"+sendCC+"</CC_ADD>");
			commInfo.append("<BCC_ADD>"+senBCC+"</BCC_ADD>");
			commInfo.append("<FORMAT_CODE></FORMAT_CODE>");
			commInfo.append("<EMP_CODE></EMP_CODE>");
			commInfo.append("<TRAVEL_CODE></TRAVL_CODE>");
			commInfo.append("<NSM_CODE>empcode</NSM_CODE>");
			commInfo.append("<EMPNAME></EMPNAME>");
			
			commInfo.append("<ATTACHMENT><BODY>Transaction Cancelled </BODY><LOCATION></LOCATION></ATTACHMENT>");
			commInfo.append("</MAIL>");
			commInfo.append("</ROOT>");			

			EMail email = new EMail();
			email.sendMail(commInfo.toString(), ""); 
			email = null;*/

			sql = " update emp_travel_info set status = 'X'  where tran_id  =  ? ";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, tranId);
			upd =  pstmt.executeUpdate();
			pstmt.close();pstmt = null;
			System.out.println(" status updated:: "+upd);
			if(upd > 0)
			{				
				retString = "submited";
				System.out.println("  information submited::");
			}			
		}
		catch( Exception e )
		{
			try
			{
				conn.rollback();
				retString = e.getMessage();
				e.printStackTrace();
			}
			catch (Exception e1)
			{
				System.out.println("Exception : "+e);e.printStackTrace();
			}
			throw new ITMException(e);
		}
		finally
		{	
			if(retString != null && retString.indexOf("submited") > -1)
			{
				try {
					conn.commit();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				retString = itmDBAccess.getErrorString("","VTCANCEL5","");
			}			
			else
			{
				retString = itmDBAccess.getErrorString("","VTERRCAN","");
			}
			try{

				if(rs != null)
				{
					rs.close();rs = null;
				}
				if(pstmt != null)
				{
					pstmt.close();pstmt = null;
				}
				if(conn != null)
				{
					conn.close();
					conn = null;	
				}
			}
			catch(Exception e)
			{System.out.println("Exception : "+e);e.printStackTrace();}

		}		
		return retString ;
	}
	
}
