Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EA-202 - Review and update ADT Reporting Definitions to reflect API c… #242

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
@@ -1,10 +1,16 @@
package org.openmrs.module.emrapi.adt.reporting.definition;

import lombok.Getter;
import lombok.Setter;
import org.openmrs.Location;
import org.openmrs.module.emrapi.disposition.DispositionType;
import org.openmrs.module.reporting.data.BaseDataDefinition;
import org.openmrs.module.reporting.data.visit.definition.VisitDataDefinition;
import org.openmrs.module.reporting.definition.configuration.ConfigurationProperty;
import org.openmrs.module.reporting.definition.configuration.ConfigurationPropertyCachingStrategy;
import org.openmrs.module.reporting.evaluation.caching.Caching;

import java.util.List;
import java.util.Map;

/**
Expand All @@ -22,11 +28,21 @@
* via getPrimaryDiagnoses(Encounter) method of the DiagnosisService)
*/
@Caching(strategy=ConfigurationPropertyCachingStrategy.class)
@Getter
@Setter
public class MostRecentAdmissionRequestVisitDataDefinition extends BaseDataDefinition implements VisitDataDefinition {


public static final long serialVersionUID = 1L;

@ConfigurationProperty
private Location visitLocation;

@ConfigurationProperty
private List<Location> dispositionLocations;

@ConfigurationProperty
private List<DispositionType> dispositionTypes;

/**
* Default Constructor
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,49 +1,35 @@
package org.openmrs.module.emrapi.adt.reporting.evaluator;

import org.openmrs.Location;
import org.openmrs.Visit;
import org.openmrs.annotation.Handler;
import org.openmrs.annotation.OpenmrsProfile;
import org.openmrs.module.emrapi.adt.AdtService;
import org.openmrs.module.emrapi.adt.reporting.definition.MostRecentAdmissionRequestVisitDataDefinition;
import org.openmrs.module.emrapi.adt.reporting.query.AwaitingAdmissionVisitQuery;
import org.openmrs.module.reporting.data.visit.EvaluatedVisitData;
import org.openmrs.module.reporting.data.visit.service.VisitDataService;
import org.openmrs.module.reporting.evaluation.EvaluationContext;
import org.openmrs.module.reporting.evaluation.EvaluationException;
import org.openmrs.module.reporting.evaluation.context.VisitEvaluationContext;
import org.openmrs.module.reporting.query.visit.VisitQueryResult;
import org.openmrs.module.reporting.query.visit.definition.VisitQuery;
import org.openmrs.module.reporting.query.visit.evaluator.VisitQueryEvaluator;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collection;
import java.util.List;
import java.util.Collections;

@Handler(supports = AwaitingAdmissionVisitQuery.class)
@OpenmrsProfile(modules = { "reporting:*" })
public class AwaitingAdmissionVisitQueryEvaluator implements VisitQueryEvaluator {

@Autowired
AdtService adtService;
private VisitDataService visitDataService;

@Override
public VisitQueryResult evaluate(VisitQuery visitQuery, EvaluationContext evaluationContext) throws EvaluationException {
AwaitingAdmissionVisitQuery eq = (AwaitingAdmissionVisitQuery) visitQuery;
Location location = eq.getLocation();
Collection<Integer> patientIds = null;
Collection<Integer> visitIds = null;
if (evaluationContext.getBaseCohort() != null) {
patientIds = evaluationContext.getBaseCohort().getMemberIds();
}
if (evaluationContext instanceof VisitEvaluationContext) {
VisitEvaluationContext visitEvaluationContext = (VisitEvaluationContext) evaluationContext;
if (visitEvaluationContext.getBaseVisits() != null) {
visitIds = visitEvaluationContext.getBaseVisits().getMemberIds();
}
}
List<Visit> results = adtService.getVisitsAwaitingAdmission(location, patientIds, visitIds);
VisitQueryResult result = new VisitQueryResult(visitQuery, evaluationContext);
for (Visit v : results) {
result.add(v.getVisitId());
}
AwaitingAdmissionVisitQuery query = (AwaitingAdmissionVisitQuery) visitQuery;
MostRecentAdmissionRequestVisitDataDefinition dataDef = new MostRecentAdmissionRequestVisitDataDefinition();
dataDef.setVisitLocation(query.getLocation());
EvaluatedVisitData visitData = visitDataService.evaluate(dataDef, evaluationContext);
result.addAll(visitData.getData().keySet());
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,23 @@

import org.openmrs.Encounter;
import org.openmrs.EncounterProvider;
import org.openmrs.Location;
import org.openmrs.Obs;
import org.openmrs.Provider;
import org.openmrs.annotation.Handler;
import org.openmrs.annotation.OpenmrsProfile;
import org.openmrs.api.LocationService;
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.adt.reporting.definition.MostRecentAdmissionRequestVisitDataDefinition;
import org.openmrs.module.emrapi.adt.util.AdtUtil;
import org.openmrs.module.emrapi.concept.EmrConceptService;
import org.openmrs.module.emrapi.diagnosis.DiagnosisService;
import org.openmrs.module.emrapi.disposition.DispositionDescriptor;
import org.openmrs.module.emrapi.disposition.DispositionService;
import org.openmrs.module.reporting.data.visit.EvaluatedVisitData;
import org.openmrs.module.reporting.data.visit.definition.VisitDataDefinition;
import org.openmrs.module.reporting.data.visit.evaluator.VisitDataEvaluator;
import org.openmrs.module.reporting.evaluation.EvaluationContext;
import org.openmrs.module.reporting.evaluation.EvaluationException;
import org.openmrs.module.reporting.evaluation.querybuilder.HqlQueryBuilder;
import org.openmrs.module.reporting.evaluation.service.EvaluationService;
import org.openmrs.module.reporting.evaluation.context.VisitEvaluationContext;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -36,99 +31,51 @@
public class MostRecentAdmissionRequestVisitDataEvaluator implements VisitDataEvaluator {

@Autowired
private LocationService locationService;

@Autowired
private DispositionService dispositionService;
AdtService adtService;

@Autowired
private DiagnosisService diagnosisService;

@Autowired
private EmrConceptService emrConceptService;

@Autowired
private EvaluationService evaluationService;

@Override
public EvaluatedVisitData evaluate(VisitDataDefinition visitDataDefinition, EvaluationContext evaluationContext) throws EvaluationException {

EvaluatedVisitData data = new EvaluatedVisitData(visitDataDefinition, evaluationContext);

HqlQueryBuilder query = new HqlQueryBuilder();

query.select("encounter.visit.id, encounter").from(Encounter.class, "encounter")
.innerJoin("encounter.obs", "dispo")
.whereEqual("dispo.concept", dispositionService.getDispositionDescriptor().getDispositionConcept())
.whereIn("dispo.valueCoded", AdtUtil.getAdmissionDispositionsConcepts(emrConceptService, dispositionService))
.whereEqual("dispo.voided", false)
.whereEqual("encounter.voided", false)
.whereVisitIn("encounter.visit.id", evaluationContext);


List<Object[]> result = evaluationService.evaluateToList(query, evaluationContext);

for (Object[] row : result) {

Integer visitId = (Integer) row[0];
Encounter encounter = (Encounter) row[1];

// if there are multiple admission requests on the visit, we only want the most recent one
if (data.getData().containsKey(visitId)) {
Map<String,Object> resultRow = (Map<String,Object>) data.getData().get(visitId);

if (encounter.getEncounterDatetime().before((Date) resultRow.get("datetime"))) {
continue;
}
public EvaluatedVisitData evaluate(VisitDataDefinition visitDataDefinition, EvaluationContext context) throws EvaluationException {
EvaluatedVisitData data = new EvaluatedVisitData(visitDataDefinition, context);
InpatientRequestSearchCriteria criteria = new InpatientRequestSearchCriteria();
if (context instanceof VisitEvaluationContext) {
VisitEvaluationContext visitEvaluationContext = (VisitEvaluationContext) context;
if (visitEvaluationContext.getBaseVisits() != null && !visitEvaluationContext.getBaseVisits().isEmpty()) {
criteria.setVisitIds(new ArrayList<>(visitEvaluationContext.getBaseVisits().getMemberIds()));
}

Map<String,Object> resultRow = new HashMap<String, Object>();

}
if (context.getBaseCohort() != null && !context.getBaseCohort().isEmpty()) {
criteria.setPatientIds(new ArrayList<>(context.getBaseCohort().getMemberIds()));
}
MostRecentAdmissionRequestVisitDataDefinition definition = (MostRecentAdmissionRequestVisitDataDefinition) visitDataDefinition;
criteria.setVisitLocation(definition.getVisitLocation());
criteria.setDispositionLocations(definition.getDispositionLocations());
criteria.setDispositionTypes(definition.getDispositionTypes());

List<InpatientRequest> inpatientRequests = adtService.getInpatientRequests(criteria);
for (InpatientRequest request : inpatientRequests) {
Integer visitId = request.getVisit().getVisitId();
Encounter encounter = request.getDispositionEncounter();
Map<String,Object> resultRow = new HashMap<>();
resultRow.put("fromLocation", encounter.getLocation());
resultRow.put("toLocation", getToLocation(encounter, dispositionService, locationService));
resultRow.put("toLocation", request.getDispositionLocation());
resultRow.put("datetime", encounter.getEncounterDatetime());
resultRow.put("provider", getProvider(encounter));
resultRow.put("diagnoses", diagnosisService.getPrimaryDiagnoses(encounter));

data.getData().put(visitId, resultRow);
}


return data;

}


// TODO: right now this just returns the first (non-voided) provider on an encounter, need to fix
private Provider getProvider(Encounter encounter) {

// TODO: right now this just returns the first (non-voided) provider on an encounter
// TODO: need to fix

for (EncounterProvider encounterProvider : encounter.getEncounterProviders()) {
if (!encounterProvider.isVoided()) {
return encounterProvider.getProvider();
}
}

return null;

}

private Location getToLocation(Encounter encounter, DispositionService dispositionService, LocationService locationService) {

// TODO: the assumption here is that there is only one disposition request per encounter
// TODO and that the disposition is on the top level

DispositionDescriptor dispositionDescriptor = dispositionService.getDispositionDescriptor();

for (Obs obs : encounter.getObsAtTopLevel(false)) {
if (dispositionDescriptor.isDisposition(obs)) {
return dispositionDescriptor.getAdmissionLocation(obs, locationService);
}

}

return null;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.openmrs.module.emrapi.adt.reporting.query;

import lombok.Getter;
import lombok.Setter;
import org.openmrs.Location;
import org.openmrs.Visit;
import org.openmrs.module.reporting.definition.configuration.ConfigurationProperty;
Expand All @@ -18,17 +20,11 @@
* If a location is specified, restricts the query to only visits that have the chosen location as a visit location
*/
@Caching(strategy=ConfigurationPropertyCachingStrategy.class)
@Getter
@Setter
public class AwaitingAdmissionVisitQuery extends BaseQuery<Visit> implements VisitQuery {

@ConfigurationProperty
private Location location;

public Location getLocation() {
return location;
}

public void setLocation(Location location) {
this.location = location;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,7 @@ public void shouldNotReturnSameVisitTwice() throws Exception {
.encounterType(emrApiProperties.getVisitNoteEncounterType())
.visit(visit)
.save();
testDataManager.obs()
.person(patient)
.encounter(visitNoteEncounter2)
.concept(dispositionDescriptor.getDispositionConcept())
.value(admitToHospital)
.save();
createDispositionObs(visitNoteEncounter2, admitToHospital);

VisitQueryResult result = visitQueryService.evaluate(query, null);
assertThat(result.getMemberIds().size(), is(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ public class MostRecentAdmissionRequestVisitDataEvaluatorTest extends BaseReport
@Autowired
private EmrApiProperties emrApiProperties;

@Autowired
private EmrConceptService emrConceptService;

private DispositionDescriptor dispositionDescriptor;

private DiagnosisMetadata diagnosisMetadata;
Expand All @@ -69,10 +66,10 @@ public void setup() throws Exception {
executeDataSet("baseTestDataset.xml");
def = new MostRecentAdmissionRequestVisitDataDefinition();
dispositionDescriptor = ContextSensitiveMetadataTestUtils.setupDispositionDescriptor(conceptService, dispositionService);
ContextSensitiveMetadataTestUtils.setupAdmissionDecisionConcept(conceptService, emrApiProperties);
diagnosisMetadata = ContextSensitiveMetadataTestUtils.setupDiagnosisMetadata(conceptService, emrApiProperties);
context = new VisitEvaluationContext();
Context.getAdministrationService().setGlobalProperty(EmrApiConstants.GP_USE_LEGACY_DIAGNOSIS_SERVICE, "true");
Context.flushSession();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,7 @@ public List<Location> getInpatientLocations() {

@Override
@Transactional(readOnly = true)
@Deprecated
public List<Visit> getVisitsAwaitingAdmission(Location location, Collection<Integer> patientIds, Collection<Integer> visitIds) {
Location visitLocation = null ;
if (location != null ) {
Expand Down
Loading