Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RA-2036 - Provide a mechanism for modules to extend the merge visits … #224

Merged
merged 2 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions api/src/main/java/org/openmrs/module/emrapi/adt/AdtService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.openmrs.api.OpenmrsService;
import org.openmrs.module.emrapi.adt.exception.ExistingVisitDuringTimePeriodException;
import org.openmrs.module.emrapi.merge.PatientMergeAction;
import org.openmrs.module.emrapi.merge.VisitMergeAction;
import org.openmrs.module.emrapi.visit.VisitDomainWrapper;

import java.util.Date;
Expand Down Expand Up @@ -221,6 +222,26 @@ Encounter checkInPatient(Patient patient, Location where, Provider checkInClerk,
*/
void removePatientMergeAction(PatientMergeAction patientMergeAction);

/**
* Allows another module to add a visit merge action to the list of actions to be performed
* when a visit is merged; (ideally, this would just happen via @Autowired, but because @Autowired
* in service impls greatly slow down startup, instead we have other modules inject the beans explicitly
* via this method)
*
* @since 1.36.0
* @param visitMergeAction
*/
void addVisitMergeAction(VisitMergeAction visitMergeAction);
mseaton marked this conversation as resolved.
Show resolved Hide resolved

/**
* Allows another module to remove a visit merge action to the list of actions to be performed
* when a visit is merged; (see addVisitMergeAction above)
*
* @since 1.36.0
* @param visitMergeAction
*/
void removeVisitMergeAction(VisitMergeAction visitMergeAction);

/**
* Merge a set of consecutive patient visits
* @param visits
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.openmrs.module.emrapi.disposition.Disposition;
import org.openmrs.module.emrapi.domainwrapper.DomainWrapperFactory;
import org.openmrs.module.emrapi.merge.PatientMergeAction;
import org.openmrs.module.emrapi.merge.VisitMergeAction;
import org.openmrs.module.emrapi.patient.PatientDomainWrapper;
import org.openmrs.module.emrapi.visit.VisitDomainWrapper;
import org.openmrs.serialization.SerializationException;
Expand Down Expand Up @@ -84,6 +85,8 @@ public class AdtServiceImpl extends BaseOpenmrsService implements AdtService {

private List<PatientMergeAction> patientMergeActions;

private List<VisitMergeAction> visitMergeActions;

public void setPatientService(PatientService patientService) {
this.patientService = patientService;
}
Expand Down Expand Up @@ -121,6 +124,11 @@ public List<PatientMergeAction> getPatientMergeActions() {
return patientMergeActions;
}

// for testing
List<VisitMergeAction> getVisitMergeActions() {
return visitMergeActions;
}

@Override
public void closeInactiveVisits() {
Collection<Location> possibleLocations = getPossibleLocationsToCloseVisit();
Expand Down Expand Up @@ -705,8 +713,23 @@ public Visit mergeVisits(Visit preferred, Visit nonPreferred) {
}
nonPreferred.setEncounters(null); // we need to manually the encounters from the non-preferred visit before voiding or all the encounters we just moved will also get voided!

// do any "before visit save actions" that have been registered
if (visitMergeActions != null) {
for (VisitMergeAction visitMergeAction : visitMergeActions) {
visitMergeAction.beforeSavingVisits(preferred, nonPreferred);
}
}

visitService.voidVisit(nonPreferred, "EMR - Merge Patients: merged into visit " + preferred.getVisitId());
visitService.saveVisit(preferred);

// do any "after visit save actions" that have been registered
if (visitMergeActions != null) {
for (VisitMergeAction visitMergeAction : visitMergeActions) {
visitMergeAction.afterSavingVisits(preferred, nonPreferred);
}
}

return preferred;
}

Expand Down Expand Up @@ -747,6 +770,22 @@ public void removePatientMergeAction(PatientMergeAction patientMergeAction) {
this.patientMergeActions.remove(patientMergeAction);
}

@Override
public void addVisitMergeAction(VisitMergeAction visitMergeAction) {
if (this.visitMergeActions == null) {
this.visitMergeActions = new ArrayList<VisitMergeAction>();
}
this.visitMergeActions.add(visitMergeAction);
}

@Override
public void removeVisitMergeAction(VisitMergeAction visitMergeAction) {
if (this.visitMergeActions == null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we intentionally ignore cases where one tries to remove a visit merge action when we do not have any?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dkayiwa I just followed exactly the same pattern as is already in place for the PatientMergeActions. I figured it was best to stay consistent.

this.visitMergeActions = new ArrayList<VisitMergeAction>();
}
this.visitMergeActions.remove(visitMergeAction);
}

@Transactional
@Override
public Encounter createAdtEncounterFor(AdtAction action) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.openmrs.module.emrapi.merge;

import org.openmrs.Visit;

/**
* {@link org.openmrs.module.emrapi.adt.AdtService#mergeVisits(Visit, Visit)} will invoke all instances of this
* interface that are registered with it before and after voiding and saving the respective visits.
*/
public interface VisitMergeAction {

/**
* This method will be called before calling the visit service to save / void either of the given visits,
* but in the same transaction. Any thrown exception will cancel the merge
*
* @param preferred the Visit to keep
* @param notPreferred the Visit to merge into the preferred visit
*/
void beforeSavingVisits(Visit preferred, Visit notPreferred);

/**
* This method will be called after calling the visit service to save / void either of the given visits,
* but in the same transaction. Any thrown exception will cancel the merge
* same transaction.
*
* @param preferred the Visit to keep
* @param notPreferred the Visit to merge into the preferred visit
*/
void afterSavingVisits(Visit preferred, Visit notPreferred);

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@
import org.openmrs.Visit;
import org.openmrs.VisitType;
import org.openmrs.api.EncounterService;
import org.openmrs.api.LocationService;
import org.openmrs.api.PatientService;
import org.openmrs.api.ProviderService;
import org.openmrs.api.VisitService;
import org.openmrs.api.LocationService;
import org.openmrs.Concept;
import org.openmrs.api.context.Context;
import org.openmrs.module.emrapi.EmrApiConstants;
import org.openmrs.module.emrapi.EmrApiProperties;
Expand All @@ -53,8 +52,8 @@
import org.openmrs.module.emrapi.disposition.DispositionService;
import org.openmrs.module.emrapi.domainwrapper.DomainWrapperFactory;
import org.openmrs.module.emrapi.merge.PatientMergeAction;
import org.openmrs.module.emrapi.merge.VisitMergeAction;
import org.openmrs.module.emrapi.patient.PatientDomainWrapper;
import org.openmrs.module.emrapi.visit.EmrVisitService;
import org.openmrs.module.emrapi.visit.VisitDomainWrapper;
import org.openmrs.module.reporting.query.visit.service.VisitQueryService;
import org.openmrs.serialization.SerializationException;
Expand Down Expand Up @@ -1109,6 +1108,45 @@ public void test_removePaperRecordMergeActionShouldNotFailIfActionsNotInitialize
assertThat(service.getPatientMergeActions().size(), is(0));
}

@Test
public void test_shouldAddVisitMergeActions() throws Exception {
VisitMergeAction action1 = new DummyVisitMergeAction();
VisitMergeAction action2 = new DummyVisitMergeAction();

service.addVisitMergeAction(action1);
service.addVisitMergeAction(action2);

assertThat(service.getVisitMergeActions().size(), is(2));
assertTrue(service.getVisitMergeActions().contains(action1));
assertTrue(service.getVisitMergeActions().contains(action2));
}

@Test
public void test_shouldRemoveVisitMergeAction() throws Exception {

VisitMergeAction action1 = new DummyVisitMergeAction();
VisitMergeAction action2 = new DummyVisitMergeAction();

service.addVisitMergeAction(action1);
service.addVisitMergeAction(action2);

service.removeVisitMergeAction(action1);

assertThat(service.getVisitMergeActions().size(), is(1));
assertFalse(service.getVisitMergeActions().contains(action1));
assertTrue(service.getVisitMergeActions().contains(action2));

}

@Test
public void test_removeVisitMergeActionShouldNotFailIfActionsNotInitialized() throws Exception {

VisitMergeAction action = new DummyVisitMergeAction();
service.removeVisitMergeAction(action);

assertThat(service.getVisitMergeActions().size(), is(0));
}


private Encounter buildEncounter(Patient patient, Date encounterDatetime) {
Encounter encounter = new Encounter();
Expand Down Expand Up @@ -1140,6 +1178,17 @@ public void afterMergingPatients(Patient preferred, Patient notPreferred) {

}

private class DummyVisitMergeAction implements VisitMergeAction {

@Override
public void beforeSavingVisits(Visit preferred, Visit notPreferred) {
}

@Override
public void afterSavingVisits(Visit preferred, Visit notPreferred) {
}
}

private class MockDomainWrapperFactory extends DomainWrapperFactory{

@Override
Expand Down
Loading