/*	Developed by : TaraniseMeher
	Company : Base Information Management Pvt. Ltd
	Window Name : w_bom
	Date :18/09/2006
	Menu Reference:Master-ProductStructure-BillOfMaterial(MFG)
	Button:Deactivate */
	
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 BOMDeactivate extends ActionHandlerEJB implements  BOMDeactivateLocal , BOMDeactivateRemote //SessionBean
{
	GenericUtility genericUtility = null;
	ConnDriver connDriver = new ConnDriver();
	ITMDBAccessEJB itmDBAccessEJB = 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[BOMDeactivateEJB].................");
		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 = DeactivateBOM(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 DeactivateBOM(String bomCode, String xtraParams) throws RemoteException , ITMException
	{
		System.out.println("DeactivateBOM String.......");
		String loginEmpCode = "";
		String chgUser = "";
		String chgTerm = "";
		String retString = "";
		String active = "";
		String sql = "";
		int count = 0;
		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");
			System.out.println("loginEmpCode::::::::"+loginEmpCode);
			System.out.println("chgUser::::::::"+chgUser);
			if(loginEmpCode == null || loginEmpCode.trim().length()>0)
			{
				retString = itmDBAccessEJB.getErrorString("","VMEMPORD2","","",conn);
			}
			if(bomCode!=null)
			{
				
				sql = "SELECT ACTIVE FROM BOM WHERE BOM_CODE = ?";
				System.out.println("Setting Parmeter:::"+bomCode);
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1,bomCode);
				rs = pstmt.executeQuery();
				if (rs.next())
				{
					active = (rs.getString(1)==null?" ":rs.getString(1));				
				}
				System.out.println("Bom Code : " + bomCode +  "  Active : [" + active + "]");
				rs.close();
				pstmt.close();
				if(active.trim().equalsIgnoreCase("N"))
				{
					System.out.println("Active : " + active);
					retString = itmDBAccessEJB.getErrorString("","VTBOMACT1",chgUser);
					System.out.println("retString : " + retString);
					return retString;
				}
				today = new java.sql.Timestamp(System.currentTimeMillis());
				sql = "UPDATE BOM SET ACTIVE = 'N',CHG_USER = ?, CHG_DATE = ?, CHG_TERM = ?" 
				+ " WHERE BOM_CODE = '" + bomCode + "'";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1,chgUser);
				pstmt.setTimestamp(2,today);
				pstmt.setString(3,chgUser);
				int rowsUpdated = pstmt.executeUpdate();
				pstmt.close();
				System.out.println("No of Rows Updated in BOM : [" + rowsUpdated +"]");
				if(rowsUpdated > 0 )
				{
					conn.commit();
					retString = itmDBAccessEJB.getErrorString("","VTBOMACT2",chgUser);
				}
				else
				{
					conn.rollback();
					itmDBAccessEJB.getErrorString("","VTBOMACT1",chgUser);
				}
			}
		}
		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;
	}
}