<commit_before>package org.endeavourhealth.transform.enterprise.transforms;

import com.google.common.base.Strings;
import org.endeavourhealth.core.data.ehr.models.ResourceByExchangeBatch;
import org.endeavourhealth.core.xml.enterprise.Encounter;
import org.endeavourhealth.core.xml.enterprise.EnterpriseData;
import org.endeavourhealth.core.xml.enterprise.SaveMode;
import org.endeavourhealth.transform.common.exceptions.TransformException;
import org.endeavourhealth.transform.fhir.FhirExtensionUri;
import org.endeavourhealth.transform.fhir.ReferenceHelper;
import org.hl7.fhir.instance.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class ReferralRequestTransformer extends AbstractTransformer {

    private static final Logger LOG = LoggerFactory.getLogger(ReferralRequestTransformer.class);

    public void transform(ResourceByExchangeBatch resource,
                                 EnterpriseData data,
                                 Map<String, ResourceByExchangeBatch> otherResources,
                                 Integer enterpriseOrganisationUuid) throws Exception {

        org.endeavourhealth.core.xml.enterprise.ReferralRequest model = new org.endeavourhealth.core.xml.enterprise.ReferralRequest();

        if (!mapIdAndMode(resource, model)) {
            return;
        }

        //if it will be passed to Enterprise as an Insert or Update, then transform the remaining fields
        if (model.getSaveMode() == SaveMode.UPSERT) {

            ReferralRequest fhir = (ReferralRequest)deserialiseResouce(resource);

            model.setOrganizationId(enterpriseOrganisationUuid);

            Reference patientReference = fhir.getPatient();
            Integer enterprisePatientUuid = findEnterpriseId(new org.endeavourhealth.core.xml.enterprise.Patient(), patientReference);

            //the test pack has data that refers to deleted or missing patients, so if we get a null
            //patient ID here, then skip this resource
            if (enterprisePatientUuid == null) {
                LOG.warn("Skipping " + fhir.getResourceType() + " " + fhir.getId() + " as no Enterprise patient ID could be found for it");
                return;
            }

            model.setPatientId(enterprisePatientUuid);

            if (fhir.hasEncounter()) {
                Reference encounterReference = (Reference)fhir.getEncounter();
                Integer enterpriseEncounterUuid = findEnterpriseId(new Encounter(), encounterReference);
                model.setEncounterId(enterpriseEncounterUuid);
            }

            if (fhir.hasRequester()) {
                Reference practitionerReference = fhir.getRequester();
                Integer enterprisePractitionerUuid = findEnterpriseId(new org.endeavourhealth.core.xml.enterprise.Practitioner(), practitionerReference);
                model.setPractitionerId(enterprisePractitionerUuid);
            }

            if (fhir.hasDateElement()) {
                DateTimeType dt = fhir.getDateElement();
                model.setClinicalEffectiveDate(convertDate(dt.getValue()));
                model.setDatePrecisionId(convertDatePrecision(dt.getPrecision()));
            }

            //changed where the observation code is stored
            if (fhir.hasServiceRequested()) {
                if (fhir.getServiceRequested().size() > 1) {
                    throw new TransformException("Transform doesn't support referrals with multiple service codes " + fhir.getId());
                }
                Long snomedConceptId = findSnomedConceptId(fhir.getServiceRequested().get(0));
                model.setSnomedConceptId(snomedConceptId);
            }
            /*Long snomedConceptId = findSnomedConceptId(fhir.getType());
            model.setSnomedConceptId(snomedConceptId);*/

            if (fhir.hasRequester()) {
                Reference requesterReference = fhir.getRequester();
                ResourceType resourceType = ReferenceHelper.getResourceType(requesterReference);

                //the requester can be an organisation or practitioner
                if (resourceType == ResourceType.Organization) {

                    Integer enterpriseId = findEnterpriseId(new org.endeavourhealth.core.xml.enterprise.Organization(), requesterReference);
                    model.setRequesterOrganizationId(enterpriseId);

                } else if (resourceType == ResourceType.Practitioner) {

                    Practitioner fhirPractitioner = (Practitioner)findResource(requesterReference, otherResources);
                    Practitioner.PractitionerPractitionerRoleComponent role = fhirPractitioner.getPractitionerRole().get(0);
                    Reference organisationReference = role.getManagingOrganization();
                    Integer enterpriseId = findEnterpriseId(new org.endeavourhealth.core.xml.enterprise.Organization(), organisationReference);
                    if (enterpriseId != null) {
                        model.setRequesterOrganizationId(enterpriseId);
                    }
                }
            }

            if (fhir.hasRecipient()) {
                if (fhir.getRecipient().size() > 1) {
                    throw new TransformException("Cannot handle referral requests with more than one recipient " + fhir.getId());
                }
                Reference recipientReference = fhir.getRecipient().get(0);
                ResourceType resourceType = ReferenceHelper.getResourceType(recipientReference);

                //the recipient can be an organisation or practitioner
                if (resourceType == ResourceType.Organization) {

                    //the EMIS test pack contains referrals that point to recipient organisations that don't exist,
                    //so we need to handle the failure to find the organisation
                    Integer enterpriseId = findEnterpriseId(new org.endeavourhealth.core.xml.enterprise.Organization(), recipientReference);
                    if (enterpriseId != null) {
                        model.setRecipientOrganizationId(enterpriseId);
                    }
                } else if (resourceType == ResourceType.Practitioner) {

                    Practitioner fhirPractitioner = (Practitioner)findResource(recipientReference, otherResources);
                    Practitioner.PractitionerPractitionerRoleComponent role = fhirPractitioner.getPractitionerRole().get(0);
                    Reference organisationReference = role.getManagingOrganization();
                    Integer enterpriseId = findEnterpriseId(new org.endeavourhealth.core.xml.enterprise.Organization(), organisationReference);
                    if (enterpriseId != null) {
                        model.setRecipientOrganizationId(enterpriseId);
                    }
                }
            }

            //base the outgoing flag simply on whether the recipient ID matches the owning ID
            boolean outgoing = model.getRequesterOrganizationId() == model.getOrganizationId();
            model.setOutgoingReferral(outgoing);

            if (fhir.hasPriority()) {
                CodeableConcept codeableConcept = fhir.getPriority();
                if (!Strings.isNullOrEmpty(codeableConcept.getText())) {
                    model.setPriority(codeableConcept.getText());
                } else {
                    for (Coding coding: codeableConcept.getCoding()) {
                        model.setPriority(coding.getDisplay());
                    }
                }
            }
/*
            if (fhir.hasType()) {
                CodeableConcept codeableConcept = fhir.getType();
                if (!Strings.isNullOrEmpty(codeableConcept.getText())) {
                    model.setServiceRequested(codeableConcept.getText());
                } else {
                    for (Coding coding: codeableConcept.getCoding()) {
                        model.setServiceRequested(coding.getDisplay());
                    }
                }
            }
*/
            if (fhir.hasExtension()) {
                for (Extension extension: fhir.getExtension()) {<commit_msg>Fix to protocol checking pipeline component<commit_after>package org.endeavourhealth.transform.enterprise.transforms;

import com.google.common.base.Strings;
import org.endeavourhealth.core.data.ehr.models.ResourceByExchangeBatch;
import org.endeavourhealth.core.xml.enterprise.Encounter;
import org.endeavourhealth.core.xml.enterprise.EnterpriseData;
import org.endeavourhealth.core.xml.enterprise.SaveMode;
import org.endeavourhealth.transform.common.exceptions.TransformException;
import org.endeavourhealth.transform.fhir.FhirExtensionUri;
import org.endeavourhealth.transform.fhir.ReferenceHelper;
import org.hl7.fhir.instance.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class ReferralRequestTransformer extends AbstractTransformer {

    private static final Logger LOG = LoggerFactory.getLogger(ReferralRequestTransformer.class);

    public void transform(ResourceByExchangeBatch resource,
                                 EnterpriseData data,
                                 Map<String, ResourceByExchangeBatch> otherResources,
                                 Integer enterpriseOrganisationUuid) throws Exception {

        org.endeavourhealth.core.xml.enterprise.ReferralRequest model = new org.endeavourhealth.core.xml.enterprise.ReferralRequest();

        if (!mapIdAndMode(resource, model)) {
            return;
        }

        //if it will be passed to Enterprise as an Insert or Update, then transform the remaining fields
        if (model.getSaveMode() == SaveMode.UPSERT) {

            ReferralRequest fhir = (ReferralRequest)deserialiseResouce(resource);

            model.setOrganizationId(enterpriseOrganisationUuid);

            Reference patientReference = fhir.getPatient();
            Integer enterprisePatientUuid = findEnterpriseId(new org.endeavourhealth.core.xml.enterprise.Patient(), patientReference);

            //the test pack has data that refers to deleted or missing patients, so if we get a null
            //patient ID here, then skip this resource
            if (enterprisePatientUuid == null) {
                LOG.warn("Skipping " + fhir.getResourceType() + " " + fhir.getId() + " as no Enterprise patient ID could be found for it");
                return;
            }

            model.setPatientId(enterprisePatientUuid);

            if (fhir.hasEncounter()) {
                Reference encounterReference = (Reference)fhir.getEncounter();
                Integer enterpriseEncounterUuid = findEnterpriseId(new Encounter(), encounterReference);
                model.setEncounterId(enterpriseEncounterUuid);
            }

            if (fhir.hasRequester()) {
                Reference practitionerReference = fhir.getRequester();
                Integer enterprisePractitionerUuid = findEnterpriseId(new org.endeavourhealth.core.xml.enterprise.Practitioner(), practitionerReference);
                model.setPractitionerId(enterprisePractitionerUuid);
            }

            if (fhir.hasDateElement()) {
                DateTimeType dt = fhir.getDateElement();
                model.setClinicalEffectiveDate(convertDate(dt.getValue()));
                model.setDatePrecisionId(convertDatePrecision(dt.getPrecision()));
            }

            //changed where the observation code is stored
            if (fhir.hasServiceRequested()) {
                if (fhir.getServiceRequested().size() > 1) {
                    throw new TransformException("Transform doesn't support referrals with multiple service codes " + fhir.getId());
                }
                Long snomedConceptId = findSnomedConceptId(fhir.getServiceRequested().get(0));
                model.setSnomedConceptId(snomedConceptId);
            }
            /*Long snomedConceptId = findSnomedConceptId(fhir.getType());
            model.setSnomedConceptId(snomedConceptId);*/

            if (fhir.hasRequester()) {
                Reference requesterReference = fhir.getRequester();
                ResourceType resourceType = ReferenceHelper.getResourceType(requesterReference);

                //the requester can be an organisation or practitioner
                if (resourceType == ResourceType.Organization) {

                    Integer enterpriseId = findEnterpriseId(new org.endeavourhealth.core.xml.enterprise.Organization(), requesterReference);
                    model.setRequesterOrganizationId(enterpriseId);

                } else if (resourceType == ResourceType.Practitioner) {

                    Practitioner fhirPractitioner = (Practitioner)findResource(requesterReference, otherResources);
                    Practitioner.PractitionerPractitionerRoleComponent role = fhirPractitioner.getPractitionerRole().get(0);
                    Reference organisationReference = role.getManagingOrganization();
                    Integer enterpriseId = findEnterpriseId(new org.endeavourhealth.core.xml.enterprise.Organization(), organisationReference);
                    if (enterpriseId != null) {
                        model.setRequesterOrganizationId(enterpriseId);
                    }
                }
            }

            if (fhir.hasRecipient()) {
                if (fhir.getRecipient().size() > 1) {
                    throw new TransformException("Cannot handle referral requests with more than one recipient " + fhir.getId());
                }
                Reference recipientReference = fhir.getRecipient().get(0);
                ResourceType resourceType = ReferenceHelper.getResourceType(recipientReference);

                //the recipient can be an organisation or practitioner
                if (resourceType == ResourceType.Organization) {

                    //the EMIS test pack contains referrals that point to recipient organisations that don't exist,
                    //so we need to handle the failure to find the organisation
                    Integer enterpriseId = findEnterpriseId(new org.endeavourhealth.core.xml.enterprise.Organization(), recipientReference);
                    if (enterpriseId != null) {
                        model.setRecipientOrganizationId(enterpriseId);
                    }
                } else if (resourceType == ResourceType.Practitioner) {

                    Practitioner fhirPractitioner = (Practitioner)findResource(recipientReference, otherResources);
                    Practitioner.PractitionerPractitionerRoleComponent role = fhirPractitioner.getPractitionerRole().get(0);
                    Reference organisationReference = role.getManagingOrganization();
                    Integer enterpriseId = findEnterpriseId(new org.endeavourhealth.core.xml.enterprise.Organization(), organisationReference);
                    if (enterpriseId != null) {
                        model.setRecipientOrganizationId(enterpriseId);
                    }
                }
            }

            //base the outgoing flag simply on whether the recipient ID matches the owning ID
            boolean outgoing = model.getRequesterOrganizationId() == model.getOrganizationId();
            model.setOutgoingReferral(outgoing);

            if (fhir.hasPriority()) {
                CodeableConcept codeableConcept = fhir.getPriority();
                if (!Strings.isNullOrEmpty(codeableConcept.getText())) {
                    model.setPriority(codeableConcept.getText());
                } else {
                    for (Coding coding: codeableConcept.getCoding()) {
                        model.setPriority(coding.getDisplay());
                    }
                }
            }

            //to restore
/*
            if (fhir.hasType()) {
                CodeableConcept codeableConcept = fhir.getType();
                if (!Strings.isNullOrEmpty(codeableConcept.getText())) {
                    model.setServiceRequested(codeableConcept.getText());
                } else {
                    for (Coding coding: codeableConcept.getCoding()) {
                        model.setServiceRequested(coding.getDisplay());
                    }
                }
            }
*/
            if (fhir.hasExtension()) {
                for (Extension extension: fhir.getExtension()) {
{'fraction_non_alphanumeric': 0.08246665850972715, 'fraction_numerical': 0.0011011868346996207, 'mean_word_length': 2.218546958062611, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 22}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2503949', 'n_tokens_mistral': 4122, 'n_tokens_neox': 3850, 'n_words': 777}