package com.adi.npi.savvion.wsclient.infodepot_eim;

import java.io.*;
import java.net.*;
import java.util.*;
import javax.xml.rpc.*;

/**
 * This Class is a Client for the Info Depot EIM (Employee Information Module)
 * Web Service. It accesses two functional interfaces of the Info Depot EIM Web
 * Service - GetPlannerNTLogon and GetEmailAddress.
 * 
 * @author Abhishek Kundu
 * @version 1.0 9/17/2006 Initial Version.
 */
public class EIMClient {

	private static EmployeeInformationModuleSoap soap;
	private static Properties npiURLPropertiesFile;

	private static final String URL_PROPERTIES_FILE = "NPI_URL.properties";
	private static final String EIM_URL_KEY = ".url.infodepot.eim";

	/**
     * This method loads the URL Properties File (URL_PROPERTIES_FILE) if it has
     * not been already loaded.
     * 
     * @throws IOException
     */
	private static void loadPropertiesFile () throws IOException {

		// Load the Properties file if it is NULL (not already loaded).
		if (npiURLPropertiesFile == null) {

			URL _url = ClassLoader.getSystemResource (URL_PROPERTIES_FILE);
			Properties _prop = new Properties ();
			_prop.load (_url.openStream ());

			// Load the Properties file.
			npiURLPropertiesFile = _prop;
		}
	}

	/**
     * This method invokes the Info Depot EIM web service and gets the SOAP
     * object. It first loads the Properties file if it has not been already
     * loaded by calling the "loadPropertiesFile" method.
     * 
     * @throws ServiceException
     * @throws MalformedURLException
     * @throws IOException
     */
	private static void invokeWebService () throws ServiceException,
			MalformedURLException, IOException {

		// Load the Properties file if its not loaded already.
		EIMClient.loadPropertiesFile ();

		// Load the SOAP object if it has not been loaded already.
		if (soap == null) {

			// Get the URL for the EIM Web Service by appending EIM_URL_KEY to
			// the environment from the npiURLPropertiesFile.
			String _eimURL = EIMClient.npiURLPropertiesFile
					.getProperty (EIMClient.npiURLPropertiesFile
							.getProperty ("server.environment")
							+ EIMClient.EIM_URL_KEY);

			// Make standard service connection to the web service.
			EmployeeInformationModule _service = new EmployeeInformationModuleLocator ();

			// Then establish the SOAP connection.
			soap = _service
					.getEmployeeInformationModuleSoap (new URL (_eimURL));
		}
	}

	/**
     * This method gets the Planner NT ID from the Info Depot EIM
     * GetPlannerNTLogon web service.
     * 
     * @param plannerCode The Planner Code whose NT ID is to be fetched.
     * @return the Planner NT ID for the supplied Planner Code.
     * @throws Exception for failed execution or failure to find requested data.
     */
	public synchronized static String getPlannerNTID (String plannerCode)
			throws Exception {

		// Invoke the Web Service if the
		EIMClient.invokeWebService ();

		// Initialize the Response object.
		com.adi.npi.savvion.wsclient.infodepot_eim.WebServiceReturnParameter _response = null;

		// Get the Response.
		_response = soap.getPlannerNTLogon (plannerCode);

		String _returnMessage = _response.getReturnMessage ();
		String _returnCode = _response.getReturnCode ().toString ();

		// Throw exception containing Error Code and Error message if Return
		// Code is not "SUCCESS".
		if (_returnCode != null
				&& _returnCode.equalsIgnoreCase ("SUCCESS") == false) {
			throw new Exception (
					"Info Depot EIM GetPlannerNTLogon web service returned an Error. The Return Code is: "
							+ _returnCode
							+ ". The Return Message is: "
							+ _returnMessage);
		}

		// Return the Planner NT ID.
		return _response.getValueObject ().toString ();
	}

