Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,7 +30,11 @@
public class LabIntegrationReportServiceImpl extends BaseOpenmrsService implements LabIntegrationReportService {

@Override
public List<Obs> getLabResults(Date startDate, Date endDate) {
public List<Obs> 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();
Expand All @@ -52,7 +57,7 @@ public List<Obs> getLabResults(Date startDate, Date endDate) {
}

List<Obs> orders = obsService.getObservations(null, null, orderConcepts, null, null, null, null, null, null,
startDate, endDate, false, null);
startDate, endDate, false);

Set<Person> persons = new LinkedHashSet<>(orders.size());
Set<Concept> resultTests = new LinkedHashSet<>(orders.size());
Expand All @@ -70,9 +75,14 @@ public List<Obs> getLabResults(Date startDate, Date endDate) {
if (freeTextResults != null) {
resultTests.add(freeTextResults);
}
List<Obs> 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<Obs> 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<Integer> resultEncounterIds = resultEncounters.stream().map(Encounter::getId).collect(Collectors.toSet());

Expand All @@ -89,10 +99,12 @@ public List<Obs> getLabResults(Date startDate, Date endDate) {
orderPersons.add(order.getPerson());
orderEncounters.add(order.getEncounter());
}

List<Obs> 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<Obs> 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) {
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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() : "";
}
}