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

Ward Level DashBoard API Changes #30

Merged
merged 5 commits into from
Feb 19, 2024
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
5 changes: 5 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>bedmanagement-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.openmrs.web</groupId>
<artifactId>openmrs-web</artifactId>
Expand Down
15 changes: 15 additions & 0 deletions api/src/main/java/org/openmrs/module/ipd/api/dao/WardDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.openmrs.module.ipd.api.dao;

import org.openmrs.Location;
import org.openmrs.module.ipd.api.model.AdmittedPatient;
import org.openmrs.module.ipd.api.model.WardPatientsSummary;

import java.util.List;

public interface WardDAO {

List<AdmittedPatient> getAdmittedPatients(Location location);

WardPatientsSummary getWardPatientSummary(Location location);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.openmrs.module.ipd.api.dao.impl;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.openmrs.Location;
import org.openmrs.module.ipd.api.dao.WardDAO;
import org.openmrs.module.ipd.api.model.AdmittedPatient;
import org.openmrs.module.ipd.api.model.WardPatientsSummary;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import org.hibernate.query.Query;

import java.util.ArrayList;
import java.util.List;

@Repository
public class HibernateWardDAO implements WardDAO {

private static final Logger log = LoggerFactory.getLogger(HibernateWardDAO.class);

private final SessionFactory sessionFactory;

@Autowired
public HibernateWardDAO(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

@Override
public List<AdmittedPatient> getAdmittedPatients(Location location) {
Session session = this.sessionFactory.getCurrentSession();
try {
Query query = session.createQuery("select NEW org.openmrs.module.ipd.api.model.AdmittedPatient(assignment," +
"(COUNT(DISTINCT o.orderId) - COUNT (DISTINCT s.order.orderId)))" +
"from org.openmrs.module.bedmanagement.entity.BedPatientAssignment assignment " +
"JOIN org.openmrs.Visit v on v.patient = assignment.patient " +
"JOIN org.openmrs.Encounter e on e.visit = v " +
"JOIN org.openmrs.module.bedmanagement.entity.BedLocationMapping locmap on locmap.bed = assignment.bed " +
"JOIN org.openmrs.Location l on locmap.location = l " +
"LEFT JOIN org.openmrs.Order o on o.encounter = e " +
"LEFT JOIN Slot s on s.order = o " +
"where assignment.endDatetime is null and v.stopDatetime is null and l.parentLocation = :location " +
"GROUP BY assignment.patient, v " +
"ORDER BY assignment.startDatetime desc");

query.setParameter("location", location);
return query.getResultList();
}
catch (Exception ex){
log.error("Exception at WardDAO getAdmittedPatients ",ex.getStackTrace());
}
return new ArrayList<>();
}

@Override
public WardPatientsSummary getWardPatientSummary(Location location) {
Session session = this.sessionFactory.getCurrentSession();
try {
Query query = session.createQuery(
"select NEW org.openmrs.module.ipd.api.model.WardPatientsSummary(COUNT(assignment)) " +
"from org.openmrs.module.bedmanagement.entity.BedPatientAssignment assignment " +
"JOIN org.openmrs.module.bedmanagement.entity.BedLocationMapping locmap on locmap.bed = assignment.bed " +
"JOIN org.openmrs.Location l on locmap.location = l " +
"JOIN org.openmrs.Visit v on v.patient = assignment.patient " +
"where assignment.endDatetime is null and v.stopDatetime is null and l.parentLocation = :location");
query.setParameter("location", location);
return (WardPatientsSummary) query.getSingleResult();
} catch (Exception e) {
log.error("Exception at WardDAO getAdmittedPatients ",e.getStackTrace());
}
return new WardPatientsSummary();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.openmrs.module.ipd.api.model;

import org.openmrs.module.bedmanagement.entity.BedPatientAssignment;

public class AdmittedPatient extends BedPatientAssignment {

private Long newTreatments;

public AdmittedPatient(BedPatientAssignment bedPatientAssignment, Long newTreatments){
super.setId(bedPatientAssignment.getId());
super.setBed(bedPatientAssignment.getBed());
super.setPatient(bedPatientAssignment.getPatient());
super.setEncounter(bedPatientAssignment.getEncounter());
super.setStartDatetime(bedPatientAssignment.getStartDatetime());
super.setEndDatetime(bedPatientAssignment.getEndDatetime());
super.setUuid(bedPatientAssignment.getUuid());
super.setVoided(bedPatientAssignment.getVoided());
super.setVoidReason(bedPatientAssignment.getVoidReason());
this.newTreatments=newTreatments;
}

public void setNewTreatments(Long newTreatments) {
this.newTreatments = newTreatments;
}

public Long getNewTreatments() {
return newTreatments;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.openmrs.module.ipd.api.model;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@NoArgsConstructor
@AllArgsConstructor
@Getter
public class IPDWardPatientDetails {

private List<AdmittedPatient> activePatients;
private WardPatientsSummary ipdWardWardPatientsSummary;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.openmrs.module.ipd.api.model;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
public class WardPatientsSummary {
private Long totalPatients = 0L;

// to be added in future
// private Integer myPatients;
// private Integer toBeDischargedPatients;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.openmrs.module.ipd.api.service;

import org.openmrs.module.ipd.api.model.IPDWardPatientDetails;
import org.openmrs.module.ipd.api.model.WardPatientsSummary;


public interface WardService {

WardPatientsSummary getIPDWardPatientSummary(String wardUuid);

IPDWardPatientDetails getWardPatientsByUuid(String wardUuid, Integer offset, Integer limit);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.openmrs.module.ipd.api.service.impl;

import org.openmrs.Location;
import org.openmrs.api.LocationService;
import org.openmrs.api.context.Context;
import org.openmrs.module.ipd.api.dao.WardDAO;
import org.openmrs.module.ipd.api.model.AdmittedPatient;
import org.openmrs.module.ipd.api.model.IPDWardPatientDetails;
import org.openmrs.module.ipd.api.model.WardPatientsSummary;
import org.openmrs.module.ipd.api.service.WardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class WardServiceImpl implements WardService {

private final WardDAO wardDAO;

@Autowired
public WardServiceImpl(WardDAO wardDAO) {
this.wardDAO = wardDAO;
}


@Override
public WardPatientsSummary getIPDWardPatientSummary(String wardUuid) {
Location location= Context.getService(LocationService.class).getLocationByUuid(wardUuid);
return wardDAO.getWardPatientSummary(location);
}

@Override
public IPDWardPatientDetails getWardPatientsByUuid(String wardUuid, Integer offset, Integer limit) {
Location location= Context.getService(LocationService.class).getLocationByUuid(wardUuid);
List<AdmittedPatient> activePatients= wardDAO.getAdmittedPatients(location);
if (activePatients==null){
return new IPDWardPatientDetails(new ArrayList<>(),new WardPatientsSummary());
}
offset = Math.min(offset, activePatients.size());
limit = Math.min(limit, activePatients.size() - offset);

WardPatientsSummary wardPatientsSummary = computePatientStats(activePatients);
return new IPDWardPatientDetails(activePatients.subList(offset, offset + limit), wardPatientsSummary);
}

private WardPatientsSummary computePatientStats(List<AdmittedPatient> activePatients){
WardPatientsSummary wardPatientsSummary =new WardPatientsSummary();
wardPatientsSummary.setTotalPatients(activePatients !=null ? activePatients.size() : 0L);
return wardPatientsSummary;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.openmrs.module.ipd.api.dao.impl;

import org.hibernate.SessionFactory;
import org.junit.Test;
import org.openmrs.module.ipd.api.BaseIntegrationTest;
import org.openmrs.module.ipd.api.dao.WardDAO;
import org.openmrs.module.ipd.api.model.AdmittedPatient;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class HibernateWardDAOIntegrationTest extends BaseIntegrationTest {

@Autowired
private WardDAO wardDAO;

@Autowired
private SessionFactory sessionFactory;


@Test
public void shouldGetAdmittedPatients() {
List<AdmittedPatient> assignments= wardDAO.getAdmittedPatients(null);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.openmrs.module.ipd.contract;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.openmrs.module.ipd.api.model.AdmittedPatient;
import org.openmrs.module.webservices.rest.web.ConversionUtil;
import org.openmrs.module.webservices.rest.web.representation.Representation;

@Getter
@Setter
@Builder
public class AdmittedPatientResponse {

private Object patientDetails;
private Object bedDetails;
private Long newTreatments;

public static AdmittedPatientResponse createFrom(AdmittedPatient admittedPatient) {
return AdmittedPatientResponse.builder().
patientDetails(ConversionUtil.convertToRepresentation(admittedPatient.getPatient(), Representation.DEFAULT)).
bedDetails(ConversionUtil.convertToRepresentation(admittedPatient.getBed(),Representation.REF)).
newTreatments(admittedPatient.getNewTreatments()).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.openmrs.module.ipd.contract;

import lombok.Builder;
import lombok.Getter;
import org.openmrs.module.ipd.api.model.IPDWardPatientDetails;

import java.util.List;
import java.util.stream.Collectors;

@Getter
@Builder
public class IPDWardPatientDetailsResponse {

private List<AdmittedPatientResponse> admittedPatients;
private Long totalPatients;


public static IPDWardPatientDetailsResponse createFrom(IPDWardPatientDetails ipdWardPatientDetails) {
return IPDWardPatientDetailsResponse.builder().
admittedPatients(ipdWardPatientDetails.getActivePatients().stream().map(AdmittedPatientResponse::createFrom).collect(Collectors.toList())).
totalPatients(ipdWardPatientDetails.getIpdWardWardPatientsSummary().getTotalPatients()).
build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.openmrs.module.ipd.contract;


import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.openmrs.module.ipd.api.model.WardPatientsSummary;

@Getter
@Setter
@Builder
public class IPDWardPatientSummaryResponse {

private Long totalPatients;

public static IPDWardPatientSummaryResponse createFrom(WardPatientsSummary wardPatientsSummary){
return IPDWardPatientSummaryResponse.builder().
totalPatients(wardPatientsSummary.getTotalPatients()).
build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.openmrs.module.ipd.contract;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.openmrs.Location;
import org.openmrs.module.bedmanagement.AdmissionLocation;

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class IPDWardResponse {
private String uuid;
private String name;

public static IPDWardResponse createFrom (AdmissionLocation admissionLocation){
return IPDWardResponse.builder().
uuid(admissionLocation.getWard().getUuid()).
name(admissionLocation.getWard().getName()).
build();
}

}
Loading
Loading