	/**
     * This method returns the NT ID of the requested data type from the Info
     * Depot EIM GetEmailAddress web service. It would be used as the helper
     * method for all the different DataTypes.
     * 
     * @param datatype The DataType whose NT ID is to be fetched.
     * @param key1 Key 1 for the requested data type.
     * @param key2 Key 2 for the requested data type. May be blank for a few
     *            data types.
     * @return the NT ID for the requested DataType and keys.
     * @throws Exception
     */
	public synchronized static String getDataTypeNTID (String datatype,
			String key1, String key2) throws Exception {

		// Invoke the Web Service if the
		EIMClient.invokeWebService ();

		// Initialize the Response object.
		com.adi.npi.savvion.wsclient.infodepot_eim.WebServiceReturnParameter _response = null;

		// Get the Response.
		_response = soap.getEmailAddress (DataTypes.fromValue (datatype), key1,
				key2);

		String _returnMessage = _response.getReturnMessage ();
		String _returnCode = _response.getReturnCode ().toString ();

		// Throw exception containing Error Code and Error message if Return
		// Code is not "SUCCESS".
		if (_returnCode != null
				&& _returnCode.equalsIgnoreCase ("SUCCESS") == false) {
			throw new Exception (
					"Info Depot EIM GetEmailAddress web service returned an Error. The Return Code is: "
							+ _returnCode
							+ ". The Return Message is: "
							+ _returnMessage
							+ " Data Type: "
							+ datatype
							+ ", Key 1: " + key1 + ", Key 2: " + key2);
		}

		// Return the NT ID for the requested DataType.
		return _response.getValueObject ().toString ();
	}

	/**
     * This method returns the Finance Rep NT ID. It is a wrapper for the
     * getDataTypeNTID method.
     * 
     * @param developmentSite Development Site whose Finance Rep NT ID is to be
     *            fetched.
     * @return NT ID of the Finance Rep.
     * @throws Exception
     */
	public synchronized static String getFinanceRepNTID (String developmentSite)
			throws Exception {
		return EIMClient.getDataTypeNTID ("Finance_Rep", developmentSite, "");
	}

	/**
     * This method returns the Industrial Engineer NT ID. It is a wrapper for
     * the getDataTypeNTID method.
     * 
     * @param developmentSite Development Site whose Industrial Engineer NT ID
     *            is to be fetched.
     * @return NT ID of the Industrial Engineer.
     * @throws Exception
     */
	public synchronized static String getIndustrialEngineerNTID (
			String developmentSite) throws Exception {
		return EIMClient.getDataTypeNTID ("Industrial_Engineer",
				developmentSite, "");
	}

	/**
     * This method returns the Mfg Assembly ECN Engineer NT ID. It is a wrapper
     * for the getDataTypeNTID method.
     * 
     * @param developmentSite Development Site whose Mfg Assembly ECN Engineer
     *            NT ID is to be fetched.
     * @return NT ID of the Mfg Assembly ECN Engineer.
     * @throws Exception
     */
	public synchronized static String getMfgAssyECNEngineerNTID (
			String developmentSite) throws Exception {
		return EIMClient.getDataTypeNTID ("Mfg_Assembly_ECN_Engineer",
				developmentSite, "");
	}

	/**
     * This method returns the Resident Engineer NT ID. It is a wrapper for the
     * getDataTypeNTID method.
     * 
     * @param testSite Test Site whose Resident Engineer is to be fetched.
     * @param strategyCode Strategy Code whose Resident Engineer is to be
     *            fetched.
     * @return NT ID of the Resident Engineer.
     * @throws Exception
     */
	public synchronized static String getResidentEngineerNTID (String testSite,
			String strategyCode) throws Exception {
		return EIMClient.getDataTypeNTID ("Resident_Engineer", testSite,
				strategyCode);
	}

	/**
     * This method returns the Development Site Operations Manager NT ID. It is
     * a wrapper for the getDataTypeNTID method.
     * 
     * @param testSite Test Site whose Development Site Operations Manager is to
     *            be fetched.
     * @return NT ID of the Development Site Operations Manager.
     * @throws Exception
     */
	public synchronized static String getDevSiteOpsManagerNTID (String testSite)
			throws Exception {
		return EIMClient.getDataTypeNTID ("Dev_Site_Ops_Manager", testSite, "");
	}

