Search in sources :

Example 31 with MEDICATIONSTATEMENT

use of org.hl7.fhir.r4.model.codesystems.ResourceTypes.MEDICATIONSTATEMENT in project org.hl7.fhir.core by hapifhir.

the class ProfileUtilitiesTests method execute.

public void execute(String[] args) throws FileNotFoundException, IOException, FHIRException {
    System.out.println("loading context");
    context = SimpleWorkerContext.fromPack(Utilities.path(root, "validation.zip"));
    comp = new ProfileComparer(context);
    compare("patient-daf-dafpatient.profile.xml", "patient-qicore-qicore-patient.profile.xml");
    compare("encounter-daf-dafencounter.profile.xml", "encounter-qicore-qicore-encounter.profile.xml");
    compare("substance-daf-dafsubstance.profile.xml", "substance-qicore-qicore-substance.profile.xml");
    compare("medication-daf-dafmedication.profile.xml", "medication-qicore-qicore-medication.profile.xml");
    compare("procedure-daf-dafprocedure.profile.xml", "procedure-qicore-qicore-procedure.profile.xml");
    compare("familymemberhistory-daf-daffamilymemberhistory.profile.xml", "familymemberhistory-qicore-qicore-familymemberhistory.profile.xml");
    compare("immunization-daf-dafimmunization.profile.xml", "immunization-qicore-qicore-immunization.profile.xml");
    compare("condition-daf-dafcondition.profile.xml", "condition-qicore-qicore-condition.profile.xml");
    compare("allergyintolerance-daf-dafallergyintolerance.profile.xml", "allergyintolerance-qicore-qicore-allergyintolerance.profile.xml");
    compare("medicationadministration-daf-dafmedicationadministration.profile.xml", "medicationadministration-qicore-qicore-medicationadministration.profile.xml");
    compare("medicationdispense-daf-dafmedicationdispense.profile.xml", "medicationdispense-qicore-qicore-medicationdispense.profile.xml");
    compare("medicationprescription-daf-dafmedicationprescription.profile.xml", "medicationprescription-qicore-qicore-medicationprescription.profile.xml");
    compare("medicationstatement-daf-dafmedicationstatement.profile.xml", "medicationstatement-qicore-qicore-medicationstatement.profile.xml");
    compare("observation-daf-smokingstatus-dafsmokingstatus.profile.xml", "observation-qicore-qicore-observation.profile.xml");
    compare("observation-daf-vitalsigns-dafvitalsigns.profile.xml", "observation-qicore-qicore-observation.profile.xml");
    // compare("observation-daf-results-dafresultobs.profile.xml", "observation-qicore-qicore-observation.profile.xml");
    // compare("diagnosticorder-daf-dafdiagnosticorder.profile.xml", "diagnosticorder-qicore-qicore-diagnosticorder.profile.xml");
    // compare("diagnosticreport-daf-dafdiagnosticreport.profile.xml", "diagnosticreport-qicore-qicore-diagnosticreport.profile.xml");
    System.out.println("processing output");
    for (ProfileComparison outcome : comp.getComparisons()) {
        if (outcome.getSubset() != null)
            new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path("[tmp]", "intersection-" + outcome.getId() + ".xml")), outcome.getSubset());
        if (outcome.getSuperset() != null)
            new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path("[tmp]", "union-" + outcome.getId() + ".xml")), outcome.getSuperset());
        System.out.println("\r\n" + outcome.getId() + ": Comparison of " + outcome.getLeft().getUrl() + " and " + outcome.getRight().getUrl());
        for (ValidationMessage vm : outcome.getMessages()) if (vm.getLevel() == IssueSeverity.INFORMATION)
            System.out.println(vm.summary());
        for (ValidationMessage vm : outcome.getMessages()) if (vm.getLevel() == IssueSeverity.WARNING)
            System.out.println(vm.summary());
        for (ValidationMessage vm : outcome.getMessages()) if (vm.getLevel() == IssueSeverity.ERROR)
            System.out.println(vm.summary());
        for (ValidationMessage vm : outcome.getMessages()) if (vm.getLevel() == IssueSeverity.FATAL)
            System.out.println(vm.summary());
        System.out.println("done. " + Integer.toString(outcome.getMessages().size()) + " messages");
        System.out.println("=================================================================");
    }
}
Also used : ProfileComparison(org.hl7.fhir.dstu2.utils.ProfileComparer.ProfileComparison) XmlParser(org.hl7.fhir.dstu2.formats.XmlParser) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ProfileComparer(org.hl7.fhir.dstu2.utils.ProfileComparer) FileOutputStream(java.io.FileOutputStream)

