diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BundleBuilder.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BundleBuilder.java index 7e3c568663e..657c3f23cc4 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BundleBuilder.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BundleBuilder.java @@ -24,6 +24,7 @@ import ca.uhn.fhir.context.BaseRuntimeChildDefinition; import ca.uhn.fhir.context.BaseRuntimeElementDefinition; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.RuntimeResourceDefinition; +import ca.uhn.fhir.model.primitive.IdDt; import org.apache.commons.lang3.Validate; import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseBackboneElement; @@ -194,6 +195,45 @@ public class BundleBuilder { 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 */ @@ -251,6 +291,16 @@ public class BundleBuilder { 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() { return myBundle; diff --git a/hapi-fhir-base/src/test/java/ca/uhn/fhir/validation/PlaceholderTest.java b/hapi-fhir-base/src/test/java/ca/uhn/fhir/validation/PlaceholderTest.java index f757cc2b801..5d5b2820135 100644 --- a/hapi-fhir-base/src/test/java/ca/uhn/fhir/validation/PlaceholderTest.java +++ b/hapi-fhir-base/src/test/java/ca/uhn/fhir/validation/PlaceholderTest.java @@ -11,5 +11,4 @@ public class PlaceholderTest { public void testPass() { // nothing } - } diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/5_4_0/2571-add-delete-to-bundle-builder.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/5_4_0/2571-add-delete-to-bundle-builder.yaml new file mode 100644 index 00000000000..6da48fc802c --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/5_4_0/2571-add-delete-to-bundle-builder.yaml @@ -0,0 +1,4 @@ +--- +type: fix +issue: 2571 +title: "Added support for deleting resources to BundleBuilder via method `addTransactionDeleteEntry`." diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/BundleBuilderTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/BundleBuilderTest.java index 22bf634d525..e31bbc9fbd0 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/BundleBuilderTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/BundleBuilderTest.java @@ -156,6 +156,36 @@ public class BundleBuilderTest { 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 public void testAddEntryCreateConditional() { BundleBuilder builder = new BundleBuilder(myFhirContext);