add addBundleEntry method (#3522)
* add addBundleEntry method * change method to createNewBundleEntryWithSingleField * changes to changelog
This commit is contained in:
parent
c2f30502b0
commit
7dc302b7ef
|
@ -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;
|
||||
|
||||
|
|
|
@ -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."
|
|
@ -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<Bundle.BundleEntryComponent> entries = theBundle.getEntry();
|
||||
for (int i = 0; i < entries.size(); i++) {
|
||||
|
|
Loading…
Reference in New Issue