我有一个分组查询,可以生成诊所列表。诊所里有病人。患者体内有处方。我正在尝试使用 MarkupBuilder 输出这个结构,但我似乎无法让遏制工作。
我得到的是:
<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>
请注意,诊所元素关闭且不包含患者。并且患者元素关闭并且不包含处方。这是不正确的。它应该如下所示:
<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>
这是我的代码:
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
}
}
显然,我需要以某种方式让诊所关闭,直到我找到一家新诊所或达到收藏的尽头。与患者关闭相同。我只是不知道该怎么做。
提前感谢您的帮助。我需要让这个工作今晚。