package ibase.webitm.ejb.mfg;

import ibase.webitm.utility.*;
import org.w3c.dom.*;
import ibase.utility.CommonConstants;
import ibase.webitm.ejb.*;
import ibase.system.config.*;
import java.rmi.RemoteException;
import java.util.*;
import java.text.*;
import java.sql.*;
import javax.ejb.*;
import java.util.Calendar;
import java.util.Date;
import javax.naming.InitialContext;
import ibase.system.config.AppConnectParm;
//import ibase.webitm.ejb.MasterStateful;
//import ibase.webitm.ejb.MasterStatefulHome;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import javax.xml.rpc.ParameterMode;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3
public class ConfirmWorkCal extends ActionHandlerEJB implements ConfirmWorkCalLocal,ConfirmWorkCalRemote
 {
	GenericUtility genericUtility = GenericUtility.getInstance();
	String userId ="";
	String termId = "";
	String loginSite = "";
	/* public void ejbCreate() throws RemoteException, CreateException
	{
		System.out.println("Create Method Called....");
	}
	public void ejbRemove()
	{
	}
	public void ejbActivate()
	{
	}
	public void ejbPassivate()
	{
	} */
	public String confirm() throws RemoteException,ITMException
	{
		System.out.println("confirm() Method Called....");
		return "";
	}
	public String confirm(String xmlString, String xtraParams, String objContext) throws RemoteException,ITMException
	{
		String  retString = null;
		System.out.println("Xtra Params : " + xtraParams);
		//genericUtility = GenericUtility.getInstance();
		try
		{			
			retString = actionConfirmWorkCal(xmlString, xtraParams);
			
		}
	   	catch(Exception e)
		{
			System.out.println("Exception :actionHandler(String xmlString):" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
		System.out.println("returning from  actionHandler"+retString);
	    return (retString);
	}
	private String actionConfirmWorkCal(String xmlString ,String xtraParams) throws RemoteException,ITMException, Exception
	{
		Connection conn = null;
		Document dom = null;
		boolean isError = false;
		Document dom1 = null;
		String retString = "",errCode = "";
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int cnt = 0;
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		ConnDriver connDriver = new ConnDriver();
		String sql = "";
        //String siteCode = "";
		String custCode = "";
		String empCode = "";
		String[] values = new String[5];
		String confirmed = "";
		userId = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginCode");
		loginSite = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginSiteCode");
		empCode = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginEmpCode");
		termId = genericUtility.getValueFromXTRA_PARAMS( xtraParams, "termId" );
		java.sql.Timestamp currDate = null;
		java.sql.Timestamp tranDate = null;
		String returnString = null;
		boolean conFlag = false;
		int ctr = 0;
		String errStr = "",day = "",shift = "",workCtr = "",workDate = "";
		java.sql.Timestamp wrkDate =null,FromDate = null;
		StringTokenizer strTokens  = null;
		
		try
		{
			System.out.println("Values xmlString ::::::::::: "+xmlString);
			//dom = parseString(xmlString);				
			//System.out.println("Dom after parsing ::::::::::: "+dom);
			conn = connDriver.getConnectDB("DriverITM");
			conn.setAutoCommit(false);
			connDriver = null;
			strTokens = new StringTokenizer(xmlString,":");
			while(strTokens.hasMoreTokens())
			{
				values[ctr] = strTokens.nextToken();
				System.out.println("Values in while loop ::::::::::: "+values[ctr]);
				ctr++;
			}
			//System.out.println("Values from string tokenizer ::::::::::: "+values);
			//workDate = genericUtility.getColumnValue("work_date",dom);
			//System.out.println("workDate from dom ........."+workDate);
			//day = genericUtility.getColumnValue("day_no",dom);
			//shift = genericUtility.getColumnValue("shift",dom);
			//workCtr = genericUtility.getColumnValue("workCtr",dom);
			//day = genericUtility.getColumnValue("day_no",dom);
			//wrkDate = java.sql.Timestamp.valueOf(workDate + " 00:00:00.00");
			//currDate = new Timestamp( System.currentTimeMillis() );
			//workDate = genericUtility.getValidDateString(dateto,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat());
			sql = "select confirmed from work_calendar where site_code = ? "
			+" and work_date =? and day_no =? and shift = ? and work_ctr = ? ";
			FromDate = java.sql.Timestamp.valueOf(genericUtility.getValidDateString(values[1],genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat()) +" 00:00:00.0");						
			
			pstmt = conn.prepareStatement(sql);
			pstmt.setString( 1, values[0] );
			pstmt.setTimestamp( 2, FromDate );
			pstmt.setString( 3, values[2] );
			pstmt.setString( 4, values[3] );
			pstmt.setString( 5, values[4] );
			rs = pstmt.executeQuery();
			if( rs.next() )
			{
				confirmed = rs.getString(1);
				//System.out.println("get confirmed :::::: " +confirmed);
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;
			if( confirmed != null && confirmed.trim().equalsIgnoreCase("Y") )
			{
				errStr = "TRNADYCONF";
				returnString = itmDBAccessEJB.getErrorString("",errStr,userId);
				return returnString;
			}
			else
			{
			sql = " update work_calendar set confirmed ='Y' "
				 +" where site_code = ? and work_date = ? "
				 +" and day_no = ? and shift =? and work_ctr = ?";		
			
			System.out.println( "upd sql " + sql );
			pstmt = conn.prepareStatement( sql );
			pstmt.setString( 1, values[0] );
			pstmt.setTimestamp( 2, FromDate);
			pstmt.setString( 3, values[2] );
			pstmt.setString( 4, values[3] );
			pstmt.setString( 5, values[4] );
			int chkupdt = pstmt.executeUpdate();

			System.out.println("If 1 then updated = "+chkupdt);
			
			pstmt.close();
			pstmt = null;
			if(chkupdt != 1)
			{
				errCode = "VTUPDHDR";
			}
			}			
		}
		catch(SQLException e)
		{
			isError = true;
			System.out.println("Exception :ConfirmWorkCalEJB :confirm :SQLException :==>\n"+e.getMessage());
			retString = GenericUtility.getInstance().createErrorString(e);
			throw new ITMException(e);
		}
		catch(Exception e)
		{
			isError = true;
			System.out.println("Exception :ConfirmWorkCalEJB :confirm :==>\n"+e.getMessage());
			retString = GenericUtility.getInstance().createErrorString(e);
			throw new ITMException(e);
		}		
		finally
		{
			try
			{
				if( !isError && retString.trim().length() == 0 )
				{
					conn.commit();
					retString = new ITMDBAccessEJB().getErrorString("","VTCONF","");
					System.out.println("ConfirmWorkCalEJB:confirm:Committed");
				}
				else
				{
					conn.rollback();
					System.out.println("CConfirmWorkCalEJB:confirm:Rolled back");
					retString = new ITMDBAccessEJB().getErrorString("",errCode,"");
				}
				if(conn != null)
				{					
					if(pstmt != null)
					{
						pstmt.close();
						pstmt = null;
					}
					conn.close();
					conn = null;
				}
			}
			catch(Exception e)
			{
				System.out.println("Exception :ConfirmOOSIncidentEJB :confirm :==>\n"+e.getMessage());
				throw new ITMException(e);
			}
		}
		return retString;
	}
	
 }