Example 32 with MEDICATIONSTATEMENT

use of org.hl7.fhir.r4.model.codesystems.ResourceTypes.MEDICATIONSTATEMENT in project eCRNow by drajer-health.

the class R4ResourcesData method getMedicationStatementData.

public List<MedicationStatement> getMedicationStatementData(FhirContext context, IGenericClient client, LaunchDetails launchDetails, R4FhirData r4FhirData, Encounter encounter, Date start, Date end) {
    logger.trace("Get MedicationStatement Data");
    Bundle bundle = (Bundle) resourceData.getResourceByPatientId(launchDetails, client, context, "MedicationStatement");
    List<MedicationStatement> medStatements = new ArrayList<>();
    List<CodeableConcept> medicationCodes = new ArrayList<>();
    if (bundle != null && bundle.getEntry() != null) {
        // Filter MedicationStatement based on Encounter Reference
        if (encounter != null && !encounter.getIdElement().getValue().isEmpty()) {
            for (BundleEntryComponent entry : bundle.getEntry()) {
                MedicationStatement medStatement = (MedicationStatement) entry.getResource();
                if (!medStatement.getContext().isEmpty() && medStatement.getContext().getReferenceElement().getIdPart().equals(encounter.getIdElement().getIdPart())) {
                    medStatements.add(medStatement);
                    medicationCodes.addAll(findMedicationStatementCodes(medStatement));
                }
            }
        // If Encounter Id is not present using start and end dates to filter
        // MedicationStatement
        } else {
            for (BundleEntryComponent entry : bundle.getEntry()) {
                MedicationStatement medStatement = (MedicationStatement) entry.getResource();
                // Checking If Effective Date is present in MedicationStatement resource
                if (medStatement.hasEffectiveDateTimeType()) {
                    Type effectiveDateTime = medStatement.getEffectiveDateTimeType();
                    Date effDate = effectiveDateTime.dateTimeValue().getValue();
                    if (isResourceWithinDateTime(start, end, effDate)) {
                        medStatements.add(medStatement);
                        medicationCodes.addAll(findMedicationStatementCodes(medStatement));
                    }
                } else // If Effective Date is not present looking for LastUpdatedDate
                {
                    Date lastUpdatedDateTime = medStatement.getMeta().getLastUpdated();
                    if (isResourceWithinDateTime(start, end, lastUpdatedDateTime)) {
                        medStatements.add(medStatement);
                        medicationCodes.addAll(findMedicationStatementCodes(medStatement));
                    }
                }
            }
        }
        r4FhirData.setR4MedicationCodes(medicationCodes);
    }
    logger.info("Filtered MedicationStatement -----------> {}", medStatements.size());
    return medStatements;
}
Also used : BundleEntryComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) ArrayList(java.util.ArrayList) Date(java.util.Date)

Example 33 with MEDICATIONSTATEMENT

use of org.hl7.fhir.r4.model.codesystems.ResourceTypes.MEDICATIONSTATEMENT in project eCRNow by drajer-health.

the class CdaMedicationGenerator method generateMedicationSection.

