Skip to content

Commit

Permalink
Add initial REST controller and add some fixes for performance and to…
Browse files Browse the repository at this point in the history
… mitigate errors related to infinite recursion when serializing rest data
  • Loading branch information
mseaton committed Jul 11, 2024
1 parent aae9f3e commit 5727a3d
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,4 @@ VisitDomainWrapper createRetrospectiveVisit(Patient patient, Location location,
* @return List<InpatientRequest> of the matching InpatientRequests that match the criteria
*/
List<InpatientRequest> 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<Visit> of the matching visits<
*/
List<Visit> getVisitsAwaitingTransfer(Location transferLocation);
}
49 changes: 21 additions & 28 deletions api/src/main/java/org/openmrs/module/emrapi/adt/AdtServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -943,9 +943,11 @@ public List<InpatientRequest> getInpatientRequests(InpatientRequestSearchCriteri
// Get all disposition concepts based on the given disposition type(s)
Map<Concept, DispositionType> dispositionValuesToType = new HashMap<>();
List<Disposition> 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());
}
}
}

Expand Down Expand Up @@ -978,38 +980,29 @@ public List<InpatientRequest> 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<Obs> dispositionObs = emrApiDAO.executeHqlFromResource("hql/inpatient_request_dispositions.hql", parameters, Obs.class);
List<?> reqs = emrApiDAO.executeHqlFromResource("hql/inpatient_request_dispositions.hql", parameters, List.class);
List<InpatientRequest> 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<Visit> getVisitsAwaitingTransfer(Location transferLocation) {
// TODO implement!
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ public class InpatientRequest {
private Obs dispositionObsGroup;
private Concept disposition;
private Location dispositionLocation;
private Date dispositionDate;
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -40,3 +46,4 @@ where
and locationObs.valueText in (:dispositionLocationIds)
) > 0
)
order by dispo.obsId
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,7 +27,8 @@
DiagnosisMetadata.class,
Disposition.class,
DispositionObs.class,
DispositionDescriptor.class
DispositionDescriptor.class,
InpatientRequest.class
}, order = 0)
public class SimpleBeanConverter<T> implements Converter<T> {

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Location> dispositionLocations,
@RequestParam(required = false, value = "dispositionType") List<DispositionType> dispositionTypes
) {
RequestContext context = RestUtil.getRequestContext(request, response, Representation.REF);
InpatientRequestSearchCriteria criteria = new InpatientRequestSearchCriteria();
criteria.setVisitLocation(visitLocation);
criteria.setDispositionLocations(dispositionLocations);
criteria.setDispositionTypes(dispositionTypes);
List<InpatientRequest> requests = adtService.getInpatientRequests(criteria);
return new NeedsPaging<>(requests, context).toSimpleObject(new SimpleBeanConverter<InpatientRequest>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,7 @@ private List<SimpleObject> getVisitsAwaitingAdmissionHelper(Location admissionLo
}

private List<SimpleObject> getVisitsAwaitingTransferHelper(Location transferLocation) {
List<Visit> visits = adtService.getVisitsAwaitingTransfer(transferLocation);
List<SimpleObject> visitObjects = new ArrayList<SimpleObject>();
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<>();
}

}
Expand Down

0 comments on commit 5727a3d

Please sign in to comment.