Question

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.

Was it helpful?

Solution

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>"""

OTHER TIPS

I've got some ugliness in here with the global _index variable, termination conditions, and try..catch nonsense, but I've been at this for many hours and this code works:

    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 _index = null   // global index into groupedScripts collection

        def addClinic = { scripts, index ->
            addPatient.delegate = delegate
            def (rxId, clinicId, patientId) = scripts[index]
            def startingClinicId = clinicId
            def cl = Clinic.get(clinicId)

            clinic(id:cl.id, name:cl.name) {
                while( clinicId == startingClinicId ) {
                    _index = index
                    index = addPatient(scripts, index)
                    if( index == -1 ) return -1
                    try {
                        (rxId, clinicId, patientId) = scripts[++index]
                    } catch(NullPointerException e) {
                        return -1
                    }
                }
            }
            return _index
        }

        def addPatient = { scripts, index ->
            def result = index
            def (rxId, clinicId, patientId) = scripts[index]
            def startingPatientId = patientId
            def pat = Patient.get(patientId)

            patient(id:pat.id, firstName:pat.firstName, lastName:pat.lastName) {
                while( patientId == startingPatientId ) {
                    _index = index
                    def script = Prescription.get(rxId)
                    prescription(id:script.id) {
                        prescriptionType(script.prescriptionType)
                        duration(script.duration)
                        drugName(script.drugName)
                        route(script.route)
                        refills(script.refills)
                    }
                    try {
                        (rxId, clinicId, patientId) = scripts[++index]
                    } catch(NullPointerException e) {
                        return -1
                    }
                }
            }
            return _index
        }

        @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 finished = false
            def worklist = new StreamingMarkupBuilder().bind {
                worklist {
                    clinics {
                        _index = 0
                        addClinic.delegate = delegate

                        while( !finished && _index < groupedScripts.size() ) {
                            _index = addClinic(groupedScripts, _index)
                            if( _index == -1 ) finished = true
                            ++_index
                        }
                    }
                }
            }

            def xml = XmlUtil.serialize(worklist)
            xml
        }
    }

The output is:

    <worklist>
       <clinics>
          <clinic id="1" name="Clinic 1">
             <patient firstName="Mariah" id="3" lastName="Brookstone">
                <prescription id="1">
                   <prescriptionType>New</prescriptionType>
                   <duration>30 days</duration>
                   <drugName>Lisinopril 20mg Tablet</drugName>
                   <route>By Mouth</route>
                   <refills>2</refills>
                </prescription>
             </patient>
             <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="Mariah" id="3" lastName="Brookstone">
                <prescription id="11">
                   <prescriptionType/>
                   <duration/>
                   <drugName>Milk Duds</drugName>
                   <route/>
                   <refills>0</refills>
                </prescription>
                <prescription id="12">
                   <prescriptionType/>
                   <duration/>
                   <drugName>Hershey</drugName>
                   <route/>
                   <refills>0</refills>
                </prescription>
                <prescription id="7">
                   <prescriptionType/>
                   <duration/>
                   <drugName>Skittles</drugName>
                   <route/>
                   <refills>0</refills>
                </prescription>
             </patient>
             <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>
          <clinic id="3" name="Clinic 3">
             <patient firstName="Jane" id="12" lastName="Doe">
                <prescription id="9">
                   <prescriptionType/>
                   <duration/>
                   <drugName>Reese's Pieces</drugName>
                   <route/>
                   <refills>0</refills>
                </prescription>
             </patient>
             <patient firstName="Jill" id="13" lastName="Doe">
                <prescription id="8">
                   <prescriptionType/>
                   <duration/>
                   <drugName>Toothpaste</drugName>
                   <route/>
                   <refills>0</refills>
                </prescription>
             </patient>
             <patient firstName="Jim" id="11" lastName="Doe">
                <prescription id="10">
                   <prescriptionType/>
                   <duration/>
                   <drugName>Hallmark</drugName>
                   <route/>
                   <refills>0</refills>
                </prescription>
             </patient>
             <patient firstName="John" id="2" lastName="Doe">
                <prescription id="6">
                   <prescriptionType/>
                   <duration/>
                   <drugName>Gauze</drugName>
                   <route/>
                   <refills>0</refills>
                </prescription>
             </patient>
          </clinic>
       </clinics>
    </worklist>

That's (almost) exactly what I was after.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top