diff --git a/api/src/main/java/org/openmrs/module/labintegration/api/impl/LabIntegrationReportServiceImpl.java b/api/src/main/java/org/openmrs/module/labintegration/api/impl/LabIntegrationReportServiceImpl.java index 22bf3d4..7baeb07 100644 --- a/api/src/main/java/org/openmrs/module/labintegration/api/impl/LabIntegrationReportServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/labintegration/api/impl/LabIntegrationReportServiceImpl.java @@ -14,6 +14,7 @@ import java.util.ArrayList; import java.util.Arrays; +import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.Date; @@ -29,7 +30,11 @@ public class LabIntegrationReportServiceImpl extends BaseOpenmrsService implements LabIntegrationReportService { @Override - public List getLabResults(Date startDate, Date endDate) { + public List getLabResults(Date inputStartDate, Date inputEndDate) { + + Date startDate = getStartOfDay(inputStartDate); + Date endDate = getEndOfDay(inputEndDate); + ConceptService conceptService = Context.getConceptService(); ObsService obsService = Context.getObsService(); ObsSelector obsSelector = new ObsSelector(); @@ -52,7 +57,7 @@ public List getLabResults(Date startDate, Date endDate) { } List orders = obsService.getObservations(null, null, orderConcepts, null, null, null, null, null, null, - startDate, endDate, false, null); + startDate, endDate, false); Set persons = new LinkedHashSet<>(orders.size()); Set resultTests = new LinkedHashSet<>(orders.size()); @@ -70,9 +75,14 @@ public List getLabResults(Date startDate, Date endDate) { if (freeTextResults != null) { resultTests.add(freeTextResults); } - List testResults = obsService.getObservations(new ArrayList<>(persons), new ArrayList<>(resultEncounters), - new ArrayList<>(resultTests), null, null, null, Arrays.asList("obsDatetime desc", "obsId asc"), null, null, null, - null, false); + List testResults = new ArrayList<>(); + + if (!persons.isEmpty() && !resultEncounters.isEmpty()) { + testResults = obsService.getObservations(new ArrayList<>(persons), new ArrayList<>(resultEncounters), + new ArrayList<>(resultTests), null, null, null, Arrays.asList("obsDatetime desc", "obsId asc"), null, null, + startDate, endDate, false); + + } Set resultEncounterIds = resultEncounters.stream().map(Encounter::getId).collect(Collectors.toSet()); @@ -89,10 +99,12 @@ public List getLabResults(Date startDate, Date endDate) { orderPersons.add(order.getPerson()); orderEncounters.add(order.getEncounter()); } - - List orderResults = obsService.getObservations(new ArrayList<>(orderPersons), new ArrayList<>(orderEncounters), - Collections.singletonList(labOrderConcept), null, null, null, Arrays.asList("obsDatetime desc", "obsId asc"), - null, null, null, null, false); + List orderResults = new ArrayList<>(); + if (!orderPersons.isEmpty() && !orderEncounters.isEmpty()) { + orderResults = obsService.getObservations(new ArrayList<>(orderPersons), new ArrayList<>(orderEncounters), + Collections.singletonList(labOrderConcept), null, null, null, Arrays.asList("obsDatetime desc", "obsId asc"), + null, null, startDate, endDate, false); + } if (testResults != null) { if (orderResults != null) { @@ -117,4 +129,24 @@ private Obs translateToDisplayResultTest(Obs obs) { displayResult.setValueText(""); return displayResult; } + + public static Date getStartOfDay(Date date) { + Calendar cal = Calendar.getInstance(); + cal.setTime(date); + cal.set(Calendar.HOUR_OF_DAY, 0); + cal.set(Calendar.MINUTE, 0); + cal.set(Calendar.SECOND, 0); + cal.set(Calendar.MILLISECOND, 0); + return cal.getTime(); + } + + public static Date getEndOfDay(Date date) { + Calendar cal = Calendar.getInstance(); + cal.setTime(date); + cal.set(Calendar.HOUR_OF_DAY, 23); + cal.set(Calendar.MINUTE, 59); + cal.set(Calendar.SECOND, 59); + cal.set(Calendar.MILLISECOND, 999); + return cal.getTime(); + } } diff --git a/api/src/main/java/org/openmrs/module/labintegration/api/reports/LabResultDataSetEvaluator.java b/api/src/main/java/org/openmrs/module/labintegration/api/reports/LabResultDataSetEvaluator.java index e4a5f02..460d9e6 100644 --- a/api/src/main/java/org/openmrs/module/labintegration/api/reports/LabResultDataSetEvaluator.java +++ b/api/src/main/java/org/openmrs/module/labintegration/api/reports/LabResultDataSetEvaluator.java @@ -1,6 +1,7 @@ package org.openmrs.module.labintegration.api.reports; import org.openmrs.Concept; +import org.openmrs.ConceptNumeric; import org.openmrs.Location; import org.openmrs.LocationAttribute; import org.openmrs.Obs; @@ -114,7 +115,7 @@ public DataSet evaluate(DataSetDefinition dataSetDefinition, EvaluationContext e value = obs.getObsDatetime(); break; case COLUMN_RESULT: - value = obsValueConverter.convert(obs); + value = obsValueConverter.convert(obs) + getUnits(obs); break; } if (value != null) { @@ -158,4 +159,9 @@ protected String getPatientIdentifier(Integer personId, String locationCode) { PatientIdentifier pid = patient.getPatientIdentifier(pit); return pid != null ? pid.getIdentifier() : locationCode + '4' + personId; } + + protected String getUnits(Obs obs) { + ConceptNumeric conceptNumeric = Context.getConceptService().getConceptNumeric(obs.getConcept().getId()); + return conceptNumeric != null && obs.getValueNumeric() != null ? " " + conceptNumeric.getUnits() : ""; + } }