forked from hapifhir/hapi-fhir
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add transactionbuilder (hapifhir#2041)
* Add transactionbuilder * Add changelog * Add creates
- Loading branch information
1 parent
5a86770
commit 89339b1
Showing
6 changed files
with
299 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
hapi-fhir-docs/src/main/java/ca/uhn/hapi/fhir/docs/TransactionBuilderExamples.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package ca.uhn.hapi.fhir.docs; | ||
|
||
/*- | ||
* #%L | ||
* HAPI FHIR - Docs | ||
* %% | ||
* Copyright (C) 2014 - 2020 University Health Network | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
import ca.uhn.fhir.context.FhirContext; | ||
import ca.uhn.fhir.rest.client.api.IGenericClient; | ||
import ca.uhn.fhir.util.TransactionBuilder; | ||
import org.hl7.fhir.exceptions.FHIRException; | ||
import org.hl7.fhir.instance.model.api.IBaseBundle; | ||
import org.hl7.fhir.r4.model.Patient; | ||
|
||
@SuppressWarnings("unused") | ||
public class TransactionBuilderExamples { | ||
|
||
private FhirContext myFhirContext; | ||
private IGenericClient myFhirClient; | ||
|
||
public void update() throws FHIRException { | ||
//START SNIPPET: update | ||
// Create a TransactionBuilder | ||
TransactionBuilder builder = new TransactionBuilder(myFhirContext); | ||
|
||
// Create a Patient to update | ||
Patient patient = new Patient(); | ||
patient.setId("http://foo/Patient/123"); | ||
patient.setActive(true); | ||
|
||
// Add the patient as an update (aka PUT) to the Bundle | ||
builder.addUpdateEntry(patient); | ||
|
||
// Execute the transaction | ||
IBaseBundle outcome = myFhirClient.transaction().withBundle(builder.getBundle()).execute(); | ||
//END SNIPPET: update | ||
} | ||
|
||
public void updateConditional() throws FHIRException { | ||
//START SNIPPET: updateConditional | ||
// Create a TransactionBuilder | ||
TransactionBuilder builder = new TransactionBuilder(myFhirContext); | ||
|
||
// Create a Patient to update | ||
Patient patient = new Patient(); | ||
patient.setActive(true); | ||
patient.addIdentifier().setSystem("http://foo").setValue("bar"); | ||
|
||
// Add the patient as an update (aka PUT) to the Bundle | ||
builder.addUpdateEntry(patient).conditional("Patient?identifier=http://foo|bar"); | ||
|
||
// Execute the transaction | ||
IBaseBundle outcome = myFhirClient.transaction().withBundle(builder.getBundle()).execute(); | ||
//END SNIPPET: updateConditional | ||
} | ||
|
||
public void create() throws FHIRException { | ||
//START SNIPPET: create | ||
// Create a TransactionBuilder | ||
TransactionBuilder builder = new TransactionBuilder(myFhirContext); | ||
|
||
// Create a Patient to create | ||
Patient patient = new Patient(); | ||
patient.setActive(true); | ||
|
||
// Add the patient as a create (aka POST) to the Bundle | ||
builder.addCreateEntry(patient); | ||
|
||
// Execute the transaction | ||
IBaseBundle outcome = myFhirClient.transaction().withBundle(builder.getBundle()).execute(); | ||
//END SNIPPET: create | ||
} | ||
|
||
public void createConditional() throws FHIRException { | ||
//START SNIPPET: createConditional | ||
// Create a TransactionBuilder | ||
TransactionBuilder builder = new TransactionBuilder(myFhirContext); | ||
|
||
// Create a Patient to create | ||
Patient patient = new Patient(); | ||
patient.setActive(true); | ||
patient.addIdentifier().setSystem("http://foo").setValue("bar"); | ||
|
||
// Add the patient as a create (aka POST) to the Bundle | ||
builder.addCreateEntry(patient).conditional("Patient?identifier=http://foo|bar"); | ||
|
||
// Execute the transaction | ||
IBaseBundle outcome = myFhirClient.transaction().withBundle(builder.getBundle()).execute(); | ||
//END SNIPPET: createConditional | ||
} | ||
|
||
} |
4 changes: 4 additions & 0 deletions
4
...docs/src/main/resources/ca/uhn/hapi/fhir/changelog/5_2_0/2041-add-transactionbuilder.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
type: add | ||
issue: 2041 | ||
title: "A new class called TransactionBuilder has been added. This class can be used to build FHIR Transaction Bundles easily." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...fhir-docs/src/main/resources/ca/uhn/hapi/fhir/docs/model/transaction_builder.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Transaction Builder | ||
|
||
The TransactionBuilder ([JavaDoc](/hapi-fhir/apidocs/hapi-fhir-base/ca/uhn/fhir/util/TransactionBuilder.html)) can be used to construct FHIR Transaction Bundles. | ||
|
||
Note that this class is a work in progress! It does not yet support all transaction features. We will add more features over time, and document them here. Pull requests are welcomed. | ||
|
||
# Resource Create | ||
|
||
To add an update (aka PUT) operation to a transaction bundle | ||
|
||
```java | ||
{{snippet:classpath:/ca/uhn/hapi/fhir/docs/TransactionBuilderExamples.java|create}} | ||
``` | ||
|
||
## Conditional Create | ||
|
||
If you want to perform a conditional create: | ||
|
||
```java | ||
{{snippet:classpath:/ca/uhn/hapi/fhir/docs/TransactionBuilderExamples.java|createConditional}} | ||
``` | ||
|
||
# Resource Updates | ||
|
||
To add an update (aka PUT) operation to a transaction bundle | ||
|
||
```java | ||
{{snippet:classpath:/ca/uhn/hapi/fhir/docs/TransactionBuilderExamples.java|update}} | ||
``` | ||
|
||
## Conditional Update | ||
|
||
If you want to perform a conditional update: | ||
|
||
```java | ||
{{snippet:classpath:/ca/uhn/hapi/fhir/docs/TransactionBuilderExamples.java|updateConditional}} | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters