add instantiateBackboneElement method to TerserUtil (#5060)

* add instantiateBackboneElement method to TerserUtil

* changelog
This commit is contained in:
JasonRoberts-smile 2023-07-05 17:57:19 -04:00 committed by GitHub
parent a93d06c25f
commit a87321a227
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 0 deletions

View File

@ -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<IBase> values) {
if (values == null) {
return;

View File

@ -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."

View File

@ -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());
}
}