Skip to content

Commit

Permalink
BAH-1013 | Events raised for recurring appointments and fix weekly re…
Browse files Browse the repository at this point in the history
…curring appointment (Bahmni#28)

* Sowmika, Bindu | MOBN-927 | Create event records for recurring appointments creation

* Sowmika, Bindu | MOBN-927 | Create event records for recurring appointments update

* Sowmika, Bindu | MOBN-927 | Create event records for recurring appointments change status

* Sowmika, Bindu | MOBN-927 | Retain removed appointments in patient appointment occurrence table to get event records

* Sowmika, Bindu | MOBN-927 | Create event records for single recurring appointment update

* Sowmika, Bindu | MOBN-927 | Add voided field to defaultAppointmentResponse

* Sowmika, Bindu | MOBN-927 | Add recurring appointments url for event records

* Sowmika, Bindu | MOBN-927 | Peer review changes

* Vineela, Bindu | MOBN-998 | Add dayCode handling if we have a related appointment in the series for extend sceanrio

* Kirity, Vineela | MOBN-1032 | Cover the existing functionality with test cases and Fix Missing appointment while editing a recurring appointment

* Ignore the test case which is yet to be modified

* Siva, Vinisha | Fix weekly recurring extension bug

* Siva, Vinisha | Fix weekly recurring bug extension when any appointment in pattern got moved and removed

* Add integration test for extending weekly recurring appointments

* Bindu | Addresses the PR review comments

Co-authored-by: sowmika <[email protected]>
Co-authored-by: Himabindu Thungathurty <[email protected]>
Co-authored-by: binduak <[email protected]>
Co-authored-by: Siva Rachakonda <[email protected]>
  • Loading branch information
5 people authored Apr 29, 2020
1 parent 1960eeb commit 86cba2e
Show file tree
Hide file tree
Showing 22 changed files with 2,018 additions and 230 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
import java.util.Arrays;

import static java.util.Objects.isNull;
import static org.openmrs.module.appointments.constants.AppointmentsEventRecordsConstants.CATEGORY;
import static org.openmrs.module.appointments.constants.AppointmentsEventRecordsConstants.RAISE_EVENT_GLOBAL_PROPERTY;

public class AppointmentAdvice extends AbstractBaseAdvice {

private static final String TITLE = "Appointment";
private static final String CATEGORY = "appointments";
private static final ArrayList<String> METHOD_NAMES = new ArrayList<>(Arrays.asList("validateAndSave", "changeStatus", "undoStatusChange"));
private static final ArrayList<String> VOIDED_METHOD_NAMES = new ArrayList<>(Arrays.asList("changeStatus", "undoStatusChange"));
private static final String DEFAULT_URL_PATTERN = "/openmrs/ws/rest/v1/appointment?uuid={uuid}";
private static final String RAISE_EVENT_GLOBAL_PROPERTY = "atomfeed.publish.eventsForAppointments";
private static final String URL_PATTERN_GLOBAL_PROPERTY = "atomfeed.event.urlPatternForAppointments";
private static final String DEFAULT_URL_PATTERN = "/openmrs/ws/rest/v1/appointment?uuid={uuid}";


@Override
protected String getContents(Object returnValue) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.openmrs.module.appointments.advice;

import org.openmrs.api.context.Context;
import org.openmrs.module.appointments.model.Appointment;
import org.openmrs.module.appointments.model.AppointmentRecurringPattern;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.openmrs.module.appointments.constants.AppointmentsEventRecordsConstants.CATEGORY;
import static org.openmrs.module.appointments.constants.AppointmentsEventRecordsConstants.RAISE_EVENT_GLOBAL_PROPERTY;

public class RecurringAppointmentsAdvice extends AbstractBaseAdvice {

private static final String TITLE = "RecurringAppointments";
private static final String VALIDATE_AND_SAVE = "validateAndSave";
private static final String UPDATE = "update";
private static final String CHANGE_STATUS = "changeStatus";
private static final List<String> METHOD_NAMES = Arrays.asList(VALIDATE_AND_SAVE, UPDATE, CHANGE_STATUS);
private static final String URL_PATTERN_GLOBAL_PROPERTY = "atomfeed.event.urlPatternForRecurringAppointments";
private static final String DEFAULT_URL_PATTERN = "/openmrs/ws/rest/v1/recurring-appointments?uuid={uuid}";

@Override
public void afterReturning(Object returnValue, Method method, Object[] arguments, Object target) throws Throwable {
List<Appointment> updatedAppointments = new ArrayList<>();
if (VALIDATE_AND_SAVE.equals(method.getName()) || isAllRecurringAppointmentsUpdate(method, returnValue)) {
AppointmentRecurringPattern appointmentRecurringPattern = (AppointmentRecurringPattern) returnValue;
updatedAppointments = new ArrayList<>(appointmentRecurringPattern.getAppointments());
} else if (isSingleRecurringAppointmentUpdate(method, returnValue)) {
updatedAppointments = (List<Appointment>) arguments[1];
} else if (CHANGE_STATUS.equals(method.getName())) {
updatedAppointments = (List<Appointment>) returnValue;
}
raiseEventsForAppointments(method, arguments, target, updatedAppointments);
}

private void raiseEventsForAppointments(Method method, Object[] arguments, Object target,
List<Appointment> appointments) throws Throwable {
for (Appointment appointment : appointments) {
super.afterReturning(appointment, method, arguments, target);
}
}

private boolean isSingleRecurringAppointmentUpdate(Method method, Object processedReturnValue) {
return UPDATE.equals(method.getName()) && (processedReturnValue instanceof Appointment);
}

private boolean isAllRecurringAppointmentsUpdate(Method method, Object processedReturnValue) {
return UPDATE.equals(method.getName()) && (processedReturnValue instanceof AppointmentRecurringPattern);
}

@Override
protected String getContents(Object returnValue) {
return getUrlPattern(URL_PATTERN_GLOBAL_PROPERTY, DEFAULT_URL_PATTERN)
.replace("{uuid}", ((Appointment) returnValue).getUuid());
}

@Override
protected String getTitle() {
return TITLE;
}

@Override
protected String getCategory() {
return CATEGORY;
}

@Override
protected boolean shouldRaiseEventForMethod(String methodName) {
return METHOD_NAMES.contains(methodName);
}

@Override
protected boolean shouldRaiseEvent() {
return Boolean.valueOf(Context.getAdministrationService().getGlobalProperty(RAISE_EVENT_GLOBAL_PROPERTY));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.openmrs.module.appointments.constants;

public class AppointmentsEventRecordsConstants {
public static final String CATEGORY = "appointments";
public static final String RAISE_EVENT_GLOBAL_PROPERTY = "atomfeed.publish.eventsForAppointments";

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,23 @@ public Set<Appointment> getActiveAppointments() {
.filter(appointment -> !appointment.getVoided()).collect(Collectors.toSet());
}

public Set<Appointment> getRelatedAppointments() {
Set<Appointment> relatedAppointments = new LinkedHashSet<>();
getActiveAppointments().forEach(appointment -> {
if (appointment.getRelatedAppointment() != null) {
relatedAppointments.add(appointment.getRelatedAppointment());
}
});
return relatedAppointments;
}

public Set<Appointment> getRemovedAppointments() {
Set<Appointment> removedAppointments = new LinkedHashSet<>();
Set<Appointment> relatedAppointments = getRelatedAppointments();
getAppointments().forEach(appointment -> {
if (appointment.getVoided() && !relatedAppointments.contains(appointment))
removedAppointments.add(appointment);
});
return removedAppointments;
}
}
15 changes: 15 additions & 0 deletions api/src/main/resources/liquibase.xml
Original file line number Diff line number Diff line change
Expand Up @@ -561,4 +561,19 @@
<column name="uuid" valueComputed="UUID()"/>
</insert>
</changeSet>

<changeSet id="202001031712" author="Sowmika, Bindu">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="0">
SELECT COUNT(*) FROM global_property where property = 'atomfeed.event.urlPatternForRecurringAppointments';
</sqlCheck>
</preConditions>
<comment>Adding global property to specify the URL pattern for published appointment events</comment>
<insert tableName="global_property">
<column name="property" value="atomfeed.event.urlPatternForRecurringAppointments"/>
<column name="property_value" value="/openmrs/ws/rest/v1/recurring-appointments?uuid={uuid}"/>
<column name="uuid" valueComputed="UUID()"/>
<column name="description" value="URL pattern to use for published Recurring appointment events. Default is /openmrs/ws/rest/v1/recurring-appointments?uuid={uuid}. If you change default value, please add your custom implementation for the given URL"/>
</insert>
</changeSet>
</databaseChangeLog>
9 changes: 9 additions & 0 deletions api/src/main/resources/moduleApplicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@
</property>
</bean>

<bean parent="serviceContext">
<property name="moduleService">
<list merge="true">
<value>org.openmrs.module.appointments.service.AppointmentRecurringPatternService</value>
<ref bean="recurringAppointmentService"/>
</list>
</property>
</bean>

<bean parent="serviceContext">
<property name="moduleService">
<list merge="true">
Expand Down
Loading

0 comments on commit 86cba2e

Please sign in to comment.