diff --git a/omod/src/main/java/org/openmrs/web/controller/PortletController.java b/omod/src/main/java/org/openmrs/web/controller/PortletController.java index 959e523c..8fc675d2 100644 --- a/omod/src/main/java/org/openmrs/web/controller/PortletController.java +++ b/omod/src/main/java/org/openmrs/web/controller/PortletController.java @@ -11,6 +11,7 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.HashSet; @@ -49,6 +50,8 @@ public class PortletController implements Controller { protected Log log = LogFactory.getLog(this.getClass()); + private static final int DEFAULT_PAGE_SIZE = 50; + /** * This method produces a model containing the following mappings: * @@ -106,6 +109,7 @@ public ModelAndView handleRequest(HttpServletRequest request, HttpServletRespons Object uri = request.getAttribute("javax.servlet.include.servlet_path"); String portletPath = ""; Map model = null; + { HttpSession session = request.getSession(); String uniqueRequestId = (String) request.getAttribute(WebConstants.INIT_REQ_UNIQUE_ID); @@ -113,7 +117,7 @@ public ModelAndView handleRequest(HttpServletRequest request, HttpServletRespons if (uniqueRequestId.equals(lastRequestId)) { model = (Map) session.getAttribute(WebConstants.OPENMRS_PORTLET_CACHED_MODEL); - // remove cached parameters + // remove cached parameters List parameterKeys = (List) model.get("parameterKeys"); if (parameterKeys != null) { for (String key : parameterKeys) { @@ -161,7 +165,8 @@ public ModelAndView handleRequest(HttpServletRequest request, HttpServletRespons } model.put("parameterKeys", parameterKeys); // so we can clean these up in the next request - // if there's an authenticated user, put them, and their patient set, in the model + // if there's an authenticated user, put them, and their patient set, in the + // model if (Context.getAuthenticatedUser() != null) { model.put("authenticatedUser", Context.getAuthenticatedUser()); } @@ -183,7 +188,11 @@ public ModelAndView handleRequest(HttpServletRequest request, HttpServletRespons // add encounters if this user can view them if (Context.hasPrivilege(PrivilegeConstants.GET_ENCOUNTERS)) { - model.put("patientEncounters", Context.getEncounterService().getEncountersByPatient(p)); + Integer limit = getLimitParameter(request, as, "dashboard.encounters.maximumNumberToShow"); + Integer startIndex = getStartIndexParameter(request); + + model.put("patientEncounters", + Context.getEncounterService().getEncounters(null, p.getPatientId(), startIndex, limit, false)); } // add visits if this user can view them @@ -195,43 +204,57 @@ public ModelAndView handleRequest(HttpServletRequest request, HttpServletRespons } if (Context.hasPrivilege(PrivilegeConstants.GET_OBS)) { - List patientObs = Context.getObsService().getObservationsByPerson(p); - model.put("patientObs", patientObs); - Obs latestWeight = null; - Obs latestHeight = null; + Integer limit = getLimitParameter(request, as, "dashboard.observations.maximumNumberToShow"); + Integer startIndex = getStartIndexParameter(request); + + Person person = (Person) p; + List persons = Collections.singletonList(person); + + // Get paginated observations for display + List paginatedObs = Context.getObsService().getObservations(persons, null, null, null, null, + null, Collections.singletonList("obsDatetime desc"), limit, startIndex, null, null, false); + + model.put("patientObs", paginatedObs); + + // Handle BMI calculation String bmiAsString = "?"; + try { String weightString = as.getGlobalProperty("concept.weight"); + String heightString = as.getGlobalProperty("concept.height"); + ConceptNumeric weightConcept = null; + ConceptNumeric heightConcept = null; + Obs latestWeight = null; + Obs latestHeight = null; + if (StringUtils.hasLength(weightString)) { weightConcept = cs.getConceptNumeric(GeneralUtils.getConcept(weightString).getConceptId()); + List weightObs = Context.getObsService().getObservations(persons, null, + Collections.singletonList(weightConcept), null, null, null, + Collections.singletonList("obsDatetime desc"), 1, null, null, null, false); + if (!weightObs.isEmpty()) { + latestWeight = weightObs.get(0); + model.put("patientWeight", latestWeight); + } } - String heightString = as.getGlobalProperty("concept.height"); - ConceptNumeric heightConcept = null; + if (StringUtils.hasLength(heightString)) { heightConcept = cs.getConceptNumeric(GeneralUtils.getConcept(heightString).getConceptId()); - } - for (Obs obs : patientObs) { - if (obs.getConcept().equals(weightConcept)) { - if (latestWeight == null - || obs.getObsDatetime().compareTo(latestWeight.getObsDatetime()) > 0) { - latestWeight = obs; - } - } else if (obs.getConcept().equals(heightConcept) - && (latestHeight == null || obs.getObsDatetime().compareTo( - latestHeight.getObsDatetime()) > 0)) { - latestHeight = obs; + List heightObs = Context.getObsService().getObservations(persons, null, + Collections.singletonList(heightConcept), null, null, null, + Collections.singletonList("obsDatetime desc"), 1, null, null, null, false); + if (!heightObs.isEmpty()) { + latestHeight = heightObs.get(0); + model.put("patientHeight", latestHeight); } } - if (latestWeight != null) { - model.put("patientWeight", latestWeight); - } - if (latestHeight != null) { - model.put("patientHeight", latestHeight); - } - if (latestWeight != null && latestHeight != null) { + + if (latestWeight != null && latestHeight != null && weightConcept != null + && heightConcept != null) { double weightInKg; double heightInM; + if (weightConcept.getUnits().equalsIgnoreCase("kg")) { weightInKg = latestWeight.getValueNumeric(); } else if (weightConcept.getUnits().equalsIgnoreCase("lb")) { @@ -240,6 +263,7 @@ public ModelAndView handleRequest(HttpServletRequest request, HttpServletRespons throw new IllegalArgumentException("Can't handle units of weight concept: " + weightConcept.getUnits()); } + if (heightConcept.getUnits().equalsIgnoreCase("cm")) { heightInM = latestHeight.getValueNumeric() / 100; } else if (heightConcept.getUnits().equalsIgnoreCase("m")) { @@ -250,6 +274,7 @@ public ModelAndView handleRequest(HttpServletRequest request, HttpServletRespons throw new IllegalArgumentException("Can't handle units of height concept: " + heightConcept.getUnits()); } + double bmi = weightInKg / (heightInM * heightInM); model.put("patientBmi", bmi); String temp = "" + bmi; @@ -257,10 +282,9 @@ public ModelAndView handleRequest(HttpServletRequest request, HttpServletRespons } } catch (Exception ex) { - if (latestWeight != null && latestHeight != null) { - log.error("Failed to calculate BMI even though a weight and height were found", ex); - } + log.error("Failed to calculate BMI", ex); } + model.put("patientBmiAsString", bmiAsString); } else { model.put("patientObs", new HashSet()); @@ -354,7 +378,8 @@ public ModelAndView handleRequest(HttpServletRequest request, HttpServletRespons } } - // if an encounter id is available, put "encounter" and "encounterObs" in the model + // if an encounter id is available, put "encounter" and "encounterObs" in the + // model o = request.getAttribute("org.openmrs.portlet.encounterId"); if (o != null && !model.containsKey("encounterId")) { if (!model.containsKey("encounter") && Context.hasPrivilege(PrivilegeConstants.GET_ENCOUNTERS)) { @@ -425,4 +450,35 @@ public ModelAndView handleRequest(HttpServletRequest request, HttpServletRespons protected void populateModel(HttpServletRequest request, Map model) { } + private Integer getStartIndexParameter(HttpServletRequest request) { + try { + String startIndexParam = request.getParameter("startIndex"); + if (StringUtils.hasText(startIndexParam)) { + return Integer.parseInt(startIndexParam); + } + } + catch (NumberFormatException e) { + log.debug("Unable to parse startIndex parameter, using default", e); + } + return 0; // Return first page if not specified or invalid + } + + private Integer getLimitParameter(HttpServletRequest request, AdministrationService as, String globalPropertyKey) { + try { + String limitParam = request.getParameter("limit"); + if (StringUtils.hasText(limitParam)) { + return Integer.parseInt(limitParam); + } + + String globalLimit = as.getGlobalProperty(globalPropertyKey); + if (StringUtils.hasText(globalLimit)) { + return Integer.parseInt(globalLimit); + } + } + catch (NumberFormatException e) { + log.debug("Unable to parse limit parameter, using default", e); + } + return DEFAULT_PAGE_SIZE; + } + } diff --git a/omod/src/main/resources/config.xml b/omod/src/main/resources/config.xml index 4e78b785..b96f2e3a 100644 --- a/omod/src/main/resources/config.xml +++ b/omod/src/main/resources/config.xml @@ -382,6 +382,13 @@ Allows one to limit the number of encounters shown on the form entry tab of the patient dashboard specifically + + dashboard.observations.maximumNumberToShow + + + Allows one to limit the number of observations shown on the patient dashboard + specifically. + + -