-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(fix) O3-3066 update queue entries accordingly after merging patients
- Loading branch information
Showing
3 changed files
with
170 additions
and
2 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
api/src/main/java/org/openmrs/module/queue/api/MergePatientsWithQueueEntriesSaveHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<PersonMergeLog> { | ||
|
||
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<QueueEntry> 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()); | ||
} | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
.../test/java/org/openmrs/module/queue/api/MergePatientsWithQueueEntriesSaveHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> 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); | ||
} | ||
|
||
/* | ||
* | ||
<queue_entry queue_entry_id="11" | ||
uuid="481129b2-bc3f-4e13-8c87-01941604e4cf" | ||
queue_id="1" | ||
patient_id="101" | ||
visit_id="[NULL]" | ||
started_at="2024-02-04 18:40:56.0" | ||
ended_at="[NULL]" | ||
priority="1001" | ||
priority_comment="[NULL]" | ||
status="3001" | ||
sort_weight="0" | ||
location_waiting_for="[NULL]" | ||
provider_waiting_for="[NULL]" | ||
queue_coming_from="[NULL]" | ||
voided="false" | ||
voided_by="[NULL]" | ||
date_voided="[NULL]" | ||
creator="1" | ||
date_created="2024-02-02 16:38:56.0" | ||
/> | ||
*/ | ||
|
||
@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())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters