From 5727a3d333dd3d6925a1888f5b807843d1667c29 Mon Sep 17 00:00:00 2001 From: Michael Seaton Date: Thu, 11 Jul 2024 12:42:23 -0400 Subject: [PATCH] Add initial REST controller and add some fixes for performance and to mitigate errors related to infinite recursion when serializing rest data --- .../openmrs/module/emrapi/adt/AdtService.java | 7 --- .../module/emrapi/adt/AdtServiceImpl.java | 49 ++++++++----------- .../module/emrapi/adt/InpatientRequest.java | 1 - .../hql/inpatient_request_dispositions.hql | 9 +++- .../rest/converter/SimpleBeanConverter.java | 4 +- .../InpatientRequestController.java | 48 ++++++++++++++++++ .../controller/InpatientVisitsController.java | 11 +---- 7 files changed, 81 insertions(+), 48 deletions(-) create mode 100644 omod/src/main/java/org/openmrs/module/emrapi/web/controller/InpatientRequestController.java diff --git a/api/src/main/java/org/openmrs/module/emrapi/adt/AdtService.java b/api/src/main/java/org/openmrs/module/emrapi/adt/AdtService.java index a5dc0570..996743ca 100644 --- a/api/src/main/java/org/openmrs/module/emrapi/adt/AdtService.java +++ b/api/src/main/java/org/openmrs/module/emrapi/adt/AdtService.java @@ -353,11 +353,4 @@ VisitDomainWrapper createRetrospectiveVisit(Patient patient, Location location, * @return List of the matching InpatientRequests that match the criteria */ List getInpatientRequests(InpatientRequestSearchCriteria criteria); - - /** - * Returns all patient awaiting transfer - * @param transferLocation - if non-null, only return matches for patients awaiting transfer to this location - * @return List of the matching visits< - */ - List getVisitsAwaitingTransfer(Location transferLocation); } diff --git a/api/src/main/java/org/openmrs/module/emrapi/adt/AdtServiceImpl.java b/api/src/main/java/org/openmrs/module/emrapi/adt/AdtServiceImpl.java index ba643783..a69cce1d 100644 --- a/api/src/main/java/org/openmrs/module/emrapi/adt/AdtServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/emrapi/adt/AdtServiceImpl.java @@ -943,9 +943,11 @@ public List getInpatientRequests(InpatientRequestSearchCriteri // Get all disposition concepts based on the given disposition type(s) Map dispositionValuesToType = new HashMap<>(); List dispositions = dispositionService.getDispositions(); - for (Disposition d : dispositions) { - if (dispositionTypes.contains(d.getType())) { - dispositionValuesToType.put(emrConceptService.getConcept(d.getConceptCode()), d.getType()); + if (dispositions != null) { + for (Disposition d : dispositions) { + if (dispositionTypes.contains(d.getType())) { + dispositionValuesToType.put(emrConceptService.getConcept(d.getConceptCode()), d.getType()); + } } } @@ -978,38 +980,29 @@ public List getInpatientRequests(InpatientRequestSearchCriteri parameters.put("adtDecisionConcept", emrApiProperties.getAdmissionDecisionConcept()); parameters.put("denyConcept", emrApiProperties.getDenyAdmissionConcept()); parameters.put("dispositionLocationIds", dispositionLocationIds); + parameters.put("admitLocationConcept", descriptor.getAdmissionLocationConcept()); + parameters.put("transferLocationConcept", descriptor.getInternalTransferLocationConcept()); - List dispositionObs = emrApiDAO.executeHqlFromResource("hql/inpatient_request_dispositions.hql", parameters, Obs.class); + List reqs = emrApiDAO.executeHqlFromResource("hql/inpatient_request_dispositions.hql", parameters, List.class); List ret = new ArrayList<>(); - for (Obs o : dispositionObs) { + for (Object req : reqs) { + Object[] o = (Object[]) req; InpatientRequest r = new InpatientRequest(); - r.setVisit(o.getEncounter().getVisit()); - r.setPatient(o.getEncounter().getPatient()); - r.setDispositionEncounter(o.getEncounter()); - r.setDispositionObsGroup(o.getObsGroup()); - r.setDispositionType(dispositionValuesToType.get(o.getValueCoded())); - r.setDisposition(o.getValueCoded()); - - Location dispositionLocation = null; - if (r.getDispositionType() == DispositionType.ADMIT) { - dispositionLocation = descriptor.getAdmissionLocation(o.getObsGroup(), locationService); + r.setVisit((Visit)o[0]); + r.setPatient((Patient)o[1]); + r.setDispositionEncounter((Encounter)o[2]); + r.setDispositionObsGroup((Obs)o[3]); + Obs dispositionObs = (Obs)o[4]; + if (dispositionObs != null) { + r.setDisposition(dispositionObs.getValueCoded()); + r.setDispositionType(dispositionValuesToType.get(dispositionObs.getValueCoded())); } - else if (r.getDispositionType() == DispositionType.TRANSFER) { - dispositionLocation = descriptor.getInternalTransferLocation(o.getObsGroup(), locationService); + Obs locationObs = (Obs)(o[5] != null ? o[5] : o[6]); + if (locationObs != null) { + r.setDispositionLocation(locationService.getLocation(Integer.parseInt(locationObs.getValueText()))); } - r.setDispositionLocation(dispositionLocation); - - Date deathDate = descriptor.getDateOfDeath(o.getObsGroup()); - r.setDispositionDate(deathDate == null ? o.getObsDatetime() : deathDate); ret.add(r); } return ret; } - - @Override - @Transactional(readOnly = true) - public List getVisitsAwaitingTransfer(Location transferLocation) { - // TODO implement! - return Collections.emptyList(); - } } diff --git a/api/src/main/java/org/openmrs/module/emrapi/adt/InpatientRequest.java b/api/src/main/java/org/openmrs/module/emrapi/adt/InpatientRequest.java index 0eb18a9e..11db3d0f 100644 --- a/api/src/main/java/org/openmrs/module/emrapi/adt/InpatientRequest.java +++ b/api/src/main/java/org/openmrs/module/emrapi/adt/InpatientRequest.java @@ -23,5 +23,4 @@ public class InpatientRequest { private Obs dispositionObsGroup; private Concept disposition; private Location dispositionLocation; - private Date dispositionDate; } diff --git a/api/src/main/resources/hql/inpatient_request_dispositions.hql b/api/src/main/resources/hql/inpatient_request_dispositions.hql index 013fa378..7b0de809 100644 --- a/api/src/main/resources/hql/inpatient_request_dispositions.hql +++ b/api/src/main/resources/hql/inpatient_request_dispositions.hql @@ -1,5 +1,11 @@ select - dispo + visit, + dispoEncounter.patient, + dispoEncounter, + dispo.obsGroup, + dispo, + (select o from Obs o where o.obsGroup = dispo.obsGroup and o.voided = 0 and o.concept = :admitLocationConcept) as admitLocation, + (select o from Obs o where o.obsGroup = dispo.obsGroup and o.voided = 0 and o.concept = :transferLocationConcept) as transferLocation from Obs as dispo inner join dispo.encounter as dispoEncounter @@ -40,3 +46,4 @@ where and locationObs.valueText in (:dispositionLocationIds) ) > 0 ) +order by dispo.obsId diff --git a/omod/src/main/java/org/openmrs/module/emrapi/rest/converter/SimpleBeanConverter.java b/omod/src/main/java/org/openmrs/module/emrapi/rest/converter/SimpleBeanConverter.java index 59dc3bbd..276cf793 100644 --- a/omod/src/main/java/org/openmrs/module/emrapi/rest/converter/SimpleBeanConverter.java +++ b/omod/src/main/java/org/openmrs/module/emrapi/rest/converter/SimpleBeanConverter.java @@ -5,6 +5,7 @@ import org.apache.commons.logging.LogFactory; import org.openmrs.annotation.Handler; import org.openmrs.module.emrapi.EmrApiProperties; +import org.openmrs.module.emrapi.adt.InpatientRequest; import org.openmrs.module.emrapi.diagnosis.DiagnosisMetadata; import org.openmrs.module.emrapi.disposition.Disposition; import org.openmrs.module.emrapi.disposition.DispositionDescriptor; @@ -26,7 +27,8 @@ DiagnosisMetadata.class, Disposition.class, DispositionObs.class, - DispositionDescriptor.class + DispositionDescriptor.class, + InpatientRequest.class }, order = 0) public class SimpleBeanConverter implements Converter { diff --git a/omod/src/main/java/org/openmrs/module/emrapi/web/controller/InpatientRequestController.java b/omod/src/main/java/org/openmrs/module/emrapi/web/controller/InpatientRequestController.java new file mode 100644 index 00000000..2e745a7b --- /dev/null +++ b/omod/src/main/java/org/openmrs/module/emrapi/web/controller/InpatientRequestController.java @@ -0,0 +1,48 @@ +package org.openmrs.module.emrapi.web.controller; + +import org.openmrs.Location; +import org.openmrs.module.emrapi.adt.AdtService; +import org.openmrs.module.emrapi.adt.InpatientRequest; +import org.openmrs.module.emrapi.adt.InpatientRequestSearchCriteria; +import org.openmrs.module.emrapi.disposition.DispositionType; +import org.openmrs.module.emrapi.rest.converter.SimpleBeanConverter; +import org.openmrs.module.webservices.rest.SimpleObject; +import org.openmrs.module.webservices.rest.web.RequestContext; +import org.openmrs.module.webservices.rest.web.RestUtil; +import org.openmrs.module.webservices.rest.web.representation.Representation; +import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.List; + +@Controller +public class InpatientRequestController { + + @Autowired + private AdtService adtService; + + @RequestMapping(method = RequestMethod.GET, value = "/rest/v1/emrapi/inpatient/request") + @ResponseBody + public SimpleObject getInpatientRequests( + HttpServletRequest request, + HttpServletResponse response, + @RequestParam(required = false, value = "visitLocation") Location visitLocation, + @RequestParam(required = false, value = "dispositionLocation") List dispositionLocations, + @RequestParam(required = false, value = "dispositionType") List dispositionTypes + ) { + RequestContext context = RestUtil.getRequestContext(request, response, Representation.REF); + InpatientRequestSearchCriteria criteria = new InpatientRequestSearchCriteria(); + criteria.setVisitLocation(visitLocation); + criteria.setDispositionLocations(dispositionLocations); + criteria.setDispositionTypes(dispositionTypes); + List requests = adtService.getInpatientRequests(criteria); + return new NeedsPaging<>(requests, context).toSimpleObject(new SimpleBeanConverter()); + } +} diff --git a/omod/src/main/java/org/openmrs/module/emrapi/web/controller/InpatientVisitsController.java b/omod/src/main/java/org/openmrs/module/emrapi/web/controller/InpatientVisitsController.java index ddc1b381..2c4d9188 100644 --- a/omod/src/main/java/org/openmrs/module/emrapi/web/controller/InpatientVisitsController.java +++ b/omod/src/main/java/org/openmrs/module/emrapi/web/controller/InpatientVisitsController.java @@ -88,16 +88,7 @@ private List getVisitsAwaitingAdmissionHelper(Location admissionLo } private List getVisitsAwaitingTransferHelper(Location transferLocation) { - List visits = adtService.getVisitsAwaitingTransfer(transferLocation); - List visitObjects = new ArrayList(); - for (Visit visit : visits) { - SimpleObject inpatientVisit = new SimpleObject(); - inpatientVisit.put("visit", ConversionUtil.convertToRepresentation(visit, Representation.DEFAULT)); - inpatientVisit.put("patient", ConversionUtil.convertToRepresentation(visit.getPatient(), Representation.DEFAULT)); - inpatientVisit.put("type", AdtAction.Type.TRANSFER); - visitObjects.add(inpatientVisit); - } - return visitObjects; + return new ArrayList<>(); } }