From 552a5792d82e2b5c4851f6bbbeb911bbd7ecd9f4 Mon Sep 17 00:00:00 2001 From: Ian <52504170+ibacher@users.noreply.github.com> Date: Tue, 28 May 2024 02:28:49 -0400 Subject: [PATCH] BAH-3854 | Fix. AppointmentEventsAdvice Should Not Throw Exception When Older validateAndSave Is Called --- .../advice/AppointmentEventsAdvice.java | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/api/src/main/java/org/openmrs/module/appointments/events/advice/AppointmentEventsAdvice.java b/api/src/main/java/org/openmrs/module/appointments/events/advice/AppointmentEventsAdvice.java index 58ee33b0..e1d2ddc1 100644 --- a/api/src/main/java/org/openmrs/module/appointments/events/advice/AppointmentEventsAdvice.java +++ b/api/src/main/java/org/openmrs/module/appointments/events/advice/AppointmentEventsAdvice.java @@ -4,13 +4,13 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.openmrs.api.context.Context; -import org.openmrs.module.appointments.events.AppointmentEvent; import org.openmrs.module.appointments.events.publisher.AppointmentEventPublisher; import org.openmrs.module.appointments.model.Appointment; import org.openmrs.module.appointments.events.AppointmentBookingEvent; import org.openmrs.module.appointments.events.AppointmentEventType; import org.springframework.aop.AfterReturningAdvice; import org.springframework.aop.MethodBeforeAdvice; +import org.springframework.lang.Nullable; import java.lang.reflect.Method; import java.util.HashMap; @@ -48,9 +48,24 @@ public void afterReturning(Object returnValue, Method method, Object[] arguments } } @Override - public void before(Method method, Object[] objects, Object o) { + public void before(Method method, Object[] objects, @Nullable Object o) { if (adviceMethodNames.contains(method.getName())) { - Appointment appointment = ((Supplier) objects[0]).get(); + Appointment appointment = null; + + Object firstArg = objects[0]; + if (firstArg instanceof Supplier) { + Object result = ((Supplier) firstArg).get(); + if (result instanceof Appointment) { + appointment = (Appointment) result; + } + } else if (firstArg instanceof Appointment) { + appointment = (Appointment) firstArg; + } + + if (appointment == null) { + return; + } + Map appointmentInfo = new HashMap<>(1); appointmentInfo.put(APPOINTMENT_ID_KEY, appointment.getId()); threadLocal.set(appointmentInfo);