String finalQuery = ("?encounterType=" + params.get("encounterType") + "&inList=" + inList.getUuid() + "¬InList=" + notInList
.getUuid());
PatientList patientList = new PatientList();
patientList.setSearchQuery(finalQuery);
SimpleObject obj = new SimpleObject();
obj.add("uuid", patientList.getUuid());
obj.add("name", patientList.getName());
obj.add("description", patientList.getDescription());
obj.add("searchQuery", patientList.getSearchQuery());
ArrayList patients = new ArrayList();
List<Patient> patientsInPatientList = service.getPatientsInPatientList(patientList);
List<Encounter> encountersInPatientList = service.getEncountersInPatientList(patientList);
for (Patient p : patientsInPatientList) {
SimpleObject patient = new SimpleObject();
patient.add("uuid", p.getUuid());
SimpleObject person = new SimpleObject();
person.add("uuid", p.getUuid());
person.add("display", p.getPersonName().getFullName());
SimpleObject name = new SimpleObject();
name.add("display", p.getPersonName().getFullName());
person.add("preferredName", name);
person.add("gender", p.getGender());
person.add("age", p.getAge());
patient.add("person", person);
ArrayList identifiers = new ArrayList();
SimpleObject id = new SimpleObject();
id.add("identifier", p.getPatientIdentifier().getIdentifier());
identifiers.add(id);
patient.add("identifiers", identifiers);
//patient.add("identifiers", p.getActiveIdentifiers());
ArrayList encounters = new ArrayList();
//TODO: refactor this so we don't have to go through each time
for (Encounter e : encountersInPatientList) {
if (e.getPatient().equals(p)) {
SimpleObject encounter = new SimpleObject();
encounter.add("uuid", e.getUuid());
encounter.add("display", e.getEncounterType().getName() + " - " + e.getEncounterDatetime());
encounter.add("encounterType", e.getEncounterType().getUuid());
encounter.add("encounterDatetime", df.format(e.getEncounterDatetime()));
if (e.getProvider() != null) {
encounter.add("provider", e.getProvider().getUuid());
} else {
encounter.add("provider", null);
}
if (params.get("containsOrderType") != null) {
encounter.add("obs", null);
} else {
ArrayList obsArray = new ArrayList();
Set<Obs> obsAll = e.getObs();
for (Obs o : obsAll) {
SimpleObject obs = new SimpleObject();
obs.add("uuid", o.getUuid());
obs.add("display", o.getConcept().getName().getName() + " = "
+ o.getValueAsString(request.getLocale()));
obs.add("obsDatetime", df.format(o.getObsDatetime()));
obs.add("value", o.getValueAsString(request.getLocale()));
obs.add("comment", o.getComment());
if (o.getOrder() != null) {
obs.add("order", o.getOrder().getUuid());
} else {
obs.add("order", null);
}
obsArray.add(obs);
}
encounter.add("obs", obsArray);
}