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

BAH-3723|Kavitha| Event handling model & create task for admit event #48

Merged
merged 3 commits into from
Apr 3, 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
12 changes: 12 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,18 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.bahmni.module</groupId>
<artifactId>fhir2Extension-api</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.bahmni.module</groupId>
<artifactId>fhir2Extension-omod</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.bahmni.module</groupId>
<artifactId>medication-administration-api</artifactId>
Expand Down
12 changes: 12 additions & 0 deletions omod/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.bahmni.module</groupId>
<artifactId>fhir2Extension-api</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.bahmni.module</groupId>
<artifactId>fhir2Extension-omod</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>emrapi-api</artifactId>
Expand Down
41 changes: 41 additions & 0 deletions omod/src/main/java/org/openmrs/module/ipd/events/ConfigLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.openmrs.module.ipd.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.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class ConfigLoader {
private List<ConfigDetail> configs = new ArrayList<>();

private ObjectMapper objectMapper = new ObjectMapper();

@Value("${config-file.path}")
private String routeConfigurationFileLocation;

public List<ConfigDetail> getConfigs() {
if (configs.isEmpty()) {
loadConfiguration();
}
return this.configs;
}

private void loadConfiguration() {
try {
File routeConfigurationFile = new FileSystemResource(routeConfigurationFileLocation).getFile();
this.configs = objectMapper.readValue(routeConfigurationFile, new TypeReference<List<ConfigDetail>>() {});
} catch (IOException exception) {
log.error("Failed to load configuration for file : " + routeConfigurationFileLocation, exception);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.openmrs.module.ipd.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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class IPDEventManager {
@Autowired
ConfigLoader configLoader;

@Autowired
IPDEventFactory eventFactory;

public IPDEventType getEventTypeForEncounter(String type) {
switch (type) {
case "ADMISSION":
return IPDEventType.PATIENT_ADMIT;
case "SHIFT_START_TASK":
return IPDEventType.SHIFT_START_TASK;
case "ROLLOVER_TASK":
return IPDEventType.ROLLOVER_TASK;
default:
return null;
}
}

public void processEvent(IPDEvent event) {
IPDEventHandler handler = eventFactory.createEventHandler(event.getIpdEventType());
handler.handleEvent(event);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.openmrs.module.ipd.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 java.util.Date;

public class IPDEventUtils {

public static TaskRequest createNonMedicationTaskRequest(IPDEvent ipdEvent, String name, String taskType) {
TaskRequest taskRequest = new TaskRequest();
taskRequest.setName(name);
taskRequest.setTaskType(taskType);
taskRequest.setEncounterUuid(ipdEvent.getEncounterUuid());
taskRequest.setPatientUuid(ipdEvent.getPatientUuid());
taskRequest.setRequestedStartTime(new Date());
taskRequest.setIntent(FhirTask.TaskIntent.ORDER);
taskRequest.setStatus(FhirTask.TaskStatus.REQUESTED);
return taskRequest;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.openmrs.module.ipd.events.factory;

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

public interface IPDEventFactory {
IPDEventHandler createEventHandler(IPDEventType eventType);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.openmrs.module.ipd.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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class IPDEventFactoryImpl implements IPDEventFactory {

@Autowired
PatientAdmitEventHandler patientAdmitEventHandler;

@Autowired
ShiftStartTaskEventHandler shiftStartTaskEventHandler;

@Autowired
RolloverTaskEventHandler rolloverTaskEventHandler;

@Override
public IPDEventHandler createEventHandler(IPDEventType eventType) {
switch (eventType) {
case PATIENT_ADMIT:
return patientAdmitEventHandler;
case SHIFT_START_TASK:
return shiftStartTaskEventHandler;
case ROLLOVER_TASK:
return rolloverTaskEventHandler;
default:
throw new IllegalArgumentException("Unsupported event type: " + eventType);
}
}

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

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

public interface IPDEventHandler {
void handleEvent(IPDEvent event);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.openmrs.module.ipd.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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class PatientAdmitEventHandler implements IPDEventHandler {

private final Log log = LogFactory.getLog(this.getClass());

@Autowired
ConfigLoader configLoader;

@Autowired
private TaskMapper taskMapper;

@Autowired
private TaskService taskService;

@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);
if (eventConfig != null) {
for(TaskDetail taskDetail : eventConfig.getTasks()) {
TaskRequest taskRequest = IPDEventUtils.createNonMedicationTaskRequest(event, taskDetail.getName(), "nursing_activity_system");
Task task = taskMapper.fromRequest(taskRequest);
taskService.saveTask(task);
log.info("Task created " + taskDetail.getName());
}
}


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.openmrs.module.ipd.events.handler.impl;

import org.openmrs.module.ipd.events.ConfigLoader;
import org.openmrs.module.ipd.events.model.ConfigDetail;
import org.openmrs.module.ipd.events.model.IPDEvent;
import org.openmrs.module.ipd.events.handler.IPDEventHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RolloverTaskEventHandler implements IPDEventHandler {

@Autowired
ConfigLoader configLoader;

@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);
System.out.println("eventConfig type RolloverTaskEventHandler " + eventConfig.getType());
System.out.println("eventConfig tasks RolloverTaskEventHandler " + eventConfig.getTasks() + " size --- " + eventConfig.getTasks().size());
//create task based on configuration
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.openmrs.module.ipd.events.handler.impl;

import org.openmrs.module.ipd.events.ConfigLoader;
import org.openmrs.module.ipd.events.model.ConfigDetail;
import org.openmrs.module.ipd.events.model.IPDEvent;
import org.openmrs.module.ipd.events.handler.IPDEventHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ShiftStartTaskEventHandler implements IPDEventHandler {

@Autowired
ConfigLoader configLoader;

@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);

//create task based on configuration
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.openmrs.module.ipd.events.model;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class ConfigDetail {
private String type;
private List<TaskDetail> tasks;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.openmrs.module.ipd.events.model;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class IPDEvent {
private String encounterUuid;
private String patientUuid;
private IPDEventType ipdEventType;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.openmrs.module.ipd.events.model;

public enum IPDEventType {
PATIENT_ADMIT,
SHIFT_START_TASK,
ROLLOVER_TASK;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.openmrs.module.ipd.events.model;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class TaskDetail {
private String name;
}
Loading
Loading