2.7Bundle Builder

 

The BundleBuilder (JavaDoc) can be used to construct FHIR 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.

2.7.1Transaction Resource Create

 

To add an update (aka PUT) operation to a transaction bundle

// Create a TransactionBuilder
BundleBuilder builder = new BundleBuilder(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();

Conditional Create

If you want to perform a conditional create:

// Create a TransactionBuilder
BundleBuilder builder = new BundleBuilder(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();

2.7.2Transaction Resource Updates

 

To add an update (aka PUT) operation to a transaction bundle:

// Create a TransactionBuilder
BundleBuilder builder = new BundleBuilder(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.addTransactionUpdateEntry(patient);

// Execute the transaction
IBaseBundle outcome = myFhirClient.transaction().withBundle(builder.getBundle()).execute();

Conditional Update

If you want to perform a conditional update:

// Create a TransactionBuilder
BundleBuilder builder = new BundleBuilder(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.addTransactionUpdateEntry(patient).conditional("Patient?identifier=http://foo|bar");

// Execute the transaction
IBaseBundle outcome = myFhirClient.transaction().withBundle(builder.getBundle()).execute();

2.7.3Customizing bundle

 

If you want to manipulate a bundle:

// Create a TransactionBuilder
BundleBuilder builder = new BundleBuilder(myFhirContext);
// Set bundle type to be searchset
builder
   .setBundleField("type", "searchset")
   .setBundleField("id", UUID.randomUUID().toString())
   .setMetaField("lastUpdated", builder.newPrimitive("instant", new Date()));

// Create bundle entry
IBase entry = builder.addEntry();

// Create a Patient to create
Patient patient = new Patient();
patient.setActive(true);
patient.addIdentifier().setSystem("http://foo").setValue("bar");
builder.addToEntry(entry, "resource", patient);

// Add search results
IBase search = builder.addSearch(entry);
builder.setSearchField(search, "mode", "match");
builder.setSearchField(search, "score", builder.newPrimitive("decimal", BigDecimal.ONE));