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

RA-2044 - Update awaiting admission page to use the latest ADT api me… #487

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,114 +1,104 @@
package org.openmrs.module.coreapps.page.controller.adt;


import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.StopWatch;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.api.context.Context;
import org.openmrs.Encounter;
import org.openmrs.EncounterProvider;
import org.openmrs.Patient;
import org.openmrs.PatientIdentifier;
import org.openmrs.PatientIdentifierType;
import org.openmrs.Provider;
import org.openmrs.api.AdministrationService;
import org.openmrs.api.PatientService;
import org.openmrs.module.appframework.domain.AppDescriptor;
import org.openmrs.module.appframework.domain.Extension;
import org.openmrs.module.appframework.service.AppFrameworkService;
import org.openmrs.module.coreapps.CoreAppsConstants;
import org.openmrs.module.coreapps.CoreAppsProperties;
import org.openmrs.module.emrapi.EmrApiConstants;
import org.openmrs.module.emrapi.adt.reporting.query.AwaitingAdmissionVisitQuery;
import org.openmrs.module.reporting.common.SortCriteria;
import org.openmrs.module.reporting.data.converter.MapElementConverter;
import org.openmrs.module.reporting.data.converter.ObjectFormatter;
import org.openmrs.module.reporting.data.converter.PropertyConverter;
import org.openmrs.module.reporting.data.patient.definition.PatientDataDefinition;
import org.openmrs.module.reporting.data.visit.definition.VisitDataDefinition;
import org.openmrs.module.reporting.dataset.DataSet;
import org.openmrs.module.reporting.dataset.DataSetUtil;
import org.openmrs.module.reporting.dataset.definition.VisitDataSetDefinition;
import org.openmrs.module.reporting.dataset.definition.service.DataSetDefinitionService;
import org.openmrs.module.reporting.definition.library.AllDefinitionLibraries;
import org.openmrs.module.reporting.evaluation.EvaluationContext;
import org.openmrs.module.reporting.evaluation.EvaluationException;
import org.openmrs.module.emrapi.EmrApiProperties;
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.diagnosis.Diagnosis;
import org.openmrs.module.emrapi.diagnosis.DiagnosisService;
import org.openmrs.module.emrapi.utils.GeneralUtils;
import org.openmrs.ui.framework.annotation.SpringBean;
import org.openmrs.ui.framework.page.PageModel;
import org.openmrs.util.OpenmrsUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class AwaitingAdmissionPageController {
private final Log log = LogFactory.getLog(getClass());

public void get(PageModel model,
@SpringBean AllDefinitionLibraries libraries,
@SpringBean DataSetDefinitionService dsdService,
@SpringBean("patientService") PatientService patientService,
@SpringBean("adtService") AdtService adtService,
@SpringBean("emrApiProperties") EmrApiProperties emrApiProperties,
@SpringBean("emrDiagnosisService") DiagnosisService diagnosisService,
@SpringBean("adminService") AdministrationService adminService,
@SpringBean CoreAppsProperties coreAppsProperties,
@SpringBean("appFrameworkService") AppFrameworkService appFrameworkService) throws EvaluationException {
@SpringBean("appFrameworkService") AppFrameworkService appFrameworkService) {

EvaluationContext context = new EvaluationContext();
List<Extension> admissionActions = appFrameworkService.getExtensionsForCurrentUser("coreapps.app.awaitingAdmissionActions");
Collections.sort(admissionActions);
model.addAttribute("admissionActions", admissionActions);

DataSet dataSet = null;

VisitDataSetDefinition dsd = new VisitDataSetDefinition();
AwaitingAdmissionVisitQuery query = new AwaitingAdmissionVisitQuery();
dsd.addRowFilter(query, null);

dsd.addColumn("visitId",
libraries.getDefinition(VisitDataDefinition.class, "reporting.library.visitDataDefinition.builtIn.visitId"), "" );

dsd.addColumn("patientId",
libraries.getDefinition(PatientDataDefinition.class, "reporting.library.patientDataDefinition.builtIn.patientId"), "");

dsd.addColumn("patientLastName",
libraries.getDefinition(PatientDataDefinition.class, "reporting.library.patientDataDefinition.builtIn.preferredName.familyName"), "");

dsd.addColumn("patientFirstName",
libraries.getDefinition(PatientDataDefinition.class, "reporting.library.patientDataDefinition.builtIn.preferredName.givenName"), "");

dsd.addColumn("primaryIdentifier",
libraries.getDefinition(PatientDataDefinition.class, "emrapi.patientDataDefinition.primaryIdentifier"),
"", new PropertyConverter(String.class, "identifier"));

dsd.addColumn("mostRecentAdmissionRequestFromLocation",
libraries.getDefinition(VisitDataDefinition.class, "emrapi.visitDataDefinition.mostRecentAdmissionRequest"),
"", new MapElementConverter("fromLocation", new ObjectFormatter()));

dsd.addColumn("mostRecentAdmissionRequestToLocation",
libraries.getDefinition(VisitDataDefinition.class, "emrapi.visitDataDefinition.mostRecentAdmissionRequest"),
"", new MapElementConverter("toLocation", new ObjectFormatter()));

dsd.addColumn("mostRecentAdmissionRequestDatetime",
libraries.getDefinition(VisitDataDefinition.class, "emrapi.visitDataDefinition.mostRecentAdmissionRequest"),
"", new MapElementConverter("datetime", null));

dsd.addColumn("mostRecentAdmissionRequestProvider",
libraries.getDefinition(VisitDataDefinition.class, "emrapi.visitDataDefinition.mostRecentAdmissionRequest"),
"", new MapElementConverter("provider", new ObjectFormatter()));

dsd.addColumn("mostRecentAdmissionRequestDiagnoses",
libraries.getDefinition(VisitDataDefinition.class, "emrapi.visitDataDefinition.mostRecentAdmissionRequest"),
"", new MapElementConverter("diagnoses", new AwaitingAdmissionDiagnosisFormatter()));

model.addAttribute("paperRecordIdentifierDefinitionAvailable", false);
String gpPaperRecordIdDef = Context.getAdministrationService().getGlobalProperty("emr.paperRecordIdentifierType");
// add the paper record identifier, if the definition is available (provided by the paper record module)
PatientIdentifierType paperRecordIdentifierType = null;
String gpPaperRecordIdDef = adminService.getGlobalProperty("emr.paperRecordIdentifierType");
if (StringUtils.isNotBlank(gpPaperRecordIdDef)) {
PatientDataDefinition paperRecordIdentifierDefinition = libraries.getDefinition(PatientDataDefinition.class, "paperrecord.patientDataDefinition.paperRecordIdentifier");
if (paperRecordIdentifierDefinition != null) {
model.addAttribute("paperRecordIdentifierDefinitionAvailable", true);
dsd.addColumn("paperRecordIdentifier", paperRecordIdentifierDefinition, "",
new PropertyConverter(String.class, "identifier"));
paperRecordIdentifierType = GeneralUtils.getPatientIdentifierType(gpPaperRecordIdDef, patientService);
}
model.addAttribute("paperRecordIdentifierDefinitionAvailable", (paperRecordIdentifierType != null));

log.warn("Searching for inpatient requests");
InpatientRequestSearchCriteria criteria = new InpatientRequestSearchCriteria();
StopWatch sw = new StopWatch();
sw.start();
List<InpatientRequest> requests = adtService.getInpatientRequests(criteria);
sw.stop();
log.warn("Found " + requests.size() + " inpatient requests in " + sw);

sw.reset();
sw.start();
List<Map<String, Object>> rows = new ArrayList<>();
for (InpatientRequest request : requests) {
Map<String, Object> row = new LinkedHashMap<>();
row.put("visitId", request.getVisit().getId());
row.put("patientId", request.getPatient().getId());
row.put("patientLastName", request.getPatient().getPersonName().getFamilyName());
row.put("patientFirstName", request.getPatient().getPersonName().getGivenName());
row.put("primaryIdentifier", getIdentifier(request.getPatient(), emrApiProperties.getPrimaryIdentifierType()));
row.put("mostRecentAdmissionRequestFromLocation", request.getDispositionEncounter().getLocation());
row.put("mostRecentAdmissionRequestToLocation", request.getDispositionLocation());
row.put("mostRecentAdmissionRequestDatetime", request.getDispositionEncounter().getEncounterDatetime());
row.put("mostRecentAdmissionRequestProvider", getProvider(request.getDispositionEncounter()));
row.put("mostRecentAdmissionRequestDiagnoses", getPrimaryDiagnoses(request.getDispositionEncounter(), diagnosisService));
if (paperRecordIdentifierType != null) {
row.put("paperRecordIdentifier", getIdentifier(request.getPatient(), paperRecordIdentifierType));
}
rows.add(row);
}

dsd.addSortCriteria("mostRecentAdmissionRequestDatetime", SortCriteria.SortDirection.ASC);

dataSet = dsdService.evaluate(dsd, context);
model.addAttribute("awaitingAdmissionList", DataSetUtil.simplify(dataSet));
String sortBy = "mostRecentAdmissionRequestDatetime";
rows.sort((r1, r2) -> OpenmrsUtil.compare((Date)r1.get(sortBy), (Date)r2.get(sortBy)));
model.addAttribute("awaitingAdmissionList", rows);
sw.stop();
log.warn("Retrieved data from inpatient requests in: " + sw);

// add location tag constants
model.addAttribute("supportsAdmissionLocationTag", EmrApiConstants.LOCATION_TAG_SUPPORTS_ADMISSION);
model.addAttribute("supportsLoginLocationTag", EmrApiConstants.LOCATION_TAG_SUPPORTS_LOGIN);

// used to determine whether or not we display a link to the patient in the results list
// used to determine whether we display a link to the patient in the results list
model.addAttribute("privilegePatientDashboard", CoreAppsConstants.PRIVILEGE_PATIENT_DASHBOARD);

AppDescriptor app = appFrameworkService.getApp(CoreAppsConstants.AWAITING_ADMISSION);
Expand All @@ -117,4 +107,25 @@ public void get(PageModel model,
model.addAttribute("patientPageUrl", patientPageUrl);
}

protected String getIdentifier(Patient patient, PatientIdentifierType type) {
if (type != null) {
PatientIdentifier pi = patient.getPatientIdentifier(type);
if (pi != null) {
return pi.getIdentifier();
}
}
return "";
}

protected Provider getProvider(Encounter encounter) {
for (EncounterProvider encounterProvider : encounter.getActiveEncounterProviders()) {
return encounterProvider.getProvider();
}
return null;
}

protected String getPrimaryDiagnoses(Encounter encounter, DiagnosisService diagnosisService) {
List<Diagnosis> diagnoses = diagnosisService.getPrimaryDiagnoses(encounter);
return (String) new AwaitingAdmissionDiagnosisFormatter().convert(diagnoses);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
float: right;
margin-top: 5px;
margin-bottom: 5px;
width: 100%;
text-align: right;
}

#content h2 {
Expand Down