
/*	Developed by : TaraniseMeher
	Company : Base Information Management Pvt. Ltd
	Window Name : w_bom_alt
	Date :18/09/2006
	Menu Reference:Master-ProductStructure-BillOfMaterial(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 ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.*;
import ibase.webitm.utility.GenericUtility;
import ibase.ejb.*;
import javax.annotation.*;
import ibase.system.config.*;
import java.text.SimpleDateFormat;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3

public class BOMAltConf extends ActionHandlerEJB implements  BOMAltConfLocal , BOMAltConfRemote //SessionBean
{
	GenericUtility genericUtility = null;
	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 = ConfirmAltBOM(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 ConfirmAltBOM(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	itemCode = null;
		String	itemref = null;
		String	itemCodeAlt = null;
		String	bomCode = null;
		String userId = null;
		String confirm = null;
		
		java.sql.Timestamp today = 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(":");
			itemCode = token[0];
			itemref = token[1];
			itemCodeAlt = token[2];
			bomCode = token[3];
			System.out.println("itemCode:::"+itemCode);
			System.out.println("itemref:::"+itemref);
			System.out.println("itemCodeAlt:::"+itemCodeAlt);
			System.out.println("bomCode:::"+bomCode);
			
			 sql ="SELECT CASE WHEN CONFIRMED IS NULL THEN 'N' ELSE CONFIRMED END "
						+" FROM BOM_ALT WHERE BOM_CODE = ?  AND ITEM_CODE = ? AND ITEM_REF = ? AND ITEM_CODE__ALT = ?";
			System.out.println("Executing Query:::"+sql);				
			System.out.println("Setting Parameters :::["+bomCode+"] ["+itemCode+"] ["+itemref+"] ["+itemCodeAlt+"]");
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1,bomCode);
			pstmt.setString(2,itemCode);
			pstmt.setString(3,itemref);
			pstmt.setString(4,itemCodeAlt);
			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 BOM_ALT SET CONFIRMED ='Y', CONF_DATE = ? 	WHERE BOM_CODE = ? AND ITEM_CODE = ? AND "
					+"ITEM_REF = ? AND ITEM_CODE__ALT = ?" ;
				System.out.println("Executing Query:::"+sql);				
				System.out.println("Setting Parameters :::["+bomCode+"] ["+itemCode+"] ["+itemref+"] ["+itemCodeAlt+"]");
				pstmt=conn.prepareStatement(sql);
				pstmt.setTimestamp(1,new Timestamp(System.currentTimeMillis()));
				pstmt.setString(2,bomCode);
				pstmt.setString(3,itemCode);
				pstmt.setString(4,itemref);
				pstmt.setString(5,itemCodeAlt);
				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