
/*	Developed by : TaraniseMeher
	Company : Base Information Management Pvt. Ltd
	Window Name : w_work_calender
	Date :21/09/2006
	Menu Reference:Master-manufacturingWorkCalender-work center wisework calender(MFG)
	Button:Confirm */



package ibase.webitm.ejb.mfg.adv;

import java.rmi.RemoteException;
import java.util.*;
import java.io.*;
import java.util.Date;
import java.sql.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.ejb.*;
import javax.naming.InitialContext;
import javax.annotation.*;
import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.*;
import ibase.webitm.utility.GenericUtility;
import ibase.ejb.*;
import ibase.system.config.*;
import java.text.SimpleDateFormat;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3

public class CWWorkCalConf extends ActionHandlerEJB implements  CWWorkCalConfLocal , CWWorkCalConfRemote //SessionBean
{
	GenericUtility genericUtility =  GenericUtility.getInstance();
	ConnDriver connDriver = new ConnDriver();
	ITMDBAccessEJB itmDBAccess = new ITMDBAccessEJB();
	Connection conn = null;
	PreparedStatement pstmt = null;
	@PostConstruct
	public void ejbCreate() //throws RemoteException	//, CreateException 
	{
		System.out.println("Create Method Called....");
		System.out.println("Create Method Called[BOMAltConfEJB].................");
		try
		{
			conn = connDriver.getConnectDB("DriverITM");
		}
		catch(Exception e)
		{
			e.printStackTrace();
			System.out.println("Exception in creating Connection");
		}
	}
	/*public void ejbRemove()
	{
	}

	public void ejbActivate() 
	{
	}

	public void ejbPassivate() 
	{
	}*/

    public String actionHandler() throws RemoteException,ITMException
	{
		System.out.println("actionHandler() Method Called....");
		return "";
	}
	

	public String actionHandler(String xmlString, String xtraParams, String objContext) throws RemoteException,ITMException
	{
		Document dom = null;
		String  retString = null;
		System.out.println("Xtra Params : " + xtraParams);
		genericUtility = GenericUtility.getInstance();
		try
		{
			retString = ConfirmWorkCal(xmlString, xtraParams);
		}
	   	catch(Exception e)
		{
			System.out.println("Exception :Enquiry :actionHandler(String xmlString):" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
		System.out.println("returning from actionPickList actionHandler"+retString);
	    return (retString);
	}
   
	private String ConfirmWorkCal(String tranId, String xtraParams) throws RemoteException , ITMException
	{
		System.out.println("DeactivateBOM String.......");
		String loginEmpCode = "";
		String chgUser = null;
		String chgTerm = null;
		String retString = "";
		String active = null;
		String sql = null;
		int count = 0;
		
		String	siteCode = null;
		String	workDate = null;
		String	dayNo = null;
		String	shift = null;
		String workCtr = null;
		String userId = null;
		String confirm = null;
		
		java.sql.Timestamp today = null;
		java.sql.Timestamp workDateTs = null;
		
		ResultSet rs = null;
	
		try
		{
			
			if(conn==null)
			{
				conn = connDriver.getConnectDB("DriverITM");
			}
			System.out.println("xtraParams : " + xtraParams);
			loginEmpCode = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginEmpCode");
			chgUser = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginCode");
			userId = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"userId");
			System.out.println("loginEmpCode::::::::"+loginEmpCode);
			System.out.println("chgUser::::::::"+chgUser);
			if(loginEmpCode == null || loginEmpCode.trim().length()>0)
			{
				retString = itmDBAccess.getErrorString("","VMEMPORD2","","",conn);
			}
							
			System.out.println("TradId::::::::::::::::::::::::::::"+tranId);
			String[] token = tranId.split(":");
			workDate = token[0];
			dayNo = token[1];
			shift = token[2];
			siteCode = token[3]; 
			workCtr = token[4];
			System.out.println("siteCode:::"+siteCode);
			System.out.println("workDate:::"+workDate);
			System.out.println("dayNo:::"+dayNo);
			System.out.println("shift:::"+shift);
			System.out.println("workCtr:::"+workCtr);
			System.out.println("genericUtility.getApplDateFormat:::"+genericUtility.getApplDateFormat());
			System.out.println("genericUtility.getDBDateFormat:::"+genericUtility.getDBDateFormat());
			//converting date
			try
			{ 
				Object date = null;
				java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(genericUtility.getApplDateFormat());
				date = sdf.parse(workDate);
				java.text.SimpleDateFormat sdf1 = new java.text.SimpleDateFormat(genericUtility.getDBDateFormat());
				String dateStr = sdf1.format(date).toString();
				workDateTs = java.sql.Timestamp.valueOf(dateStr+" 00:00:00");
			}
			catch(Exception e) 
			{ 
				e.printStackTrace();
			}
			
			 sql ="SELECT NVL(CONFIRMED,'N') FROM WORK_CALENDAR "
			 	+" WHERE	SITE_CODE = ? AND WORK_DATE = ? AND DAY_NO =? AND SHIFT = ? AND WORK_CTR= ?";
			System.out.println("Executing Query:::"+sql);				
			System.out.println("Setting Parameters :::["+siteCode+"] ["+workDateTs+"] ["+dayNo+"] ["+shift+"] ["+workCtr+"]");
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1,siteCode);
			pstmt.setTimestamp(2,workDateTs);
			pstmt.setString(3,dayNo); 
			pstmt.setString(4,shift);
			pstmt.setString(5,workCtr);
			rs = pstmt.executeQuery();
			if(rs.next()) 
			{
				confirm = rs.getString(1);
			}
			rs.close();
			System.out.println("Confirm Status:::["+confirm+"]");
			if(confirm==null)confirm=" ";
			if(confirm.equalsIgnoreCase("Y"))
			{ 
				retString = itmDBAccess.getErrorString("", "VTMCONF1", userId);
			 
			} 
			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("Executing Query:::"+sql);				
				System.out.println("Setting Parameters :::["+siteCode+"] ["+workDateTs+"] ["+dayNo+"] ["+shift+"] ["+workCtr+"]");
				pstmt=conn.prepareStatement(sql);
				pstmt.setString(1,siteCode);
				pstmt.setTimestamp(2,workDateTs);
				pstmt.setString(3,dayNo);
				pstmt.setString(4,shift);
				pstmt.setString(5,workCtr);
				int update = pstmt.executeUpdate();
				System.out.println("No Of Record Update:::::"+update);
				if(update<=0)
				{
					retString = itmDBAccess.getErrorString("", "DS0001", userId);
				
				}
				else
				{
					retString =	itmDBAccess.getErrorString("","VTMCONF2","","",conn);
				}
			} 
			
        }
		catch (SQLException se)
		{
			System.out.println("Exception : BOMDeactivateEJB : DeactivateBOM " +se.getMessage());
			se.printStackTrace();
			throw new ITMException(se);
		}
		catch(Exception e)
		{
			System.out.println("Exception : BOMDeactivateEJB : DeactivateBOM :" +e.getMessage());
			e.printStackTrace();
			throw new ITMException(e);
		}
		finally
		{
			try{
					conn.close();
					conn = null;
					pstmt.close();
				}
				catch(Exception e){}
		}
	return	retString;
	}
		
	
}//end of ejb