Have got this working now. Doubt it's correct java (Pretty sure it's my first Java program) but it working for me.
This feeds a FHIR server with a word document (Binary resource) and associated metadata (in DocumentReference resource). I've posted the bundle/rss to a Mailbox on the server.
May not contain all the mandatory fields - the metadata I'm working with would be less than I've included here.
package uk.nhs.jorvik.algiz;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.Bundle;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.dev.composite.CodeableConceptDt;
import ca.uhn.fhir.model.dstu.composite.PeriodDt;
import ca.uhn.fhir.model.dev.resource.DocumentReference;
import ca.uhn.fhir.model.dev.resource.Patient;
import ca.uhn.fhir.model.dev.resource.Practitioner;
import ca.uhn.fhir.model.dev.valueset.DocumentReferenceStatusEnum;
import ca.uhn.fhir.model.dstu.composite.ContainedDt;
import ca.uhn.fhir.model.dev.composite.HumanNameDt;
import ca.uhn.fhir.model.dstu.composite.ResourceReferenceDt;
import ca.uhn.fhir.model.dstu.resource.Binary;
import ca.uhn.fhir.model.dstu.resource.Encounter;
import ca.uhn.fhir.model.primitive.DateDt;
import ca.uhn.fhir.model.primitive.DateTimeDt;
import ca.uhn.fhir.model.primitive.InstantDt;
import ca.uhn.fhir.rest.client.IGenericClient;
public class DocumentReferenceExample {
public static void main(String[] args) throws IOException {
// Create a client factory
FhirContext ctx = new FhirContext();
// Create the client
IGenericClient client = ctx.newRestfulGenericClient(serverBase);
DocumentReference DocRef = new DocumentReference();
DocRef.setId("000040_A00551189-6687378-1_02-12-2010_08-44-13_B1960_GP.doc");
DocRef
.addIdentifier()
.setValue("000040_A00551189-6687378-1_02-12-2010_08-44-13_B1960_GP.doc");
ResourceReferenceDt PatRef = new ResourceReferenceDt("Patient/pat1");
DocRef.setSubject(PatRef);
List<ResourceReferenceDt> AuthList = new ArrayList<ResourceReferenceDt>();
ResourceReferenceDt PracRef = new ResourceReferenceDt("Practitioner/prac1");
AuthList.add(PracRef);
DocRef.setAuthor(AuthList);
DateTimeDt Created = new DateTimeDt("2014-08-01T00:23:00Z");
DocRef.setCreated(Created);
ZonedDateTime now = ZonedDateTime.now( ZoneOffset.UTC );
InstantDt Indexed = new InstantDt(now.toString());
DocRef.setIndexed(Indexed);
DocRef.setStatus(DocumentReferenceStatusEnum.CURRENT);
DocRef.setMimeType("application/msword");
CodeableConceptDt docType = new CodeableConceptDt();
docType
.addCoding()
.setCode("823701000000103")
.setDisplay("Discharge letter (record artifact)")
DocRef.setType(docType);
ContainedDt Con1 = new ContainedDt();
List<IResource> res1 = new ArrayList<IResource>();
// Add Patient metadata
Patient Pat = new Patient();
Pat.setId("Patient/pat1");
Pat
.addIdentifier()
.setSystem("urn:oid:2.16.840.1.113883.2.1.4.1")
.setValue("9990276579");
Pat
.addIdentifier()
.setValue("6687378");
Pat
.addIdentifier()
.setValue("A00551189");
Pat
.addName()
.addFamily("XXTESTPATIENTREAH")
.addGiven("nic-qtp-donnotuse");
DateDt birthDate = new DateDt("1997-07-10");
Pat.setBirthDate(birthDate);
ResourceReferenceDt DrRef = new ResourceReferenceDt("urn:oid:2.16.840.1.113883.2.1.4.4|B1960");
Pat.setManagingOrganization(DrRef);
res1.add(Pat);
// Add consultant metadata
Practitioner Consultant = new Practitioner();
Consultant
.addIdentifier()
.setSystem("urn:oid:2.16.840.1.113883.2.1.4.4")
.setValue("C9999998");
Consultant
.addIdentifier()
.setValue("436");
HumanNameDt ConsultantName = new HumanNameDt();
ConsultantName.addFamily("DR UNKNOWN");
Consultant.setName(ConsultantName);
res1.add(Consultant);
// DSTU1
Encounter Enc = new Encounter();
Enc
.addIdentifier()
.setValue("376579-1");
DateTimeDt Started = new DateTimeDt("2014-08-01T00:15:00Z");
PeriodDt period = new PeriodDt();
period.setStart(Started);
period.setEnd(Started);
Enc.setPeriod(period);
res1.add(Enc);
Con1.setContainedResources(res1);
DocRef.setContained(Con1);
// using DSTU1
Binary Document = new Binary();
Document.setId("000040_A00551189-6687378-1_02-12-2010_08-44-13_B1960_GP.doc");
Document.setContentType("application/msword");
// Read letter in byte array
Path path = Paths.get("C:\\Apache\\Gold_standard_clinic_letter.doc");
byte[] data = Files.readAllBytes(path);
Document.setContent(data);
List<IResource> resources = new ArrayList<IResource>();
resources.add(DocRef);
resources.add(Document);
// Create a bundle with both
Bundle b = Bundle.withResources(resources, ctx, "");
client.transaction().withBundle(b).execute();
}
}