Skip to content

Commit

Permalink
BAH-3854 | Fix. AppointmentEventsAdvice Should Not Throw Exception Wh…
Browse files Browse the repository at this point in the history
…en Older validateAndSave Is Called
  • Loading branch information
ibacher authored May 28, 2024
1 parent c952494 commit 552a579
Showing 1 changed file with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Appointment>) 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<String, Integer> appointmentInfo = new HashMap<>(1);
appointmentInfo.put(APPOINTMENT_ID_KEY, appointment.getId());
threadLocal.set(appointmentInfo);
Expand Down

0 comments on commit 552a579

Please sign in to comment.