From 97cfb6de37719c94c24255aaaeb091a521486ff3 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Mon, 20 May 2024 17:00:18 -0400 Subject: [PATCH] Add utility method to convert Bundle into transaction (#5945) * Add utility method to convert Bundle into transaction * Add utility method * Spotless * Build fix --- .../java/ca/uhn/fhir/util/BundleUtil.java | 68 ++++++++++++++++++ ...-utility-transaction-converter-method.yaml | 6 ++ .../ips/generator/IpsGenerationR4Test.java | 46 ++++++------ .../large-patient-everything-4.json.gz | Bin 0 -> 5797 bytes .../uhn/fhir/util/bundle/BundleUtilTest.java | 50 +++++++++++++ 5 files changed, 150 insertions(+), 20 deletions(-) create mode 100644 hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_4_0/5945-add-bundleutil-utility-transaction-converter-method.yaml create mode 100644 hapi-fhir-jpaserver-ips/src/test/resources/large-patient-everything-4.json.gz diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BundleUtil.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BundleUtil.java index 770aa1498c9..f0872b62287 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BundleUtil.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/BundleUtil.java @@ -39,6 +39,7 @@ import ca.uhn.fhir.util.bundle.SearchBundleEntryParts; import com.google.common.collect.Sets; import jakarta.annotation.Nonnull; import jakarta.annotation.Nullable; +import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.tuple.Pair; import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBaseBinary; @@ -59,6 +60,7 @@ import java.util.Set; import java.util.function.Consumer; import java.util.stream.Collectors; +import static org.apache.commons.lang3.StringUtils.defaultString; import static org.apache.commons.lang3.StringUtils.isBlank; import static org.apache.commons.lang3.StringUtils.isNotBlank; import static org.hl7.fhir.instance.model.api.IBaseBundle.LINK_PREV; @@ -67,6 +69,12 @@ import static org.hl7.fhir.instance.model.api.IBaseBundle.LINK_PREV; * Fetch resources from a bundle */ public class BundleUtil { + + /** Non instantiable */ + private BundleUtil() { + // nothing + } + private static final Logger ourLog = LoggerFactory.getLogger(BundleUtil.class); private static final String PREVIOUS = LINK_PREV; @@ -339,6 +347,66 @@ public class BundleUtil { TerserUtil.setField(theContext, "entry", theBundle, retVal.toArray(new IBase[0])); } + /** + * Converts a Bundle containing resources into a FHIR transaction which + * creates/updates the resources. This method does not modify the original + * bundle, but returns a new copy. + *

+ * This method is mostly intended for test scenarios where you have a Bundle + * containing search results or other sourced resources, and want to upload + * these resources to a server using a single FHIR transaction. + *

+ *

+ * The Bundle is converted using the following logic: + *

+ *

+ * + * @param theContext The FhirContext to use with the bundle + * @param theBundle The Bundle to modify. All resources in the Bundle should have an ID. + * @param thePrefixIdsOrNull If not null, all resource IDs and all references in the Bundle will be + * modified to such that their IDs contain the given prefix. For example, for a value + * of "A", the resource "Patient/123" will be changed to be "Patient/A123". If set to + * null, resource IDs are unchanged. + * @since 7.4.0 + */ + public static T convertBundleIntoTransaction( + @Nonnull FhirContext theContext, @Nonnull T theBundle, @Nullable String thePrefixIdsOrNull) { + String prefix = defaultString(thePrefixIdsOrNull); + + BundleBuilder bb = new BundleBuilder(theContext); + + FhirTerser terser = theContext.newTerser(); + List entries = terser.getValues(theBundle, "Bundle.entry"); + for (var entry : entries) { + IBaseResource resource = terser.getSingleValueOrNull(entry, "resource", IBaseResource.class); + if (resource != null) { + Validate.isTrue(resource.getIdElement().hasIdPart(), "Resource in bundle has no ID"); + String newId = theContext.getResourceType(resource) + "/" + prefix + + resource.getIdElement().getIdPart(); + + IBaseResource resourceClone = terser.clone(resource); + resourceClone.setId(newId); + + if (isNotBlank(prefix)) { + for (var ref : terser.getAllResourceReferences(resourceClone)) { + var refElement = ref.getResourceReference().getReferenceElement(); + ref.getResourceReference() + .setReference(refElement.getResourceType() + "/" + prefix + refElement.getIdPart()); + } + } + + bb.addTransactionUpdateEntry(resourceClone); + } + } + + return bb.getBundleTyped(); + } + private static void validatePartsNotNull(LinkedHashSet theDeleteParts) { if (theDeleteParts == null) { throw new IllegalStateException( diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_4_0/5945-add-bundleutil-utility-transaction-converter-method.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_4_0/5945-add-bundleutil-utility-transaction-converter-method.yaml new file mode 100644 index 00000000000..a397012f304 --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_4_0/5945-add-bundleutil-utility-transaction-converter-method.yaml @@ -0,0 +1,6 @@ +--- +type: add +issue: 5945 +title: "A new utility method has been added to `BundleUtil` which converts a FHIR Bundle + containing resources (e.g. a search result bundle) into a FHIR transaction bundle which + could be used to upload those resources to a server." diff --git a/hapi-fhir-jpaserver-ips/src/test/java/ca/uhn/fhir/jpa/ips/generator/IpsGenerationR4Test.java b/hapi-fhir-jpaserver-ips/src/test/java/ca/uhn/fhir/jpa/ips/generator/IpsGenerationR4Test.java index e697ef20b2a..34cf71c0435 100644 --- a/hapi-fhir-jpaserver-ips/src/test/java/ca/uhn/fhir/jpa/ips/generator/IpsGenerationR4Test.java +++ b/hapi-fhir-jpaserver-ips/src/test/java/ca/uhn/fhir/jpa/ips/generator/IpsGenerationR4Test.java @@ -47,6 +47,7 @@ import java.util.List; import java.util.Optional; import java.util.stream.Collectors; +import static ca.uhn.fhir.util.BundleUtil.convertBundleIntoTransaction; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.stringContainsInOrder; @@ -81,11 +82,7 @@ public class IpsGenerationR4Test extends BaseResourceProviderR4Test { @Test public void testGenerateLargePatientSummary() throws IOException { Bundle sourceData = ClasspathUtil.loadCompressedResource(myFhirContext, Bundle.class, "/large-patient-everything.json.gz"); - sourceData.setType(Bundle.BundleType.TRANSACTION); - for (Bundle.BundleEntryComponent nextEntry : sourceData.getEntry()) { - nextEntry.getRequest().setMethod(Bundle.HTTPVerb.PUT); - nextEntry.getRequest().setUrl(nextEntry.getResource().getIdElement().toUnqualifiedVersionless().getValue()); - } + sourceData = convertBundleIntoTransaction(myFhirContext, sourceData, null); Bundle outcome = mySystemDao.transaction(mySrd, sourceData); ourLog.info("Created {} resources", outcome.getEntry().size()); @@ -119,11 +116,7 @@ public class IpsGenerationR4Test extends BaseResourceProviderR4Test { myStorageSettings.setResourceClientIdStrategy(JpaStorageSettings.ClientIdStrategyEnum.ANY); Bundle sourceData = ClasspathUtil.loadCompressedResource(myFhirContext, Bundle.class, "/large-patient-everything-2.json.gz"); - sourceData.setType(Bundle.BundleType.TRANSACTION); - for (Bundle.BundleEntryComponent nextEntry : sourceData.getEntry()) { - nextEntry.getRequest().setMethod(Bundle.HTTPVerb.PUT); - nextEntry.getRequest().setUrl(nextEntry.getResource().getIdElement().toUnqualifiedVersionless().getValue()); - } + sourceData = convertBundleIntoTransaction(myFhirContext, sourceData, null); Bundle outcome = mySystemDao.transaction(mySrd, sourceData); ourLog.info("Created {} resources", outcome.getEntry().size()); @@ -145,11 +138,7 @@ public class IpsGenerationR4Test extends BaseResourceProviderR4Test { myStorageSettings.setResourceClientIdStrategy(JpaStorageSettings.ClientIdStrategyEnum.ANY); Bundle sourceData = ClasspathUtil.loadCompressedResource(myFhirContext, Bundle.class, "/large-patient-everything-3.json.gz"); - sourceData.setType(Bundle.BundleType.TRANSACTION); - for (Bundle.BundleEntryComponent nextEntry : sourceData.getEntry()) { - nextEntry.getRequest().setMethod(Bundle.HTTPVerb.PUT); - nextEntry.getRequest().setUrl(nextEntry.getResource().getIdElement().toUnqualifiedVersionless().getValue()); - } + sourceData = convertBundleIntoTransaction(myFhirContext, sourceData, null); Bundle outcome = mySystemDao.transaction(mySrd, sourceData); ourLog.info("Created {} resources", outcome.getEntry().size()); @@ -166,16 +155,33 @@ public class IpsGenerationR4Test extends BaseResourceProviderR4Test { assertEquals(80, output.getEntry().size()); } + @Test + public void testGenerateLargePatientSummary4() { + Bundle sourceData = ClasspathUtil.loadCompressedResource(myFhirContext, Bundle.class, "/large-patient-everything-4.json.gz"); + sourceData = convertBundleIntoTransaction(myFhirContext, sourceData, "EPD"); + + Bundle outcome = mySystemDao.transaction(mySrd, sourceData); + ourLog.info("Created {} resources", outcome.getEntry().size()); + + Bundle output = myClient + .operation() + .onInstance("Patient/EPD2223") + .named(JpaConstants.OPERATION_SUMMARY) + .withNoParameters(Parameters.class) + .returnResourceType(Bundle.class) + .execute(); + ourLog.info("Output: {}", myFhirContext.newJsonParser().setPrettyPrint(true).encodeResourceToString(output)); + + // Verify + assertEquals(55, output.getEntry().size()); + } + @Test public void testGenerateTinyPatientSummary() throws IOException { myStorageSettings.setResourceClientIdStrategy(JpaStorageSettings.ClientIdStrategyEnum.ANY); Bundle sourceData = ClasspathUtil.loadCompressedResource(myFhirContext, Bundle.class, "/tiny-patient-everything.json.gz"); - sourceData.setType(Bundle.BundleType.TRANSACTION); - for (Bundle.BundleEntryComponent nextEntry : sourceData.getEntry()) { - nextEntry.getRequest().setMethod(Bundle.HTTPVerb.PUT); - nextEntry.getRequest().setUrl(nextEntry.getResource().getIdElement().toUnqualifiedVersionless().getValue()); - } + sourceData = convertBundleIntoTransaction(myFhirContext, sourceData, null); Bundle outcome = mySystemDao.transaction(mySrd, sourceData); ourLog.info("Created {} resources", outcome.getEntry().size()); diff --git a/hapi-fhir-jpaserver-ips/src/test/resources/large-patient-everything-4.json.gz b/hapi-fhir-jpaserver-ips/src/test/resources/large-patient-everything-4.json.gz new file mode 100644 index 0000000000000000000000000000000000000000..cc2fd1e4eaa59c19648a483421c40745d0901f7d GIT binary patch literal 5797 zcmV;W7Fy{aiwFo607_;818iY(XJsvLVRUI_ZgeeWc4cyTbZBXAXDu`?YIARH0PS6C zbK5wU{ha<4EYH?VcWuMQn?x$pvy$xCil33++1Z-HixzE)R7lFUGS&ZmK|LHNF%$QQ z-7(wx;D}g98QW4Y$2V%zkhh~VkMS&aXd7#u#F8Hzae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rTSIb>Tgz7w^Kaj>O4|-Qcj(BHcH)y@SC? zgQvsF&7xc>4hZ%ka3jAd7kr>|z;zfp=`ir|?>pRo+!>Uo!@~Q~q}hiwmV?MxZ_O*B zL5aejnmPz$A_88KH=b|A#rTg^<(wCbdSe->cj2fX|^>uGyUcQ zkqLzr6&P@12%r|>(&L7JYwi;$71Go;!0tYTXrp4mCzgd9Qr*z%G^S$Q{k+H$@V_JA zeOMFVeONExsb8H5YamZ<&oUEV)AsF7o~I4Aw28DnXA(%C1ZC*F0s?Uaq)>)%%b5wA z0Qer{t%^x+6P3qa_Qxi3qj~itTiYhmWaGH0h^HvP&eDE3!)Mnaw|v5bAs{Br<%R7g z`9u*vQ(T$vS!=9mr_Mc>@R!M3_0 zfz^&OfP&5=r&v=b8Zf}Efj&k$6s!qTbJLiR2N@%gZtTCU4hi|>dJ~O#3 zl0OMe)D>Fg<9caA@ajZ&>Bif>(pM+Gczb(0j)LaW1XKZo$hQC^-^m;2Qv`+@*AklI zz{oOVW&>zq58`%(CozydLN`b)Za;&f40K%8G|KpbDiuzKRbzl|dQ z%|16)tf5%BQ|=w{a#WrY8@gvZ150@AfY}D0g^-jDg^|BBIsOoX%S{T za4Of3MewtS{ZcpZTu4Abkwq8rSlcWzHus`L$E#ji^lj!%R)+Iw)W42QpbG zl^h{KVAVQ#r=O+e@zcZ9sYhr3q`MA~@bXz^#i}MbxGU{-lQ5Qp!KAalGo{oOQRlOI zDTRyb=|2Bp??ZpxT<(ky-X2`~4GwbEkin2~4G4u2Q0>NAB{c-Z5~z{86$!~7Gt8bu j6U*3J_bx_*L9|##t0i=+q3eqO`osSNr=%+F-qZj9=0#y{ literal 0 HcmV?d00001 diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/bundle/BundleUtilTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/bundle/BundleUtilTest.java index ccbb3c60080..85d6c4b4a96 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/bundle/BundleUtilTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/bundle/BundleUtilTest.java @@ -680,6 +680,56 @@ public class BundleUtilTest { assertNull(BundleUtil.getBundleTypeEnum(ourCtx, bundle)); } + @Test + public void testConvertBundleIntoTransaction() { + Bundle input = createBundleWithPatientAndObservation(); + + Bundle output = BundleUtil.convertBundleIntoTransaction(ourCtx, input, null); + assertEquals(Bundle.BundleType.TRANSACTION, output.getType()); + assertEquals("Patient/123", output.getEntry().get(0).getFullUrl()); + assertEquals("Patient/123", output.getEntry().get(0).getRequest().getUrl()); + assertEquals(Bundle.HTTPVerb.PUT, output.getEntry().get(0).getRequest().getMethod()); + assertTrue(((Patient) output.getEntry().get(0).getResource()).getActive()); + assertEquals("Observation/456", output.getEntry().get(1).getFullUrl()); + assertEquals("Observation/456", output.getEntry().get(1).getRequest().getUrl()); + assertEquals(Bundle.HTTPVerb.PUT, output.getEntry().get(1).getRequest().getMethod()); + assertEquals("Patient/123", ((Observation)output.getEntry().get(1).getResource()).getSubject().getReference()); + assertEquals(Observation.ObservationStatus.AMENDED, ((Observation)output.getEntry().get(1).getResource()).getStatus()); + } + + @Test + public void testConvertBundleIntoTransaction_WithPrefix() { + Bundle input = createBundleWithPatientAndObservation(); + + Bundle output = BundleUtil.convertBundleIntoTransaction(ourCtx, input, "A"); + assertEquals(Bundle.BundleType.TRANSACTION, output.getType()); + assertEquals("Patient/A123", output.getEntry().get(0).getFullUrl()); + assertEquals("Patient/A123", output.getEntry().get(0).getRequest().getUrl()); + assertEquals(Bundle.HTTPVerb.PUT, output.getEntry().get(0).getRequest().getMethod()); + assertTrue(((Patient) output.getEntry().get(0).getResource()).getActive()); + assertEquals("Observation/A456", output.getEntry().get(1).getFullUrl()); + assertEquals("Observation/A456", output.getEntry().get(1).getRequest().getUrl()); + assertEquals(Bundle.HTTPVerb.PUT, output.getEntry().get(1).getRequest().getMethod()); + assertEquals("Patient/A123", ((Observation)output.getEntry().get(1).getResource()).getSubject().getReference()); + assertEquals(Observation.ObservationStatus.AMENDED, ((Observation)output.getEntry().get(1).getResource()).getStatus()); + } + + private static @Nonnull Bundle createBundleWithPatientAndObservation() { + Bundle input = new Bundle(); + input.setType(Bundle.BundleType.COLLECTION); + Patient patient = new Patient(); + patient.setActive(true); + patient.setId("123"); + input.addEntry().setResource(patient); + Observation observation = new Observation(); + observation.setId("456"); + observation.setStatus(Observation.ObservationStatus.AMENDED); + observation.setSubject(new Reference("Patient/123")); + input.addEntry().setResource(observation); + return input; + } + + @Nonnull private static Bundle withBundle(Resource theResource) { final Bundle bundle = new Bundle();