I've got a grouped query that results in a list of clinics. Within the clinics are patients. And within the patients are prescriptions. I'm trying to output this structure using MarkupBuilder, but I can't seem to get the containment working.
What I'm getting is this:
<worklist>
<clinics>
<clinic id="1" name="Clinic 1"/>
<patient firstName="John" id="2" lastName="Doe"/>
<prescription id="4">
<prescriptionType/>
<duration/>
<drugName>Tums</drugName>
<route/>
<refills>0</refills>
</prescription>
<clinic id="2" name="Clinic 2"/>
<patient firstName="John" id="2" lastName="Doe"/>
<prescription id="2">
<prescriptionType>Formulary</prescriptionType>
<duration>duration</duration>
<drugName>Lipitor</drugName>
<route>route</route>
<refills>5</refills>
</prescription>
<patient firstName="Sylvia" id="4" lastName="Plath"/>
<prescription id="5">
<prescriptionType/>
<duration/>
<drugName>BandAids</drugName>
<route/>
<refills>0</refills>
</prescription>
</clinics>
</worklist>
Note that clinic element closes and does not contain the patients. And patient element closes and does not contain the prescriptions. This is incorrect. It should look like this:
<worklist>
<clinics>
<clinic id="1" name="Clinic 1">
<patient firstName="John" id="2" lastName="Doe">
<prescription id="4">
<prescriptionType/>
<duration/>
<drugName>Tums</drugName>
<route/>
<refills>0</refills>
</prescription>
</patient>
</clinic>
<clinic id="2" name="Clinic 2"/>
<patient firstName="John" id="2" lastName="Doe">
<prescription id="2">
<prescriptionType>Formulary</prescriptionType>
<duration>duration</duration>
<drugName>Lipitor</drugName>
<route>route</route>
<refills>5</refills>
</prescription>
</patient>
<patient firstName="Sylvia" id="4" lastName="Plath">
<prescription id="5">
<prescriptionType/>
<duration/>
<drugName>BandAids</drugName>
<route/>
<refills>0</refills>
</prescription>
</patient>
</clinic>
</clinics>
</worklist>
Here is my code:
import groovy.xml.StreamingMarkupBuilder
import groovy.xml.XmlUtil
import javax.ws.rs.GET
import javax.ws.rs.Path
import javax.ws.rs.Produces
@Path('/api/worklist')
class WorklistResource {
def addClinic = { idx, name ->
clinic(id:idx, name:name)
}
def addPatient = { idx, fname, lname ->
patient(id:idx, firstName:fname, lastName:lname)
}
@GET
@Produces(['application/xml','application/json'])
String getWorklistRepresentation() {
def groupedScripts = Prescription.createCriteria().list {
createAlias('clinic', 'clinicAlias')
createAlias('patient', 'patientAlias')
projections {
groupProperty "id"
groupProperty "clinicAlias.id"
groupProperty "patientAlias.id"
}
order "clinicAlias.name"
order "patientAlias.lastName"
order "patientAlias.firstName"
}
def curClinic = null
def curPatient = null
def worklist = new StreamingMarkupBuilder().bind {
worklist {
clinics {
groupedScripts.each { arr ->
def (rx, clinic, patient) = arr
def script = Prescription.get(rx)
def cl = Clinic.get(clinic)
def pat = Patient.get(patient)
if( curClinic != cl ) {
curClinic = cl
addClinic.delegate = delegate
addClinic(cl.id, cl.name)
}
if( curPatient != pat ) {
curPatient = pat
addPatient.delegate = delegate
addPatient(pat.id, pat.firstName, pat.lastName)
}
prescription(id:script.id) {
prescriptionType(script.prescriptionType)
duration(script.duration)
drugName(script.drugName)
route(script.route)
refills(script.refills)
}
}
}
}
}
def xml = XmlUtil.serialize(worklist)
xml
}
}
Obviously, I need to somehow keep the clinic closure open until I hit a new clinic or reach the end of the collection. And the same with patient closure. I'm just not sure how to do that.
Thanks in advance for any help. I need to get this working tonight.
You can use just the each
method on lists. You have to take care on the following: every matched method call inside the builder.bind
will call the respective method/variable; thus you will need different names. That's even better, because you don't make ambiguous naming. You can just structure the whole XML inside the eachs
. It's a somewhat large, but this fills your XML the way you want:
UPDATE:
Since your model is reversed, i made the following:
// definition of the model and mock data
@Canonical class Prescription {
int id
String prescriptionType, duration, drugName, route
int refills
Clinic clinic
Patient patient
}
@Canonical class Clinic {
int id
String name
}
@Canonical class Patient {
int id
String firstName, lastName
}
def patient2 = new Patient(2, "John", "Doe")
def patient4 = new Patient(4, "Sylvia", "Plath")
def clinic1 = new Clinic(1, "Clinic 1")
def clinic2 = new Clinic(2, "Clinic 2")
def prescriptions = [
new Prescription(2, "Formulary", "duration", "Lipitor", "route", 5, clinic1, patient2),
new Prescription(4, null, null, "Tums", null, 0, clinic2, patient2),
new Prescription(5, null, null, "BandAids", null, 5, clinic2, patient4)
]
The best bet is to reverse the model so it matches the XML structure. You can easily reverse it using this snippet:
clins = prescriptions.inject([:].withDefault({ [:] })) { map, pres ->
map[ pres.clinic ] << [ (pres.patient) : pres ]
map
}
Now you build the XML according to the map structure:
builder = new groovy.xml.StreamingMarkupBuilder()
xml = builder.bind {
clinics {
clins.each { cliEntry -> cli = cliEntry.key
clinic(id: cli.id, name: cli.name) {
cliEntry.value.each { patEntry -> pat = patEntry.key
patient(id: pat.id, firstName: pat.firstName, lastName: pat.lastName){
patEntry.value.each { pres ->
prescription(id: pres.id) {
prescriptionType pres.prescriptionType
duration pres.duration
drugName pres.drugName
route pres.route
refills pres.refills
}
}
}
}
}
}
}
}
And the test:
assert xml.toString() == """<clinics><clinic id='1' name='Clinic 1'><patient id='2' firstName='John' lastName='Doe'><prescription id='2'><prescriptionType>Formulary</prescriptionType><duration>duration</duration><drugName>Lipitor</drugName><route>route</route><refills>5</refills></prescription></patient></clinic><clinic id='2' name='Clinic 2'><patient id='2' firstName='John' lastName='Doe'><prescription id='4'><prescriptionType/><duration/><drugName>Tums</drugName><route/><refills>0</refills></prescription></patient><patient id='4' firstName='Sylvia' lastName='Plath'><prescription id='4'><prescriptionType/><duration/><drugName>Tums</drugName><route/><refills>0</refills></prescription></patient></clinic></clinics>"""