Search in sources :

Example 41 with Annotation

use of org.hl7.fhir.r4b.model.Annotation in project CRD by HL7-DaVinci.

the class FhirRequestProcessor method addNoteToRequest.

public static IBaseResource addNoteToRequest(IBaseResource request, Annotation note) {
    IBaseResource output = request;
    switch(request.fhirType()) {
        case "DeviceRequest":
            DeviceRequest deviceRequest = ((DeviceRequest) request).copy();
            deviceRequest.addNote(note);
            output = deviceRequest;
            break;
        case "MedicationRequest":
            MedicationRequest medicationRequest = ((MedicationRequest) request).copy();
            medicationRequest.addNote(note);
            output = medicationRequest;
            break;
        case "MedicationDispense":
            MedicationDispense medicationDispense = ((MedicationDispense) request).copy();
            medicationDispense.addNote(note);
            output = medicationDispense;
            break;
        case "ServiceRequest":
            ServiceRequest serviceRequest = ((ServiceRequest) request).copy();
            serviceRequest.addNote(note);
            output = serviceRequest;
            break;
        case "NutritionOrder":
            NutritionOrder nutritionOrder = ((NutritionOrder) request).copy();
            nutritionOrder.addNote(note);
            output = nutritionOrder;
            break;
        case "SupplyRequest":
        case "Appointment":
        case "Encounter":
        default:
            logger.info("Unsupported fhir R4 resource type (" + request.fhirType() + ") when adding note");
            throw new RuntimeException("Unsupported fhir R4 resource type " + request.fhirType());
    }
    return output;
}
Also used : IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource)

Example 42 with Annotation

use of org.hl7.fhir.r4b.model.Annotation in project CRD by HL7-DaVinci.

the class CardBuilder method createSuggestionWithNote.

public static Suggestion createSuggestionWithNote(Card card, IBaseResource request, FhirComponentsT fhirComponents, String label, String description, boolean isRecommended, CoverageGuidance coverageGuidance) {
    Date now = new Date();
    Suggestion requestWithNoteSuggestion = new Suggestion();
    requestWithNoteSuggestion.setLabel(label);
    requestWithNoteSuggestion.setIsRecommended(isRecommended);
    List<Action> actionList = new ArrayList<>();
    // build the Annotation
    Annotation annotation = new Annotation();
    StringType author = new StringType();
    author.setValue(card.getSource().getLabel());
    annotation.setAuthor(author);
    String text = card.getSummary() + ": " + card.getDetail();
    annotation.setText(text);
    // set the date and time to now
    annotation.setTime(now);
    IBaseResource resource = FhirRequestProcessor.addNoteToRequest(request, annotation);
    try {
        // build the Extension with the coverage information
        Extension extension = new Extension();
        extension.setUrl("http://hl7.org/fhir/us/davinci-crd/StructureDefinition/ext-coverage-information");
        Extension coverageInfo = new Extension();
        coverageInfo.setUrl("coverageInfo").setValue(coverageGuidance.getCoding());
        extension.addExtension(coverageInfo);
        Extension coverageExtension = new Extension();
        Reference coverageReference = new Reference();
        // TODO: get the coverage from the prefetch and pass it into here instead of retrieving it from the request
        coverageReference.setReference(FhirRequestProcessor.getCoverageFromRequest(request).getReference());
        coverageExtension.setUrl("coverage").setValue(coverageReference);
        extension.addExtension(coverageExtension);
        Extension date = new Extension();
        date.setUrl("date").setValue(new DateType().setValue(now));
        extension.addExtension(date);
        Extension identifier = new Extension();
        String id = UUID.randomUUID().toString();
        identifier.setUrl("identifier").setValue(new StringType(id));
        extension.addExtension(identifier);
        resource = FhirRequestProcessor.addExtensionToRequest(resource, extension);
    } catch (NoCoverageException e) {
        logger.warn("No Coverage, discrete coverage extension will not be included: " + e.getMessage());
    }
    Action updateAction = new Action(fhirComponents);
    updateAction.setType(Action.TypeEnum.update);
    updateAction.setDescription(description);
    updateAction.setResource(resource);
    actionList.add(updateAction);
    requestWithNoteSuggestion.setActions(actionList);
    return requestWithNoteSuggestion;
}
Also used : NoCoverageException(org.hl7.davinci.endpoint.cdshooks.services.crd.r4.NoCoverageException) Extension(org.hl7.fhir.r4.model.Extension) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource)