public static String generateMedicationSection(R4FhirData data, LaunchDetails details) {
    StringBuilder sb = new StringBuilder(2000);
    List<Medication> medList = data.getMedicationList();
    List<MedicationStatement> meds = data.getMedications();
    List<MedicationAdministration> medAdms = data.getMedicationAdministrations();
    List<MedicationRequest> medReqs = getValidMedicationRequests(data, medList);
    if ((meds != null && !meds.isEmpty()) || (medAdms != null && !medAdms.isEmpty()) || (medReqs != null && !medReqs.isEmpty())) {
        // Generate the component and section end tags
        sb.append(CdaGeneratorUtils.getXmlForStartElement(CdaGeneratorConstants.COMP_EL_NAME));
        sb.append(CdaGeneratorUtils.getXmlForStartElement(CdaGeneratorConstants.SECTION_EL_NAME));
        sb.append(CdaGeneratorUtils.getXmlForTemplateId(CdaGeneratorConstants.MED_ADM_SEC_TEMPLATE_ID));
        sb.append(CdaGeneratorUtils.getXmlForTemplateId(CdaGeneratorConstants.MED_ADM_SEC_TEMPLATE_ID, CdaGeneratorConstants.MED_SEC_TEMPLATE_ID_EXT));
        sb.append(CdaGeneratorUtils.getXmlForCD(CdaGeneratorConstants.CODE_EL_NAME, CdaGeneratorConstants.MED_ADM_SEC_CODE, CdaGeneratorConstants.LOINC_CODESYSTEM_OID, CdaGeneratorConstants.LOINC_CODESYSTEM_NAME, CdaGeneratorConstants.MED_ADM_SEC_NAME));
        // add Title
        sb.append(CdaGeneratorUtils.getXmlForText(CdaGeneratorConstants.TITLE_EL_NAME, CdaGeneratorConstants.MED_ADM_SEC_TITLE));
        // add Narrative Text
        sb.append(CdaGeneratorUtils.getXmlForStartElement(CdaGeneratorConstants.TEXT_EL_NAME));
        // Create Table Header.
        List<String> list = new ArrayList<>();
        list.add(CdaGeneratorConstants.MED_TABLE_COL_1_TITLE);
        list.add(CdaGeneratorConstants.MED_TABLE_COL_2_TITLE);
        sb.append(CdaGeneratorUtils.getXmlForTableHeader(list, CdaGeneratorConstants.TABLE_BORDER, CdaGeneratorConstants.TABLE_WIDTH));
        // add Table Body
        sb.append(CdaGeneratorUtils.getXmlForStartElement(CdaGeneratorConstants.TABLE_BODY_EL_NAME));
        // add Body Rows
        int rowNum = 1;
        StringBuilder medEntries = new StringBuilder();
        for (MedicationStatement med : meds) {
            String medDisplayName = CdaGeneratorConstants.UNKNOWN_VALUE;
            if (med.getMedication() != null) {
                medDisplayName = CdaFhirUtilities.getStringForMedicationType(med);
            }
            String dt = null;
            if (med.getEffective() != null) {
                dt = CdaFhirUtilities.getStringForType(med.getEffective());
            }
            Map<String, String> bodyvals = new LinkedHashMap<>();
            bodyvals.put(CdaGeneratorConstants.MED_TABLE_COL_1_BODY_CONTENT, medDisplayName);
            bodyvals.put(CdaGeneratorConstants.MED_TABLE_COL_2_BODY_CONTENT, dt);
            sb.append(CdaGeneratorUtils.addTableRow(bodyvals, rowNum));
            // TODO: ++rowNum or rowNum++
            ++rowNum;
            // Create the Med Entry for the Medication Statement.
            String medstatus = "";
            if (med.getStatus() != null) {
                medstatus = CdaFhirUtilities.getStatusCodeForFhirMedStatusCodes(med.getStatus().toString());
            } else {
                medstatus = COMPLETED;
            }
            Dosage dosage = null;
            if (med.getDosageFirstRep() != null)
                dosage = med.getDosageFirstRep();
            medEntries.append(getEntryForMedication(med.getId(), med.getMedication(), med.getEffective(), medstatus, dosage, details, null, null, CdaGeneratorConstants.MOOD_CODE_DEF, med));
        }
        // Add Medication Administration
        for (MedicationAdministration medAdm : medAdms) {
            String medDisplayName = CdaGeneratorConstants.UNKNOWN_VALUE;
            if (medAdm.getMedication() != null) {
                medDisplayName = CdaFhirUtilities.getStringForMedicationType(medAdm);
            }
            String dt = null;
            if (medAdm.getEffective() != null) {
                dt = CdaFhirUtilities.getStringForType(medAdm.getEffective());
            }
            Map<String, String> bodyvals = new HashMap<>();
            bodyvals.put(CdaGeneratorConstants.MED_TABLE_COL_1_BODY_CONTENT, medDisplayName);
            bodyvals.put(CdaGeneratorConstants.MED_TABLE_COL_2_BODY_CONTENT, dt);
            sb.append(CdaGeneratorUtils.addTableRow(bodyvals, rowNum));
            // TODO: ++rowNum or rowNum++
            ++rowNum;
            // Create the Med Entry for the Medication Statement.
            String medstatus = "";
            if (medAdm.getStatus() != null) {
                medstatus = CdaFhirUtilities.getStatusCodeForFhirMedStatusCodes(medAdm.getStatus());
            } else {
                medstatus = COMPLETED;
            }
            Quantity dose = null;
            if (medAdm.getDosage() != null && medAdm.getDosage().getDose() != null)
                dose = medAdm.getDosage().getDose();
            medEntries.append(getEntryForMedication(medAdm.getId(), medAdm.getMedication(), medAdm.getEffective(), medstatus, null, details, dose, null, CdaGeneratorConstants.MOOD_CODE_DEF, medAdm));
        }
        // Add Medication Requests
        for (MedicationRequest medReq : medReqs) {
            String medDisplayName = CdaGeneratorConstants.UNKNOWN_VALUE;
            if (medReq.getMedication() != null) {
                medDisplayName = CdaFhirUtilities.getStringForMedicationType(medReq);
            }
            DateTimeType startDate = null;
            Dosage dosage = null;
            if (medReq.getDosageInstructionFirstRep() != null && medReq.getDosageInstructionFirstRep().getTiming() != null) {
                dosage = medReq.getDosageInstructionFirstRep();
                Timing t = medReq.getDosageInstructionFirstRep().getTiming();
                if (t != null && t.getRepeat() != null && t.getRepeat().getBoundsPeriod() != null) {
                    startDate = t.getRepeat().getBoundsPeriod().getStartElement();
                }
            }
            String dt = CdaGeneratorConstants.UNKNOWN_VALUE;
            if (startDate != null) {
                dt = CdaFhirUtilities.getDisplayStringForDateTimeType(startDate);
            } else {
                logger.error(" Dosage field does not have a valid period either due to datetime or timezone being null ");
            }
            Map<String, String> bodyvals = new HashMap<>();
            bodyvals.put(CdaGeneratorConstants.MED_TABLE_COL_1_BODY_CONTENT, medDisplayName);
            bodyvals.put(CdaGeneratorConstants.MED_TABLE_COL_2_BODY_CONTENT, dt);
            sb.append(CdaGeneratorUtils.addTableRow(bodyvals, rowNum));
            // TODO: ++rowNum or rowNum++
            ++rowNum;
            // Create the Med Entry for the Medication Request.
            String medstatus = "";
            String moodCode = CdaGeneratorConstants.MOOD_CODE_INT;
            if (medReq.getStatus() != null) {
                medstatus = CdaFhirUtilities.getStatusCodeForFhirMedStatusCodes(medReq.getStatus().toString());
                if (medstatus.equalsIgnoreCase(COMPLETED)) {
                    moodCode = CdaGeneratorConstants.MOOD_CODE_DEF;
                }
            } else {
                medstatus = "active";
            }
            medEntries.append(getEntryForMedication(medReq.getId(), medReq.getMedication(), null, medstatus, dosage, details, null, startDate, moodCode, medReq));
        }
        sb.append(CdaGeneratorUtils.getXmlForEndElement(CdaGeneratorConstants.TABLE_BODY_EL_NAME));
        // End Table.
        sb.append(CdaGeneratorUtils.getXmlForEndElement(CdaGeneratorConstants.TABLE_EL_NAME));
        sb.append(CdaGeneratorUtils.getXmlForEndElement(CdaGeneratorConstants.TEXT_EL_NAME));
        // Add Medication Entries
        sb.append(medEntries);
        // Complete the section end tags.
        sb.append(CdaGeneratorUtils.getXmlForEndElement(CdaGeneratorConstants.SECTION_EL_NAME));
        sb.append(CdaGeneratorUtils.getXmlForEndElement(CdaGeneratorConstants.COMP_EL_NAME));
    } else {
        sb.append(generateEmptyMedications());
    }
    return sb.toString();
}
Also used : MedicationRequest(org.hl7.fhir.r4.model.MedicationRequest) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) Quantity(org.hl7.fhir.r4.model.Quantity) Dosage(org.hl7.fhir.r4.model.Dosage) LinkedHashMap(java.util.LinkedHashMap) DateTimeType(org.hl7.fhir.r4.model.DateTimeType) Medication(org.hl7.fhir.r4.model.Medication) MedicationAdministration(org.hl7.fhir.r4.model.MedicationAdministration) Timing(org.hl7.fhir.r4.model.Timing) MedicationStatement(org.hl7.fhir.r4.model.MedicationStatement)

