Search in sources :

Example 16 with MedicationAdministration

use of org.hl7.fhir.r4.model.MedicationAdministration in project org.hl7.fhir.core by hapifhir.

the class RdfParser method composeMedicationAdministration.

protected void composeMedicationAdministration(Complex parent, String parentType, String name, MedicationAdministration element, int index) {
    if (element == null)
        return;
    Complex t;
    if (Utilities.noString(parentType))
        t = parent;
    else {
        t = parent.predicate("fhir:" + parentType + '.' + name);
    }
    composeDomainResource(t, "MedicationAdministration", name, element, index);
    for (int i = 0; i < element.getIdentifier().size(); i++) composeIdentifier(t, "MedicationAdministration", "identifier", element.getIdentifier().get(i), i);
    for (int i = 0; i < element.getInstantiates().size(); i++) composeUri(t, "MedicationAdministration", "instantiates", element.getInstantiates().get(i), i);
    for (int i = 0; i < element.getPartOf().size(); i++) composeReference(t, "MedicationAdministration", "partOf", element.getPartOf().get(i), i);
    if (element.hasStatusElement())
        composeEnum(t, "MedicationAdministration", "status", element.getStatusElement(), -1);
    for (int i = 0; i < element.getStatusReason().size(); i++) composeCodeableConcept(t, "MedicationAdministration", "statusReason", element.getStatusReason().get(i), i);
    if (element.hasCategory())
        composeCodeableConcept(t, "MedicationAdministration", "category", element.getCategory(), -1);
    if (element.hasMedication())
        composeType(t, "MedicationAdministration", "medication", element.getMedication(), -1);
    if (element.hasSubject())
        composeReference(t, "MedicationAdministration", "subject", element.getSubject(), -1);
    if (element.hasContext())
        composeReference(t, "MedicationAdministration", "context", element.getContext(), -1);
    for (int i = 0; i < element.getSupportingInformation().size(); i++) composeReference(t, "MedicationAdministration", "supportingInformation", element.getSupportingInformation().get(i), i);
    if (element.hasEffective())
        composeType(t, "MedicationAdministration", "effective", element.getEffective(), -1);
    for (int i = 0; i < element.getPerformer().size(); i++) composeMedicationAdministrationMedicationAdministrationPerformerComponent(t, "MedicationAdministration", "performer", element.getPerformer().get(i), i);
    for (int i = 0; i < element.getReasonCode().size(); i++) composeCodeableConcept(t, "MedicationAdministration", "reasonCode", element.getReasonCode().get(i), i);
    for (int i = 0; i < element.getReasonReference().size(); i++) composeReference(t, "MedicationAdministration", "reasonReference", element.getReasonReference().get(i), i);
    if (element.hasRequest())
        composeReference(t, "MedicationAdministration", "request", element.getRequest(), -1);
    for (int i = 0; i < element.getDevice().size(); i++) composeReference(t, "MedicationAdministration", "device", element.getDevice().get(i), i);
    for (int i = 0; i < element.getNote().size(); i++) composeAnnotation(t, "MedicationAdministration", "note", element.getNote().get(i), i);
    if (element.hasDosage())
        composeMedicationAdministrationMedicationAdministrationDosageComponent(t, "MedicationAdministration", "dosage", element.getDosage(), -1);
    for (int i = 0; i < element.getEventHistory().size(); i++) composeReference(t, "MedicationAdministration", "eventHistory", element.getEventHistory().get(i), i);
}
Also used : Complex(org.hl7.fhir.r4.utils.formats.Turtle.Complex)

Example 17 with MedicationAdministration

use of org.hl7.fhir.r4.model.MedicationAdministration in project eCRNow by drajer-health.

the class R4ResourcesData method getMedicationAdministrationData.

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

Example 18 with MedicationAdministration

use of org.hl7.fhir.r4.model.MedicationAdministration in project eCRNow by drajer-health.

the class R4ResourcesData method loadMedicationsData.

