diff --git a/api/src/main/java/org/openmrs/module/queue/model/Queue.java b/api/src/main/java/org/openmrs/module/queue/model/Queue.java index 344b222..6d7fc27 100644 --- a/api/src/main/java/org/openmrs/module/queue/model/Queue.java +++ b/api/src/main/java/org/openmrs/module/queue/model/Queue.java @@ -21,11 +21,14 @@ import javax.persistence.OneToMany; import javax.persistence.Table; +import java.util.ArrayList; +import java.util.Date; import java.util.List; +import java.util.stream.Collectors; import lombok.Data; import lombok.EqualsAndHashCode; -import org.hibernate.annotations.Where; +import org.apache.commons.lang.BooleanUtils; import org.openmrs.BaseChangeableOpenmrsMetadata; import org.openmrs.Concept; import org.openmrs.Location; @@ -52,13 +55,59 @@ public class Queue extends BaseChangeableOpenmrsMetadata { private Concept service; @OneToMany(mappedBy = "queue", cascade = CascadeType.ALL, fetch = FetchType.LAZY) - @Where(clause = "voided = 0 and (started_at <= current_timestamp() and ended_at is null)") private List queueEntries; @OneToMany(mappedBy = "queue", cascade = CascadeType.ALL, fetch = FetchType.LAZY) - @Where(clause = "retired = 0") private List queueRooms; + /** + * @return all non-voided QueueEntries that do not start in the future and are not already ended + */ + public List getActiveQueueEntries() { + List activeQueueEntries = new ArrayList<>(); + Date now = new Date(); + if (queueEntries != null) { + for (QueueEntry queueEntry : queueEntries) { + if (BooleanUtils.isNotTrue(queueEntry.getVoided())) { + if (!queueEntry.getStartedAt().after(now) && queueEntry.getEndedAt() == null) { + activeQueueEntries.add(queueEntry); + } + } + } + } + return activeQueueEntries; + } + + /** + * @return all non-retired QueueRooms + */ + public List getActiveQueueRooms() { + if (queueRooms == null) { + return new ArrayList<>(); + } + return queueRooms.stream().filter(r -> BooleanUtils.isNotTrue(r.getRetired())).collect(Collectors.toList()); + } + + /** + * @param queueEntry the QueueEntry to add + */ + public void addQueueEntry(QueueEntry queueEntry) { + if (queueEntries == null) { + queueEntries = new ArrayList<>(); + } + queueEntries.add(queueEntry); + } + + /** + * @param queueRoom the QueueRoom to add + */ + public void addQueueRoom(QueueRoom queueRoom) { + if (queueRooms == null) { + queueRooms = new ArrayList<>(); + } + queueRooms.add(queueRoom); + } + @Override public Integer getId() { return getQueueId(); diff --git a/api/src/test/java/org/openmrs/module/queue/model/QueueTest.java b/api/src/test/java/org/openmrs/module/queue/model/QueueTest.java new file mode 100644 index 0000000..767993c --- /dev/null +++ b/api/src/test/java/org/openmrs/module/queue/model/QueueTest.java @@ -0,0 +1,116 @@ +/* + * 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.model; + +import org.apache.commons.lang.time.DateUtils; +import org.junit.Test; + +import java.util.Date; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; + +public class QueueTest { + + Date now = new Date(); + + @Test + public void addQueueEntry_shouldAddANewQueueEntryEvenIfQueueEntriesIsNull() { + Queue queue = new Queue(); + queue.setQueueEntries(null); + QueueEntry queueEntry = new QueueEntry(); + queue.addQueueEntry(queueEntry); + assertThat(queue.getQueueEntries().size(), equalTo(1)); + } + + @Test + public void addQueueRoom_shouldAddANewQueueRoomEvenIfQueueRoomsIsNull() { + Queue queue = new Queue(); + queue.setQueueRooms(null); + QueueRoom queueRoom = new QueueRoom(); + queue.addQueueRoom(queueRoom); + assertThat(queue.getQueueRooms().size(), equalTo(1)); + } + + @Test + public void getActiveQueueEntries_shouldReturnAnEmptyListIfNoQueueEntriesExist() { + Queue queue = new Queue(); + queue.setQueueEntries(null); + assertThat(queue.getQueueEntries(), nullValue()); + assertThat(queue.getActiveQueueEntries(), notNullValue()); + assertThat(queue.getActiveQueueEntries().size(), equalTo(0)); + } + + @Test + public void getActiveQueueEntries_shouldReturnOnlyNonVoidedEntries() { + Queue queue = new Queue(); + + QueueEntry queueEntry = new QueueEntry(); + queueEntry.setStartedAt(DateUtils.addHours(now, -1)); + queueEntry.setEndedAt(null); + queue.addQueueEntry(queueEntry); + + assertThat(queue.getQueueEntries(), contains(queueEntry)); + assertThat(queue.getActiveQueueEntries(), contains(queueEntry)); + queueEntry.setVoided(true); + assertThat(queue.getQueueEntries(), contains(queueEntry)); + assertThat(queue.getActiveQueueEntries(), not(contains(queueEntry))); + } + + @Test + public void getActiveQueueEntries_shouldNotReturnFutureQueueEntries() { + Queue queue = new Queue(); + + QueueEntry queueEntry = new QueueEntry(); + queueEntry.setStartedAt(DateUtils.addHours(now, -1)); + queueEntry.setEndedAt(null); + queue.addQueueEntry(queueEntry); + + assertThat(queue.getQueueEntries(), contains(queueEntry)); + assertThat(queue.getActiveQueueEntries(), contains(queueEntry)); + queueEntry.setStartedAt(DateUtils.addHours(now, 1)); + assertThat(queue.getQueueEntries(), contains(queueEntry)); + assertThat(queue.getActiveQueueEntries(), not(contains(queueEntry))); + } + + @Test + public void getActiveQueueEntries_shouldNotReturnEndedQueueEntries() { + Queue queue = new Queue(); + + QueueEntry queueEntry = new QueueEntry(); + queueEntry.setStartedAt(DateUtils.addHours(now, -2)); + queueEntry.setEndedAt(null); + queue.addQueueEntry(queueEntry); + + assertThat(queue.getQueueEntries(), contains(queueEntry)); + assertThat(queue.getActiveQueueEntries(), contains(queueEntry)); + queueEntry.setEndedAt(DateUtils.addHours(now, 1)); + assertThat(queue.getQueueEntries(), contains(queueEntry)); + assertThat(queue.getActiveQueueEntries(), not(contains(queueEntry))); + } + + @Test + public void getActiveQueueRooms_shouldNotReturnRetiredQueueRooms() { + Queue queue = new Queue(); + + QueueRoom queueRoom = new QueueRoom(); + queue.addQueueRoom(queueRoom); + + assertThat(queue.getQueueRooms(), contains(queueRoom)); + assertThat(queue.getActiveQueueRooms(), contains(queueRoom)); + queueRoom.setRetired(true); + assertThat(queue.getQueueRooms(), contains(queueRoom)); + assertThat(queue.getActiveQueueRooms(), not(contains(queueRoom))); + } +} diff --git a/api/src/test/java/org/openmrs/module/queue/model/AutoCloseVisitQueueEntryTaskTest.java b/api/src/test/java/org/openmrs/module/queue/tasks/AutoCloseVisitQueueEntryTaskTest.java similarity index 95% rename from api/src/test/java/org/openmrs/module/queue/model/AutoCloseVisitQueueEntryTaskTest.java rename to api/src/test/java/org/openmrs/module/queue/tasks/AutoCloseVisitQueueEntryTaskTest.java index 1e6b550..84cee5d 100644 --- a/api/src/test/java/org/openmrs/module/queue/model/AutoCloseVisitQueueEntryTaskTest.java +++ b/api/src/test/java/org/openmrs/module/queue/tasks/AutoCloseVisitQueueEntryTaskTest.java @@ -7,11 +7,12 @@ * 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.model; +package org.openmrs.module.queue.tasks; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.nullValue; +import org.junit.Test; +import org.openmrs.Visit; +import org.openmrs.module.queue.model.QueueEntry; +import org.openmrs.module.queue.model.VisitQueueEntry; import java.text.DateFormat; import java.text.SimpleDateFormat; @@ -21,9 +22,9 @@ import java.util.List; import java.util.stream.Collectors; -import org.junit.Test; -import org.openmrs.Visit; -import org.openmrs.module.queue.tasks.AutoCloseVisitQueueEntryTask; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.nullValue; public class AutoCloseVisitQueueEntryTaskTest { @@ -44,7 +45,7 @@ protected void saveQueueEntry(QueueEntry queueEntry) { @Test public void shouldAutoCloseVisitQueueEntriesIfVisitIsClosed() throws Exception { - + Visit visit1 = new Visit(); visit1.setStartDatetime(getDate("2020-01-01 10:00")); QueueEntry queueEntry1 = new QueueEntry(); @@ -53,7 +54,7 @@ public void shouldAutoCloseVisitQueueEntriesIfVisitIsClosed() throws Exception { visitQueueEntry1.setVisit(visit1); visitQueueEntry1.setQueueEntry(queueEntry1); queueEntries.add(visitQueueEntry1); - + Visit visit2 = new Visit(); visit2.setStartDatetime(getDate("2021-01-01 10:00")); QueueEntry queueEntry2 = new QueueEntry(); @@ -62,17 +63,17 @@ public void shouldAutoCloseVisitQueueEntriesIfVisitIsClosed() throws Exception { visitQueueEntry2.setVisit(visit2); visitQueueEntry2.setQueueEntry(queueEntry2); queueEntries.add(visitQueueEntry2); - + TestAutoCloseVisitEntryTask task = new TestAutoCloseVisitEntryTask(); task.run(); assertThat(queueEntry1.getEndedAt(), nullValue()); assertThat(queueEntry2.getEndedAt(), nullValue()); - + visit1.setStopDatetime(getDate("2020-01-01 23:15")); task.run(); assertThat(queueEntry1.getEndedAt(), equalTo(visit1.getStopDatetime())); assertThat(queueEntry2.getEndedAt(), nullValue()); - + visit2.setStopDatetime(getDate("2021-01-05 11:30")); task.run(); assertThat(queueEntry1.getEndedAt(), equalTo(visit1.getStopDatetime())); diff --git a/omod/src/main/java/org/openmrs/module/queue/web/resources/QueueEntryCountSubResource.java b/omod/src/main/java/org/openmrs/module/queue/web/resources/QueueEntryCountSubResource.java index c4367bb..1263ccf 100644 --- a/omod/src/main/java/org/openmrs/module/queue/web/resources/QueueEntryCountSubResource.java +++ b/omod/src/main/java/org/openmrs/module/queue/web/resources/QueueEntryCountSubResource.java @@ -52,7 +52,7 @@ public void setParent(QueueEntryCount queueEntryCount, Queue queue) { @Override public PageableResult doGetAll(Queue queue, RequestContext requestContext) throws ResponseException { return new GenericSingleObjectResult(Arrays.asList(new PropValue("queueName", queue.getName()), - new PropValue("queueEntriesCount", queue.getQueueEntries().size()))); + new PropValue("queueEntriesCount", queue.getActiveQueueEntries().size()))); } @Override diff --git a/omod/src/main/java/org/openmrs/module/queue/web/resources/QueueEntrySubResource.java b/omod/src/main/java/org/openmrs/module/queue/web/resources/QueueEntrySubResource.java index 89363cc..885fbd4 100644 --- a/omod/src/main/java/org/openmrs/module/queue/web/resources/QueueEntrySubResource.java +++ b/omod/src/main/java/org/openmrs/module/queue/web/resources/QueueEntrySubResource.java @@ -53,7 +53,7 @@ public void setParent(QueueEntry queueEntry, Queue queue) { @Override public PageableResult doGetAll(Queue queue, RequestContext requestContext) throws ResponseException { - Collection queueEntries = queue.getQueueEntries(); + Collection queueEntries = queue.getActiveQueueEntries(); return new NeedsPaging<>(new ArrayList<>(queueEntries), requestContext); }