From 01bf6f4883611097a370c8cfe9c3557ce330be84 Mon Sep 17 00:00:00 2001 From: Chi Bong Ho Date: Thu, 11 Apr 2024 08:56:12 -0400 Subject: [PATCH] (fix) O3-3066 update queue entries accordingly after merging patients --- ...gePatientsWithQueueEntriesSaveHandler.java | 60 ++++++++++++ ...tientsWithQueueEntriesSaveHandlerTest.java | 98 +++++++++++++++++++ ...ueueEntryDaoTest_patientInitialDataset.xml | 14 ++- 3 files changed, 170 insertions(+), 2 deletions(-) create mode 100644 api/src/main/java/org/openmrs/module/queue/api/MergePatientsWithQueueEntriesSaveHandler.java create mode 100644 integration-tests/src/test/java/org/openmrs/module/queue/api/MergePatientsWithQueueEntriesSaveHandlerTest.java diff --git a/api/src/main/java/org/openmrs/module/queue/api/MergePatientsWithQueueEntriesSaveHandler.java b/api/src/main/java/org/openmrs/module/queue/api/MergePatientsWithQueueEntriesSaveHandler.java new file mode 100644 index 00000000..6d8fb397 --- /dev/null +++ b/api/src/main/java/org/openmrs/module/queue/api/MergePatientsWithQueueEntriesSaveHandler.java @@ -0,0 +1,60 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.queue.api; + +import java.util.Date; +import java.util.List; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.openmrs.Patient; +import org.openmrs.Person; +import org.openmrs.User; +import org.openmrs.annotation.Handler; +import org.openmrs.api.context.Context; +import org.openmrs.api.handler.SaveHandler; +import org.openmrs.module.queue.api.search.QueueEntrySearchCriteria; +import org.openmrs.module.queue.model.QueueEntry; +import org.openmrs.person.PersonMergeLog; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; + +@Handler(supports = PersonMergeLog.class) +public class MergePatientsWithQueueEntriesSaveHandler implements SaveHandler { + + private final Log log = LogFactory.getLog(getClass()); + + private final QueueEntryService queueEntryService; + + @Autowired + public MergePatientsWithQueueEntriesSaveHandler( + @Qualifier("queue.QueueEntryService") QueueEntryService queueEntryService) { + this.queueEntryService = queueEntryService; + } + + @Override + public void handle(PersonMergeLog mergeLog, User creator, Date dateCreated, String other) { + Person winner = mergeLog.getWinner(); + Person loser = mergeLog.getLoser(); + Patient winnerPatient = Context.getPatientService().getPatient(winner.getPersonId()); + Patient loserPatient = Context.getPatientService().getPatient(loser.getPersonId()); + System.out.println("@@@@@@@@@ Handling merge of " + winner + " and " + loser); + + QueueEntrySearchCriteria criteria = new QueueEntrySearchCriteria(); + criteria.setPatient(loserPatient); + List queueEntries = queueEntryService.getQueueEntries(criteria); + System.out.println("@@@@@@" + queueEntries.size()); + for (QueueEntry qe : queueEntries) { + qe.setPatient(winnerPatient); + queueEntryService.saveQueueEntry(qe); + log.trace("Changed queue entry " + qe.getUuid() + ", setting patient to " + winner.getUuid()); + } + } +} diff --git a/integration-tests/src/test/java/org/openmrs/module/queue/api/MergePatientsWithQueueEntriesSaveHandlerTest.java b/integration-tests/src/test/java/org/openmrs/module/queue/api/MergePatientsWithQueueEntriesSaveHandlerTest.java new file mode 100644 index 00000000..672214a0 --- /dev/null +++ b/integration-tests/src/test/java/org/openmrs/module/queue/api/MergePatientsWithQueueEntriesSaveHandlerTest.java @@ -0,0 +1,98 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.queue.api; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +import java.util.Arrays; +import java.util.Date; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.openmrs.Patient; +import org.openmrs.api.PatientService; +import org.openmrs.api.context.Context; +import org.openmrs.module.queue.SpringTestConfiguration; +import org.openmrs.module.queue.model.QueueEntry; +import org.openmrs.test.BaseModuleContextSensitiveTest; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.test.context.ContextConfiguration; + +@ContextConfiguration(classes = SpringTestConfiguration.class, inheritLocations = false) +public class MergePatientsWithQueueEntriesSaveHandlerTest extends BaseModuleContextSensitiveTest { + + private static final List INITIAL_DATASET_XML = Arrays.asList( + "org/openmrs/module/queue/api/dao/QueueDaoTest_locationInitialDataset.xml", + "org/openmrs/module/queue/api/dao/QueueEntryDaoTest_conceptsInitialDataset.xml", + "org/openmrs/module/queue/api/dao/QueueEntryDaoTest_patientInitialDataset.xml", + "org/openmrs/module/queue/api/dao/VisitQueueEntryDaoTest_visitInitialDataset.xml", + "org/openmrs/module/queue/api/dao/QueueDaoTest_initialDataset.xml", + "org/openmrs/module/queue/api/dao/QueueEntryDaoTest_initialDataset.xml", + "org/openmrs/module/queue/validators/QueueEntryValidatorTest_globalPropertyInitialDataset.xml"); + + @Autowired + @Qualifier("queue.QueueEntryService") + private QueueEntryService queueEntryService; + + @Before + public void setup() { + INITIAL_DATASET_XML.forEach(this::executeDataSet); + } + + /* + * + + + */ + + @Test + public void shouldChangeQueueEntryPatientOnMergingPatient() throws Exception { + PatientService ps = Context.getPatientService(); + Patient winnerPatient = ps.getPatient(100); + Patient loserPatient = ps.getPatient(101); + + // set up queue entry for loserPatient + QueueEntry queueEntryToCloneFrom = queueEntryService.getQueueEntryById(1).get(); + QueueEntry queueEntry = new QueueEntry(); + queueEntry.setPatient(loserPatient); + queueEntry.setQueue(queueEntryToCloneFrom.getQueue()); + queueEntry.setStatus(queueEntryToCloneFrom.getStatus()); + queueEntry.setPriority(queueEntryToCloneFrom.getPriority()); + queueEntry.setStartedAt(new Date()); + queueEntryService.saveQueueEntry(queueEntry); + + assertThat(queueEntry.getPatient().getPatientId(), is(loserPatient.getPatientId())); + ps.mergePatients(winnerPatient, loserPatient); + queueEntry = queueEntryService.getQueueEntryById(11).get(); + assertThat(queueEntry.getPatient().getPatientId(), is(winnerPatient.getPatientId())); + } +} diff --git a/integration-tests/src/test/resources/org/openmrs/module/queue/api/dao/QueueEntryDaoTest_patientInitialDataset.xml b/integration-tests/src/test/resources/org/openmrs/module/queue/api/dao/QueueEntryDaoTest_patientInitialDataset.xml index 87800fb8..aa3d6f17 100644 --- a/integration-tests/src/test/resources/org/openmrs/module/queue/api/dao/QueueEntryDaoTest_patientInitialDataset.xml +++ b/integration-tests/src/test/resources/org/openmrs/module/queue/api/dao/QueueEntryDaoTest_patientInitialDataset.xml @@ -11,9 +11,9 @@ + uuid="c5576187-9a67-43a7-9b7c-04db22851233" validator="[NULL]"/> + uuid="90b38324-e2fd-4feb-95b7-9e9a2a8876fg" birthdate_estimated="false" /> @@ -21,4 +21,14 @@ + + + + +