Skip to content

Commit

Permalink
Merge pull request #49 from Bahmni/create-shift-scheduler
Browse files Browse the repository at this point in the history
Create shift scheduler
  • Loading branch information
SooryaKumaranC-tw authored Apr 15, 2024
2 parents f219c9a + cff31ac commit 3d23bcf
Show file tree
Hide file tree
Showing 26 changed files with 302 additions and 106 deletions.
1 change: 0 additions & 1 deletion api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@
<scope>provided</scope>
</dependency>


<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>webservices.rest-omod-common</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ public List<AdmittedPatient> getAdmittedPatients(Location location, Provider pro
"LEFT JOIN careTeam.participants ctp ON ctp.voided = 0 " +
"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 ";

"where assignment.endDatetime is null and v.stopDatetime is null ";

if(location != null){
queryString += "and l.parentLocation = :location ";
}

if (provider != null) {
queryString += "and ctp.provider = :provider ";
}
Expand All @@ -79,7 +83,9 @@ public List<AdmittedPatient> getAdmittedPatients(Location location, Provider pro

Query query = session.createQuery(finalQuery);

query.setParameter("location", location);
if(location != null) {
query.setParameter("location", location);
}

if (provider != null) {
query.setParameter("provider", provider);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.openmrs.module.ipd.events;
package org.openmrs.module.ipd.api.events;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.openmrs.module.ipd.events.model.ConfigDetail;
import org.openmrs.module.ipd.api.events.model.ConfigDetail;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Component;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.openmrs.module.ipd.events;
package org.openmrs.module.ipd.api.events;

import org.openmrs.module.ipd.events.factory.IPDEventFactory;
import org.openmrs.module.ipd.events.handler.IPDEventHandler;
import org.openmrs.module.ipd.events.model.IPDEvent;
import org.openmrs.module.ipd.events.model.IPDEventType;
import org.openmrs.module.ipd.api.events.model.IPDEvent;
import org.openmrs.module.ipd.api.events.factory.IPDEventFactory;
import org.openmrs.module.ipd.api.events.handler.IPDEventHandler;
import org.openmrs.module.ipd.api.events.model.IPDEventType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.openmrs.module.ipd.events;
package org.openmrs.module.ipd.api.events;

import org.openmrs.module.fhir2.model.FhirTask;
import org.openmrs.module.fhirExtension.web.contract.TaskRequest;
import org.openmrs.module.ipd.events.model.IPDEvent;
import org.openmrs.module.ipd.api.events.model.IPDEvent;

import java.util.Date;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.openmrs.module.ipd.api.events.factory;

import org.openmrs.module.ipd.api.events.model.IPDEventType;
import org.openmrs.module.ipd.api.events.handler.IPDEventHandler;

public interface IPDEventFactory {
IPDEventHandler createEventHandler(IPDEventType eventType);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.openmrs.module.ipd.events.factory.impl;
package org.openmrs.module.ipd.api.events.factory.impl;

import org.openmrs.module.ipd.events.model.IPDEventType;
import org.openmrs.module.ipd.events.factory.IPDEventFactory;
import org.openmrs.module.ipd.events.handler.IPDEventHandler;
import org.openmrs.module.ipd.events.handler.impl.PatientAdmitEventHandler;
import org.openmrs.module.ipd.events.handler.impl.RolloverTaskEventHandler;
import org.openmrs.module.ipd.events.handler.impl.ShiftStartTaskEventHandler;
import org.openmrs.module.ipd.api.events.handler.impl.ShiftStartTaskEventHandler;
import org.openmrs.module.ipd.api.events.model.IPDEventType;
import org.openmrs.module.ipd.api.events.factory.IPDEventFactory;
import org.openmrs.module.ipd.api.events.handler.IPDEventHandler;
import org.openmrs.module.ipd.api.events.handler.impl.PatientAdmitEventHandler;
import org.openmrs.module.ipd.api.events.handler.impl.RolloverTaskEventHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.openmrs.module.ipd.api.events.handler;

import org.openmrs.module.ipd.api.events.model.IPDEvent;

public interface IPDEventHandler {
void handleEvent(IPDEvent event);
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package org.openmrs.module.ipd.events.handler.impl;
package org.openmrs.module.ipd.api.events.handler.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.module.fhirExtension.model.Task;
import org.openmrs.module.fhirExtension.service.TaskService;
import org.openmrs.module.fhirExtension.web.contract.TaskRequest;
import org.openmrs.module.fhirExtension.web.mapper.TaskMapper;
import org.openmrs.module.ipd.events.ConfigLoader;
import org.openmrs.module.ipd.events.IPDEventUtils;
import org.openmrs.module.ipd.events.handler.IPDEventHandler;
import org.openmrs.module.ipd.events.model.ConfigDetail;
import org.openmrs.module.ipd.events.model.IPDEvent;
import org.openmrs.module.ipd.events.model.TaskDetail;
import org.openmrs.module.ipd.api.events.ConfigLoader;
import org.openmrs.module.ipd.api.events.IPDEventUtils;
import org.openmrs.module.ipd.api.events.model.ConfigDetail;
import org.openmrs.module.ipd.api.events.model.IPDEvent;
import org.openmrs.module.ipd.api.events.model.TaskDetail;
import org.openmrs.module.ipd.api.events.handler.IPDEventHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.openmrs.module.ipd.api.events.handler.impl;

import org.openmrs.module.fhir2.model.FhirTask;
import org.openmrs.module.fhirExtension.dao.TaskRequestedPeriodDao;
import org.openmrs.module.fhirExtension.model.FhirTaskRequestedPeriod;
import org.openmrs.module.fhirExtension.model.Task;
import org.openmrs.module.fhirExtension.service.TaskService;
import org.openmrs.module.ipd.api.events.ConfigLoader;
import org.openmrs.module.ipd.api.events.handler.IPDEventHandler;
import org.openmrs.module.ipd.api.events.model.ConfigDetail;
import org.openmrs.module.ipd.api.events.model.IPDEvent;
import org.openmrs.module.ipd.api.events.model.TaskDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class RolloverTaskEventHandler implements IPDEventHandler {

@Autowired
ConfigLoader configLoader;
@Autowired
private TaskService taskService;
@Autowired
private TaskRequestedPeriodDao taskRequestedPeriodDao;
private static final String TASK_STATUS = "REQUESTED";

@Override
public void handleEvent(IPDEvent event) {
List<ConfigDetail> configList = configLoader.getConfigs();
ConfigDetail eventConfig = configList.stream()
.filter(config -> config.getType().equals(event.getIpdEventType().name()))
.findFirst()
.orElse(null);

List<String> taskNames = eventConfig.getTasks().stream()
.map(TaskDetail::getName)
.collect(Collectors.toList());
List<Task> rolloverTasks = taskService.searchTasks(taskNames, FhirTask.TaskStatus.REQUESTED);
List<FhirTaskRequestedPeriod> fhirTaskRequestedPeriods = new ArrayList<FhirTaskRequestedPeriod>();
for (Task task : rolloverTasks) {
if (task.getFhirTaskRequestedPeriod() != null) {
FhirTaskRequestedPeriod fhirTaskRequestedPeriod = task.getFhirTaskRequestedPeriod();
fhirTaskRequestedPeriod.setRequestedStartTime(new Date());
fhirTaskRequestedPeriods.add(fhirTaskRequestedPeriod);
}
}
taskRequestedPeriodDao.update(fhirTaskRequestedPeriods);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.openmrs.module.ipd.api.events.handler.impl;

import org.openmrs.module.fhirExtension.model.Task;
import org.openmrs.module.fhirExtension.service.TaskService;
import org.openmrs.module.fhirExtension.web.contract.TaskRequest;
import org.openmrs.module.fhirExtension.web.mapper.TaskMapper;
import org.openmrs.module.ipd.api.events.ConfigLoader;
import org.openmrs.module.ipd.api.events.IPDEventUtils;
import org.openmrs.module.ipd.api.events.handler.IPDEventHandler;
import org.openmrs.module.ipd.api.events.model.ConfigDetail;
import org.openmrs.module.ipd.api.events.model.IPDEvent;
import org.openmrs.module.ipd.api.events.model.TaskDetail;
import org.openmrs.module.ipd.api.model.AdmittedPatient;
import org.openmrs.module.ipd.api.service.WardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class ShiftStartTaskEventHandler implements IPDEventHandler {

@Autowired
ConfigLoader configLoader;
@Autowired
private TaskMapper taskMapper;
@Autowired
private TaskService taskService;
@Autowired
private WardService wardService;

@Override
public void handleEvent(IPDEvent event) {
List<AdmittedPatient> admittedPatients = wardService.getAdmittedPatients();
ConfigDetail eventConfig = getEventConfig(event);
List<Task> tasks = new ArrayList<Task>();
for(AdmittedPatient admittedPatient: admittedPatients){
String patientUuid = admittedPatient.getBedPatientAssignment().getPatient().getUuid();
IPDEvent ipdEvent = new IPDEvent(null, patientUuid, event.getIpdEventType());
for(TaskDetail taskDetail : eventConfig.getTasks()) {
TaskRequest taskRequest = IPDEventUtils.createNonMedicationTaskRequest(ipdEvent, taskDetail.getName(), "nursing_activity_system");
Task task = taskMapper.fromRequest(taskRequest);
tasks.add(task);
}
}
if(tasks.size() > 0){
taskService.saveTask(tasks);
}
}

private ConfigDetail getEventConfig(IPDEvent event){
List<ConfigDetail> configList = configLoader.getConfigs();
ConfigDetail eventConfig = configList.stream()
.filter(config -> config.getType().equals(event.getIpdEventType().name()))
.findFirst()
.orElse(null);
return eventConfig;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.openmrs.module.ipd.events.model;
package org.openmrs.module.ipd.api.events.model;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.openmrs.module.ipd.events.model;
package org.openmrs.module.ipd.api.events.model;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.openmrs.module.ipd.events.model;
package org.openmrs.module.ipd.api.events.model;

public enum IPDEventType {
PATIENT_ADMIT,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.openmrs.module.ipd.events.model;
package org.openmrs.module.ipd.api.events.model;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.openmrs.module.ipd.api.scheduler.tasks;

import org.openmrs.module.ipd.api.events.IPDEventManager;
import org.openmrs.module.ipd.api.events.model.IPDEvent;
import org.openmrs.module.ipd.api.events.model.IPDEventType;
import org.openmrs.scheduler.tasks.AbstractTask;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class RollOverNonMedicationTasks extends AbstractTask implements ApplicationContextAware {

private static ApplicationContext context;

@Override
public void execute() {
IPDEventManager eventManager = context.getBean(IPDEventManager.class);
IPDEventType eventType = eventManager.getEventTypeForEncounter(String.valueOf(IPDEventType.ROLLOVER_TASK));
if (eventType != null) {
IPDEvent ipdEvent = new IPDEvent(null, null, eventType);
eventManager.processEvent(ipdEvent);
}
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.context = applicationContext;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.openmrs.module.ipd.api.scheduler.tasks;

import org.openmrs.module.ipd.api.events.IPDEventManager;
import org.openmrs.module.ipd.api.events.model.IPDEvent;
import org.openmrs.module.ipd.api.events.model.IPDEventType;
import org.openmrs.scheduler.tasks.AbstractTask;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class ShiftStartTasks extends AbstractTask implements ApplicationContextAware {

private static ApplicationContext context;
@Override
public void execute() {
IPDEventManager eventManager = context.getBean(IPDEventManager.class);
IPDEventType eventType = eventManager.getEventTypeForEncounter(String.valueOf(IPDEventType.SHIFT_START_TASK));
if (eventType != null) {
IPDEvent ipdEvent = new IPDEvent(null, null, eventType);
eventManager.processEvent(ipdEvent);
}
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.context = applicationContext;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public interface WardService {
List<AdmittedPatient> searchWardPatients(String wardUuid, List<String> searchKeys, String searchValue, String sortBy);

List<AdmittedPatient> getPatientsByWardAndProvider(String wardUuid, String providerUuid, String sortBy);

List<AdmittedPatient> getAdmittedPatients();
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ public List<AdmittedPatient> searchWardPatients(String wardUuid, List<String> se
return wardDAO.searchAdmittedPatients(location,searchKeys,searchValue,sortBy);
}

@Override
public List<AdmittedPatient> getAdmittedPatients() {
return wardDAO.getAdmittedPatients(null,null, null, null);
}
}
Loading

0 comments on commit 3d23bcf

Please sign in to comment.