Example 34 with MEDICATIONSTATEMENT

use of org.hl7.fhir.r4.model.codesystems.ResourceTypes.MEDICATIONSTATEMENT in project eCRNow by drajer-health.

the class CdaFhirUtilities method getStringForMedicationType.

public static String getStringForMedicationType(Resource r) {
    String retVal = CdaGeneratorConstants.UNKNOWN_VALUE;
    if (r instanceof MedicationRequest) {
        logger.debug("Found Med Request ");
        MedicationRequest mr = (MedicationRequest) r;
        if (mr.getMedication() instanceof Reference) {
            logger.debug("Found Med Request.Medication Reference ");
            Reference med = (Reference) mr.getMedication();
            if (med.getReference().startsWith(CdaGeneratorConstants.FHIR_CONTAINED_REFERENCE)) {
                logger.debug("Found Med Request.Medication which is a contained reference");
                // Check contained.
                String refId = med.getReference().substring(1);
                logger.debug("Ref Id {} ", refId);
                if (mr.getContained() != null) {
                    retVal = getStringForMedicationFromContainedResources(mr.getContained(), refId);
                    logger.debug("Return Val = {}", retVal);
                }
            // contained present
            }
            return retVal;
        } else if (mr.getMedication() instanceof CodeableConcept) {
            CodeableConcept cc = (CodeableConcept) mr.getMedication();
            return getStringForType(cc);
        }
    } else if (r instanceof MedicationAdministration) {
        MedicationAdministration medAdminRef = (MedicationAdministration) r;
        if (medAdminRef.getMedication() instanceof Reference) {
            Reference med = (Reference) medAdminRef.getMedication();
            if (med.getReference().startsWith(CdaGeneratorConstants.FHIR_CONTAINED_REFERENCE)) {
                // Check contained.
                String refId = med.getReference().substring(1);
                if (medAdminRef.getContained() != null) {
                    retVal = getStringForMedicationFromContainedResources(medAdminRef.getContained(), refId);
                }
            // contained present
            }
            return retVal;
        } else if (medAdminRef.getMedication() instanceof CodeableConcept) {
            CodeableConcept cc = (CodeableConcept) medAdminRef.getMedication();
            return getStringForType(cc);
        }
    } else if (r instanceof MedicationStatement) {
        MedicationStatement medStmtRef = (MedicationStatement) r;
        if (medStmtRef.getMedication() instanceof Reference) {
            Reference med = (Reference) medStmtRef.getMedication();
            if (med.getReference().startsWith(CdaGeneratorConstants.FHIR_CONTAINED_REFERENCE)) {
                // Check contained.
                String refId = med.getReference().substring(1);
                if (medStmtRef.getContained() != null) {
                    retVal = getStringForMedicationFromContainedResources(medStmtRef.getContained(), refId);
                }
            // contained present
            }
            return retVal;
        } else if (medStmtRef.getMedication() instanceof CodeableConcept) {
            CodeableConcept cc = (CodeableConcept) medStmtRef.getMedication();
            return getStringForType(cc);
        }
    }
    return retVal;
}
Also used : MedicationRequest(org.hl7.fhir.r4.model.MedicationRequest) Reference(org.hl7.fhir.r4.model.Reference) MedicationAdministration(org.hl7.fhir.r4.model.MedicationAdministration) MedicationStatement(org.hl7.fhir.r4.model.MedicationStatement) CodeableConcept(org.hl7.fhir.r4.model.CodeableConcept)