Example 43 with Annotation

use of org.hl7.fhir.r4b.model.Annotation in project loinc2hpo by monarch-initiative.

the class ObservationAnalysisFromInterpretation method getHPOforObservation.

@Override
public HpoTermId4LoincTest getHPOforObservation() throws UnsupportedCodingSystemException, AmbiguousResultsFoundException, AnnotationNotFoundException, UnrecognizedCodeException {
    // here we use a map to store the results: since there could be more than one interpretation coding system,
    // we try them all and store the results in a map <external code, result in internal code>
    Map<Code, Code> results = new HashMap<>();
    // get the annotation class for this loinc code
    UniversalLoinc2HPOAnnotation annotationForLoinc = annotationMap.get(this.loincId);
    if (annotationForLoinc == null)
        throw new AnnotationNotFoundException();
    // all interpretation codes in different coding systems. Expect one in most cases.
    Set<Code> interpretationCodes = getInterpretationCodes();
    interpretationCodes.stream().filter(p -> CodeSystemConvertor.getCodeContainer().getCodeSystemMap().containsKey(p.getSystem())).forEach(p -> {
        Code internalCode = null;
        try {
            internalCode = CodeSystemConvertor.convertToInternalCode(p);
            results.put(p, internalCode);
        } catch (InternalCodeNotFoundException e) {
            e.printStackTrace();
        }
    });
    List<Code> distinct = results.values().stream().distinct().collect(Collectors.toList());
    if (distinct.size() == 1) {
        HpoTermId4LoincTest hpoTermId4LoincTest = annotationForLoinc.loincInterpretationToHPO(distinct.get(0));
        if (hpoTermId4LoincTest == null)
            throw new UnrecognizedCodeException();
        return hpoTermId4LoincTest;
    } else {
        throw new AmbiguousResultsFoundException();
    }
}
Also used : java.util(java.util) HpoTermId4LoincTest(org.monarchinitiative.loinc2hpo.loinc.HpoTermId4LoincTest) LoincId(org.monarchinitiative.loinc2hpo.loinc.LoincId) UniversalLoinc2HPOAnnotation(org.monarchinitiative.loinc2hpo.loinc.UniversalLoinc2HPOAnnotation) CodeSystemConvertor(org.monarchinitiative.loinc2hpo.codesystems.CodeSystemConvertor) Loinc2HPOAnnotation(org.monarchinitiative.loinc2hpo.loinc.Loinc2HPOAnnotation) CodeableConcept(org.hl7.fhir.dstu3.model.CodeableConcept) Code(org.monarchinitiative.loinc2hpo.codesystems.Code) org.monarchinitiative.loinc2hpo.exception(org.monarchinitiative.loinc2hpo.exception) Collectors(java.util.stream.Collectors) UniversalLoinc2HPOAnnotation(org.monarchinitiative.loinc2hpo.loinc.UniversalLoinc2HPOAnnotation) HpoTermId4LoincTest(org.monarchinitiative.loinc2hpo.loinc.HpoTermId4LoincTest) Code(org.monarchinitiative.loinc2hpo.codesystems.Code)

Example 44 with Annotation

use of org.hl7.fhir.r4b.model.Annotation in project loinc2hpo by monarch-initiative.

the class ObservationAnalysisFromQnValue method getHPOforObservation.

