From a87321a227e4f88b0495e57caa4f84cf700a886b Mon Sep 17 00:00:00 2001 From: JasonRoberts-smile <85363818+JasonRoberts-smile@users.noreply.github.com> Date: Wed, 5 Jul 2023 17:57:19 -0400 Subject: [PATCH] add instantiateBackboneElement method to TerserUtil (#5060) * add instantiateBackboneElement method to TerserUtil * changelog --- .../main/java/ca/uhn/fhir/util/TerserUtil.java | 15 +++++++++++++++ .../6_8_0/5061-instantiate-backbone-element.yaml | 4 ++++ .../java/ca/uhn/fhir/util/TerserUtilTest.java | 9 +++++++++ 3 files changed, 28 insertions(+) create mode 100644 hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_8_0/5061-instantiate-backbone-element.yaml diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/TerserUtil.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/TerserUtil.java index 3666886feb2..f686b4e4c24 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/TerserUtil.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/TerserUtil.java @@ -29,6 +29,7 @@ import ca.uhn.fhir.i18n.Msg; import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.tuple.Triple; import org.hl7.fhir.instance.model.api.IBase; +import org.hl7.fhir.instance.model.api.IBaseBackboneElement; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.slf4j.Logger; @@ -656,6 +657,20 @@ public final class TerserUtil { return (T) def.newInstance(theConstructorParam); } + /** + * Creates a new BackboneElement. + * + * @param theFhirContext Context holding resource definition + * @param theTargetResourceName Name of the resource in the context + * @param theTargetFieldName Name of the backbone element in the resource + * @return Returns a new instance of the element + */ + public static IBaseBackboneElement instantiateBackboneElement(FhirContext theFhirContext, String theTargetResourceName, String theTargetFieldName) { + BaseRuntimeElementDefinition targetParentElementDefinition = theFhirContext.getResourceDefinition(theTargetResourceName); + BaseRuntimeChildDefinition childDefinition = targetParentElementDefinition.getChildByName(theTargetFieldName); + return (IBaseBackboneElement) childDefinition.getChildByName(theTargetFieldName).newInstance(); + } + private static void clear(List values) { if (values == null) { return; diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_8_0/5061-instantiate-backbone-element.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_8_0/5061-instantiate-backbone-element.yaml new file mode 100644 index 00000000000..7043d2a1446 --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_8_0/5061-instantiate-backbone-element.yaml @@ -0,0 +1,4 @@ +--- +type: add +issue: 5061 +title: "Added a utility method to the TerserUtil class to facilitate the creation of new instances of BackboneElement classes." diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/TerserUtilTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/TerserUtilTest.java index 2d5bfbd011e..493f2eb05b9 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/TerserUtilTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/TerserUtilTest.java @@ -2,6 +2,7 @@ package ca.uhn.fhir.util; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.RuntimeResourceDefinition; +import org.hl7.fhir.instance.model.api.IBaseBackboneElement; import org.hl7.fhir.r4.model.Address; import org.hl7.fhir.r4.model.DateTimeType; import org.hl7.fhir.r4.model.DateType; @@ -579,4 +580,12 @@ class TerserUtilTest { assertNotNull(TerserUtil.newResource(ourFhirContext, "Patient", null)); } + @Test + public void testInstantiateBackboneElement() { + IBaseBackboneElement patientContact = TerserUtil.instantiateBackboneElement(ourFhirContext, "Patient", "contact"); + assertNotNull(patientContact); + assertEquals(Patient.ContactComponent.class, patientContact.getClass()); + assertTrue(patientContact.isEmpty()); + } + }