Example 35 with MEDICATIONSTATEMENT

use of org.hl7.fhir.r4.model.codesystems.ResourceTypes.MEDICATIONSTATEMENT in project nia-patient-switching-standard-adaptor by NHSDigital.

the class MedicationRequestOrderMapperTest method When_MappingPrescribeResourceWithNoOptionals_Expect_AllFieldsToBeMappedCorrectly.

@Test
public void When_MappingPrescribeResourceWithNoOptionals_Expect_AllFieldsToBeMappedCorrectly() {
    var medicationStatement = unmarshallMedicationStatement("medicationStatementPrescribeNoOptionals.xml");
    var prescribe = medicationStatement.getComponent().stream().filter(RCMRMT030101UK04Component2::hasEhrSupplyPrescribe).map(RCMRMT030101UK04Component2::getEhrSupplyPrescribe).findFirst();
    when(medicationMapper.extractMedicationReference(any())).thenReturn(Optional.of(new Reference(new IdType(ResourceType.Medication.name(), MEDICATION_ID))));
    assertThat(prescribe.isPresent()).isTrue();
    var medicationRequest = medicationRequestOrderMapper.mapToOrderMedicationRequest(new RCMRMT030101UK04EhrExtract(), medicationStatement, prescribe.get(), PRACTISE_CODE);
    assertCommonValues(medicationRequest);
    medicationRequest.getExtensionsByUrl("https://fhir.nhs.uk/STU3/StructureDefinition/Extension-CareConnect-GPC-PrescriptionType-1").forEach(extension -> assertPrescriptionType(extension, "Repeat"));
    assertThat(medicationRequest.getBasedOnFirstRep().getReferenceElement().getIdPart()).isEqualTo(TEST_ID);
    assertThat(medicationRequest.getNote().size()).isEqualTo(1);
    assertThat(medicationRequest.getDosageInstructionFirstRep().getText()).isEqualTo(TAKE_ONE_DAILY);
    assertThat(medicationRequest.getDispenseRequest().getQuantity().getValue()).isNull();
    assertThat(medicationRequest.getDispenseRequest().getValidityPeriod().getStartElement().getValue()).isEqualTo(DateFormatUtil.parseToDateTimeType(AVAILABILITY_TIME).getValue());
}
Also used : RCMRMT030101UK04EhrExtract(org.hl7.v3.RCMRMT030101UK04EhrExtract) Reference(org.hl7.fhir.dstu3.model.Reference) RCMRMT030101UK04Component2(org.hl7.v3.RCMRMT030101UK04Component2) IdType(org.hl7.fhir.dstu3.model.IdType) Test(org.junit.jupiter.api.Test)