	/**
     * This method returns the WW Mfg Finance Rep NT ID. It is a wrapper for the
     * getDataTypeNTID method.
     * 
     * @param developmentSite Development Site whose WW Mfg Finance Rep NT ID is
     *            to be fetched.
     * @return NT ID of the WW Mfg Finance Rep.
     * @throws Exception
     */
	public synchronized static String getWWMfgFinanceRepNTID (
			String developmentSite) throws Exception {
		return EIMClient.getDataTypeNTID ("WW_Mfg_Finance_Rep",
				developmentSite, "");
	}

	/**
     * This method returns the WW Mfg Tester Engineer NT ID. It is a wrapper for
     * the getDataTypeNTID method.
     * 
     * @param developmentSite Development Site whose WW Mfg Tester Engineer NT
     *            ID is to be fetched.
     * @param tester Tester whose WW Mfg Tester Engineer NT ID is to be fetched.
     * @return NT ID of the WW Mfg Tester Engineer.
     * @throws Exception
     */
	public synchronized static String getWWMfgTesterEngineerNTID (
			String developmentSite, String tester) throws Exception {
		return EIMClient.getDataTypeNTID ("WW_Mfg_Test_Engineer",
				developmentSite, tester);
	}

	/**
     * This method returns the WW Mfg Handler Contactor Engineer NT ID. It is a
     * wrapper for the getDataTypeNTID method.
     * 
     * @param developmentSite Development Site whose WW Mfg Handler Contactor
     *            Engineer NT ID is to be fetched.
     * @param packageType Package Type whose WW Mfg Handler Contactor Engineer
     *            NT ID is to be fetched.
     * @return NT ID of the WW Mfg Handler Contactor Engineer.
     * @throws Exception
     */
	public synchronized static String getWWMfgHandlerContactorEngineerNTID (
			String developmentSite, String packageType) throws Exception {
		return EIMClient.getDataTypeNTID ("WW_Mfg_Handler_Center_Engineer",
				developmentSite, packageType);
	}

	/**
     * This method returns the PICS Capability Admin NT ID. It is a wrapper for
     * the getDataTypeNTID method.
     * 
     * @param developmentSite Development Site whose PICS Capability Admin NT ID
     *            is to be fetched.
     * @return NT ID of the PICS Capability Admin.
     * @throws Exception
     */
	public synchronized static String getPICSCapabilityAdminNTID (
			String developmentSite) throws Exception {
		return EIMClient.getDataTypeNTID ("PICS_Capability_Admin",
				developmentSite, "");
	}

	/**
     * This method returns the Qual Engineer's NT ID. It is a wrapper for the
     * getDataTypeNTID method.
     * 
     * @param developmentSite Development Site whose Qual Engineer NT ID is to
     *            be fetched.
     * @param strategyCode Strategy code whose Qual Engineer NT ID is to be
     *            fetched.
     * @return NT ID of the Qual Engineer.
     * @throws Exception
     */
	public synchronized static String getQualEngineerNTID (
			String developmentSite, String strategyCode) throws Exception {
		return EIMClient.getDataTypeNTID ("Quality_Engineer", developmentSite,
				strategyCode);
	}

	/**
     * This method returns the Focus Factory Manager NT ID. It is a wrapper for
     * the getDataTypeNTID method.
     * 
     * @param focusFactory Focus Factory whose Focus Factory Manager NT ID is to
     *            be fetched.
     * @return NT ID of the Focus Factory Manager.
     * @throws Exception
     */
	public synchronized static String getFocusFactoryManagerNTID (
			String focusFactory) throws Exception {
		return EIMClient.getDataTypeNTID ("Focus_Factory_Manager",
				focusFactory, "");
	}

