Skip to content

Commit 4dd3887

Browse files
committed
O3-4985 Transition QueueEntry should not end initial if new is duplicate
1 parent 703b2fc commit 4dd3887

2 files changed

Lines changed: 93 additions & 6 deletions

File tree

api/src/main/java/org/openmrs/module/queue/api/impl/QueueEntryServiceImpl.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ public QueueEntry saveQueueEntry(QueueEntry queueEntry) {
101101
throw new IllegalArgumentException("Patient mismatch - visit.patient does not match patient");
102102
}
103103
}
104+
105+
if (isDuplicate(queueEntry)) {
106+
throw new DuplicateQueueEntryException("queue.entry.duplicate.patient");
107+
}
108+
109+
Double sortWeight = getSortWeightGenerator().generateSortWeight(queueEntry);
110+
queueEntry.setSortWeight(sortWeight);
111+
return dao.createOrUpdate(queueEntry);
112+
}
113+
114+
private boolean isDuplicate(QueueEntry queueEntry) {
104115
QueueEntrySearchCriteria searchCriteria = new QueueEntrySearchCriteria();
105116
searchCriteria.setPatient(queueEntry.getPatient());
106117
searchCriteria.setQueues(Collections.singletonList(queueEntry.getQueue()));
@@ -109,26 +120,30 @@ public QueueEntry saveQueueEntry(QueueEntry queueEntry) {
109120
if (!qe.equals(queueEntry)) {
110121
if (QueueUtils.datesOverlap(qe.getStartedAt(), qe.getEndedAt(), queueEntry.getStartedAt(),
111122
queueEntry.getEndedAt())) {
112-
throw new DuplicateQueueEntryException("queue.entry.duplicate.patient");
123+
return true;
113124
}
114125
}
115126
}
116-
Double sortWeight = getSortWeightGenerator().generateSortWeight(queueEntry);
117-
queueEntry.setSortWeight(sortWeight);
118-
return dao.createOrUpdate(queueEntry);
127+
return false;
119128
}
120129

121130
/**
122131
* @see QueueEntryService#transitionQueueEntry(QueueEntryTransition)
123132
*/
124133
@Override
125134
public QueueEntry transitionQueueEntry(QueueEntryTransition queueEntryTransition) {
135+
// Create a new queue entry
136+
QueueEntry queueEntryToStart = queueEntryTransition.constructNewQueueEntry();
137+
if (isDuplicate(queueEntryToStart)) {
138+
throw new DuplicateQueueEntryException("queue.entry.duplicate.patient");
139+
}
140+
126141
// End the initial queue entry
127142
QueueEntry queueEntryToStop = queueEntryTransition.getQueueEntryToTransition();
128143
queueEntryToStop.setEndedAt(queueEntryTransition.getTransitionDate());
129144
getProxiedQueueEntryService().saveQueueEntry(queueEntryToStop);
130-
// Create a new queue entry
131-
QueueEntry queueEntryToStart = queueEntryTransition.constructNewQueueEntry();
145+
146+
// Save the new queue entry
132147
return getProxiedQueueEntryService().saveQueueEntry(queueEntryToStart);
133148
}
134149

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public License,
3+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
4+
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5+
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6+
*
7+
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8+
* graphic logo is a trademark of OpenMRS Inc.
9+
*/
10+
package org.openmrs.module.queue.api;
11+
12+
import static org.junit.Assert.assertNotNull;
13+
import static org.junit.Assert.assertNull;
14+
15+
import java.util.Arrays;
16+
import java.util.List;
17+
18+
import org.junit.Before;
19+
import org.junit.Test;
20+
import org.openmrs.module.queue.SpringTestConfiguration;
21+
import org.openmrs.module.queue.exception.DuplicateQueueEntryException;
22+
import org.openmrs.module.queue.model.QueueEntry;
23+
import org.openmrs.module.queue.model.QueueEntryTransition;
24+
import org.openmrs.test.BaseModuleContextSensitiveTest;
25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.beans.factory.annotation.Qualifier;
27+
import org.springframework.test.context.ContextConfiguration;
28+
29+
@ContextConfiguration(classes = SpringTestConfiguration.class, inheritLocations = false)
30+
public class QueueEntryServiceTest extends BaseModuleContextSensitiveTest {
31+
32+
private static final List<String> INITIAL_DATASET_XML = Arrays.asList(
33+
"org/openmrs/module/queue/api/dao/QueueDaoTest_locationInitialDataset.xml",
34+
"org/openmrs/module/queue/api/dao/QueueEntryDaoTest_conceptsInitialDataset.xml",
35+
"org/openmrs/module/queue/api/dao/QueueEntryDaoTest_patientInitialDataset.xml",
36+
"org/openmrs/module/queue/api/dao/VisitQueueEntryDaoTest_visitInitialDataset.xml",
37+
"org/openmrs/module/queue/api/dao/QueueDaoTest_initialDataset.xml",
38+
"org/openmrs/module/queue/api/dao/QueueEntryDaoTest_initialDataset.xml",
39+
"org/openmrs/module/queue/validators/QueueEntryValidatorTest_globalPropertyInitialDataset.xml");
40+
41+
@Autowired
42+
@Qualifier("queue.QueueEntryService")
43+
private QueueEntryService queueEntryService;
44+
45+
@Before
46+
public void setup() {
47+
INITIAL_DATASET_XML.forEach(this::executeDataSet);
48+
}
49+
50+
@Test
51+
public void transitionQueueEntryShouldNotEndInitialIfNewIsDuplicate() {
52+
QueueEntry queueEntry = queueEntryService.getQueueEntryById(3).get();
53+
QueueEntryTransition transition = new QueueEntryTransition();
54+
transition.setQueueEntryToTransition(queueEntry);
55+
transition.setTransitionDate(queueEntry.getStartedAt());
56+
try {
57+
queueEntryService.transitionQueueEntry(transition);
58+
}
59+
catch (DuplicateQueueEntryException ex) {
60+
assertNull(queueEntryService.getQueueEntryById(3).get().getEndedAt());
61+
}
62+
}
63+
64+
@Test
65+
public void transitionQueueEntryShouldEndInitialIfNewIsNotDuplicate() {
66+
QueueEntry queueEntry = queueEntryService.getQueueEntryById(1).get();
67+
QueueEntryTransition transition = new QueueEntryTransition();
68+
transition.setQueueEntryToTransition(queueEntry);
69+
transition.setTransitionDate(queueEntry.getStartedAt());
70+
assertNotNull(queueEntryService.getQueueEntryById(1).get().getEndedAt());
71+
}
72+
}

0 commit comments

Comments
 (0)