Class TransactionBuilder

java.lang.Object
io.github.dizuker.tofhir.TransactionBuilder

public class TransactionBuilder extends Object
Builder for creating FHIR transaction bundles. By default, using the update-as-create approach.
  • Constructor Details

    • TransactionBuilder

      public TransactionBuilder()
      Creates a new TransactionBuilder with the default type of TRANSACTION.
  • Method Details

    • withType

      public TransactionBuilder withType(org.hl7.fhir.r4.model.Bundle.BundleType bundleType)
      Sets the bundle type.
      Parameters:
      bundleType - the type of bundle to build
      Returns:
      this builder instance for chaining
    • failOnDuplicateEntries

      public TransactionBuilder failOnDuplicateEntries()
      Configures the builder to throw an exception if multiple resources with the same ID are added when the transaction is built.
      Returns:
      this builder instance for chaining
    • addEntry

      public TransactionBuilder addEntry(org.hl7.fhir.r4.model.Resource resource)
      Adds a FHIR resource to the transaction bundle.
      Parameters:
      resource - the FHIR resource to add to the bundle
      Returns:
      this builder instance for chaining
    • addEntries

      public TransactionBuilder addEntries(org.hl7.fhir.r4.model.Resource... resources)
      Adds FHIR resources to the transaction bundle.
      Parameters:
      resources - the FHIR resources to add to the bundle
      Returns:
      this builder instance for chaining
    • addEntries

      public TransactionBuilder addEntries(Collection<? extends org.hl7.fhir.r4.model.Resource> resources)
      Adds a collection of FHIR resources to the transaction bundle.
      Parameters:
      resources - the FHIR resources to add to the bundle
      Returns:
      this builder instance for chaining
    • addDeleteEntry

      public TransactionBuilder addDeleteEntry(org.hl7.fhir.r4.model.Reference resource)
      Adds a reference to a resource that should be deleted as part of the transaction.
      Parameters:
      resource - a reference to the resource to delete
      Returns:
      this builder instance for chaining
    • addDeleteEntries

      public TransactionBuilder addDeleteEntries(org.hl7.fhir.r4.model.Reference... resources)
      Adds a list of references to resources that should be deleted as part of the transaction.
      Parameters:
      resources - a list of references to the resources to delete
      Returns:
      this builder instance for chaining
    • withId

      public TransactionBuilder withId(org.hl7.fhir.instance.model.api.IIdType id)
      Sets the ID of the bundle. Takes precedence over `useFirstEntryResourceIdAsBundleId` if both are set.
      Parameters:
      id - the ID to set for the bundle
      Returns:
      this builder instance for chaining
    • withId

      public TransactionBuilder withId(String id)
      Sets the ID of the bundle. Takes precedence over `useFirstEntryResourceIdAsBundleId` if both are set.
      Parameters:
      id - the ID to set for the bundle
      Returns:
      this builder instance for chaining
    • withFullUrlBase

      public TransactionBuilder withFullUrlBase(String baseUrl)
      Sets an absolute base URL used to build each entry's fullUrl as <baseUrl>/<ResourceType>/<id>, making the fullUrl absolute as FHIR requires. Plain ResourceType/id references used elsewhere in the bundle (e.g. in a Provenance.target) still resolve correctly against it, since those match by the tail of a hierarchical fullUrl. The base URL does not need to be a real, dereferenceable server endpoint.
      Parameters:
      baseUrl - an absolute base URL, e.g. https://example.org/fhir (a trailing slash is optional)
      Returns:
      this builder instance for chaining
    • withProvenance

      public TransactionBuilder withProvenance(@NonNull org.hl7.fhir.r4.model.Reference who, @NonNull org.hl7.fhir.r4.model.Reference what)
      Enables the inclusion of a Provenance resource in the transaction bundle, with the specified `Provenance.entity.what` and `Provenance.agent.who` references. If the bundle contains both delete and update/create entries, two Provenance resources wil be included.

      The `Provenance.id` is built from the hash of the `who` and `what` references.

      Parameters:
      who - a reference to the resource that are the source of the transformation. For delete entries, this is automatically set to the resource being deleted instead.
      what - a reference to the agent responsible for the transformation or deletion. This is typically the transformation service itself.
      Returns:
      this builder instance for chaining
    • withProvenance

      public TransactionBuilder withProvenance(@NonNull org.hl7.fhir.r4.model.Device device, @NonNull org.hl7.fhir.r4.model.Reference what)
      Enables the inclusion of a Provenance resource in the transaction bundle, with the specified `Provenance.entity.what` and `Provenance.agent.who` references. If the bundle contains both delete and update/create entries, two Provenance resources wil be included.

      The `Provenance.id` is built from the hash of the `who` and `what` references.

      Parameters:
      device - the Device resource representing the agent responsible for the transformation or deletion. The resource is automatically added to the bundle and referenced in the Provenance.agent.who element.
      what - a reference to the agent responsible for the transformation or deletion. This is typically the transformation service itself.
      Returns:
      this builder instance for chaining
    • build

      public org.hl7.fhir.r4.model.Bundle build()
      Builds and returns a FHIR Bundle with the configured type.
      Returns:
      a new Bundle instance with the configured type
      Throws:
      IllegalArgumentException - if failOnDuplicateEntries is enabled and duplicate resource IDs are found
    • buildWithSeparateProvenance

      public TransactionBuilder.DataAndProvenanceBundles buildWithSeparateProvenance()
      Builds and returns two FHIR Bundles: a data bundle containing only the data resources and delete entries, and a provenance bundle containing the Provenance resource(s) and optionally the Device resource (if configured via withProvenance(Device, Reference)). The provenance bundle always uses Bundle.BundleType.TRANSACTION.
      Returns:
      a TransactionBuilder.DataAndProvenanceBundles containing the data and provenance bundles
      Throws:
      IllegalStateException - if provenance has not been enabled via withProvenance(Reference, Reference) or withProvenance(Device, Reference)
      IllegalArgumentException - if failOnDuplicateEntries is enabled and duplicate resource IDs are found