	/**
     * This method returns the Assembly Engineer's NT ID. It is a wrapper for
     * the getDataTypeNTID method.
     * 
     * @param developmentSite Development Site whose Assembly Engineer NT ID is
     *            to be fetched.
     * @param strategyCode Strategy code whose Assembly Engineer NT ID is to be
     *            fetched.
     * @return NT ID of the Assembly Engineer.
     * @throws Exception
     */
	public synchronized static String getAssemblyEngineerNTID (
			String developmentSite, String strategyCode) throws Exception {
		return EIMClient.getDataTypeNTID ("Assembly_Engineer", developmentSite,
				strategyCode);
	}

	/**
     * This method returns the Subcon Planner NT ID. It is a wrapper for the
     * getDataTypeNTID method.
     * 
     * @param testSite Test Site whose Subcon Planner is to be fetched.
     * @return NT ID of the Subcon Planner.
     * @throws Exception
     */
	public synchronized static String getSubconPlannerNTID (String testSite)
			throws Exception {
		return EIMClient.getDataTypeNTID ("SubCon_Planner", testSite, "");
	}

	/**
     * This method returns the WW Development Site Coordinator NT ID. It is a
     * wrapper for the getDataTypeNTID method.
     * 
     * @param developmentSite Development Site whose WW Development Site
     *            Coordinator NT ID is to be fetched.
     * @return NT ID of the WW Development Site Coordinator.
     * @throws Exception
     */
	public synchronized static String getDevSiteCoordinatorNTID (
			String developmentSite) throws Exception {
		return EIMClient.getDataTypeNTID ("WW_Dev_Site_Coordinator",
				developmentSite, "");
	}

	/**
     * This method returns the WW Pack Capability Engineer NT ID. It is a
     * wrapper for the getDataTypeNTID method.
     * 
     * @param developmentSite Development Site whose WW Pack Capability Engineer
     *            NT ID is to be fetched.
     * @return NT ID of the WW Pack Capability Engineer.
     * @throws Exception
     */
	public synchronized static String getWWPackCapabilityEngineerNTID (
			String developmentSite) throws Exception {
		return EIMClient.getDataTypeNTID ("WW_Pack_Capability",
				developmentSite, "");
	}

	/**
     * This method returns the Forecaster NT ID. It is a wrapper for the
     * getDataTypeNTID method.
     * 
     * @param forecasterID Forecaster ID whose Forecaster NT ID is to be
     *            fetched.
     * @return NT ID for the Forecaster ID.
     * @throws Exception
     */
	public synchronized static String getForecasterNTID (String forecasterID)
			throws Exception {
		return EIMClient.getDataTypeNTID ("Forecaster_Id", forecasterID, "");
	}

	/**
     * This method returns the BOM Coordinator NT ID Group. It is a wrapper for
     * the getDataTypeNTID method.
     * 
     * @param developmentSite Development site whose BOM Coordinator NT ID Group
     *            is to be fetched.
     * @return NT ID of the BOM Coordinator Group.
     * @throws Exception
     */
	public synchronized static String getBOMCoordinatorNTID (
			String developmentSite) throws Exception {
		return EIMClient.getDataTypeNTID ("BOM_Coordinator", developmentSite,
				"");
	}

	/**
     * This method returns the WW Test Engineering Manager's NT ID. It is a
     * wrapper for the getDataTypeNTID method.
     * 
     * @param strategyCode Strategy code whose WW Test Engineering Manager NT ID
     *            is to be fetched.
     * @return NT ID of the WW Test Engineering Manager.
     * @throws Exception
     */
	public synchronized static String getWWTestEngineeringMgrNTID (
			String strategyCode) throws Exception {
		return EIMClient.getDataTypeNTID ("WW_Test_Engineering_Mgr",
				strategyCode, "");
	}