@Override
public HpoTermId4LoincTest getHPOforObservation() throws ReferenceNotFoundException, AmbiguousReferenceException, UnrecognizedCodeException {
    HpoTermId4LoincTest hpoTermId4LoincTest = null;
    // find applicable reference range
    List<Observation.ObservationReferenceRangeComponent> references = this.references.stream().filter(p -> withinAgeRange(p)).collect(Collectors.toList());
    if (references.size() < 1) {
        throw new ReferenceNotFoundException();
    } else if (references.size() == 1) {
        Observation.ObservationReferenceRangeComponent targetReference = references.get(0);
        double low = targetReference.hasLow() ? targetReference.getLow().getValue().doubleValue() : Double.MIN_VALUE;
        double high = targetReference.hasHigh() ? targetReference.getHigh().getValue().doubleValue() : Double.MAX_VALUE;
        double observed = valueQuantity.getValue().doubleValue();
        Loinc2HPOCodedValue result;
        if (observed < low) {
            result = Loinc2HPOCodedValue.fromCode("L");
        } else if (observed > high) {
            result = Loinc2HPOCodedValue.fromCode("H");
        } else {
            result = Loinc2HPOCodedValue.fromCode("N");
        }
        Code resultCode = Code.getNewCode().setSystem(Loinc2HPOCodedValue.CODESYSTEM).setCode(result.toCode());
        hpoTermId4LoincTest = annotationMap.get(loincId).loincInterpretationToHPO(resultCode);
    } else if (references.size() == 2) {
        // what does it mean with multiple references
        throw new AmbiguousReferenceException();
    } else if (references.size() == 3) {
    // it can happen when there is actually one range but coded in three ranges
    // e.g. normal 20-30
    // in this case, one range ([20, 30]) is sufficient;
    // however, it is written as three ranges: ( , 20) [20, 30] (30, )
    // We should handle this case
    } else {
        throw new AmbiguousReferenceException();
    }
    // if we can still not find an answer, it is probably that we did not have the annotation
    if (hpoTermId4LoincTest == null)
        throw new UnrecognizedCodeException();
    return hpoTermId4LoincTest;
}
Also used : AgeCalculator(org.monarchinitiative.loinc2hpo.util.AgeCalculator) Date(java.util.Date) Loinc2HPOCodedValue(org.monarchinitiative.loinc2hpo.codesystems.Loinc2HPOCodedValue) org.monarchinitiative.loinc2hpo.exception(org.monarchinitiative.loinc2hpo.exception) Collectors(java.util.stream.Collectors) BigDecimal(java.math.BigDecimal) List(java.util.List) HpoTermId4LoincTest(org.monarchinitiative.loinc2hpo.loinc.HpoTermId4LoincTest) LoincId(org.monarchinitiative.loinc2hpo.loinc.LoincId) UniversalLoinc2HPOAnnotation(org.monarchinitiative.loinc2hpo.loinc.UniversalLoinc2HPOAnnotation) Loinc2HPOAnnotation(org.monarchinitiative.loinc2hpo.loinc.Loinc2HPOAnnotation) Year(java.time.Year) LocalDate(java.time.LocalDate) Map(java.util.Map) org.hl7.fhir.dstu3.model(org.hl7.fhir.dstu3.model) FHIRException(org.hl7.fhir.exceptions.FHIRException) Code(org.monarchinitiative.loinc2hpo.codesystems.Code) Loinc2HPOCodedValue(org.monarchinitiative.loinc2hpo.codesystems.Loinc2HPOCodedValue) HpoTermId4LoincTest(org.monarchinitiative.loinc2hpo.loinc.HpoTermId4LoincTest) Code(org.monarchinitiative.loinc2hpo.codesystems.Code)

Example 45 with Annotation

use of org.hl7.fhir.r4b.model.Annotation in project gpconnect-demonstrator by nhsconnect.

the class WarningCodeExtHelper method addWarningCodeExtensions.

/**
 * confidential items are per record but the other two are global values per
 * patient
 *
 * @param warningCodes
 * @param list
 * @param patientRepository
 * @param medicationStatementRepository
 * @param structuredAllergySearch
 */
