package com.agilex.healthcare.schedulingmanager.datalayer;

import com.agilex.healthcare.mobilehealthplatform.domain.Patient;
import com.agilex.healthcare.mobilehealthplatform.domain.PatientIdentifier;
import com.agilex.healthcare.schedulingmanager.mdws.MdwsSchedulingDataLayer;
import com.agilex.healthcare.schedulingmanager.mdws.MdwsResponseException;
import com.agilex.healthcare.schedulingmanager.mdws.connection.SchedulingConnection;
import com.agilex.healthcare.schedulingmanager.mdws.connection.MdwsConfiguration;
import com.agilex.healthcare.schedulingmanager.mdws.connection.MdwsConfigurationHelper;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.schedulingservice.PatientTO;
import com.agilex.healthcare.schedulingmanager.mdws.translator.PatientTranslator;

/**
 * Created with IntelliJ IDEA.
 * User: patelh
 * Date: 12/6/13
 * Time: 12:13 AM
 * To change this template use File | Settings | File Templates.
 */

public class SMPatientDataLayerMdws extends MdwsSchedulingDataLayer implements SMPatientDataLayer {

    private final static org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory.getLog(SMPatientDataLayerMdws.class);

    public SMPatientDataLayerMdws(MdwsConfiguration mdwsConfiguration) {
        super(mdwsConfiguration);
    }

    /**
	 * Calls the MDWS service SchedulingSvc.select for the given patient
	 */
	@Override
    public Patient getPatient(PatientIdentifier patientIdentifier) {
        PatientTranslator translator = new PatientTranslator();
        SchedulingConnection authenticatedSite = super.getAuthenticatedConnectionWithPatientSelected(new MdwsConfigurationHelper().extractSiteCodeFromAssigningAuthority(patientIdentifier.getAssigningAuthority()), patientIdentifier.getUniqueId());
        PatientTO mdwsPatient = authenticatedSite.getPort().select(patientIdentifier.getUniqueId());
        if (mdwsPatient.getFault() != null) {
            String errorMsg = mdwsPatient.getFault().getMessage();
            logger.error(errorMsg);
            throw new MdwsResponseException(MdwsResponseException.UserMessages.INVALID_PATIENT, errorMsg);
        }
        return translator.translate(mdwsPatient);
    }
}
