Class TransactionBuilder
java.lang.Object
io.github.dizuker.tofhir.TransactionBuilder
Builder for creating FHIR transaction bundles. By default, using the update-as-create approach.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordA pair of FHIR Bundles: one containing data resources and one containing provenance resources. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new TransactionBuilder with the default type of TRANSACTION. -
Method Summary
Modifier and TypeMethodDescriptionaddDeleteEntries(org.hl7.fhir.r4.model.Reference... resources) Adds a list of references to resources that should be deleted as part of the transaction.addDeleteEntry(org.hl7.fhir.r4.model.Reference resource) Adds a reference to a resource that should be deleted as part of the transaction.addEntries(Collection<? extends org.hl7.fhir.r4.model.Resource> resources) Adds a collection of FHIR resources to the transaction bundle.addEntries(org.hl7.fhir.r4.model.Resource... resources) Adds FHIR resources to the transaction bundle.addEntry(org.hl7.fhir.r4.model.Resource resource) Adds a FHIR resource to the transaction bundle.org.hl7.fhir.r4.model.Bundlebuild()Builds and returns a FHIR Bundle with the configured type.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 viawithProvenance(Device, Reference)).Configures the builder to throw an exception if multiple resources with the same ID are added when the transaction is built.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.Sets the ID of the bundle.withId(org.hl7.fhir.instance.model.api.IIdType id) Sets the ID of the bundle.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.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.withType(org.hl7.fhir.r4.model.Bundle.BundleType bundleType) Sets the bundle type.
-
Constructor Details
-
TransactionBuilder
public TransactionBuilder()Creates a new TransactionBuilder with the default type of TRANSACTION.
-
-
Method Details
-
withType
Sets the bundle type.- Parameters:
bundleType- the type of bundle to build- Returns:
- this builder instance for chaining
-
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
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
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
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
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
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
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
Sets an absolute base URL used to build each entry's fullUrl as<baseUrl>/<ResourceType>/<id>, making the fullUrl absolute as FHIR requires. PlainResourceType/idreferences 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
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 viawithProvenance(Device, Reference)). The provenance bundle always usesBundle.BundleType.TRANSACTION.- Returns:
- a
TransactionBuilder.DataAndProvenanceBundlescontaining the data and provenance bundles - Throws:
IllegalStateException- if provenance has not been enabled viawithProvenance(Reference, Reference)orwithProvenance(Device, Reference)IllegalArgumentException- if failOnDuplicateEntries is enabled and duplicate resource IDs are found
-