public void loadMedicationsData(FhirContext context, IGenericClient client, LaunchDetails launchDetails, R4FhirData r4FhirData, Encounter encounter, Bundle bundle, Date start, Date end) {
    // also to the list of medicationCodes.
    try {
        List<MedicationAdministration> medAdministrationsList = getMedicationAdministrationData(context, client, launchDetails, r4FhirData, encounter, start, end);
        if (medAdministrationsList != null && !medAdministrationsList.isEmpty()) {
            List<Medication> medicationList = r4FhirData.getMedicationList();
            r4FhirData.setMedicationAdministrations(medAdministrationsList);
            for (MedicationAdministration medAdministration : medAdministrationsList) {
                if (medAdministration.getMedication() != null && !medAdministration.getMedication().isEmpty() && medAdministration.getMedication() instanceof Reference) {
                    Reference medRef = (Reference) medAdministration.getMedication();
                    String medReference = medRef.getReferenceElement().getValue();
                    if (medReference.startsWith("#")) {
                        List<Resource> medAdministrationContained = medAdministration.getContained();
                        if (medAdministrationContained.stream().anyMatch(resource -> resource.getIdElement().getValue().equals(medReference))) {
                            logger.debug("Medication Resource {} exists in MedicationAdministration.contained, So no need to add again in Bundle.", medReference);
                        }
                    } else {
                        logger.debug("Medication Reference Found=============> {}", medReference);
                        Medication medication = getMedicationData(context, client, launchDetails, r4FhirData, medReference);
                        if (medication != null) {
                            BundleEntryComponent medicationEntry = new BundleEntryComponent().setResource(medication);
                            bundle.addEntry(medicationEntry);
                            medicationList.add(medication);
                        }
                    }
                }
                BundleEntryComponent medAdministrationEntry = new BundleEntryComponent().setResource(medAdministration);
                bundle.addEntry(medAdministrationEntry);
            }
            r4FhirData.setMedicationList(medicationList);
        }
    } catch (Exception e) {
        logger.error("Error in getting the MedicationAdministration Data", e);
    }
    try {
        List<MedicationRequest> medRequestsList = getMedicationRequestData(context, client, launchDetails, r4FhirData, encounter, start, end);
        if (medRequestsList != null && !medRequestsList.isEmpty()) {
            List<Medication> medicationList = r4FhirData.getMedicationList();
            r4FhirData.setMedicationRequests(medRequestsList);
            for (MedicationRequest medRequest : medRequestsList) {
                if (medRequest.getMedication() != null && !medRequest.getMedication().isEmpty() && medRequest.getMedication() instanceof Reference) {
                    Reference medRef = (Reference) medRequest.getMedication();
                    String medReference = medRef.getReferenceElement().getValue();
                    if (medReference.startsWith("#")) {
                        List<Resource> medRequestContained = medRequest.getContained();
                        if (medRequestContained.stream().anyMatch(resource -> resource.getIdElement().getValue().equals(medReference))) {
                            logger.debug("Medication Resource {} exists in MedicationRequest.contained, So no need to add again in Bundle.", medReference);
                        }
                    } else {
                        logger.debug("Medication Reference Found=============> {}", medReference);
                        Medication medication = getMedicationData(context, client, launchDetails, r4FhirData, medReference);
                        if (medication != null) {
                            BundleEntryComponent medicationEntry = new BundleEntryComponent().setResource(medication);
                            bundle.addEntry(medicationEntry);
                            medicationList.add(medication);
                        }
                    }
                }
                BundleEntryComponent medRequestEntry = new BundleEntryComponent().setResource(medRequest);
                bundle.addEntry(medRequestEntry);
            }
            r4FhirData.setMedicationList(medicationList);
        }
    } catch (Exception e) {
        logger.error("Error in getting the MedicationRequest Data", e);
    }
}
Also used : BundleEntryComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) ResourceNotFoundException(ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException)

Example 19 with MedicationAdministration

use of org.hl7.fhir.r4.model.MedicationAdministration 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 20 with MedicationAdministration

use of org.hl7.fhir.r4.model.MedicationAdministration 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)

Aggregations

ArrayList (java.util.ArrayList)6 Date (java.util.Date)6 BundleEntryComponent (org.hl7.fhir.r4.model.Bundle.BundleEntryComponent)5 JsonElement (com.google.gson.JsonElement)4 JsonObject (com.google.gson.JsonObject)4 Code (org.mitre.synthea.world.concepts.HealthRecord.Code)4 Reference (org.hl7.fhir.dstu3.model.Reference)3 MedicationAdministration (org.hl7.fhir.r4.model.MedicationAdministration)3 MedicationRequest (org.hl7.fhir.r4.model.MedicationRequest)3 Quantity (org.hl7.fhir.r4.model.Quantity)3 Reference (org.hl7.fhir.r4.model.Reference)3 Complex (org.hl7.fhir.r4.utils.formats.Turtle.Complex)3 FileOutputStream (java.io.FileOutputStream)2 Complex (org.hl7.fhir.dstu2016may.formats.RdfGenerator.Complex)2 BundleEntryComponent (org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent)2 Coding (org.hl7.fhir.dstu3.model.Coding)2 Condition (org.hl7.fhir.dstu3.model.Condition)2 MedicationAdministration (org.hl7.fhir.dstu3.model.MedicationAdministration)2 Quantity (org.hl7.fhir.dstu3.model.Quantity)2 SimpleQuantity (org.hl7.fhir.dstu3.model.SimpleQuantity)2