Add BundleUtil utility method (#2356)

This commit is contained in:
James Agnew 2021-02-04 13:13:25 -05:00 committed by GitHub
parent 92669b29da
commit 273a5bbbd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 4 deletions

View File

@ -233,7 +233,7 @@ public class BundleUtil {
* All 3 might be null - That's ok because we still want to know the
* order in the original bundle.
*/
BundleEntryMutator mutator = new BundleEntryMutator(nextEntry, requestChildDef, requestChildContentsDef);
BundleEntryMutator mutator = new BundleEntryMutator(theContext, nextEntry, requestChildDef, requestChildContentsDef, entryChildContentsDef);
ModifiableBundleEntry entry = new ModifiableBundleEntry(new BundleEntryParts(fullUrl, requestType, url, resource, conditionalUrl), mutator);
theProcessor.accept(entry);
}

View File

@ -31,11 +31,15 @@ public class BundleEntryMutator {
private final IBase myEntry;
private final BaseRuntimeChildDefinition myRequestChildDef;
private final BaseRuntimeElementCompositeDefinition<?> myRequestChildContentsDef;
private final FhirContext myFhirContext;
private final BaseRuntimeElementCompositeDefinition<?> myEntryDefinition;
public BundleEntryMutator(IBase theEntry, BaseRuntimeChildDefinition theRequestChildDef, BaseRuntimeElementCompositeDefinition<?> theRequestChildContentsDef) {
public BundleEntryMutator(FhirContext theFhirContext, IBase theEntry, BaseRuntimeChildDefinition theRequestChildDef, BaseRuntimeElementCompositeDefinition<?> theChildContentsDef, BaseRuntimeElementCompositeDefinition<?> theEntryDefinition) {
myFhirContext = theFhirContext;
myEntry = theEntry;
myRequestChildDef = theRequestChildDef;
myRequestChildContentsDef = theRequestChildContentsDef;
myRequestChildContentsDef = theChildContentsDef;
myEntryDefinition = theEntryDefinition;
}
void setRequestUrl(FhirContext theFhirContext, String theRequestUrl) {
@ -45,4 +49,13 @@ public class BundleEntryMutator {
requestUrlChildDef.getMutator().addValue(nextRequest, url);
}
}
@SuppressWarnings("unchecked")
public void setFullUrl(String theFullUrl) {
IPrimitiveType<String> value = (IPrimitiveType<String>) myFhirContext.getElementDefinition("uri").newInstance();
value.setValue(theFullUrl);
BaseRuntimeChildDefinition fullUrlChild = myEntryDefinition.getChildByName("fullUrl");
fullUrlChild.getMutator().setValue(myEntry, value);
}
}

View File

@ -58,4 +58,5 @@ public class BundleEntryParts {
public String getUrl() {
return myUrl;
}
}

View File

@ -51,4 +51,8 @@ public class ModifiableBundleEntry {
public IBaseResource getResource() {
return myBundleEntryParts.getResource();
}
public void setFullUrl(String theFullUrl) {
myBundleEntryMutator.setFullUrl(theFullUrl);
}
}

View File

@ -65,10 +65,23 @@ public class BundleUtilTest {
}
@Test
public void testProcessEntries() {
public void testProcessEntriesSetRequestUrl() {
Bundle bundle = new Bundle();
bundle.setType(Bundle.BundleType.TRANSACTION);
bundle.addEntry().getRequest().setMethod(Bundle.HTTPVerb.POST).setUrl("Patient");
Consumer<ModifiableBundleEntry> consumer = e -> e.setFullUrl("http://hello/Patient/123");
BundleUtil.processEntries(ourCtx, bundle, consumer);
assertEquals("http://hello/Patient/123", bundle.getEntryFirstRep().getFullUrl());
}
@Test
public void testProcessEntriesSetFullUrl() {
Bundle bundle = new Bundle();
bundle.setType(Bundle.BundleType.TRANSACTION);
bundle.addEntry().getRequest().setMethod(Bundle.HTTPVerb.GET).setUrl("Observation");
Consumer<ModifiableBundleEntry> consumer = e -> e.setRequestUrl(ourCtx, e.getRequestUrl() + "?foo=bar");
BundleUtil.processEntries(ourCtx, bundle, consumer);
assertEquals("Observation?foo=bar", bundle.getEntryFirstRep().getRequest().getUrl());