Skip to content

Commit

Permalink
Added recurring appointment publisher to publish events and listen in…
Browse files Browse the repository at this point in the history
… communications module.
  • Loading branch information
anubhavBeehyv committed Sep 14, 2023
1 parent 8b30205 commit 3a334c8
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public void afterReturning(Object returnValue, Method method, Object[] arguments
Object representation = appointmentMapper.constructResponse(appointment);
Event event = new Event(eventType, representation, appointment.getUuid());
eventPublisher.publishEvent(event);
System.out.println("Successfully published event with uuid : " + appointment.getUuid());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.bahmni.module.events.api.listener;

import com.google.common.collect.Sets;
import org.bahmni.module.events.api.model.BahmniEventType;
import org.bahmni.module.events.api.model.Event;
import org.bahmni.module.events.api.publisher.BahmniEventPublisher;
import org.openmrs.api.context.Context;
import org.openmrs.module.appointments.model.Appointment;
import org.openmrs.module.appointments.model.AppointmentRecurringPattern;
import org.openmrs.module.appointments.web.mapper.AppointmentMapper;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;

import static org.bahmni.module.events.api.model.BahmniEventType.BAHMNI_RECURRING_APPOINTMENT_CREATED;
import static org.bahmni.module.events.api.model.BahmniEventType.BAHMNI_RECURRING_APPOINTMENT_UPDATED;

public class RecurringAppointmentAdvice implements AfterReturningAdvice, MethodBeforeAdvice {

private final BahmniEventPublisher eventPublisher;
private final ThreadLocal<Map<String,Integer>> threadLocal = new ThreadLocal<>();
private final String RECURRING_APPOINTMENT_ID_KEY = "recurringAppointmentId";
private final AppointmentMapper appointmentMapper;
private final Set<String> adviceMethodNames = Sets.newHashSet("validateAndSave");

public RecurringAppointmentAdvice() {
this.eventPublisher = Context.getRegisteredComponent("bahmniEventPublisher", BahmniEventPublisher.class);
this.appointmentMapper = Context.getRegisteredComponent("appointmentMapper", AppointmentMapper.class);
}

public RecurringAppointmentAdvice(BahmniEventPublisher bahmniEventPublisher, AppointmentMapper appointmentMapper) {
this.eventPublisher = bahmniEventPublisher;
this.appointmentMapper = appointmentMapper;
}

@Override
public void afterReturning(Object returnValue, Method method, Object[] arguments, Object target) {
if (adviceMethodNames.contains(method.getName())) {
Map<String, Integer> appointmentInfo = threadLocal.get();
// TODO: This is a workaround to avoid publishing duplicate events because currently the event is getting called twice. Need to find out the reason and resolve it.
if (appointmentInfo != null) {
BahmniEventType eventType = appointmentInfo.get(RECURRING_APPOINTMENT_ID_KEY) == null ? BAHMNI_RECURRING_APPOINTMENT_CREATED : BAHMNI_RECURRING_APPOINTMENT_UPDATED;
threadLocal.remove();
AppointmentRecurringPattern appointmentRecurringPattern = (AppointmentRecurringPattern) returnValue;
Appointment appointment=appointmentRecurringPattern.getAppointments().iterator().next();
Object representation = appointmentMapper.constructResponse(appointment);
Event event = new Event(eventType, representation,appointment.getUuid());
eventPublisher.publishEvent(event);
}
}
}

@Override
public void before(Method method, Object[] objects, Object o) {
if (adviceMethodNames.contains(method.getName())) {
AppointmentRecurringPattern appointmentRecurringPattern = (AppointmentRecurringPattern)objects[0];
Map<String, Integer> appointmentInfo = new HashMap<>(1);
appointmentInfo.put(RECURRING_APPOINTMENT_ID_KEY, appointmentRecurringPattern.getId());
threadLocal.set(appointmentInfo);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ public enum BahmniEventType {
BAHMNI_PATIENT_UPDATED("bahmni-patient"),
BAHMNI_APPOINTMENT_CREATED("bahmni-appointment"),
BAHMNI_APPOINTMENT_UPDATED("bahmni-appointment"),
BAHMNI_RECURRING_APPOINTMENT_CREATED("bahmni-recurring-appointment"),
BAHMNI_RECURRING_APPOINTMENT_UPDATED("bahmni-recurring-appointment"),
BAHMNI_ENCOUNTER_CREATED("bahmni-encounter"),
BAHMNI_ENCOUNTER_UPDATED("bahmni-encounter");

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

import org.openmrs.api.context.Context;
import org.openmrs.api.context.UserContext;

import java.time.LocalDateTime;
import java.util.UUID;

Expand All @@ -11,12 +14,13 @@ public class Event {
public final String payloadId;
public final Object payload;
public final LocalDateTime publishedDateTime;

public final UserContext userContext;
public Event(BahmniEventType eventType, Object payload, String payloadId) {
this.eventType = eventType;
this.payload = payload;
this.eventId = UUID.randomUUID().toString();
this.payloadId = payloadId;
this.publishedDateTime = LocalDateTime.now();
this.userContext= Context.getUserContext();
}
}
9 changes: 7 additions & 2 deletions omod/src/main/resources/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<id>bahmni-events</id>
<name>Bahmni Events</name>
<version>1.0.0-SNAPSHOT</version>
<package>${MODULE_PACKAGE}</package>
<package>org.bahmni.module.events</package>
<author>Bahmni</author>
<description>${project.parent.description}</description>
<description>Bahmni events module to send business events which can be used by external services</description>

<require_modules>
<require_module>org.openmrs.module.webservices.rest</require_module>
Expand All @@ -30,6 +30,11 @@
<class>org.bahmni.module.events.api.listener.EncounterAdvice</class>
</advice>

<advice>
<point>org.openmrs.module.appointments.service.AppointmentRecurringPatternService</point>
<class>org.bahmni.module.events.api.listener.RecurringAppointmentAdvice</class>
</advice>

<activator>org.bahmni.module.events.api.EventsActivator</activator>
</module>

0 comments on commit 3a334c8

Please sign in to comment.