diff --git a/api/src/main/java/org/bahmni/module/events/api/listener/AppointmentAdvice.java b/api/src/main/java/org/bahmni/module/events/api/listener/AppointmentAdvice.java index a297dc7..387e7a1 100644 --- a/api/src/main/java/org/bahmni/module/events/api/listener/AppointmentAdvice.java +++ b/api/src/main/java/org/bahmni/module/events/api/listener/AppointmentAdvice.java @@ -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()); } } } diff --git a/api/src/main/java/org/bahmni/module/events/api/listener/RecurringAppointmentAdvice.java b/api/src/main/java/org/bahmni/module/events/api/listener/RecurringAppointmentAdvice.java new file mode 100644 index 0000000..82101e2 --- /dev/null +++ b/api/src/main/java/org/bahmni/module/events/api/listener/RecurringAppointmentAdvice.java @@ -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> threadLocal = new ThreadLocal<>(); + private final String RECURRING_APPOINTMENT_ID_KEY = "recurringAppointmentId"; + private final AppointmentMapper appointmentMapper; + private final Set 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 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 appointmentInfo = new HashMap<>(1); + appointmentInfo.put(RECURRING_APPOINTMENT_ID_KEY, appointmentRecurringPattern.getId()); + threadLocal.set(appointmentInfo); + } + } +} \ No newline at end of file diff --git a/api/src/main/java/org/bahmni/module/events/api/model/BahmniEventType.java b/api/src/main/java/org/bahmni/module/events/api/model/BahmniEventType.java index 69189e0..d970245 100644 --- a/api/src/main/java/org/bahmni/module/events/api/model/BahmniEventType.java +++ b/api/src/main/java/org/bahmni/module/events/api/model/BahmniEventType.java @@ -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"); diff --git a/api/src/main/java/org/bahmni/module/events/api/model/Event.java b/api/src/main/java/org/bahmni/module/events/api/model/Event.java index b32aab7..4a3fc65 100644 --- a/api/src/main/java/org/bahmni/module/events/api/model/Event.java +++ b/api/src/main/java/org/bahmni/module/events/api/model/Event.java @@ -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; @@ -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(); } } diff --git a/omod/src/main/resources/config.xml b/omod/src/main/resources/config.xml index 9c982f3..c2d97f5 100644 --- a/omod/src/main/resources/config.xml +++ b/omod/src/main/resources/config.xml @@ -5,9 +5,9 @@ bahmni-events Bahmni Events 1.0.0-SNAPSHOT - ${MODULE_PACKAGE} + org.bahmni.module.events Bahmni - ${project.parent.description} + Bahmni events module to send business events which can be used by external services org.openmrs.module.webservices.rest @@ -30,6 +30,11 @@ org.bahmni.module.events.api.listener.EncounterAdvice + + org.openmrs.module.appointments.service.AppointmentRecurringPatternService + org.bahmni.module.events.api.listener.RecurringAppointmentAdvice + + org.bahmni.module.events.api.EventsActivator