package com.agilex.healthcare.schedulingmanager.mdws;


import com.agilex.healthcare.schedulingmanager.mdws.connection.SchedulingConnection;
import com.agilex.healthcare.schedulingmanager.mdws.connection.SchedulingConnectionManager;
import com.agilex.healthcare.schedulingmanager.mdws.connection.SchedulingConnectionManagerFactory;
import com.agilex.healthcare.schedulingmanager.mdws.connection.MdwsConfiguration;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.PatientTO;

public class MdwsSchedulingDataLayer {

    protected MdwsConfiguration mdwsConfiguration;

    public MdwsSchedulingDataLayer(MdwsConfiguration mdwsConfiguration) {
        this.mdwsConfiguration = mdwsConfiguration;
    }

    protected SchedulingConnection getAuthenticatedConnection(String siteCode) {
        SchedulingConnectionManager connectionManager = SchedulingConnectionManagerFactory.getInstance();
        return connectionManager.getAuthenticatedSiteConnection(mdwsConfiguration, siteCode);
    }

    /**
	 * Calls the MDWS service SchedulingSvc.select for the given patient
	 */
	protected SchedulingConnection getAuthenticatedConnectionWithPatientSelected(String siteCode, String patientId){
        SchedulingConnection connection = getAuthenticatedConnection(siteCode);
        
        PatientTO result = null;
		try {
			result = connection.getPort().select(patientId);
		}
		catch (Exception e) {
			throw new MdwsResponseException(MdwsResponseFaultTOFilter.getResponseType(e.getMessage()), e.getMessage());
		}
		
        if (result.getFault() != null) {
			throw new MdwsResponseException(result.getFault());
        }
        return connection;
    }
}