Aggregations

ArrayList (java.util.ArrayList)11 MedicationStatement (org.hl7.fhir.r4.model.MedicationStatement)9 Test (org.junit.jupiter.api.Test)9 Coding (org.hl7.fhir.r4.model.Coding)8 Reference (org.hl7.fhir.dstu3.model.Reference)6 IdType (org.hl7.fhir.dstu3.model.IdType)5 Date (java.util.Date)4 RCMRMT030101UK04Component2 (org.hl7.v3.RCMRMT030101UK04Component2)4 RCMRMT030101UK04EhrExtract (org.hl7.v3.RCMRMT030101UK04EhrExtract)4 DateTimeType (org.hl7.fhir.dstu3.model.DateTimeType)3 Extension (org.hl7.fhir.dstu3.model.Extension)3 Bundle (org.hl7.fhir.r4.model.Bundle)3 UnprocessableEntityException (ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException)2 FileOutputStream (java.io.FileOutputStream)2 List (java.util.List)2 Complex (org.hl7.fhir.dstu2016may.formats.RdfGenerator.Complex)2 MedicationRequest (org.hl7.fhir.dstu3.model.MedicationRequest)2 BundleEntryComponent (org.hl7.fhir.r4.model.Bundle.BundleEntryComponent)2 CodeableConcept (org.hl7.fhir.r4.model.CodeableConcept)2 Condition (org.hl7.fhir.r4.model.Condition)2