	/**
     * This method returns the WW Mfg NPC's NT ID. It is a wrapper for the
     * getDataTypeNTID method.
     * 
     * @param strategyCode Strategy code whose WW Mfg NPC NT ID is to be
     *            fetched.
     * @return NT ID of the WW Mfg NPC.
     * @throws Exception
     */
	public synchronized static String getWWMfgNPCNTID (String strategyCode)
			throws Exception {
		return EIMClient.getDataTypeNTID ("WW_Mfg_NPC", strategyCode, "");
	}
	
	
	/**
     * This method returns the Fab Engineering Manager's NT ID. It is a wrapper for the
     * getDataTypeNTID method.
     * 
     * @param FabSite code whose Fab Engineering Manager NT ID is to be
     *            fetched.
     * @return NT ID of the Fab Engineering Manager.
     * @throws Exception
     */
	public synchronized static String getFabEngineeringMgrNTID (String fabSite)
			throws Exception {
		return EIMClient.getDataTypeNTID ("Fab_Engineering_Mgr", fabSite, "");
	}
	
	/**
     * This method returns the Fab Site Probe Manager's NT ID. It is a wrapper for the
     * getDataTypeNTID method.
     * 
     * @param FabSite code whose Fab Site Probe Manager NT ID is to be
     *            fetched.
     * @return NT ID of the Fab Site Probe Manager.
     * @throws Exception
     */
	public synchronized static String getFabSiteProbeMgrNTID (String fabSite)
			throws Exception {
		return EIMClient.getDataTypeNTID ("Fab_Site_Probe_Mgr", fabSite, "");
	}
	
	/**
     * This method returns the Probe Site Probe Manager's NT ID. It is a wrapper for the
     * getDataTypeNTID method.
     * 
     * @param ProbeSite code whose Probe Site Probe Manager NT ID is to be
     *            fetched.
     * @return NT ID of the Probe Site Probe Manager.
     * @throws Exception
     */
	public synchronized static String getProbeSiteProbeMgrNTID (String probeSite)
			throws Exception {
		return EIMClient.getDataTypeNTID ("Probe_Site_Probe_Mgr", probeSite, "");
	}
	
	/**
     * This method returns the ESD Engineer's NT ID. It is a wrapper for the
     * getDataTypeNTID method.
     * 
     * @param FabSite code whose ESD Engineer NT ID is to be
     *            fetched.
     * @return NT ID of the ESD Engineer.
     * @throws Exception
     */
	public synchronized static String getESDEngineerNTID (String fabSite)
			throws Exception {
		return EIMClient.getDataTypeNTID ("ESD_Engineer", fabSite, "");
	}

