Merge pull request #2565 from hapifhir/improve-bundle-builder
Enhance BundleBuilder to support Deletes
This commit is contained in:
commit
f842c234eb
|
@ -24,6 +24,7 @@ import ca.uhn.fhir.context.BaseRuntimeChildDefinition;
|
||||||
import ca.uhn.fhir.context.BaseRuntimeElementDefinition;
|
import ca.uhn.fhir.context.BaseRuntimeElementDefinition;
|
||||||
import ca.uhn.fhir.context.FhirContext;
|
import ca.uhn.fhir.context.FhirContext;
|
||||||
import ca.uhn.fhir.context.RuntimeResourceDefinition;
|
import ca.uhn.fhir.context.RuntimeResourceDefinition;
|
||||||
|
import ca.uhn.fhir.model.primitive.IdDt;
|
||||||
import org.apache.commons.lang3.Validate;
|
import org.apache.commons.lang3.Validate;
|
||||||
import org.hl7.fhir.instance.model.api.IBase;
|
import org.hl7.fhir.instance.model.api.IBase;
|
||||||
import org.hl7.fhir.instance.model.api.IBaseBackboneElement;
|
import org.hl7.fhir.instance.model.api.IBaseBackboneElement;
|
||||||
|
@ -194,6 +195,45 @@ public class BundleBuilder {
|
||||||
return new CreateBuilder(request);
|
return new CreateBuilder(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an entry containing a delete (DELETE) request.
|
||||||
|
* Also sets the Bundle.type value to "transaction" if it is not already set.
|
||||||
|
*
|
||||||
|
* Note that the resource is only used to extract its ID and type, and the body of the resource is not included in the entry,
|
||||||
|
*
|
||||||
|
* @param theResource The resource to delete.
|
||||||
|
*/
|
||||||
|
public void addTransactionDeleteEntry(IBaseResource theResource) {
|
||||||
|
String resourceType = myContext.getResourceType(theResource);
|
||||||
|
String idPart = theResource.getIdElement().toUnqualifiedVersionless().getIdPart();
|
||||||
|
addTransactionDeleteEntry(resourceType, idPart);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an entry containing a delete (DELETE) request.
|
||||||
|
* Also sets the Bundle.type value to "transaction" if it is not already set.
|
||||||
|
*
|
||||||
|
* @param theResourceType The type resource to delete.
|
||||||
|
* @param theIdPart the ID of the resource to delete.
|
||||||
|
*/
|
||||||
|
public void addTransactionDeleteEntry(String theResourceType, String theIdPart) {
|
||||||
|
setBundleField("type", "transaction");
|
||||||
|
IBase request = addEntryAndReturnRequest();
|
||||||
|
IdDt idDt = new IdDt(theIdPart);
|
||||||
|
|
||||||
|
// Bundle.entry.request.url
|
||||||
|
IPrimitiveType<?> url = (IPrimitiveType<?>) myContext.getElementDefinition("uri").newInstance();
|
||||||
|
url.setValueAsString(idDt.toUnqualifiedVersionless().withResourceType(theResourceType).getValue());
|
||||||
|
myEntryRequestUrlChild.getMutator().setValue(request, url);
|
||||||
|
|
||||||
|
// Bundle.entry.request.method
|
||||||
|
IPrimitiveType<?> method = (IPrimitiveType<?>) myEntryRequestMethodDef.newInstance(myEntryRequestMethodChild.getInstanceConstructorArguments());
|
||||||
|
method.setValueAsString("DELETE");
|
||||||
|
myEntryRequestMethodChild.getMutator().setValue(request, method);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds an entry for a Collection bundle type
|
* Adds an entry for a Collection bundle type
|
||||||
*/
|
*/
|
||||||
|
@ -251,6 +291,16 @@ public class BundleBuilder {
|
||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IBase addEntryAndReturnRequest() {
|
||||||
|
IBase entry = addEntry();
|
||||||
|
|
||||||
|
// Bundle.entry.request
|
||||||
|
IBase request = myEntryRequestDef.newInstance();
|
||||||
|
myEntryRequestChild.getMutator().setValue(entry, request);
|
||||||
|
return request;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public IBaseBundle getBundle() {
|
public IBaseBundle getBundle() {
|
||||||
return myBundle;
|
return myBundle;
|
||||||
|
|
|
@ -11,5 +11,4 @@ public class PlaceholderTest {
|
||||||
public void testPass() {
|
public void testPass() {
|
||||||
// nothing
|
// nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
---
|
||||||
|
type: fix
|
||||||
|
issue: 2571
|
||||||
|
title: "Added support for deleting resources to BundleBuilder via method `addTransactionDeleteEntry`."
|
|
@ -156,6 +156,36 @@ public class BundleBuilderTest {
|
||||||
assertEquals(Bundle.HTTPVerb.POST, bundle.getEntry().get(0).getRequest().getMethod());
|
assertEquals(Bundle.HTTPVerb.POST, bundle.getEntry().get(0).getRequest().getMethod());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddEntryDelete() {
|
||||||
|
BundleBuilder builder = new BundleBuilder(myFhirContext);
|
||||||
|
|
||||||
|
Patient patient = new Patient();
|
||||||
|
patient.setActive(true);
|
||||||
|
patient.setId("123");
|
||||||
|
builder.addTransactionDeleteEntry(patient);
|
||||||
|
builder.addTransactionDeleteEntry("Patient", "123");
|
||||||
|
Bundle bundle = (Bundle) builder.getBundle();
|
||||||
|
|
||||||
|
ourLog.info("Bundle:\n{}", myFhirContext.newJsonParser().setPrettyPrint(true).encodeResourceToString(bundle));
|
||||||
|
|
||||||
|
assertEquals(Bundle.BundleType.TRANSACTION, bundle.getType());
|
||||||
|
assertEquals(2, bundle.getEntry().size());
|
||||||
|
|
||||||
|
//Check the IBaseresource style entry
|
||||||
|
assertNull(bundle.getEntry().get(0).getResource());
|
||||||
|
assertEquals("Patient/123", bundle.getEntry().get(0).getRequest().getUrl());
|
||||||
|
assertEquals(Bundle.HTTPVerb.DELETE, bundle.getEntry().get(0).getRequest().getMethod());
|
||||||
|
|
||||||
|
//Check the resourcetype + id style entry.
|
||||||
|
assertNull(bundle.getEntry().get(1).getResource());
|
||||||
|
assertEquals("Patient/123", bundle.getEntry().get(1).getRequest().getUrl());
|
||||||
|
assertEquals(Bundle.HTTPVerb.DELETE, bundle.getEntry().get(1).getRequest().getMethod());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAddEntryCreateConditional() {
|
public void testAddEntryCreateConditional() {
|
||||||
BundleBuilder builder = new BundleBuilder(myFhirContext);
|
BundleBuilder builder = new BundleBuilder(myFhirContext);
|
||||||
|
|
Loading…
Reference in New Issue