public static void addWarningCodeExtensions(Set<String> warningCodes, ListResource list, PatientRepository patientRepository, MedicationStatementRepository medicationStatementRepository, StructuredAllergySearch structuredAllergySearch) {
    String NHS = list.getSubject().getIdentifier().getValue();
    PatientEntity patientEntity = patientRepository.findByNhsNumber(NHS);
    List<MedicationStatementEntity> medicationStatements = medicationStatementRepository.findByPatientId(patientEntity.getId());
    for (MedicationStatementEntity medicationStatement : medicationStatements) {
        setFlags(medicationStatement.getWarningCode());
    }
    List<StructuredAllergyIntoleranceEntity> allergies = structuredAllergySearch.getAllergyIntollerence(NHS);
    for (StructuredAllergyIntoleranceEntity allergy : allergies) {
        setFlags(allergy.getWarningCode());
    }
    // check medication_statements for either of the global flags
    if (dataInTransit) {
        warningCodes.add(DATA_IN_TRANSIT);
    }
    if (dataAwaitingFiling) {
        warningCodes.add(DATA_AWAITING_FILING);
    }
    StringBuilder sb = new StringBuilder();
    warningCodes.forEach(warningCode -> {
        if (warningCode != null) {
            String warningCodeDisplay = "";
            Annotation annotation = new Annotation();
            switch(warningCode) {
                case CONFIDENTIAL_ITEMS:
                    warningCodeDisplay = "Confidential Items";
                    // #266
                    annotation.setText(CONFIDENTIAL_ITEMS_NOTE);
                    // list.addNote(annotation);
                    sb.append("\r\n").append(annotation.getText());
                    break;
                case DATA_IN_TRANSIT:
                    warningCodeDisplay = "Data in Transit";
                    Calendar cal = new GregorianCalendar();
                    Date now = new Date();
                    cal.setTime(now);
                    // a week before now
                    cal.add(Calendar.DAY_OF_YEAR, -7);
                    // #266
                    annotation.setText(String.format(DATA_IN_TRANSIT_NOTE, DATE_FORMAT.format(cal.getTime())));
                    // list.addNote(annotation);
                    sb.append("\r\n").append(annotation.getText());
                    break;
                case DATA_AWAITING_FILING:
                    warningCodeDisplay = "Data Awaiting Filing";
                    // #266
                    annotation.setText(DATA_AWAITING_FILING_NOTE);
                    // list.addNote(annotation);
                    sb.append("\r\n").append(annotation.getText());
                    break;
                default:
                    break;
            }
            // #182
            Extension warningExt = new Extension(SystemURL.WARNING_CODE, new CodeType(warningCode));
            list.addExtension(warningExt);
        }
    });
    // cardinality of note 0..1 #266
    if (sb.length() > 0) {
        Annotation annotation = null;
        if (list.getNote().size() > 0) {
            annotation = list.getNote().get(0);
            annotation.setText(annotation.getText());
            annotation.setText(annotation.getText() + sb.toString());
        } else {
            annotation = new Annotation();
            list.addNote(annotation);
            annotation.setText(sb.toString().replaceFirst("^\r\n", ""));
        }
    }
}
Also used : StructuredAllergyIntoleranceEntity(uk.gov.hscic.patient.structuredAllergyIntolerance.StructuredAllergyIntoleranceEntity) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) Annotation(org.hl7.fhir.dstu3.model.Annotation) Date(java.util.Date) PatientEntity(uk.gov.hscic.patient.details.PatientEntity) MedicationStatementEntity(uk.gov.hscic.medication.statement.MedicationStatementEntity) Extension(org.hl7.fhir.dstu3.model.Extension) CodeType(org.hl7.fhir.dstu3.model.CodeType)

Aggregations

NotImplementedException (org.apache.commons.lang3.NotImplementedException)14 Trace (com.newrelic.api.agent.Trace)13 Operation (gov.cms.bfd.server.war.Operation)13 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)12 Annotation (org.hl7.fhir.r4.model.Annotation)12 NoResultException (javax.persistence.NoResultException)11 ResourceNotFoundException (ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException)9 Annotation (org.hl7.fhir.dstu3.model.Annotation)9 Search (ca.uhn.fhir.rest.annotation.Search)8 OffsetLinkBuilder (gov.cms.bfd.server.war.commons.OffsetLinkBuilder)8 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)8 Read (ca.uhn.fhir.rest.annotation.Read)7 Beneficiary (gov.cms.bfd.model.rif.Beneficiary)7 Patient (org.hl7.fhir.dstu3.model.Patient)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 CodeableConcept (org.hl7.fhir.r4.model.CodeableConcept)6 Resource (org.hl7.fhir.r4.model.Resource)6 Test (org.junit.jupiter.api.Test)6