	/**
     * This method returns the Chute Yield Manager's NT ID. It is a wrapper for the
     * getDataTypeNTID method.
     * 
     * @param FabSite code whose Chute Yield Manager NT ID is to be
     *            fetched.
     * @return NT ID of the Chute Yield Manager.
     * @throws Exception
     */
	public synchronized static String getChuteYieldMgrNTID (String fabSite)
			throws Exception {
		return EIMClient.getDataTypeNTID ("Chute_Yield_Manager", fabSite, "");
	}
	
	
	/**
     * This could be used as a Test Harness.
     * 
     * @param args Run Time arguments for this class.
     */
	public static void main (String[] args) {

		try {
			// Get the Planner NT ID.
			System.out.println ("Planner NT ID: "
					+ EIMClient.getPlannerNTID ("MGE"));

			// Get the Finance Rep NT ID.
			System.out.println ("Finance Rep NT ID: "
					+ EIMClient.getFinanceRepNTID ("ADWL"));

			// Get the Industrial Engineer NT ID.
			System.out.println ("Industrial Engineer NT ID: "
					+ EIMClient.getIndustrialEngineerNTID ("ADWL"));

			// Get the Mfg Assy ECN Engineer NT ID.
			System.out.println ("Mfg Assy ECN Engineer NT ID: "
					+ EIMClient.getMfgAssyECNEngineerNTID ("ADWL"));

			// Get the Resident Engineer NT ID.
			System.out.println ("Resident Engineer NT ID: "
					+ EIMClient.getResidentEngineerNTID ("STA", "ADC"));

			// Get the Development Site Operations Manager NT ID.
			System.out.println ("Development Site Operations Manager NT ID: "
					+ EIMClient.getDevSiteOpsManagerNTID ("ADWL"));

			// Get the WW Mfg Finance Rep NT ID.
			System.out.println ("WW Mfg Finance Rep NT ID: "
					+ EIMClient.getWWMfgFinanceRepNTID ("ADWL"));

			// Get the WW Mfg Tester Engineer NT ID.
			System.out.println ("WW Mfg Tester Engineer NT ID: "
					+ EIMClient.getWWMfgTesterEngineerNTID ("ADWL", "5040_B"));

			// Get the WW Mfg Handler Contactor Engineer NT ID.
			System.out.println ("WW Mfg Handler Contactor Engineer NT ID: "
					+ EIMClient.getWWMfgHandlerContactorEngineerNTID ("ADWL",
							"LFCSP"));

			// Get the PICS Capability Admin NT ID.
			System.out.println ("PICS Capability Admin NT ID: "
					+ EIMClient.getPICSCapabilityAdminNTID ("ADWL"));

			// Get the Qual Engineer NT ID.
			System.out.println ("Qual Engineer NT ID: "
					+ EIMClient.getQualEngineerNTID ("ADWL", "ADC"));

			// Get the Focus Factory Manager NT ID.
			System.out.println ("Focus Factory Manager NT ID: "
					+ EIMClient.getFocusFactoryManagerNTID ("STRIP"));

			// Get the Assembly Engineer NT ID.
			System.out.println ("Assembly Engineer NT ID: "
					+ EIMClient.getAssemblyEngineerNTID ("ADWL", "ADC"));

			// Get the Subcon Planner NT ID.
			System.out.println ("Subcon Planner NT ID: "
					+ EIMClient.getSubconPlannerNTID ("STA"));

			// Get the WW Dev Site Coordinator NT ID.
			System.out.println ("WW Dev Site Coordinator NT ID: "
					+ EIMClient.getDevSiteCoordinatorNTID ("ADWL"));

			// Get the WW Pack Capability Engineer NT ID.
			System.out.println ("WW Pack Capability Engineer NT ID: "
					+ EIMClient.getWWPackCapabilityEngineerNTID ("ADWL"));

			// Get the Forecaster NT ID.
			System.out.println ("Forecaster NT ID: "
					+ EIMClient.getForecasterNTID ("BBriano"));

			// Get the BOM Coordinator NT ID Group.
			// System.out.println ("BOM Coordinator NT ID Group: "
			// + EIMClient.getBOMCoordinatorNTID ("ADWL"));

			// Get the WW Test Engineering Manager NT ID.
			System.out.println ("WW Test Engineering Manager NT ID: "
					+ EIMClient.getWWTestEngineeringMgrNTID ("ADC"));

			// Get the WW Mfg NPC NT ID.
			System.out.println ("WW Mfg NPC NT ID: "
					+ EIMClient.getWWMfgNPCNTID ("ADC"));
			
			System.out.println ("Fab Eng Manager NT ID:" 
					+ EIMClient.getFabEngineeringMgrNTID("ADWL"));
			
			System.out.println ("Fab Site Probe Manager NT ID:" 
				+ EIMClient.getFabSiteProbeMgrNTID("ADWL"));
			
			System.out.println ("Probe Site Probe Manager NT ID:" 
					+ EIMClient.getProbeSiteProbeMgrNTID("ADWL"));
			
			System.out.println ("ESD Engineer NT ID:" 
					+ EIMClient.getESDEngineerNTID("ADWL"));
			
			System.out.println("Chute Yield Mgr NT ID:" 
					+ EIMClient.getChuteYieldMgrNTID("ADWL")); 
			
			
		}
		catch (Exception ex) {
			ex.printStackTrace ();
		}
	}
}
