@Grab(group = 'ca.uhn.hapi.fhir', module = 'hapi-fhir-structures-dstu3', version = '3.1.0') import ca.uhn.fhir.context.FhirContext import org.hl7.fhir.dstu3.model.* import static org.hl7.fhir.dstu3.model.Bundle.BundleType.TRANSACTION import static org.hl7.fhir.dstu3.model.ContactPoint.ContactPointUse.HOME import static org.hl7.fhir.dstu3.model.ContactPoint.ContactPointUse.WORK import static org.hl7.fhir.dstu3.model.Enumerations.AdministrativeGender.MALE import static org.hl7.fhir.dstu3.model.Identifier.IdentifierUse.OFFICIAL import static org.hl7.fhir.dstu3.model.Identifier.IdentifierUse.SECONDARY class JsonFactory { File rootDir = new File('data') private static Bundle.BundleEntryComponent entry(Resource patient) { new Bundle.BundleEntryComponent().setResource(patient) } void createBundle(String fileName, List resources) { def ctx = FhirContext.forDstu3() def bundle = new Bundle(). setType(TRANSACTION). setEntry(resources.collect { entry(it) }). setId(UUID.randomUUID().toString()) def jsonParser = ctx.newJsonParser() jsonParser.prettyPrint = true new File(rootDir, "${fileName}.json").text = jsonParser.encodeResourceToString(bundle) } def doc = new Practitioner().with { it.id = "Practitioner_1"; it }. setName([ new HumanName().setFamily('Kelso').setGiven([new StringType('Bob')]) ]). setGender(MALE). setIdentifier([ new Identifier(). setSystem('https://stmarys.com/practitioners'). setValue('BK001'), ]). setQualification([ new Practitioner.PractitionerQualificationComponent(). setCode(new CodeableConcept().setCoding([ new Coding('http://codesystem/', 'code', 'Some display name') ])) ]) def patient = new Patient().with { it.id = "Patient_1"; it }. setName([new HumanName(). setFamily("Doe"). setGiven([new StringType("John")])]). setAddress([new Address(). setUse(Address.AddressUse.HOME). setLine([new StringType('11 Spooner St')]). setCity('Quahog'). setState('Rhode Island'). setPostalCode('90210'). setCountry('United States of America') ]). setIdentifier([ new Identifier(). setUse(OFFICIAL). setSystem('https://ssn.gov/id'). setValue('101239980'), new Identifier(). setUse(SECONDARY). setSystem('https://stmarys.com/patients'). setValue('P100390142'), new Identifier(). setUse(SECONDARY). setSystem('https://dmv.ca.gov/'). setValue('D5103342'), ]). setActive(true). setBirthDate(Date.parse('yyyyMMdd', '19800102')). setTelecom([ new ContactPoint().setValue('jdoe@gmail.com').setUse(HOME), new ContactPoint().setValue('+18005359090').setUse(HOME), new ContactPoint().setValue('+18008889797').setUse(WORK), ]). setGender(MALE). setGeneralPractitioner([ new Reference().setReference('Practitioner_1') ]) List visits(int count) { (1..count).collect { new Encounter().with { it.id = "Encounter$it"; it }. setIdentifier([ new Identifier(). setUse(SECONDARY). setSystem('https://stmarys.com/encounters'). setValue("E00010$it"), ]). setDiagnosis([ new Encounter.DiagnosisComponent() ]). setLength(new Duration().setValue(10).setUnit('days')). setStatus(Encounter.EncounterStatus.FINISHED) } } List observations(int count) { (1..count).collect { new Observation().with { it.id = "Observation$it"; it }. setStatus(Observation.ObservationStatus.FINAL). setIdentifier([ new Identifier(). setUse(SECONDARY). setSystem('https://stmarys.com/observations'). setValue("E00010$it"), ]). setValue(new Quantity().setUnit('mg').setValue(100)). setBodySite(new CodeableConcept().setText("Foo").setCoding([ new Coding().setSystem('http://loinc.com').setCode('12043'), new Coding().setSystem('http://snomed.com').setCode('H123-J010'), ])) } } }