diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BundleUtil.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BundleUtil.java index bdff8788835..c50ae64184c 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BundleUtil.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BundleUtil.java @@ -551,6 +551,33 @@ public class BundleUtil { return isPatch; } + + /** + * create a new bundle entry and set a value for a single field + * @param theContext Context holding resource definition + * @param theFieldName Child field name of the bundle entry to set + * @param theValues The values to set on the bundle entry child field name + * @return the new bundle entry + */ + public static IBase createNewBundleEntryWithSingleField(FhirContext theContext, String theFieldName, IBase... theValues) { + IBaseBundle newBundle = TerserUtil.newResource(theContext, "Bundle"); + BaseRuntimeChildDefinition entryChildDef = theContext.getResourceDefinition(newBundle).getChildByName("entry"); + + BaseRuntimeElementCompositeDefinition entryChildElem = (BaseRuntimeElementCompositeDefinition) entryChildDef.getChildByName("entry"); + BaseRuntimeChildDefinition resourceChild = entryChildElem.getChildByName(theFieldName); + IBase bundleEntry = entryChildElem.newInstance(); + for (IBase value : theValues) { + try { + resourceChild.getMutator().addValue(bundleEntry, value); + } catch (UnsupportedOperationException e) { + ourLog.warn("Resource {} does not support multiple values, but an attempt to set {} was made. Setting the first item only", bundleEntry, theValues); + resourceChild.getMutator().setValue(bundleEntry, value); + break; + } + } + return bundleEntry; + } + private static class SortLegality { private boolean myIsLegal; diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_0_0/3521-add-a-helper-method-for-adding-a-new-entry-to-a-bundle.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_0_0/3521-add-a-helper-method-for-adding-a-new-entry-to-a-bundle.yaml new file mode 100644 index 00000000000..bea0e4feb36 --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_0_0/3521-add-a-helper-method-for-adding-a-new-entry-to-a-bundle.yaml @@ -0,0 +1,3 @@ +type: add +issue: 3521 +title: "Added a helper method to BundleUtil, to support the creation of a new bundle entry and to set the value for one of the fields." diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/bundle/BundleUtilTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/bundle/BundleUtilTest.java index df63cf5829d..c8093e0fadd 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/bundle/BundleUtilTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/bundle/BundleUtilTest.java @@ -5,7 +5,7 @@ import ca.uhn.fhir.model.valueset.BundleEntrySearchModeEnum; import ca.uhn.fhir.util.BundleBuilder; import ca.uhn.fhir.util.BundleUtil; import ca.uhn.fhir.util.TestUtil; -import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.r4.model.Bundle; import org.hl7.fhir.r4.model.ExplanationOfBenefit; import org.hl7.fhir.r4.model.Medication; @@ -14,6 +14,7 @@ import org.hl7.fhir.r4.model.Organization; import org.hl7.fhir.r4.model.Patient; import org.hl7.fhir.r4.model.Quantity; import org.hl7.fhir.r4.model.Reference; +import org.hl7.fhir.r4.model.UriType; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Test; @@ -399,6 +400,18 @@ public class BundleUtilTest { assertTrue(observationIndex < patientIndex); } + @Test + public void testCreateNewBundleEntryWithSingleField() { + Patient pat1 = new Patient(); + pat1.setId("Patient/P1"); + IBase bundleEntry = BundleUtil.createNewBundleEntryWithSingleField(ourCtx,"resource", pat1); + assertThat(((Bundle.BundleEntryComponent)bundleEntry).getResource().getIdElement().getValue(), is(equalTo(pat1.getId()))); + + UriType testUri = new UriType("http://foo"); + bundleEntry = BundleUtil.createNewBundleEntryWithSingleField(ourCtx,"fullUrl", testUri); + assertThat(((Bundle.BundleEntryComponent)bundleEntry).getFullUrl(), is(equalTo(testUri.getValue()))); + } + private int getIndexOfEntryWithId(String theResourceId, Bundle theBundle) { List entries = theBundle.getEntry(); for (int i = 0; i < entries.size(); i++) {