Skip to content

Commit

Permalink
added new narrative generator utility method
Browse files Browse the repository at this point in the history
  • Loading branch information
davidraeside committed Jan 9, 2025
1 parent 544f832 commit f2e28db
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.util.BundleUtil;
import ca.uhn.fhir.util.TerserUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.hl7.fhir.instance.model.api.IBase;
import org.hl7.fhir.instance.model.api.IBaseBundle;
import org.hl7.fhir.instance.model.api.IBaseCoding;
import org.hl7.fhir.instance.model.api.IBaseResource;

import java.util.List;
Expand Down Expand Up @@ -52,4 +56,34 @@ public boolean bundleHasEntriesWithResourceType(IBaseBundle theBaseBundle, Strin
.filter(Objects::nonNull)
.anyMatch(t -> ctx.getResourceType(t).equals(theResourceType));
}

/**
* Returns if the bundle contains a resource that has a `code` property that contains a matching code system and code.
*
* @param theBundle the bundle to inspect
* @param theResourceType the resource type to look for
* @param theCodeSystem the code system to find
* @param theCode the code to find
* @return returns true if bundle has a resource that with matching code/code system
*/
public boolean bundleHasEntriesWithCode(
IBaseBundle theBundle, String theResourceType, String theCodeSystem, String theCode) {
FhirVersionEnum fhirVersionEnum = theBundle.getStructureFhirVersionEnum();
FhirContext ctx = FhirContext.forCached(fhirVersionEnum);

List<Pair<String, IBaseResource>> entryResources = BundleUtil.getBundleEntryUrlsAndResources(ctx, theBundle);

return entryResources.stream()
.map(Pair::getValue)
.filter(Objects::nonNull)
.filter(t -> ctx.getResourceType(t).equals(theResourceType))
.anyMatch(t -> {
List<IBase> codeList = TerserUtil.getFieldByFhirPath(ctx, "code.coding", t);
return codeList.stream().anyMatch(m -> {
IBaseCoding coding = (IBaseCoding) m;
return StringUtils.equals(coding.getSystem(), theCodeSystem)
&& StringUtils.equals(coding.getCode(), theCode);
});
});
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package ca.uhn.fhir.narrative2;

import org.hl7.fhir.dstu3.model.Bundle;
import org.hl7.fhir.dstu3.model.CodeableConcept;
import org.hl7.fhir.dstu3.model.Coding;
import org.hl7.fhir.dstu3.model.Medication;
import org.hl7.fhir.dstu3.model.Observation;
import org.hl7.fhir.dstu3.model.Patient;
import org.junit.jupiter.api.Test;

Expand All @@ -24,5 +27,57 @@ public void testBundleHasEntriesWithResourceType_False() {
assertFalse(NarrativeGeneratorTemplateUtils.INSTANCE.bundleHasEntriesWithResourceType(bundle, "Patient"));
}

@Test
public void testResourcesHaveCodeValue_isTrue() {
Bundle bundle = new Bundle();
bundle.addEntry().setResource(new Observation().setCode(
new CodeableConcept(
new Coding("http://loinc.org", "123", ""))));
bundle.addEntry().setResource(new Observation().setCode(
new CodeableConcept(
new Coding("http://loinc.org", "456", ""))));

assertTrue(NarrativeGeneratorTemplateUtils.INSTANCE
.bundleHasEntriesWithCode(bundle, "Observation", "http://loinc.org", "123"));
}

@Test
public void testResourcesHaveCodeValue_isTrueWhenOneCodeMatches() {
Bundle bundle = new Bundle();
bundle.addEntry().setResource(new Observation().setCode(
new CodeableConcept()
.addCoding(new Coding("http://loinc.org", "abc", ""))
.addCoding(new Coding("http://loinc.org", "123", ""))
));
bundle.addEntry().setResource(new Observation().setCode(
new CodeableConcept(
new Coding("http://loinc.org", "456", ""))));

assertTrue(NarrativeGeneratorTemplateUtils.INSTANCE
.bundleHasEntriesWithCode(bundle, "Observation", "http://loinc.org", "123"));
}

@Test
public void testResourcesHaveCodeValue_isFalse() {
Bundle bundle = new Bundle();
bundle.addEntry().setResource(new Observation().setCode(
new CodeableConcept(
new Coding("http://loinc.org", "123", ""))));
bundle.addEntry().setResource(new Observation().setCode(
new CodeableConcept(
new Coding("http://loinc.org", "456", ""))));

assertFalse(NarrativeGeneratorTemplateUtils.INSTANCE
.bundleHasEntriesWithCode(bundle, "Observation", "http://loinc.org", "789"));
}

@Test
public void testResourcesHaveCodeValue_isFalseWhenNoResourcePresent() {
Bundle bundle = new Bundle();
bundle.addEntry().setResource(new Patient().setActive(true));
bundle.addEntry().setResource(new Medication().setIsBrand(true));

assertFalse(NarrativeGeneratorTemplateUtils.INSTANCE
.bundleHasEntriesWithCode(bundle, "Observation", "http://loinc.org", "789"));
}
}

0 comments on commit f2e28db

Please sign in to comment.