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 ConfirmWorkCalAll extends ActionHandlerEJB implements ConfirmWorkCalAllLocal,ConfirmWorkCalAllRemote
 {
	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 = "",sqlPrd = "";
		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,fr_date = null,to_date = 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());			
			boolean confFlag = false;
			FromDate = java.sql.Timestamp.valueOf(genericUtility.getValidDateString(values[1],genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat()) +" 00:00:00.0");						
			sqlPrd = "SELECT fr_date,to_date FROM PERIOD where ? BETWEEN fr_date and to_date";
			pstmt = conn.prepareStatement(sqlPrd);			
			pstmt.setTimestamp( 1, FromDate );			
			rs = pstmt.executeQuery();
			if( rs.next() )
			{
				fr_date = rs.getTimestamp(1);
				to_date = rs.getTimestamp(2);
				System.out.println("get from_date from period :::::: " +fr_date);
				System.out.println("get to_date from period :::::: " +to_date);
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;
			/* sql = "select confirmed from work_calendar where site_code = ? "
			+" and work_date =? and day_no =? and shift = ? and work_ctr = ? "; */
			sql = "select confirmed from work_calendar where " 
			+" work_date between ? and ? ";
			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] ); */
			
			//pstmt.setString( 1, values[0] );
			pstmt.setTimestamp( 1, fr_date );
			pstmt.setTimestamp( 2, to_date );
			/* pstmt.setString( 4, values[3] );
			pstmt.setString( 5, values[4] ); */
			rs = pstmt.executeQuery();
			while( rs.next() )
			{
				confirmed = rs.getString(1);
				//System.out.println("get confirmed :::::: " +confirmed);
				
				if( confirmed == null || confirmed.trim().equalsIgnoreCase("N") )
				{
					confFlag = true;
				}
				
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;
			
			if(confFlag)
			{
				//update
				sql = "update work_calendar set confirmed ='Y' " 
				+"where work_date between ? and ?";					
				System.out.println( "update sql " + sql );
				pstmt = conn.prepareStatement( sql );
				//pstmt.setString( 1, values[0] );
				pstmt.setTimestamp( 1, fr_date);
				pstmt.setTimestamp( 2, to_date );
				/* pstmt.setString( 4, values[3] );
				pstmt.setString( 5, values[4] ); */
				int chkupdt = pstmt.executeUpdate();
				System.out.println("chkupdt==> "+chkupdt);
					
				pstmt.close();
				pstmt = null;
					if(chkupdt != 1)
					{
						errCode = "VTUPDHDR";
					}
			}
			else
			{
				errStr = "TRNADYCONF";
				returnString = itmDBAccessEJB.getErrorString("",errStr,userId);
				return returnString; 
			}						
		}
		catch(SQLException e)
		{
			isError = true;
			System.out.println("Exception :ConfirmWorkCalAllEJB :confirm :SQLException :==>\n"+e.getMessage());
			retString = GenericUtility.getInstance().createErrorString(e);
			throw new ITMException(e);
		}
		catch(Exception e)
		{
			isError = true;
			System.out.println("Exception :ConfirmWorkCalAllEJB :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("ConfirmWorkCalAllEJB:confirm:Committed");
				}
				else
				{
					conn.rollback();
					System.out.println("ConfirmWorkCalAllEJB: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;
	}
	
 }
