From c6458c8dca9ab6731e9b9fbb109f4386b9202ec7 Mon Sep 17 00:00:00 2001 From: Michael Seaton Date: Wed, 25 Oct 2023 17:11:14 -0400 Subject: [PATCH] Remove VisitQueueEntry as a separate entity in favor of an optional property on QueueEntry; add additional searching capabilities; adjust web service resources to reflect changes. --- .../module/queue/api/QueueEntryService.java | 19 +- .../queue/api/QueueServicesWrapper.java | 151 ++++++++++ .../queue/api/VisitQueueEntryService.java | 114 -------- .../module/queue/api/dao/QueueEntryDao.java | 39 +-- .../queue/api/dao/VisitQueueEntryDao.java | 44 --- .../dao/impl/AbstractBaseQueueDaoImpl.java | 62 +++-- .../queue/api/dao/impl/QueueEntryDaoImpl.java | 117 +++----- .../api/dao/impl/VisitQueueEntryDaoImpl.java | 103 ------- .../queue/api/impl/QueueEntryServiceImpl.java | 60 ++-- .../api/impl/VisitQueueEntryServiceImpl.java | 148 ---------- .../module/queue/model/QueueEntry.java | 5 + .../module/queue/model/VisitQueueEntry.java | 44 --- .../tasks/AutoCloseVisitQueueEntryTask.java | 33 ++- .../queue/utils/QueueEntrySearchCriteria.java | 71 +++++ .../validators/VisitQueueEntryValidator.java | 41 --- api/src/main/resources/liquibase.xml | 34 +++ .../resources/moduleApplicationContext.xml | 24 +- .../queue/api/QueueEntryServiceTest.java | 43 +-- .../queue/api/VisitQueueEntryServiceTest.java | 86 ------ .../queue/api/dao/QueueEntryDaoTest.java | 62 ++--- .../queue/api/dao/VisitQueueEntryDaoTest.java | 261 ------------------ .../AutoCloseVisitQueueEntryTaskTest.java | 20 +- .../VisitQueueEntryValidatorTest.java | 79 ------ ...eueEntryDaoTest_conceptsInitialDataset.xml | 4 +- .../dao/QueueEntryDaoTest_initialDataset.xml | 7 +- .../visitQueueEntryDaoTest_initialDataset.xml | 20 -- ...DaoTest_visitQueueNumberInitialDataset.xml | 15 - .../resources/QueueEntryMetricsResource.java | 89 ++++-- .../web/resources/QueueEntryResource.java | 240 ++++++++++++++++ .../web/resources/QueueEntrySubResource.java | 6 +- .../resources/VisitQueueEntryResource.java | 142 ---------- .../InActiveQueueEntrySearchHandler.java | 65 ----- .../handlers/QueueCountSearchHandler.java | 60 ---- .../handlers/QueueEntrySearchHandler.java | 59 ---- .../QueueEntryMetricsResourceTest.java | 97 +++++-- .../web/resources/QueueEntryResourceTest.java | 76 +++-- .../resources/QueueEntrySubResourceTest.java | 151 ++++++++++ .../VisitQueueEntryResourceTest.java | 93 ------- 38 files changed, 1071 insertions(+), 1713 deletions(-) create mode 100644 api/src/main/java/org/openmrs/module/queue/api/QueueServicesWrapper.java delete mode 100644 api/src/main/java/org/openmrs/module/queue/api/VisitQueueEntryService.java delete mode 100644 api/src/main/java/org/openmrs/module/queue/api/dao/VisitQueueEntryDao.java delete mode 100644 api/src/main/java/org/openmrs/module/queue/api/dao/impl/VisitQueueEntryDaoImpl.java delete mode 100644 api/src/main/java/org/openmrs/module/queue/api/impl/VisitQueueEntryServiceImpl.java delete mode 100644 api/src/main/java/org/openmrs/module/queue/model/VisitQueueEntry.java create mode 100644 api/src/main/java/org/openmrs/module/queue/utils/QueueEntrySearchCriteria.java delete mode 100644 api/src/main/java/org/openmrs/module/queue/validators/VisitQueueEntryValidator.java delete mode 100644 api/src/test/java/org/openmrs/module/queue/api/VisitQueueEntryServiceTest.java delete mode 100644 api/src/test/java/org/openmrs/module/queue/api/dao/VisitQueueEntryDaoTest.java delete mode 100644 api/src/test/java/org/openmrs/module/queue/validators/VisitQueueEntryValidatorTest.java delete mode 100644 api/src/test/resources/org/openmrs/module/queue/api/dao/visitQueueEntryDaoTest_initialDataset.xml delete mode 100644 api/src/test/resources/org/openmrs/module/queue/api/dao/visitQueueEntryDaoTest_visitQueueNumberInitialDataset.xml create mode 100644 omod/src/main/java/org/openmrs/module/queue/web/resources/QueueEntryResource.java delete mode 100644 omod/src/main/java/org/openmrs/module/queue/web/resources/VisitQueueEntryResource.java delete mode 100644 omod/src/main/java/org/openmrs/module/queue/web/resources/search/handlers/InActiveQueueEntrySearchHandler.java delete mode 100644 omod/src/main/java/org/openmrs/module/queue/web/resources/search/handlers/QueueCountSearchHandler.java delete mode 100644 omod/src/main/java/org/openmrs/module/queue/web/resources/search/handlers/QueueEntrySearchHandler.java create mode 100644 omod/src/test/java/org/openmrs/module/queue/web/resources/QueueEntrySubResourceTest.java delete mode 100644 omod/src/test/java/org/openmrs/module/queue/web/resources/VisitQueueEntryResourceTest.java diff --git a/api/src/main/java/org/openmrs/module/queue/api/QueueEntryService.java b/api/src/main/java/org/openmrs/module/queue/api/QueueEntryService.java index 72499a2..4f8d301 100644 --- a/api/src/main/java/org/openmrs/module/queue/api/QueueEntryService.java +++ b/api/src/main/java/org/openmrs/module/queue/api/QueueEntryService.java @@ -11,7 +11,7 @@ import javax.validation.constraints.NotNull; -import java.util.Collection; +import java.util.List; import java.util.Optional; import org.openmrs.Location; @@ -20,6 +20,7 @@ import org.openmrs.api.APIException; import org.openmrs.module.queue.model.Queue; import org.openmrs.module.queue.model.QueueEntry; +import org.openmrs.module.queue.utils.QueueEntrySearchCriteria; public interface QueueEntryService { @@ -64,21 +65,15 @@ public interface QueueEntryService { void purgeQueueEntry(@NotNull QueueEntry queueEntry) throws APIException; /** - * Search for queue entries by conceptStatus - * - * @param conceptStatus queue entry conceptStatus - * @param includeVoided include/exclude voided queue entries - * @return {@link java.util.Collection} of queue entries with the specified statuses + * @return {@link List} of queue entries that match the given %{@link QueueEntrySearchCriteria} */ - Collection searchQueueEntriesByConceptStatus(@NotNull String conceptStatus, boolean includeVoided); + List getQueueEntries(@NotNull QueueEntrySearchCriteria searchCriteria); /** - * Gets count of queue entries by status - * - * @param status the queue entry status - * @return {@link java.lang.Long} count of queue entries by specified status + * @return {@link Long} count of queue entries that match the given + * %{@link QueueEntrySearchCriteria} */ - Long getQueueEntriesCountByStatus(@NotNull String status); + Long getCountOfQueueEntries(@NotNull QueueEntrySearchCriteria searchCriteria); /** * @param location diff --git a/api/src/main/java/org/openmrs/module/queue/api/QueueServicesWrapper.java b/api/src/main/java/org/openmrs/module/queue/api/QueueServicesWrapper.java new file mode 100644 index 0000000..896c4bf --- /dev/null +++ b/api/src/main/java/org/openmrs/module/queue/api/QueueServicesWrapper.java @@ -0,0 +1,151 @@ +/* + * 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.ArrayList; +import java.util.List; + +import lombok.Getter; +import org.apache.commons.lang3.StringUtils; +import org.openmrs.Concept; +import org.openmrs.Location; +import org.openmrs.Patient; +import org.openmrs.api.ConceptService; +import org.openmrs.api.LocationService; +import org.openmrs.api.PatientService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Component; + +@Component +@Getter +public class QueueServicesWrapper { + + private final QueueService queueService; + + private final QueueEntryService queueEntryService; + + private final QueueRoomService queueRoomService; + + private final RoomProviderMapService roomProviderMapService; + + private final ConceptService conceptService; + + private final LocationService locationService; + + private final PatientService patientService; + + @Autowired + public QueueServicesWrapper(@Qualifier("queue.QueueService") QueueService queueService, + @Qualifier("queue.QueueEntryService") QueueEntryService queueEntryService, + @Qualifier("queue.QueueRoomService") QueueRoomService queueRoomService, + @Qualifier("queue.RoomProviderMapService") RoomProviderMapService roomProviderMapService, + ConceptService conceptService, LocationService locationService, PatientService patientService) { + this.queueService = queueService; + this.queueEntryService = queueEntryService; + this.queueRoomService = queueRoomService; + this.roomProviderMapService = roomProviderMapService; + this.conceptService = conceptService; + this.locationService = locationService; + this.patientService = patientService; + } + + /** + * @param conceptRefs array of concept references + * @return a List of Concepts matching those references + */ + public List getConcepts(String[] conceptRefs) { + List ret = new ArrayList<>(); + for (String conceptRef : conceptRefs) { + ret.add(getConcept(conceptRef.trim())); + } + return ret; + } + + /** + * @param conceptRef a uuid, source:mapping, or unique name for the concept to retrieve + * @return the concept that matches the conceptRef + */ + public Concept getConcept(String conceptRef) { + if (StringUtils.isBlank(conceptRef)) { + return null; + } + Concept c = getConceptService().getConceptByUuid(conceptRef); + if (c != null) { + return c; + } + //handle mapping + int idx = conceptRef.indexOf(":"); + if (idx >= 0 && idx < conceptRef.length() - 1) { + String conceptSource = conceptRef.substring(0, idx); + String conceptCode = conceptRef.substring(idx + 1); + c = getConceptService().getConceptByMapping(conceptCode, conceptSource); + if (c != null) { + return c; + } + } + //handle name + List concepts = getConceptService().getConceptsByName(conceptRef); + if (concepts.size() == 1) { + return concepts.get(0); + } else if (concepts.size() > 1) { + throw new IllegalArgumentException("More than one concept is found with name: " + conceptRef); + } + throw new IllegalArgumentException("Unable to find concept: " + conceptRef); + } + + /** + * @param locationRefs array of concept references + * @return a List of Locations matching those references + */ + public List getLocations(String[] locationRefs) { + List ret = new ArrayList<>(); + for (String locationRef : locationRefs) { + ret.add(getLocation(locationRef.trim())); + } + return ret; + } + + /** + * @param locationRef a uuid or unique name for the location to retrieve + * @return the location that matches the locationRef + */ + public Location getLocation(String locationRef) { + if (StringUtils.isBlank(locationRef)) { + return null; + } + Location l = getLocationService().getLocationByUuid(locationRef); + if (l != null) { + return l; + } + List locations = getLocationService().getLocations(locationRef); + if (locations.size() == 1) { + return locations.get(0); + } else if (locations.size() > 1) { + throw new IllegalArgumentException("More than one location is found with name: " + locationRef); + } + throw new IllegalArgumentException("Unable to find location: " + locationRef); + } + + /** + * @param patientRef a uuid for the patient to retrieve + * @return the patient that matches the patientRef + */ + public Patient getPatient(String patientRef) { + if (StringUtils.isBlank(patientRef)) { + return null; + } + Patient p = getPatientService().getPatientByUuid(patientRef); + if (p != null) { + return p; + } + throw new IllegalArgumentException("Unable to find patient: " + patientRef); + } +} diff --git a/api/src/main/java/org/openmrs/module/queue/api/VisitQueueEntryService.java b/api/src/main/java/org/openmrs/module/queue/api/VisitQueueEntryService.java deleted file mode 100644 index ee74ad0..0000000 --- a/api/src/main/java/org/openmrs/module/queue/api/VisitQueueEntryService.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * 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 javax.validation.constraints.NotNull; - -import java.util.Collection; -import java.util.Optional; - -import org.openmrs.api.APIException; -import org.openmrs.module.queue.model.VisitQueueEntry; - -public interface VisitQueueEntryService { - - /** - * Gets a visit queue entry given UUID. - * - * @param uuid uuid of the visit queue entry to be returned. - * @return {@link org.openmrs.module.queue.model.VisitQueueEntry} - */ - Optional getVisitQueueEntryByUuid(@NotNull String uuid); - - /** - * Saves a visit queue entry record - * - * @param visitQueueEntry the visit queue entry to be saved - * @return saved {@link org.openmrs.module.queue.model.VisitQueueEntry} - */ - VisitQueueEntry createVisitQueueEntry(@NotNull VisitQueueEntry visitQueueEntry); - - /** - * Finds all {@link VisitQueueEntry} in the database - * - * @return {@link Collection} of all visit queue entries in the db - */ - Collection findAllVisitQueueEntries(); - - /** - * Gets only active queue entries (If endDate is null & not voided) - * - * @return {@link Collection} of queue entries - */ - Collection getActiveVisitQueueEntries(); - - /** - * Find visitQueueEntries by service and status i.e. Find patients waiting for(status) certain - * service - * - * @param status concept name for queueEntry status - * @param service concept name for queue service - * @param locationUuid location uuid for the queue location - * @return {@link Collection} visitQueueEntries matching specified parameters - */ - Collection findVisitQueueEntries(String status, String service, String locationUuid, - String patientUuid); - - /** - * Voids a visit queue entry record - * - * @param visitQueueEntryUuid uuid of the queue entry to be voided - * @param voidReason the reason for voiding the queue entry - */ - void voidVisitQueueEntry(@NotNull String visitQueueEntryUuid, String voidReason); - - /** - * Completely remove a visit queue entry from the database - * - * @param visitQueueEntry visit queue entry to be deleted - * @throws org.openmrs.api.APIException - */ - void purgeQueueEntry(@NotNull VisitQueueEntry visitQueueEntry) throws APIException; - - /** - * Gets count of visit queue entries filtered by status - * - * @param status concept status name - * @return {@link Long} count of visit queue entries - */ - Long getVisitQueueEntriesCountByStatus(@NotNull String status); - - /** - * Gets count of visit queue entries filtered by service - * - * @param service concept service name - * @return {@link Long} count of visit queue entries - */ - Long getVisitQueueEntriesCountByService(@NotNull String service); - - /** - * Gets count of visit queue entries filtered by location - * - * @param locationUuid location uuid - * @return {@link Long} count of visit queue entries - */ - Long getVisitQueueEntriesCountByLocation(@NotNull String locationUuid); - - /** - * Gets count of visit queue entries filtered by location, service and status - * - * @param service concept service name - * @param status concept status name - * @param locaitonUuid location uuid - * @return {@link Long} count of visit queue entries - */ - Long getVisitQueueEntriesCountByLocationStatusAndService(@NotNull String status, String service, String locationUUid); - -} diff --git a/api/src/main/java/org/openmrs/module/queue/api/dao/QueueEntryDao.java b/api/src/main/java/org/openmrs/module/queue/api/dao/QueueEntryDao.java index 45b7b26..411b9de 100644 --- a/api/src/main/java/org/openmrs/module/queue/api/dao/QueueEntryDao.java +++ b/api/src/main/java/org/openmrs/module/queue/api/dao/QueueEntryDao.java @@ -11,51 +11,24 @@ import javax.validation.constraints.NotNull; -import java.util.Collection; import java.util.List; import org.openmrs.Auditable; -import org.openmrs.Location; import org.openmrs.OpenmrsObject; -import org.openmrs.api.ConceptNameType; -import org.openmrs.module.queue.model.Queue; import org.openmrs.module.queue.model.QueueEntry; +import org.openmrs.module.queue.utils.QueueEntrySearchCriteria; public interface QueueEntryDao extends BaseQueueDao { /** - * Searches queue entries by conceptStatus - * - * @param conceptStatus the queueEntry conceptStatus - * @param includeVoided Include/exclude voided queue entries - * @return {@link java.util.Collection} of queue entries with the specified conceptStatus + * @return {@link List} of queue entries that match the given %{@link QueueEntrySearchCriteria} */ - Collection SearchQueueEntriesByConceptStatus(@NotNull String conceptStatus, ConceptNameType conceptNameType, - boolean localePreferred, boolean includeVoided); + List getQueueEntries(@NotNull QueueEntrySearchCriteria searchCriteria); /** - * Gets count of queue entries by given status - * - * @param conceptStatus the queue entry status - * @param conceptNameType the conceptNameType e.g. FULLY_SPECIFIED - * @param localePreferred locale preferred either true or false - * @return {@link java.lang.Long} count of queue entries by status + * @return {@link Long} of the number of queue entries that match the given + * %{@link QueueEntrySearchCriteria} */ - Long getQueueEntriesCountByConceptStatus(@NotNull String conceptStatus, ConceptNameType conceptNameType, - boolean localePreferred); - - /** - * @param location - * @param queue - * @return VisitQueueNumber - used to identify patients in the queue instead of using patient name - */ - String generateVisitQueueNumber(@NotNull Location location, @NotNull Queue queue); - - /** - * Gets active queue entries - * - * @return List of active queue entries - */ - List getActiveQueueEntries(); + Long getCountOfQueueEntries(@NotNull QueueEntrySearchCriteria searchCriteria); } diff --git a/api/src/main/java/org/openmrs/module/queue/api/dao/VisitQueueEntryDao.java b/api/src/main/java/org/openmrs/module/queue/api/dao/VisitQueueEntryDao.java deleted file mode 100644 index cb2c09b..0000000 --- a/api/src/main/java/org/openmrs/module/queue/api/dao/VisitQueueEntryDao.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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.dao; - -import java.util.Collection; - -import org.openmrs.Auditable; -import org.openmrs.OpenmrsObject; -import org.openmrs.api.ConceptNameType; -import org.openmrs.module.queue.model.VisitQueueEntry; - -public interface VisitQueueEntryDao extends BaseQueueDao { - - /** - * Finds {@link VisitQueueEntry} by conceptStatus and conceptService. - * - * @param conceptStatus conceptName for queueEntry conceptStatus concept. - * @param conceptService conceptName for queue conceptService concept. - * @param locationUuid location uuid for queue. - * @return {@link Collection} of visitQueueEntries matching specified parameters. - */ - Collection findVisitQueueEntriesByConceptStatusAndConceptService(String conceptStatus, - String conceptService, ConceptNameType conceptNameType, boolean localePreferred, String locationUuid, - String patientUuid); - - /** - * Gets visit queue entries filtered by service and status e.g. count of patient waiting for triage - * - * @param conceptStatus the status of patient in the queue - * @param conceptService which service is the patient waiting/attending to - * @param conceptNameType the concept name type e.g. fully_specified - * @param localePreferred locale preferred concept name - * @return {@link Long} count of patients matching the specified parameters - */ - Long getVisitQueueEntriesCountByLocationStatusAndService(String conceptStatus, String conceptService, - ConceptNameType conceptNameType, boolean localePreferred, String locationUuid); -} diff --git a/api/src/main/java/org/openmrs/module/queue/api/dao/impl/AbstractBaseQueueDaoImpl.java b/api/src/main/java/org/openmrs/module/queue/api/dao/impl/AbstractBaseQueueDaoImpl.java index e80e970..3690cbb 100644 --- a/api/src/main/java/org/openmrs/module/queue/api/dao/impl/AbstractBaseQueueDaoImpl.java +++ b/api/src/main/java/org/openmrs/module/queue/api/dao/impl/AbstractBaseQueueDaoImpl.java @@ -24,16 +24,11 @@ import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.SessionFactory; -import org.hibernate.criterion.Conjunction; -import org.hibernate.criterion.DetachedCriteria; -import org.hibernate.criterion.Projections; import org.hibernate.criterion.Restrictions; import org.openmrs.Auditable; -import org.openmrs.ConceptName; import org.openmrs.OpenmrsObject; import org.openmrs.Retireable; import org.openmrs.Voidable; -import org.openmrs.api.ConceptNameType; import org.openmrs.module.queue.api.dao.BaseQueueDao; @Slf4j @@ -123,22 +118,47 @@ protected void includeVoidedObjects(Criteria criteria, boolean includeRetired) { } /** - * Creates concept names subQuery - * - * @param conceptName Concept name - * @return {@link DetachedCriteria} conceptName subQuery + * If the passed value is null, return without limiting If the passed value is not null, add clause + * that the property must be equal to the value */ - protected DetachedCriteria conceptByNameDetachedCriteria(@NotNull String conceptName, boolean localePreferred, - ConceptNameType conceptNameType) { - DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ConceptName.class, "cn"); - Conjunction and = Restrictions.conjunction(); - and.add(Restrictions.eq("cn.name", conceptName)); - //An option to restrict by localePreferred - and.add(Restrictions.eq("cn.localePreferred", localePreferred)); - and.add(Restrictions.eq("cn.conceptNameType", conceptNameType)); - - detachedCriteria.add(and); - detachedCriteria.setProjection(Projections.property("cn.concept")); - return detachedCriteria; + protected void limitToEqualsProperty(Criteria criteria, String property, Object value) { + if (value != null) { + criteria.add(Restrictions.eq(property, value)); + } + } + + /** + * If the passed value is null, return without limiting If the passed value is not null, add clause + * that the property must greater or equal to the value + */ + protected void limitToGreaterThanOrEqualToProperty(Criteria criteria, String property, Object value) { + if (value != null) { + criteria.add(Restrictions.ge(property, value)); + } + } + + /** + * If the passed value is null, return without limiting If the passed value is not null, add clause + * that the property must be less or equal to the value + */ + protected void limitToLessThanOrEqualToProperty(Criteria criteria, String property, Object value) { + if (value != null) { + criteria.add(Restrictions.le(property, value)); + } + } + + /** + * If the passed values is null, return without limiting If the passed values is empty, add clause + * that the property must be null If the passed values is not empty, add clause that the property + * must be one of the given values + */ + protected void limitByCollectionProperty(Criteria criteria, String property, Collection values) { + if (values != null) { + if (values.isEmpty()) { + criteria.add(Restrictions.isNull(property)); + } else { + criteria.add(Restrictions.in(property, values)); + } + } } } diff --git a/api/src/main/java/org/openmrs/module/queue/api/dao/impl/QueueEntryDaoImpl.java b/api/src/main/java/org/openmrs/module/queue/api/dao/impl/QueueEntryDaoImpl.java index 477edde..4d64ab0 100644 --- a/api/src/main/java/org/openmrs/module/queue/api/dao/impl/QueueEntryDaoImpl.java +++ b/api/src/main/java/org/openmrs/module/queue/api/dao/impl/QueueEntryDaoImpl.java @@ -9,29 +9,15 @@ */ package org.openmrs.module.queue.api.dao.impl; -import static org.hibernate.criterion.Restrictions.eq; - -import javax.validation.constraints.NotNull; - -import java.time.LocalDate; -import java.time.ZoneId; -import java.util.Collection; -import java.util.Date; import java.util.List; -import org.apache.commons.lang3.StringUtils; import org.hibernate.Criteria; import org.hibernate.SessionFactory; -import org.hibernate.criterion.Conjunction; import org.hibernate.criterion.Projections; -import org.hibernate.criterion.Property; import org.hibernate.criterion.Restrictions; -import org.openmrs.Location; -import org.openmrs.api.ConceptNameType; import org.openmrs.module.queue.api.dao.QueueEntryDao; -import org.openmrs.module.queue.model.Queue; import org.openmrs.module.queue.model.QueueEntry; -import org.openmrs.module.queue.model.VisitQueueEntry; +import org.openmrs.module.queue.utils.QueueEntrySearchCriteria; import org.springframework.beans.factory.annotation.Qualifier; @SuppressWarnings("unchecked") @@ -41,84 +27,49 @@ public QueueEntryDaoImpl(@Qualifier("sessionFactory") SessionFactory sessionFact super(sessionFactory); } - /** - * @see QueueEntryDao#SearchQueueEntriesByConceptStatus(String, ConceptNameType, boolean, boolean) - */ @Override - public Collection SearchQueueEntriesByConceptStatus(@NotNull String status, ConceptNameType conceptNameType, - boolean localePreferred, boolean includeVoided) { - Criteria criteria = getCurrentSession().createCriteria(QueueEntry.class, "qe"); - //Include/exclude retired queues - includeVoidedObjects(criteria, includeVoided); - criteria.add( - Property.forName("qe.status").in(conceptByNameDetachedCriteria(status, localePreferred, conceptNameType))); - + public List getQueueEntries(QueueEntrySearchCriteria searchCriteria) { + Criteria criteria = createCriteriaFromSearchCriteria(searchCriteria); return criteria.list(); } - /** - * @see org.openmrs.module.queue.api.dao.QueueEntryDao#getQueueEntriesCountByConceptStatus(String, - * ConceptNameType, boolean) - */ @Override - public Long getQueueEntriesCountByConceptStatus(@NotNull String conceptStatus, ConceptNameType conceptNameType, - boolean localePreferred) { - Criteria criteria = getCurrentSession().createCriteria(QueueEntry.class, "qe"); - //Include/exclude retired queues - includeVoidedObjects(criteria, false); - criteria.add(Restrictions.and(Restrictions.isNull("qe.endedAt"), Restrictions.isNotNull("qe.startedAt"))); - criteria.add(Property.forName("qe.status") - .in(conceptByNameDetachedCriteria(conceptStatus, localePreferred, conceptNameType))); + public Long getCountOfQueueEntries(QueueEntrySearchCriteria searchCriteria) { + Criteria criteria = createCriteriaFromSearchCriteria(searchCriteria); criteria.setProjection(Projections.rowCount()); - return (Long) criteria.uniqueResult(); } - @Override - public String generateVisitQueueNumber(Location location, Queue queue) { - Criteria criteriaVisitQueueEntries = getCurrentSession().createCriteria(VisitQueueEntry.class, "_vqe"); - includeVoidedObjects(criteriaVisitQueueEntries, false); - Criteria criteriaQueueEntries = criteriaVisitQueueEntries.createCriteria("_vqe.queueEntry", "_qe"); - Criteria criteriaQueue = criteriaQueueEntries.createCriteria("_qe.queue", "_q"); - Criteria criteriaQueueLocation = criteriaQueue.createCriteria("_q.location", "_ql"); - criteriaQueueLocation.add(eq("_ql.uuid", location.getUuid())); - criteriaQueueLocation.add(eq("_q.uuid", queue.getUuid())); - - LocalDate minDate = LocalDate.now(); - LocalDate maxDate = LocalDate.now().plusDays(1); - - Date startOfDay = Date.from(minDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); - Date endOfDay = Date.from(maxDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); - - Conjunction queueEntryStartedAtCheck = Restrictions.conjunction(); - queueEntryStartedAtCheck.add(Restrictions.ge("startedAt", startOfDay)); - queueEntryStartedAtCheck.add(Restrictions.lt("startedAt", endOfDay)); - criteriaQueueEntries.add(queueEntryStartedAtCheck); - - List queueEntryList = criteriaQueueLocation.list(); - - int visitQueueNumber = 1; - - if (!queueEntryList.isEmpty()) { - visitQueueNumber = queueEntryList.size() + 1; - } - - String paddedString = StringUtils.leftPad(String.valueOf(visitQueueNumber), 3, "0"); - - String serviceName = queue.getName().toUpperCase(); - String prefix = serviceName.length() < 3 ? serviceName : serviceName.substring(0, 3); - return prefix + "-" + paddedString; - } - /** - * @see QueueEntryDao#getActiveQueueEntries() + * Convert the given {@link QueueEntrySearchCriteria} into ORM criteria */ - @Override - public List getActiveQueueEntries() { - Criteria criteria = getCurrentSession().createCriteria(QueueEntry.class); - // exclude voided queue entries - includeVoidedObjects(criteria, false); - criteria.add(Restrictions.isNull("endedAt")); - return (List) criteria.list(); + private Criteria createCriteriaFromSearchCriteria(QueueEntrySearchCriteria searchCriteria) { + Criteria c = getCurrentSession().createCriteria(QueueEntry.class, "qe"); + includeVoidedObjects(c, searchCriteria.isIncludedVoided()); + limitByCollectionProperty(c, "qe.queue", searchCriteria.getQueues()); + limitByCollectionProperty(c, "qe.queue.location", searchCriteria.getLocations()); + limitByCollectionProperty(c, "qe.queue.service", searchCriteria.getServices()); + limitToEqualsProperty(c, "qe.patient", searchCriteria.getPatient()); + limitToEqualsProperty(c, "qe.visit", searchCriteria.getVisit()); + limitByCollectionProperty(c, "qe.priority", searchCriteria.getPriorities()); + limitByCollectionProperty(c, "qe.status", searchCriteria.getStatuses()); + limitByCollectionProperty(c, "qe.locationWaitingFor", searchCriteria.getLocationsWaitingFor()); + limitByCollectionProperty(c, "qe.providerWaitingFor", searchCriteria.getProvidersWaitingFor()); + limitByCollectionProperty(c, "qe.queueComingFrom", searchCriteria.getQueuesComingFrom()); + limitToGreaterThanOrEqualToProperty(c, "ge.startedAt", searchCriteria.getStartedOnOrAfter()); + limitToLessThanOrEqualToProperty(c, "ge.startedAt", searchCriteria.getStartedOnOrBefore()); + limitToGreaterThanOrEqualToProperty(c, "ge.endedAt", searchCriteria.getEndedOnOrAfter()); + limitToLessThanOrEqualToProperty(c, "ge.endedAt", searchCriteria.getEndedOnOrBefore()); + if (searchCriteria.getHasVisit() == Boolean.TRUE) { + c.add(Restrictions.isNotNull("qe.visit")); + } else if (searchCriteria.getHasVisit() == Boolean.FALSE) { + c.add(Restrictions.isNull("qe.visit")); + } + if (searchCriteria.getIsEnded() == Boolean.TRUE) { + c.add(Restrictions.isNotNull("qe.endedAt")); + } else if (searchCriteria.getIsEnded() == Boolean.FALSE) { + c.add(Restrictions.isNull("qe.endedAt")); + } + return c; } } diff --git a/api/src/main/java/org/openmrs/module/queue/api/dao/impl/VisitQueueEntryDaoImpl.java b/api/src/main/java/org/openmrs/module/queue/api/dao/impl/VisitQueueEntryDaoImpl.java deleted file mode 100644 index 1955866..0000000 --- a/api/src/main/java/org/openmrs/module/queue/api/dao/impl/VisitQueueEntryDaoImpl.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * 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.dao.impl; - -import static org.hibernate.criterion.Restrictions.and; -import static org.hibernate.criterion.Restrictions.eq; - -import java.util.Collection; - -import lombok.extern.slf4j.Slf4j; -import org.hibernate.Criteria; -import org.hibernate.SessionFactory; -import org.hibernate.criterion.Order; -import org.hibernate.criterion.Projections; -import org.hibernate.criterion.Restrictions; -import org.hibernate.criterion.Subqueries; -import org.openmrs.api.ConceptNameType; -import org.openmrs.module.queue.api.dao.VisitQueueEntryDao; -import org.openmrs.module.queue.model.VisitQueueEntry; -import org.springframework.beans.factory.annotation.Qualifier; - -@Slf4j -@SuppressWarnings("unchecked") -public class VisitQueueEntryDaoImpl extends AbstractBaseQueueDaoImpl implements VisitQueueEntryDao { - - public VisitQueueEntryDaoImpl(@Qualifier("sessionFactory") SessionFactory sessionFactory) { - super(sessionFactory); - } - - /** - * @see VisitQueueEntryDao#findVisitQueueEntriesByConceptStatusAndConceptService(String, String, - * ConceptNameType, boolean, String, String) - */ - @Override - public Collection findVisitQueueEntriesByConceptStatusAndConceptService(String conceptStatus, - String conceptService, ConceptNameType conceptNameType, boolean localePreferred, String locationUuid, - String patientUuid) { - return handleVisitQueueEntriesCriteria(conceptStatus, conceptService, conceptNameType, localePreferred, locationUuid, - patientUuid).list(); - } - - @Override - public Long getVisitQueueEntriesCountByLocationStatusAndService(String conceptStatus, String conceptService, - ConceptNameType conceptNameType, boolean localePreferred, String locationUuid) { - Criteria criteria = handleVisitQueueEntriesCriteria(conceptStatus, conceptService, conceptNameType, localePreferred, - locationUuid, null); - criteria.setProjection(Projections.rowCount()); - return (Long) criteria.uniqueResult(); - } - - private Criteria handleVisitQueueEntriesCriteria(String conceptStatus, String conceptService, - ConceptNameType conceptNameType, boolean localePreferred, String locationUuid, String patientUuid) { - - // Filter queue entries for today - // LocalDate date = LocalDate.now(); - // Date startOfDay = Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant()); - // Date endOfDay = Date.from(date.plusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant()); - - Criteria criteriaVisitQueueEntries = getCurrentSession().createCriteria(VisitQueueEntry.class, "_vqe"); - includeVoidedObjects(criteriaVisitQueueEntries, false); - Criteria criteriaQueueEntries = criteriaVisitQueueEntries.createCriteria("_vqe.queueEntry", "_qe") - .addOrder(Order.desc("_qe.sortWeight")).addOrder(Order.asc("_qe.startedAt")); - Criteria criteriaQueue = criteriaQueueEntries.createCriteria("_qe.queue", "_q"); - Criteria criteriaQueueLocation = criteriaQueue.createCriteria("_q.location", "_ql"); - - criteriaQueueLocation - .add(Restrictions.and(Restrictions.isNull("_qe.endedAt"), Restrictions.isNotNull("_qe.startedAt"))); - // criteriaQueueLocation.add( - // Restrictions.and(Restrictions.ge("_qe.startedAt", startOfDay), Restrictions.lt("_qe.startedAt", endOfDay))); - if (locationUuid != null) { - criteriaQueueLocation.add(eq("_ql.uuid", locationUuid)); - } - - if (patientUuid != null) { - criteriaQueueLocation.createAlias("_qe.patient", "patient"); - criteriaQueueLocation.add(Restrictions.eq("patient.uuid", patientUuid)); - } - - if (conceptStatus != null && conceptService != null) { - criteriaQueueLocation.add(and( - Subqueries.propertiesIn(new String[] { "_qe.status" }, - conceptByNameDetachedCriteria(conceptStatus, localePreferred, conceptNameType)), - Subqueries.propertiesIn(new String[] { "_q.service" }, - conceptByNameDetachedCriteria(conceptService, localePreferred, conceptNameType)))); - } else if (conceptStatus != null) { - criteriaQueueLocation.add(Subqueries.propertiesIn(new String[] { "_qe.status" }, - conceptByNameDetachedCriteria(conceptStatus, localePreferred, conceptNameType))); - } else if (conceptService != null) { - criteriaQueueLocation.add(Subqueries.propertiesIn(new String[] { "_q.service" }, - conceptByNameDetachedCriteria(conceptService, localePreferred, conceptNameType))); - } - - return criteriaQueueLocation; - } - -} diff --git a/api/src/main/java/org/openmrs/module/queue/api/impl/QueueEntryServiceImpl.java b/api/src/main/java/org/openmrs/module/queue/api/impl/QueueEntryServiceImpl.java index a57a5ed..0bddfc8 100644 --- a/api/src/main/java/org/openmrs/module/queue/api/impl/QueueEntryServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/queue/api/impl/QueueEntryServiceImpl.java @@ -11,7 +11,10 @@ import javax.validation.constraints.NotNull; -import java.util.Collection; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.ZoneId; +import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Optional; @@ -19,18 +22,20 @@ import lombok.AccessLevel; import lombok.Setter; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.openmrs.Location; import org.openmrs.Visit; import org.openmrs.VisitAttribute; import org.openmrs.VisitAttributeType; import org.openmrs.api.APIException; -import org.openmrs.api.ConceptNameType; +import org.openmrs.api.VisitService; import org.openmrs.api.context.Context; import org.openmrs.api.impl.BaseOpenmrsService; import org.openmrs.module.queue.api.QueueEntryService; import org.openmrs.module.queue.api.dao.QueueEntryDao; import org.openmrs.module.queue.model.Queue; import org.openmrs.module.queue.model.QueueEntry; +import org.openmrs.module.queue.utils.QueueEntrySearchCriteria; import org.springframework.transaction.annotation.Transactional; @Slf4j @@ -40,14 +45,21 @@ public class QueueEntryServiceImpl extends BaseOpenmrsService implements QueueEn private QueueEntryDao dao; + private VisitService visitService; + public void setDao(QueueEntryDao dao) { this.dao = dao; } + public void setVisitService(VisitService visitService) { + this.visitService = visitService; + } + /** * @see org.openmrs.module.queue.api.QueueEntryService#getQueueEntryByUuid(String) */ @Override + @Transactional(readOnly = true) public Optional getQueueEntryByUuid(@NotNull String queueEntryUuid) { return this.dao.get(queueEntryUuid); } @@ -56,6 +68,7 @@ public Optional getQueueEntryByUuid(@NotNull String queueEntryUuid) * @see org.openmrs.module.queue.api.QueueEntryService#getQueueEntryById(Integer) */ @Override + @Transactional(readOnly = true) public Optional getQueueEntryById(@NotNull Integer queueEntryId) { return this.dao.get(queueEntryId); } @@ -65,6 +78,11 @@ public Optional getQueueEntryById(@NotNull Integer queueEntryId) { */ @Override public QueueEntry createQueueEntry(QueueEntry queueEntry) { + if (queueEntry.getVisit() != null) { + if (!queueEntry.getVisit().getPatient().equals(queueEntry.getPatient())) { + throw new IllegalArgumentException("Patient mismatch - visit.patient does not match patient"); + } + } return this.dao.createOrUpdate(queueEntry); } @@ -91,22 +109,16 @@ public void purgeQueueEntry(QueueEntry queueEntry) throws APIException { this.dao.delete(queueEntry); } - /** - * @see org.openmrs.module.queue.api.QueueEntryService#searchQueueEntriesByConceptStatus(String, - * boolean) - */ @Override - public Collection searchQueueEntriesByConceptStatus(String conceptStatus, boolean includeVoided) { - return this.dao.SearchQueueEntriesByConceptStatus(conceptStatus, ConceptNameType.FULLY_SPECIFIED, false, - includeVoided); + @Transactional(readOnly = true) + public List getQueueEntries(QueueEntrySearchCriteria searchCriteria) { + return dao.getQueueEntries(searchCriteria); } - /** - * @see org.openmrs.module.queue.api.QueueEntryService#getQueueEntriesCountByStatus(String) - */ @Override - public Long getQueueEntriesCountByStatus(@NotNull String status) { - return this.dao.getQueueEntriesCountByConceptStatus(status, ConceptNameType.FULLY_SPECIFIED, false); + @Transactional(readOnly = true) + public Long getCountOfQueueEntries(QueueEntrySearchCriteria searchCriteria) { + return dao.getCountOfQueueEntries(searchCriteria); } @Override @@ -115,20 +127,34 @@ public String generateVisitQueueNumber(Location location, Queue queue, Visit vis if (location == null || queue == null || visit == null || visitAttributeType == null) { throw new APIException("Sufficient parameters not supplied for generation of VisitQueueNumber"); } - String queueNumber = dao.generateVisitQueueNumber(location, queue); + QueueEntrySearchCriteria criteria = new QueueEntrySearchCriteria(); + criteria.setHasVisit(Boolean.TRUE); + criteria.setQueues(Collections.singletonList(queue)); + criteria.setLocations(Collections.singletonList(location)); + Date onOrAfter = Date.from(LocalDateTime.now().with(LocalTime.MIN).atZone(ZoneId.systemDefault()).toInstant()); + criteria.setStartedOnOrAfter(onOrAfter); + Date onOrBefore = Date.from(LocalDateTime.now().with(LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant()); + criteria.setStartedOnOrAfter(onOrBefore); + Long nextQueueNumber = getCountOfQueueEntries(criteria) + 1; + String paddedString = StringUtils.leftPad(String.valueOf(nextQueueNumber), 3, "0"); + String serviceName = queue.getName().toUpperCase(); + String prefix = serviceName.length() < 3 ? serviceName : serviceName.substring(0, 3); + String queueNumber = prefix + "-" + paddedString; // Create Visit Attribute using generated queue number VisitAttribute visitQueueNumber = new VisitAttribute(); visitQueueNumber.setAttributeType(visitAttributeType); visitQueueNumber.setValue(queueNumber); visit.setAttribute(visitQueueNumber); - Context.getVisitService().saveVisit(visit); + visitService.saveVisit(visit); return queueNumber; } @Override public void closeActiveQueueEntries() { - List queueEntries = dao.getActiveQueueEntries(); + QueueEntrySearchCriteria criteria = new QueueEntrySearchCriteria(); + criteria.setIsEnded(Boolean.FALSE); + List queueEntries = getQueueEntries(criteria); queueEntries.forEach(this::endQueueEntry); } diff --git a/api/src/main/java/org/openmrs/module/queue/api/impl/VisitQueueEntryServiceImpl.java b/api/src/main/java/org/openmrs/module/queue/api/impl/VisitQueueEntryServiceImpl.java deleted file mode 100644 index e7fd498..0000000 --- a/api/src/main/java/org/openmrs/module/queue/api/impl/VisitQueueEntryServiceImpl.java +++ /dev/null @@ -1,148 +0,0 @@ -/* - * 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.impl; - -import javax.validation.constraints.NotNull; - -import java.util.Collection; -import java.util.Date; -import java.util.Optional; -import java.util.stream.Collectors; - -import lombok.AccessLevel; -import lombok.Setter; -import lombok.extern.slf4j.Slf4j; -import org.openmrs.Patient; -import org.openmrs.Visit; -import org.openmrs.api.APIException; -import org.openmrs.api.ConceptNameType; -import org.openmrs.api.context.Context; -import org.openmrs.api.impl.BaseOpenmrsService; -import org.openmrs.module.queue.api.QueueEntryService; -import org.openmrs.module.queue.api.VisitQueueEntryService; -import org.openmrs.module.queue.api.dao.VisitQueueEntryDao; -import org.openmrs.module.queue.model.QueueEntry; -import org.openmrs.module.queue.model.VisitQueueEntry; -import org.springframework.transaction.annotation.Transactional; - -@Slf4j -@Transactional -@Setter(AccessLevel.MODULE) -public class VisitQueueEntryServiceImpl extends BaseOpenmrsService implements VisitQueueEntryService { - - private VisitQueueEntryDao dao; - - public void setDao(VisitQueueEntryDao dao) { - this.dao = dao; - } - - @Override - public Optional getVisitQueueEntryByUuid(@NotNull String uuid) { - return this.dao.get(uuid); - } - - @Override - public VisitQueueEntry createVisitQueueEntry(@NotNull VisitQueueEntry visitQueueEntry) { - //verify visit -patient & queue entry patient - //Todo more refactor - Visit visit = Context.getVisitService().getVisitByUuid(visitQueueEntry.getVisit().getUuid()); - Patient visitPatient = visit.getPatient(); - Patient queueEntryPatient = visitQueueEntry.getQueueEntry().getPatient(); - if (visitPatient != null & queueEntryPatient != null) { - boolean isPatientSame = visitPatient.getUuid().equals(queueEntryPatient.getUuid()); - if (!isPatientSame) { - throw new IllegalArgumentException("Patient mismatch - visit.patient does not match queueEntry.patient"); - } - QueueEntry newlyCreatedQueueEntry = Context.getService(QueueEntryService.class) - .createQueueEntry(visitQueueEntry.getQueueEntry()); - visitQueueEntry.setQueueEntry(newlyCreatedQueueEntry); - return this.dao.createOrUpdate(visitQueueEntry); - } - return null; - } - - /** - * @see VisitQueueEntryService#findAllVisitQueueEntries() - */ - @Override - public Collection findAllVisitQueueEntries() { - return dao.findAll(); - } - - @Override - public Collection getActiveVisitQueueEntries() { - Collection queueEntries = this.dao.findAll(false); - - //Remove inactive queue entries - //queueEntries.removeIf((vqe -> vqe.getQueueEntry().getEndedAt() != null)); - return queueEntries.stream().filter(visitQueueEntry -> visitQueueEntry.getQueueEntry().getEndedAt() == null) - .collect(Collectors.toList()); - } - - @Override - public Collection findVisitQueueEntries(String status, String service, String locationUuid, - String patientUuid) { - //Restrict to fully_specified concept names - return dao.findVisitQueueEntriesByConceptStatusAndConceptService(status, service, ConceptNameType.FULLY_SPECIFIED, - true, locationUuid, patientUuid); - } - - @Override - public void voidVisitQueueEntry(@NotNull String visitQueueEntryUuid, String voidReason) { - this.dao.get(visitQueueEntryUuid).ifPresent(visitQueueEntry -> { - visitQueueEntry.setVoided(true); - visitQueueEntry.setDateVoided(new Date()); - visitQueueEntry.setVoidReason(voidReason); - visitQueueEntry.setVoidedBy(Context.getAuthenticatedUser()); - this.dao.createOrUpdate(visitQueueEntry); - }); - } - - @Override - public void purgeQueueEntry(@NotNull VisitQueueEntry visitQueueEntry) throws APIException { - this.dao.delete(visitQueueEntry); - } - - /** - * @see VisitQueueEntryService#getVisitQueueEntriesCountByStatus(String) - */ - @Override - public Long getVisitQueueEntriesCountByStatus(String status) { - return dao.getVisitQueueEntriesCountByLocationStatusAndService(status, null, ConceptNameType.FULLY_SPECIFIED, true, - null); - } - - /** - * @see VisitQueueEntryService#getVisitQueueEntriesCountByService(String) - */ - @Override - public Long getVisitQueueEntriesCountByService(String service) { - return dao.getVisitQueueEntriesCountByLocationStatusAndService(null, service, ConceptNameType.FULLY_SPECIFIED, true, - null); - } - - /** - * @see VisitQueueEntryService#getVisitQueueEntriesCountByLocation(String) - */ - @Override - public Long getVisitQueueEntriesCountByLocation(String locaitonUuid) { - return dao.getVisitQueueEntriesCountByLocationStatusAndService(null, null, null, false, locaitonUuid); - } - - /** - * @see VisitQueueEntryService#getVisitQueueEntriesCountByLocationStatusAndService(String, String, - * String) - */ - @Override - public Long getVisitQueueEntriesCountByLocationStatusAndService(String status, String service, String locationUuid) { - return dao.getVisitQueueEntriesCountByLocationStatusAndService(status, service, ConceptNameType.FULLY_SPECIFIED, - true, locationUuid); - } -} diff --git a/api/src/main/java/org/openmrs/module/queue/model/QueueEntry.java b/api/src/main/java/org/openmrs/module/queue/model/QueueEntry.java index 074978d..d97a154 100644 --- a/api/src/main/java/org/openmrs/module/queue/model/QueueEntry.java +++ b/api/src/main/java/org/openmrs/module/queue/model/QueueEntry.java @@ -31,6 +31,7 @@ import org.openmrs.Location; import org.openmrs.Patient; import org.openmrs.Provider; +import org.openmrs.Visit; @EqualsAndHashCode(callSuper = true) @NoArgsConstructor @@ -57,6 +58,10 @@ public class QueueEntry extends BaseChangeableOpenmrsData { @JoinColumn(name = "patient_id", nullable = false) private Patient patient; + @ManyToOne + @JoinColumn(name = "visit_id") + private Visit visit; + @ManyToOne @JoinColumn(name = "priority", referencedColumnName = "concept_id", nullable = false) private Concept priority; diff --git a/api/src/main/java/org/openmrs/module/queue/model/VisitQueueEntry.java b/api/src/main/java/org/openmrs/module/queue/model/VisitQueueEntry.java deleted file mode 100644 index 67018d3..0000000 --- a/api/src/main/java/org/openmrs/module/queue/model/VisitQueueEntry.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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 javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; -import javax.persistence.Table; - -import lombok.Data; -import lombok.EqualsAndHashCode; -import org.openmrs.BaseOpenmrsData; -import org.openmrs.Visit; - -@EqualsAndHashCode(callSuper = true, onlyExplicitlyIncluded = true) -@Data -@Entity -@Table(name = "visit_queue_entries") -public class VisitQueueEntry extends BaseOpenmrsData { - - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - @Column(name = "visit_queue_entry_id") - private Integer id; - - @ManyToOne - @JoinColumn(name = "visit_id", nullable = false) - private Visit visit; - - @ManyToOne - @JoinColumn(name = "queue_entry_id", nullable = false) - private QueueEntry queueEntry; -} diff --git a/api/src/main/java/org/openmrs/module/queue/tasks/AutoCloseVisitQueueEntryTask.java b/api/src/main/java/org/openmrs/module/queue/tasks/AutoCloseVisitQueueEntryTask.java index a977949..0cdc92c 100644 --- a/api/src/main/java/org/openmrs/module/queue/tasks/AutoCloseVisitQueueEntryTask.java +++ b/api/src/main/java/org/openmrs/module/queue/tasks/AutoCloseVisitQueueEntryTask.java @@ -9,16 +9,15 @@ */ package org.openmrs.module.queue.tasks; -import java.util.Collection; import java.util.Date; +import java.util.List; import lombok.extern.slf4j.Slf4j; import org.openmrs.Visit; import org.openmrs.api.context.Context; import org.openmrs.module.queue.api.QueueEntryService; -import org.openmrs.module.queue.api.VisitQueueEntryService; import org.openmrs.module.queue.model.QueueEntry; -import org.openmrs.module.queue.model.VisitQueueEntry; +import org.openmrs.module.queue.utils.QueueEntrySearchCriteria; /** * This iterates over all active VisitQueueEntries If the Visit associated with any of these has @@ -39,19 +38,16 @@ public void run() { log.debug("Executing: " + getClass()); try { currentlyExecuting = true; - Collection queueEntries = getActiveVisitQueueEntries(); + List queueEntries = getActiveVisitQueueEntries(); log.debug("There are " + queueEntries.size() + " active visit queue entries"); - for (VisitQueueEntry visitQueueEntry : queueEntries) { - Visit visit = visitQueueEntry.getVisit(); - QueueEntry queueEntry = visitQueueEntry.getQueueEntry(); - if (visit != null) { - Date visitStopDatetime = visit.getStopDatetime(); - if (visitStopDatetime != null) { - log.debug("Visit " + visit.getVisitId() + " is closed at " + visitStopDatetime); - log.debug("Auto closing queue entry " + queueEntry.getQueueEntryId()); - queueEntry.setEndedAt(visitStopDatetime); - saveQueueEntry(queueEntry); - } + for (QueueEntry queueEntry : queueEntries) { + Visit visit = queueEntry.getVisit(); + Date visitStopDatetime = visit.getStopDatetime(); + if (visitStopDatetime != null) { + log.debug("Visit " + visit.getVisitId() + " is closed at " + visitStopDatetime); + log.debug("Auto closing queue entry " + queueEntry.getQueueEntryId()); + queueEntry.setEndedAt(visitStopDatetime); + saveQueueEntry(queueEntry); } } } @@ -63,8 +59,11 @@ public void run() { /** * @return the active VisitQueueEntries */ - protected Collection getActiveVisitQueueEntries() { - return Context.getService(VisitQueueEntryService.class).getActiveVisitQueueEntries(); + protected List getActiveVisitQueueEntries() { + QueueEntrySearchCriteria criteria = new QueueEntrySearchCriteria(); + criteria.setIsEnded(false); + criteria.setHasVisit(true); + return Context.getService(QueueEntryService.class).getQueueEntries(criteria); } /** diff --git a/api/src/main/java/org/openmrs/module/queue/utils/QueueEntrySearchCriteria.java b/api/src/main/java/org/openmrs/module/queue/utils/QueueEntrySearchCriteria.java new file mode 100644 index 0000000..59c985d --- /dev/null +++ b/api/src/main/java/org/openmrs/module/queue/utils/QueueEntrySearchCriteria.java @@ -0,0 +1,71 @@ +/* + * 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.utils; + +import java.io.Serializable; +import java.util.Collection; +import java.util.Date; + +import lombok.Data; +import org.openmrs.Concept; +import org.openmrs.Location; +import org.openmrs.Patient; +import org.openmrs.Provider; +import org.openmrs.Visit; +import org.openmrs.module.queue.model.Queue; + +/** + * Bean definition used for retrieving Queue Entries that meet specific criteria All properties + * should be considered to further limit results (ANDed together) Any property that is null will not + * limit by the related property Any Collection that is empty will return only those results for + * which the related property is null Any Boolean property without a default value will not limit if + * the property is null For example, to not limit by priority, set the priority property to null To + * limit to only those entries whose priority is null, set the priority property to an empty + * collection + */ +@Data +public class QueueEntrySearchCriteria implements Serializable { + + private static final long serialVersionUID = 1L; + + private Collection queues; + + private Collection locations; + + private Collection services; + + private Patient patient; + + private Visit visit; + + private Boolean hasVisit; + + private Collection priorities; + + private Collection statuses; + + private Collection locationsWaitingFor; + + private Collection providersWaitingFor; + + private Collection queuesComingFrom; + + private Date startedOnOrAfter; + + private Date startedOnOrBefore; + + private Boolean isEnded; + + private Date endedOnOrAfter; + + private Date endedOnOrBefore; + + private boolean includedVoided = false; +} diff --git a/api/src/main/java/org/openmrs/module/queue/validators/VisitQueueEntryValidator.java b/api/src/main/java/org/openmrs/module/queue/validators/VisitQueueEntryValidator.java deleted file mode 100644 index 7e73806..0000000 --- a/api/src/main/java/org/openmrs/module/queue/validators/VisitQueueEntryValidator.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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.validators; - -import org.openmrs.annotation.Handler; -import org.openmrs.module.queue.model.VisitQueueEntry; -import org.openmrs.module.queue.utils.QueueValidationUtils; -import org.springframework.validation.Errors; -import org.springframework.validation.ValidationUtils; -import org.springframework.validation.Validator; - -@Handler(supports = { VisitQueueEntry.class }, order = 50) -public class VisitQueueEntryValidator implements Validator { - - @Override - public boolean supports(Class clazz) { - return VisitQueueEntry.class.isAssignableFrom(clazz); - } - - @Override - public void validate(Object target, Errors errors) { - if (!(target instanceof VisitQueueEntry)) { - throw new IllegalArgumentException("the parameter target must be of type " + VisitQueueEntry.class); - } - //Reject null visit & queueEntry - ValidationUtils.rejectIfEmptyOrWhitespace(errors, "visit", "visitQueueEntry.visit.null", - "The property visit should not be null"); - ValidationUtils.rejectIfEmptyOrWhitespace(errors, "queueEntry", "visitQueueEntry.queueEntry.null", - "The property queueEntry should not be null"); - - VisitQueueEntry visitQueueEntry = (VisitQueueEntry) target; - QueueValidationUtils.validateQueueEntry(visitQueueEntry.getQueueEntry(), errors); - } -} diff --git a/api/src/main/resources/liquibase.xml b/api/src/main/resources/liquibase.xml index 8acb6c6..cedc982 100644 --- a/api/src/main/resources/liquibase.xml +++ b/api/src/main/resources/liquibase.xml @@ -385,4 +385,38 @@ schedulable_class = 'org.openmrs.module.queue.tasks.AutoCloseQueueEntryTask' + + + + + + + + Add column visit_id to queue entry table + + + + + + + + + + + + + update queue_entry qe + inner join visit_queue_entries vqe on qe.queue_entry_id = vqe.queue_entry_id + set qe.visit_id = vqe.visit_id; + + + + diff --git a/api/src/main/resources/moduleApplicationContext.xml b/api/src/main/resources/moduleApplicationContext.xml index 60ae554..441f902 100644 --- a/api/src/main/resources/moduleApplicationContext.xml +++ b/api/src/main/resources/moduleApplicationContext.xml @@ -50,6 +50,7 @@ + @@ -63,26 +64,6 @@ - - - - - - - - - - - - - - - org.openmrs.module.queue.api.VisitQueueEntryService - - - - @@ -127,9 +108,6 @@ - - - diff --git a/api/src/test/java/org/openmrs/module/queue/api/QueueEntryServiceTest.java b/api/src/test/java/org/openmrs/module/queue/api/QueueEntryServiceTest.java index 530f470..c9c5303 100644 --- a/api/src/test/java/org/openmrs/module/queue/api/QueueEntryServiceTest.java +++ b/api/src/test/java/org/openmrs/module/queue/api/QueueEntryServiceTest.java @@ -10,8 +10,10 @@ package org.openmrs.module.queue.api; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -24,10 +26,15 @@ import org.mockito.MockitoAnnotations; import org.mockito.junit.MockitoJUnitRunner; import org.openmrs.Concept; -import org.openmrs.api.ConceptNameType; +import org.openmrs.Location; +import org.openmrs.Visit; +import org.openmrs.VisitAttributeType; +import org.openmrs.api.VisitService; import org.openmrs.module.queue.api.dao.QueueEntryDao; import org.openmrs.module.queue.api.impl.QueueEntryServiceImpl; +import org.openmrs.module.queue.model.Queue; import org.openmrs.module.queue.model.QueueEntry; +import org.openmrs.module.queue.utils.QueueEntrySearchCriteria; @RunWith(MockitoJUnitRunner.class) public class QueueEntryServiceTest { @@ -36,20 +43,20 @@ public class QueueEntryServiceTest { private static final Integer QUEUE_ENTRY_ID = 14; - private static final String QUEUE_ENTRY_STATUS = "Waiting for Service"; - - private static final String BAD_QUEUE_ENTRY_STATUS = "Waiting for Service"; - private QueueEntryServiceImpl queueEntryService; @Mock private QueueEntryDao dao; + @Mock + private VisitService visitService; + @Before public void setupMocks() { MockitoAnnotations.openMocks(this); queueEntryService = new QueueEntryServiceImpl(); queueEntryService.setDao(dao); + queueEntryService.setVisitService(visitService); } @Test @@ -95,9 +102,7 @@ public void shouldCreateNewQueueEntryRecord() { @Test public void shouldVoidQueue() { when(dao.get(QUEUE_ENTRY_UUID)).thenReturn(Optional.empty()); - queueEntryService.voidQueueEntry(QUEUE_ENTRY_UUID, "voidReason"); - assertThat(queueEntryService.getQueueEntryByUuid(QUEUE_ENTRY_UUID).isPresent(), is(false)); } @@ -105,24 +110,28 @@ public void shouldVoidQueue() { public void shouldPurgeQueue() { QueueEntry queueEntry = mock(QueueEntry.class); when(dao.get(QUEUE_ENTRY_UUID)).thenReturn(Optional.empty()); - queueEntryService.purgeQueueEntry(queueEntry); assertThat(queueEntryService.getQueueEntryByUuid(QUEUE_ENTRY_UUID).isPresent(), is(false)); } @Test public void shouldReturnCountOfQueueEntriesByStatus() { - when(dao.getQueueEntriesCountByConceptStatus(QUEUE_ENTRY_STATUS, ConceptNameType.FULLY_SPECIFIED, false)) - .thenReturn(1L); - - assertThat(queueEntryService.getQueueEntriesCountByStatus(QUEUE_ENTRY_STATUS), is(1L)); + QueueEntrySearchCriteria criteria = new QueueEntrySearchCriteria(); + when(dao.getCountOfQueueEntries(criteria)).thenReturn(1L); + assertThat(queueEntryService.getCountOfQueueEntries(criteria), is(1L)); } @Test - public void shouldReturnZeroForBadGivenStatus() { - when(dao.getQueueEntriesCountByConceptStatus(BAD_QUEUE_ENTRY_STATUS, ConceptNameType.FULLY_SPECIFIED, false)) - .thenReturn(0L); - - assertThat(queueEntryService.getQueueEntriesCountByStatus(QUEUE_ENTRY_STATUS), is(0L)); + public void shouldGenerateVisitQueueNumber() { + Visit visit = new Visit(); + Location location = new Location(); + Queue queue = new Queue(); + queue.setName("Consultation Queue"); + VisitAttributeType visitAttributeType = new VisitAttributeType(); + when(visitService.saveVisit(visit)).thenReturn(visit); + when(queueEntryService.getCountOfQueueEntries(any())).thenReturn(52L); + String queueNumber = queueEntryService.generateVisitQueueNumber(location, queue, visit, visitAttributeType); + assertThat(queueNumber, notNullValue()); + assertThat(queueNumber, equalTo("CON-053")); } } diff --git a/api/src/test/java/org/openmrs/module/queue/api/VisitQueueEntryServiceTest.java b/api/src/test/java/org/openmrs/module/queue/api/VisitQueueEntryServiceTest.java deleted file mode 100644 index 7a07896..0000000 --- a/api/src/test/java/org/openmrs/module/queue/api/VisitQueueEntryServiceTest.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * 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.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.notNullValue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.util.Optional; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.mockito.junit.MockitoJUnitRunner; -import org.openmrs.Visit; -import org.openmrs.module.queue.api.dao.VisitQueueEntryDao; -import org.openmrs.module.queue.api.impl.VisitQueueEntryServiceImpl; -import org.openmrs.module.queue.model.QueueEntry; -import org.openmrs.module.queue.model.VisitQueueEntry; - -@RunWith(MockitoJUnitRunner.class) -public class VisitQueueEntryServiceTest { - - private static final String VISIT_QUEUE_ENTRY_UUID = "j8f0bb90-86f4-4d9c-8b6c-3713d748ef74"; - - private VisitQueueEntryServiceImpl visitQueueEntryService; - - @Mock - private VisitQueueEntryDao dao; - - @Before - public void setupMocks() { - MockitoAnnotations.openMocks(this); - visitQueueEntryService = new VisitQueueEntryServiceImpl(); - visitQueueEntryService.setDao(dao); - } - - @Test - public void shouldGetVisitQueueEntryByUuid() { - VisitQueueEntry visitQueueEntry = mock(VisitQueueEntry.class); - when(visitQueueEntry.getUuid()).thenReturn(VISIT_QUEUE_ENTRY_UUID); - when(dao.get(VISIT_QUEUE_ENTRY_UUID)).thenReturn(Optional.of(visitQueueEntry)); - - Optional result = visitQueueEntryService.getVisitQueueEntryByUuid(VISIT_QUEUE_ENTRY_UUID); - assertThat(result.isPresent(), is(true)); - result.ifPresent(q -> assertThat(q.getUuid(), is(VISIT_QUEUE_ENTRY_UUID))); - } - - @Test - public void shouldCreateNewRecordForVisitQueueEntry() { - VisitQueueEntry visitQueueEntry = mock(VisitQueueEntry.class); - Visit visit = mock(Visit.class); - QueueEntry queueEntry = mock(QueueEntry.class); - - when(visitQueueEntry.getQueueEntry()).thenReturn(queueEntry); - when(visitQueueEntry.getVisit()).thenReturn(visit); - when(dao.createOrUpdate(visitQueueEntry)).thenReturn(visitQueueEntry); - - VisitQueueEntry result = this.dao.createOrUpdate(visitQueueEntry); - assertThat(result, notNullValue()); - assertThat(result.getVisit(), is(visit)); - assertThat(result.getQueueEntry(), is(queueEntry)); - - } - - @Test - public void shouldVoidVisitQueueEntryRecord() { - when(dao.get(VISIT_QUEUE_ENTRY_UUID)).thenReturn(Optional.empty()); - - visitQueueEntryService.voidVisitQueueEntry(VISIT_QUEUE_ENTRY_UUID, "voidReason"); - - assertThat(visitQueueEntryService.getVisitQueueEntryByUuid(VISIT_QUEUE_ENTRY_UUID).isPresent(), is(false)); - } - -} diff --git a/api/src/test/java/org/openmrs/module/queue/api/dao/QueueEntryDaoTest.java b/api/src/test/java/org/openmrs/module/queue/api/dao/QueueEntryDaoTest.java index 557a071..2f5ea4b 100644 --- a/api/src/test/java/org/openmrs/module/queue/api/dao/QueueEntryDaoTest.java +++ b/api/src/test/java/org/openmrs/module/queue/api/dao/QueueEntryDaoTest.java @@ -18,6 +18,7 @@ import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Optional; @@ -25,14 +26,13 @@ import org.junit.Before; import org.junit.Test; import org.openmrs.Concept; -import org.openmrs.Location; import org.openmrs.Patient; -import org.openmrs.api.ConceptNameType; +import org.openmrs.api.ConceptService; import org.openmrs.api.context.Context; import org.openmrs.module.queue.SpringTestConfiguration; -import org.openmrs.module.queue.api.QueueEntryService; import org.openmrs.module.queue.model.Queue; import org.openmrs.module.queue.model.QueueEntry; +import org.openmrs.module.queue.utils.QueueEntrySearchCriteria; import org.openmrs.test.BaseModuleContextSensitiveTest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; @@ -58,15 +58,10 @@ public class QueueEntryDaoTest extends BaseModuleContextSensitiveTest { private static final List QUEUE_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/QueueDaoTest_initialDataset.xml", "org/openmrs/module/queue/api/dao/QueueEntryDaoTest_patientInitialDataset.xml", - "org/openmrs/module/queue/api/dao/QueueEntryDaoTest_initialDataset.xml", "org/openmrs/module/queue/api/dao/VisitQueueEntryDaoTest_visitInitialDataset.xml", - "org/openmrs/module/queue/api/dao/visitQueueEntryDaoTest_visitQueueNumberInitialDataset.xml"); - - private static final String QUEUE_ENTRY_STATUS = "Waiting for service"; - - private static final String BAD_QUEUE_ENTRY_STATUS = "Bad Waiting for service"; + "org/openmrs/module/queue/api/dao/QueueDaoTest_initialDataset.xml", + "org/openmrs/module/queue/api/dao/QueueEntryDaoTest_initialDataset.xml"); @Autowired @Qualifier("queueEntryDao") @@ -76,9 +71,15 @@ public class QueueEntryDaoTest extends BaseModuleContextSensitiveTest { @Qualifier("queueDao") private QueueDao queueDao; + @Autowired + private ConceptService conceptService; + + Concept waitingForService; + @Before public void setup() { QUEUE_INITIAL_DATASET_XML.forEach(this::executeDataSet); + waitingForService = conceptService.getConcept(3001); } @Test @@ -203,57 +204,46 @@ void verifyQueueEntryConceptPriorityProperty(QueueEntry queueEntry) { @Test public void shouldSearchQueueEntriesByStatus() { - Collection queueEntries = dao.SearchQueueEntriesByConceptStatus(QUEUE_ENTRY_STATUS, - ConceptNameType.FULLY_SPECIFIED, false, false); - + QueueEntrySearchCriteria criteria = new QueueEntrySearchCriteria(); + criteria.setStatuses(Collections.singletonList(waitingForService)); + List queueEntries = dao.getQueueEntries(criteria); assertThat(queueEntries.isEmpty(), is(false)); assertThat(queueEntries, hasSize(1)); queueEntries.forEach(queueEntry -> { assertThat(queueEntry.getStatus(), notNullValue()); - assertThat(queueEntry.getStatus().getName().getName(), is(QUEUE_ENTRY_STATUS)); + assertThat(queueEntry.getStatus(), equalTo(waitingForService)); }); } @Test public void shouldSearchQueueEntriesByStatusIncludingVoidedQueueEntries() { - Collection queueEntries = dao.SearchQueueEntriesByConceptStatus(QUEUE_ENTRY_STATUS, - ConceptNameType.FULLY_SPECIFIED, false, true); - + QueueEntrySearchCriteria criteria = new QueueEntrySearchCriteria(); + criteria.setStatuses(Collections.singletonList(waitingForService)); + criteria.setIncludedVoided(true); + List queueEntries = dao.getQueueEntries(criteria); assertThat(queueEntries.isEmpty(), is(false)); assertThat(queueEntries, hasSize(2)); queueEntries.forEach(queueEntry -> { assertThat(queueEntry.getStatus(), notNullValue()); - assertThat(queueEntry.getStatus().getName().getName(), is(QUEUE_ENTRY_STATUS)); + assertThat(queueEntry.getStatus(), equalTo(waitingForService)); }); } @Test public void shouldCountQueueEntriesByStatus() { - Long queueEntriesCountByStatusCount = dao.getQueueEntriesCountByConceptStatus(QUEUE_ENTRY_STATUS, - ConceptNameType.FULLY_SPECIFIED, false); - + QueueEntrySearchCriteria criteria = new QueueEntrySearchCriteria(); + criteria.setStatuses(Collections.singletonList(waitingForService)); + Long queueEntriesCountByStatusCount = dao.getCountOfQueueEntries(criteria); assertThat(queueEntriesCountByStatusCount, notNullValue()); assertThat(queueEntriesCountByStatusCount, is(1L)); } @Test public void shouldZeroCountQueueEntriesByBadStatus() { - Long queueEntriesCountByStatusCount = dao.getQueueEntriesCountByConceptStatus(BAD_QUEUE_ENTRY_STATUS, - ConceptNameType.FULLY_SPECIFIED, false); - + QueueEntrySearchCriteria criteria = new QueueEntrySearchCriteria(); + criteria.setStatuses(Collections.singletonList(conceptService.getConcept(1000))); + Long queueEntriesCountByStatusCount = dao.getCountOfQueueEntries(criteria); assertThat(queueEntriesCountByStatusCount, notNullValue()); assertThat(queueEntriesCountByStatusCount, is(0L)); } - - @Test - public void shouldGenerateVisitQueueNumber() { - QueueEntry queueEntry = Context.getService(QueueEntryService.class) - .getQueueEntryByUuid("7ub8fe43-2813-4kbc-80dc-2e5d30252cc5").get(); - Location location = Context.getLocationService().getLocationByUuid("d0938432-1691-11df-97a5-7038c098"); - - String queueNumber = dao.generateVisitQueueNumber(location, queueEntry.getQueue()); - - assertThat(queueNumber, notNullValue()); - assertThat(queueNumber, equalTo("CON-001")); - } } diff --git a/api/src/test/java/org/openmrs/module/queue/api/dao/VisitQueueEntryDaoTest.java b/api/src/test/java/org/openmrs/module/queue/api/dao/VisitQueueEntryDaoTest.java deleted file mode 100644 index db4a46b..0000000 --- a/api/src/test/java/org/openmrs/module/queue/api/dao/VisitQueueEntryDaoTest.java +++ /dev/null @@ -1,261 +0,0 @@ -/* - * 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.dao; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.*; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Date; -import java.util.List; -import java.util.Optional; - -import lombok.extern.slf4j.Slf4j; -import org.junit.Before; -import org.junit.Test; -import org.openmrs.Visit; -import org.openmrs.api.ConceptNameType; -import org.openmrs.api.context.Context; -import org.openmrs.module.queue.SpringTestConfiguration; -import org.openmrs.module.queue.api.QueueEntryService; -import org.openmrs.module.queue.model.QueueEntry; -import org.openmrs.module.queue.model.VisitQueueEntry; -import org.openmrs.test.BaseModuleContextSensitiveTest; -import org.openmrs.test.SkipBaseSetup; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.test.context.ContextConfiguration; - -@Slf4j -@ContextConfiguration(classes = SpringTestConfiguration.class, inheritLocations = false) -public class VisitQueueEntryDaoTest extends BaseModuleContextSensitiveTest { - - private static final String VISIT_QUEUE_ENTRY_UUID = "5eb8fe43-2813-4kbc-80dc-2e5d30252cc3"; - - private static final String NEW_VISIT_QUEUE_ENTRY_UUID = "7eb6fe43-2813-4kbc-80dc-2e5d30252kk9"; - - private static final String VOIDED_VISIT_QUEUE_ENTRY_UUID = "4eb8fe43-2813-4kbc-80dc-2e5d30252cc6"; - - private static final String QUEUE_ENTRY_UUID = "4eb8fe43-2813-4kbc-80dc-2e5d30252cc6"; - - private static final String VISIT_UUID = "j848b0c0-1ade-11e1-9c71-00248140a6eb"; - - private static final String WAITING_FOR_STATUS = "Waiting for service"; - - private static final String IN_SERVICE_STATUS = "In service"; - - private static final String TRIAGE_SERVICE = "Triage"; - - private static final String CONSULTATION_SERVICE = "Consultation"; - - private static final String LOCATION_UUID = "d0938432-1691-11df-97a5-7038c098"; - - @Autowired - @Qualifier("visitQueueEntryDao") - private VisitQueueEntryDao dao; - - //the order of the list is important! - private static final List VISIT_QUEUE_ENTRY_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/QueueDaoTest_initialDataset.xml", - "org/openmrs/module/queue/api/dao/QueueEntryDaoTest_patientInitialDataset.xml", - "org/openmrs/module/queue/api/dao/QueueEntryDaoTest_initialDataset.xml", - "org/openmrs/module/queue/api/dao/VisitQueueEntryDaoTest_visitInitialDataset.xml", - "org/openmrs/module/queue/api/dao/visitQueueEntryDaoTest_initialDataset.xml"); - - @Before - @SkipBaseSetup - public void setup() { - VISIT_QUEUE_ENTRY_INITIAL_DATASET_XML.forEach(this::executeDataSet); - } - - @Test - public void shouldGetVisitQueueEntryByUuid() { - Optional result = dao.get(VISIT_QUEUE_ENTRY_UUID); - - assertThat(result, notNullValue()); - assertThat(result.isPresent(), is(true)); - assertThat(result.get().getUuid(), is(VISIT_QUEUE_ENTRY_UUID)); - } - - @Test - public void shouldReturnNullForVoidedVisitQueueEntry() { - Optional visitQueueEntry = dao.get(VOIDED_VISIT_QUEUE_ENTRY_UUID); - assertThat(visitQueueEntry.isPresent(), is(false)); - } - - @Test - public void shouldCreateNewVisitQueryEntryRecord() { - VisitQueueEntry visitQueueEntry = new VisitQueueEntry(); - visitQueueEntry.setUuid(NEW_VISIT_QUEUE_ENTRY_UUID); - Optional queueEntryOptional = Context.getService(QueueEntryService.class) - .getQueueEntryByUuid(QUEUE_ENTRY_UUID); - assertThat(queueEntryOptional.isPresent(), is(true)); - queueEntryOptional.ifPresent(visitQueueEntry::setQueueEntry); - - Visit visit = Context.getVisitService().getVisitByUuid(VISIT_UUID); - assertThat(visit, notNullValue()); - visitQueueEntry.setVisit(visit); - visitQueueEntry.setDateCreated(new Date()); - - VisitQueueEntry result = dao.createOrUpdate(visitQueueEntry); - assertThat(result, notNullValue()); - assertThat(result.getUuid(), is(NEW_VISIT_QUEUE_ENTRY_UUID)); - assertThat(result.getQueueEntry(), notNullValue()); - assertThat(result.getQueueEntry().getUuid(), is(QUEUE_ENTRY_UUID)); - } - - @Test - public void shouldUpdateAnExistingVisitQueueEntryRecord() { - Optional existingVisitQueueEntry = dao.get(VISIT_QUEUE_ENTRY_UUID); - assertThat(existingVisitQueueEntry.isPresent(), is(true)); - - //Update the existing visit_queue_entry record - existingVisitQueueEntry.ifPresent(visitQueueEntry -> { - visitQueueEntry.setVoided(true); - visitQueueEntry.setDateVoided(new Date()); - visitQueueEntry.setVoidReason("Testing update operation"); - dao.createOrUpdate(visitQueueEntry); - }); - - //Get the updated visit_queue_entry - Optional updatedVisitQueueEntry = dao.get(VISIT_QUEUE_ENTRY_UUID); - //If false - update was successful - assertThat(updatedVisitQueueEntry.isPresent(), is(false)); - //Get the voided visit queue entry record - Optional alreadyVoidedVisitQueueEntryRecord = dao.get(existingVisitQueueEntry.get().getId()); - assertThat(alreadyVoidedVisitQueueEntryRecord.isPresent(), is(true)); - assertThat(alreadyVoidedVisitQueueEntryRecord.get().getVoided(), is(true)); - assertThat(alreadyVoidedVisitQueueEntryRecord.get().getVoidReason(), is("Testing update operation")); - } - - @Test - public void shouldFindAllVisitQueueEntries() { - Collection visitQueueEntries = dao.findAll(); - assertThat(visitQueueEntries.isEmpty(), is(false)); - assertThat(visitQueueEntries, hasSize(2)); - } - - @Test - public void shouldFindAllVisitQueueEntriesIncludingRetired() { - Collection visitQueueEntries = dao.findAll(true); - assertThat(visitQueueEntries.isEmpty(), is(false)); - assertThat(visitQueueEntries, hasSize(3)); - } - - @Test - public void shouldDeleteVisitQueueEntryByUuid() { - dao.delete(VISIT_QUEUE_ENTRY_UUID); - - Optional result = dao.get(VISIT_QUEUE_ENTRY_UUID); - //verify delete operation - assertThat(result.isPresent(), is(false)); - } - - @Test - public void shouldDeleteVisitQueueEntryByEntity() { - dao.get(VISIT_QUEUE_ENTRY_UUID).ifPresent((queueEntry) -> dao.delete(queueEntry)); - - Optional result = dao.get(VISIT_QUEUE_ENTRY_UUID); - //verify delete operation - assertThat(result.isPresent(), is(false)); - } - - @Test - public void shouldFindVisitQueueEntriesByWaitingForStatusAndService() { - Collection result = dao.findVisitQueueEntriesByConceptStatusAndConceptService(WAITING_FOR_STATUS, - TRIAGE_SERVICE, ConceptNameType.FULLY_SPECIFIED, false, null, null); - - assertThat(result, notNullValue()); - assertThat(result, hasSize(1)); - } - - @Test - public void shouldFindVisitQueueEntriesByInServiceStatusAndService() { - Collection result = dao.findVisitQueueEntriesByConceptStatusAndConceptService(IN_SERVICE_STATUS, - CONSULTATION_SERVICE, ConceptNameType.FULLY_SPECIFIED, false, null, null); - - assertThat(result, notNullValue()); - assertThat(result, hasSize(1)); - } - - @Test - public void shouldFilterVisitQueueEntriesByInServiceStatus() { - Collection result = dao.findVisitQueueEntriesByConceptStatusAndConceptService(IN_SERVICE_STATUS, - null, ConceptNameType.FULLY_SPECIFIED, false, null, null); - - assertThat(result, notNullValue()); - assertThat(result, hasSize(1)); - } - - @Test - public void shouldFilterVisitQueueEntriesByConsultationService() { - Collection result = dao.findVisitQueueEntriesByConceptStatusAndConceptService(null, - CONSULTATION_SERVICE, ConceptNameType.FULLY_SPECIFIED, false, null, null); - - assertThat(result, notNullValue()); - assertThat(result, hasSize(1)); - } - - @Test - public void shouldFilterVisitQueueEntriesByLocation() { - Collection result = dao.findVisitQueueEntriesByConceptStatusAndConceptService(null, null, null, - false, LOCATION_UUID, null); - - assertThat(result, notNullValue()); - assertThat(result, hasSize(2)); - } - - @Test - public void shouldNotFilterVisitQueueEntriesByServiceAndStatusIfBothAreNull() { - Collection result = dao.findVisitQueueEntriesByConceptStatusAndConceptService(null, null, - ConceptNameType.FULLY_SPECIFIED, false, null, null); - - assertThat(result, notNullValue()); - assertThat(result, hasSize(2)); - } - - @Test - public void shouldGetCountOfVisitQueueEntriesByInServiceStatus() { - Long result = dao.getVisitQueueEntriesCountByLocationStatusAndService(IN_SERVICE_STATUS, null, - ConceptNameType.FULLY_SPECIFIED, false, null); - - assertThat(result, notNullValue()); - assertThat(result, is(1L)); - } - - @Test - public void shouldGetCountOfVisitQueueEntriesByConsultationService() { - Long result = dao.getVisitQueueEntriesCountByLocationStatusAndService(null, CONSULTATION_SERVICE, - ConceptNameType.FULLY_SPECIFIED, false, null); - - assertThat(result, notNullValue()); - assertThat(result, is(1L)); - } - - @Test - public void shouldGetCountOfVisitQueueEntriesByLocation() { - Long result = dao.getVisitQueueEntriesCountByLocationStatusAndService(null, null, null, false, LOCATION_UUID); - - assertThat(result, notNullValue()); - assertThat(result, is(2L)); - } - - @Test - public void shouldGetCountOfVisitQueueEntriesByServiceAndStatus() { - Long result = dao.getVisitQueueEntriesCountByLocationStatusAndService(WAITING_FOR_STATUS, CONSULTATION_SERVICE, - ConceptNameType.FULLY_SPECIFIED, false, null); - - assertThat(result, notNullValue()); - assertThat(result, is(0L)); - } -} diff --git a/api/src/test/java/org/openmrs/module/queue/tasks/AutoCloseVisitQueueEntryTaskTest.java b/api/src/test/java/org/openmrs/module/queue/tasks/AutoCloseVisitQueueEntryTaskTest.java index 58a650d..22aeb28 100644 --- a/api/src/test/java/org/openmrs/module/queue/tasks/AutoCloseVisitQueueEntryTaskTest.java +++ b/api/src/test/java/org/openmrs/module/queue/tasks/AutoCloseVisitQueueEntryTaskTest.java @@ -16,7 +16,6 @@ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Collection; import java.util.Date; import java.util.List; import java.util.stream.Collectors; @@ -24,17 +23,16 @@ import org.junit.Test; import org.openmrs.Visit; import org.openmrs.module.queue.model.QueueEntry; -import org.openmrs.module.queue.model.VisitQueueEntry; public class AutoCloseVisitQueueEntryTaskTest { - final List queueEntries = new ArrayList<>(); + final List queueEntries = new ArrayList<>(); class TestAutoCloseVisitEntryTask extends AutoCloseVisitQueueEntryTask { @Override - protected Collection getActiveVisitQueueEntries() { - return queueEntries.stream().filter(e -> e.getQueueEntry().getEndedAt() == null).collect(Collectors.toList()); + protected List getActiveVisitQueueEntries() { + return queueEntries.stream().filter(e -> e.getEndedAt() == null).collect(Collectors.toList()); } @Override @@ -50,19 +48,15 @@ public void shouldAutoCloseVisitQueueEntriesIfVisitIsClosed() throws Exception { visit1.setStartDatetime(getDate("2020-01-01 10:00")); QueueEntry queueEntry1 = new QueueEntry(); queueEntry1.setStartedAt(getDate("2020-01-01 10:10")); - VisitQueueEntry visitQueueEntry1 = new VisitQueueEntry(); - visitQueueEntry1.setVisit(visit1); - visitQueueEntry1.setQueueEntry(queueEntry1); - queueEntries.add(visitQueueEntry1); + queueEntry1.setVisit(visit1); + queueEntries.add(queueEntry1); Visit visit2 = new Visit(); visit2.setStartDatetime(getDate("2021-01-01 10:00")); QueueEntry queueEntry2 = new QueueEntry(); queueEntry2.setStartedAt(getDate("2021-01-01 10:20")); - VisitQueueEntry visitQueueEntry2 = new VisitQueueEntry(); - visitQueueEntry2.setVisit(visit2); - visitQueueEntry2.setQueueEntry(queueEntry2); - queueEntries.add(visitQueueEntry2); + queueEntry2.setVisit(visit2); + queueEntries.add(queueEntry2); TestAutoCloseVisitEntryTask task = new TestAutoCloseVisitEntryTask(); task.run(); diff --git a/api/src/test/java/org/openmrs/module/queue/validators/VisitQueueEntryValidatorTest.java b/api/src/test/java/org/openmrs/module/queue/validators/VisitQueueEntryValidatorTest.java deleted file mode 100644 index 97f0d5a..0000000 --- a/api/src/test/java/org/openmrs/module/queue/validators/VisitQueueEntryValidatorTest.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * 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.validators; - -import static org.hamcrest.Matchers.notNullValue; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -import java.util.Arrays; -import java.util.List; -import java.util.Optional; - -import org.junit.Before; -import org.junit.Test; -import org.openmrs.Concept; -import org.openmrs.api.context.Context; -import org.openmrs.module.queue.SpringTestConfiguration; -import org.openmrs.module.queue.api.QueueEntryService; -import org.openmrs.module.queue.model.QueueEntry; -import org.openmrs.module.queue.utils.QueueValidationUtils; -import org.openmrs.test.BaseModuleContextSensitiveTest; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.context.ContextConfiguration; - -@ContextConfiguration(classes = { SpringTestConfiguration.class }, inheritLocations = false) -public class VisitQueueEntryValidatorTest extends BaseModuleContextSensitiveTest { - - private static final String VALID_CONCEPT_STATUS_UUID = "4eb8fe43-2813-4kbc-80dc-2e5d30252cc6"; - - private static final String INVALID_STATUS_CONCEPT_UUID = "67b910bd-298c-4ecf-a632-661ae2f446op"; - - //the order of the list is important! - private static final List VISIT_QUEUE_ENTRY_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/QueueDaoTest_initialDataset.xml", - "org/openmrs/module/queue/api/dao/QueueEntryDaoTest_patientInitialDataset.xml", - "org/openmrs/module/queue/api/dao/QueueEntryDaoTest_initialDataset.xml", - "org/openmrs/module/queue/api/dao/VisitQueueEntryDaoTest_visitInitialDataset.xml", - "org/openmrs/module/queue/api/dao/visitQueueEntryDaoTest_initialDataset.xml", - "org/openmrs/module/queue/validators/QueueEntryValidatorTest_globalPropertyInitialDataset.xml"); - - @Autowired - private VisitQueueEntryValidator validator; - - @Before - public void setup() { - VISIT_QUEUE_ENTRY_INITIAL_DATASET_XML.forEach(this::executeDataSet); - } - - @Test - public void validatorNotNull() { - assertNotNull(validator); - } - - @Test - public void shouldReturnTrueForValidStatusConcept() { - Optional queueEntry = Context.getService(QueueEntryService.class) - .getQueueEntryByUuid(VALID_CONCEPT_STATUS_UUID); - assertTrue(queueEntry.isPresent()); - QueueValidationUtils.isValidStatus(queueEntry.get().getStatus()); - } - - @Test - public void shouldReturnFalseForInvalidStatusConcept() { - Concept concept = Context.getConceptService().getConceptByUuid(INVALID_STATUS_CONCEPT_UUID); - assertThat(concept, notNullValue()); - assertFalse(QueueValidationUtils.isValidStatus(concept)); - } -} diff --git a/api/src/test/resources/org/openmrs/module/queue/api/dao/QueueEntryDaoTest_conceptsInitialDataset.xml b/api/src/test/resources/org/openmrs/module/queue/api/dao/QueueEntryDaoTest_conceptsInitialDataset.xml index fcdcad6..e74703b 100644 --- a/api/src/test/resources/org/openmrs/module/queue/api/dao/QueueEntryDaoTest_conceptsInitialDataset.xml +++ b/api/src/test/resources/org/openmrs/module/queue/api/dao/QueueEntryDaoTest_conceptsInitialDataset.xml @@ -13,8 +13,8 @@ - - + + diff --git a/api/src/test/resources/org/openmrs/module/queue/api/dao/QueueEntryDaoTest_initialDataset.xml b/api/src/test/resources/org/openmrs/module/queue/api/dao/QueueEntryDaoTest_initialDataset.xml index 5d8845c..65de019 100644 --- a/api/src/test/resources/org/openmrs/module/queue/api/dao/QueueEntryDaoTest_initialDataset.xml +++ b/api/src/test/resources/org/openmrs/module/queue/api/dao/QueueEntryDaoTest_initialDataset.xml @@ -9,12 +9,13 @@ graphic logo is a trademark of OpenMRS Inc. --> - - - + diff --git a/api/src/test/resources/org/openmrs/module/queue/api/dao/visitQueueEntryDaoTest_initialDataset.xml b/api/src/test/resources/org/openmrs/module/queue/api/dao/visitQueueEntryDaoTest_initialDataset.xml deleted file mode 100644 index efa2084..0000000 --- a/api/src/test/resources/org/openmrs/module/queue/api/dao/visitQueueEntryDaoTest_initialDataset.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - diff --git a/api/src/test/resources/org/openmrs/module/queue/api/dao/visitQueueEntryDaoTest_visitQueueNumberInitialDataset.xml b/api/src/test/resources/org/openmrs/module/queue/api/dao/visitQueueEntryDaoTest_visitQueueNumberInitialDataset.xml deleted file mode 100644 index f0d706f..0000000 --- a/api/src/test/resources/org/openmrs/module/queue/api/dao/visitQueueEntryDaoTest_visitQueueNumberInitialDataset.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - diff --git a/omod/src/main/java/org/openmrs/module/queue/web/resources/QueueEntryMetricsResource.java b/omod/src/main/java/org/openmrs/module/queue/web/resources/QueueEntryMetricsResource.java index 3562f8b..2b78ae2 100644 --- a/omod/src/main/java/org/openmrs/module/queue/web/resources/QueueEntryMetricsResource.java +++ b/omod/src/main/java/org/openmrs/module/queue/web/resources/QueueEntryMetricsResource.java @@ -10,9 +10,12 @@ package org.openmrs.module.queue.web.resources; import java.util.Arrays; +import java.util.Map; +import org.apache.commons.lang3.StringUtils; import org.openmrs.api.context.Context; -import org.openmrs.module.queue.api.VisitQueueEntryService; +import org.openmrs.module.queue.api.QueueServicesWrapper; +import org.openmrs.module.queue.utils.QueueEntrySearchCriteria; import org.openmrs.module.queue.web.resources.custom.response.GenericSingleObjectResult; import org.openmrs.module.queue.web.resources.custom.response.PropValue; import org.openmrs.module.queue.web.resources.custom.response.QueueEntryMetric; @@ -36,6 +39,18 @@ + "/queue-entry-metrics", supportedClass = QueueEntryMetric.class, supportedOpenmrsVersions = { "2.3 - 9.*" }) public class QueueEntryMetricsResource extends DelegatingCrudResource { + public static final String SEARCH_PARAM_STATUS = "status"; + + public static final String SEARCH_PARAM_SERVICE = "service"; + + public static final String SEARCH_PARAM_LOCATION = "location"; + + private final QueueServicesWrapper services; + + public QueueEntryMetricsResource() { + this.services = Context.getRegisteredComponents(QueueServicesWrapper.class).get(0); + } + @Override public SimpleObject getByUniqueId(String uuid) { throw new ResourceDoesNotSupportOperationException(); @@ -67,33 +82,57 @@ public DelegatingResourceDescription getRepresentationDescription(Representation } @Override + protected PageableResult doSearch(RequestContext requestContext) { - String status = requestContext.getParameter("status"); - String service = requestContext.getParameter("service"); - String locationUuid = requestContext.getParameter("location"); + QueueEntrySearchCriteria criteria = new QueueEntrySearchCriteria(); + Map parameterMap = getParameters(requestContext); + for (String parameterName : parameterMap.keySet()) { + switch (parameterName) { + case SEARCH_PARAM_STATUS: { + criteria.setStatuses(services.getConcepts(parameterMap.get(SEARCH_PARAM_STATUS))); + break; + } + case SEARCH_PARAM_SERVICE: { + criteria.setServices(services.getConcepts(parameterMap.get(SEARCH_PARAM_SERVICE))); + break; + } + case SEARCH_PARAM_LOCATION: { + criteria.setLocations(services.getLocations(parameterMap.get(SEARCH_PARAM_LOCATION))); + break; + } + default: { + log.debug("Unhandled search parameter found: " + parameterName); + } + } + } + if (criteria.getStatuses() == null && criteria.getServices() == null && criteria.getLocations() == null) { + return new EmptySearchResult(); + } - if (service != null && status != null && locationUuid != null) { - Long patientsCount = Context.getService(VisitQueueEntryService.class) - .getVisitQueueEntriesCountByLocationStatusAndService(status, service, locationUuid); - - return new GenericSingleObjectResult( - Arrays.asList(new PropValue("metric", status + " " + service), new PropValue("count", patientsCount))); - } else if (service != null && !service.isEmpty()) { - Long count = Context.getService(VisitQueueEntryService.class).getVisitQueueEntriesCountByService(service); - return new GenericSingleObjectResult( - Arrays.asList(new PropValue("metric", service), new PropValue("count", count))); - - } else if (status != null && !status.isEmpty()) { - Long count = Context.getService(VisitQueueEntryService.class).getVisitQueueEntriesCountByStatus(status); - return new GenericSingleObjectResult( - Arrays.asList(new PropValue("metric", status), new PropValue("count", count))); - - } else if (locationUuid != null && !locationUuid.isEmpty()) { - Long count = Context.getService(VisitQueueEntryService.class).getVisitQueueEntriesCountByLocation(locationUuid); - return new GenericSingleObjectResult( - Arrays.asList(new PropValue("metric", locationUuid), new PropValue("count", count))); + StringBuilder metric = new StringBuilder(); + if (criteria.getStatuses() != null) { + metric.append(String.join(",", parameterMap.get(SEARCH_PARAM_STATUS))); + } + if (criteria.getServices() != null) { + if (metric.length() > 0) { + metric.append(" "); + } + metric.append(String.join(",", parameterMap.get(SEARCH_PARAM_SERVICE))); + } + if (criteria.getLocations() != null && metric.length() == 0) { + metric.append(StringUtils.join(parameterMap.get(SEARCH_PARAM_LOCATION))); } - return new EmptySearchResult(); + Long count = services.getQueueEntryService().getCountOfQueueEntries(criteria); + return new GenericSingleObjectResult( + Arrays.asList(new PropValue("metric", metric.toString()), new PropValue("count", count))); + } + + /** + * @return the parameters for the given request context + */ + @SuppressWarnings("unchecked") + protected Map getParameters(RequestContext requestContext) { + return requestContext.getRequest().getParameterMap(); } } diff --git a/omod/src/main/java/org/openmrs/module/queue/web/resources/QueueEntryResource.java b/omod/src/main/java/org/openmrs/module/queue/web/resources/QueueEntryResource.java new file mode 100644 index 0000000..5e50a4c --- /dev/null +++ b/omod/src/main/java/org/openmrs/module/queue/web/resources/QueueEntryResource.java @@ -0,0 +1,240 @@ +/* + * 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.web.resources; + +import javax.validation.constraints.NotNull; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import lombok.extern.slf4j.Slf4j; +import org.openmrs.api.context.Context; +import org.openmrs.module.queue.api.QueueServicesWrapper; +import org.openmrs.module.queue.model.QueueEntry; +import org.openmrs.module.queue.utils.QueueEntrySearchCriteria; +import org.openmrs.module.webservices.rest.web.RequestContext; +import org.openmrs.module.webservices.rest.web.RestConstants; +import org.openmrs.module.webservices.rest.web.annotation.Resource; +import org.openmrs.module.webservices.rest.web.representation.CustomRepresentation; +import org.openmrs.module.webservices.rest.web.representation.DefaultRepresentation; +import org.openmrs.module.webservices.rest.web.representation.FullRepresentation; +import org.openmrs.module.webservices.rest.web.representation.RefRepresentation; +import org.openmrs.module.webservices.rest.web.representation.Representation; +import org.openmrs.module.webservices.rest.web.resource.api.PageableResult; +import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource; +import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription; +import org.openmrs.module.webservices.rest.web.resource.impl.EmptySearchResult; +import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging; +import org.openmrs.module.webservices.rest.web.response.ObjectNotFoundException; +import org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException; +import org.openmrs.module.webservices.rest.web.response.ResponseException; + +/** + * REST resource for Queue Entries + */ +@Slf4j +@SuppressWarnings("unused") +@Resource(name = RestConstants.VERSION_1 + "/queue-entry", supportedClass = QueueEntry.class, supportedOpenmrsVersions = { + "2.3 - 9.*" }) +public class QueueEntryResource extends DelegatingCrudResource { + + public static final String SEARCH_PARAM_STATUS = "status"; + + public static final String SEARCH_PARAM_SERVICE = "service"; + + public static final String SEARCH_PARAM_LOCATION = "location"; + + public static final String SEARCH_PARAM_PATIENT = "patient"; + + private final QueueServicesWrapper services; + + public QueueEntryResource() { + this.services = Context.getRegisteredComponents(QueueServicesWrapper.class).get(0); + } + + @Override + public QueueEntry getByUniqueId(@NotNull String uuid) { + Optional queueEntryOptional = services.getQueueEntryService().getQueueEntryByUuid(uuid); + if (!queueEntryOptional.isPresent()) { + throw new ObjectNotFoundException("Could not find visit queue entry with uuid " + uuid); + } + return queueEntryOptional.get(); + } + + @Override + protected void delete(QueueEntry qe, String reason, RequestContext requestContext) throws ResponseException { + services.getQueueEntryService().voidQueueEntry(qe.getUuid(), reason); + } + + @Override + public QueueEntry newDelegate() { + return new QueueEntry(); + } + + @Override + public QueueEntry save(QueueEntry queueEntry) { + return services.getQueueEntryService().createQueueEntry(queueEntry); + } + + @Override + public void purge(QueueEntry queueEntry, RequestContext requestContext) throws ResponseException { + services.getQueueEntryService().purgeQueueEntry(queueEntry); + } + + @Override + protected PageableResult doGetAll(RequestContext requestContext) throws ResponseException { + QueueEntrySearchCriteria criteria = new QueueEntrySearchCriteria(); + criteria.setIsEnded(false); + List activeEntries = services.getQueueEntryService().getQueueEntries(criteria); + return new NeedsPaging<>(new ArrayList<>(activeEntries), requestContext); + } + + @Override + @SuppressWarnings("unchecked") + protected PageableResult doSearch(RequestContext requestContext) { + boolean criteriaFound = false; + QueueEntrySearchCriteria criteria = new QueueEntrySearchCriteria(); + Map parameterMap = requestContext.getRequest().getParameterMap(); + for (String parameterName : parameterMap.keySet()) { + switch (parameterName) { + case SEARCH_PARAM_STATUS: { + criteriaFound = true; + criteria.setStatuses(services.getConcepts(parameterMap.get(SEARCH_PARAM_STATUS))); + break; + } + case SEARCH_PARAM_SERVICE: { + criteriaFound = true; + criteria.setServices(services.getConcepts(parameterMap.get(SEARCH_PARAM_SERVICE))); + break; + } + case SEARCH_PARAM_LOCATION: { + criteriaFound = true; + criteria.setLocations(services.getLocations(parameterMap.get(SEARCH_PARAM_LOCATION))); + break; + } + case SEARCH_PARAM_PATIENT: { + criteriaFound = true; + criteria.setPatient(services.getPatient(parameterMap.get(SEARCH_PARAM_PATIENT)[0])); + break; + } + default: { + log.debug("Unhandled search parameter found: " + parameterName); + } + } + } + if (!criteriaFound) { + return new EmptySearchResult(); + } + Collection queueEntries = services.getQueueEntryService().getQueueEntries(criteria); + return new NeedsPaging<>(new ArrayList<>(queueEntries), requestContext); + } + + @Override + public DelegatingResourceDescription getCreatableProperties() throws ResourceDoesNotSupportOperationException { + DelegatingResourceDescription description = new DelegatingResourceDescription(); + description.addProperty("queue"); + description.addProperty("status"); + description.addProperty("priority"); + description.addProperty("priorityComment"); + description.addProperty("patient"); + description.addProperty("visit"); + description.addProperty("sortWeight"); + description.addProperty("startedAt"); + description.addProperty("locationWaitingFor"); + description.addProperty("queueComingFrom"); + description.addProperty("providerWaitingFor"); + return description; + } + + @Override + public DelegatingResourceDescription getUpdatableProperties() throws ResourceDoesNotSupportOperationException { + DelegatingResourceDescription description = new DelegatingResourceDescription(); + description.addProperty("status"); + description.addProperty("priorityComment"); + description.addProperty("sortWeight"); + description.addProperty("endedAt"); + return description; + } + + @Override + public DelegatingResourceDescription getRepresentationDescription(Representation representation) { + DelegatingResourceDescription description = new DelegatingResourceDescription(); + if (representation instanceof RefRepresentation) { + addSharedResourceDescriptionProperties(description); + description.addProperty("uuid"); + description.addProperty("display"); + description.addProperty("queue", Representation.REF); + description.addProperty("status", Representation.REF); + description.addProperty("patient", Representation.REF); + description.addProperty("visit", Representation.REF); + description.addProperty("priority", Representation.REF); + description.addProperty("priorityComment"); + description.addProperty("sortWeight"); + description.addProperty("startedAt"); + description.addProperty("endedAt"); + description.addProperty("locationWaitingFor", Representation.REF); + description.addProperty("queueComingFrom", Representation.REF); + description.addProperty("providerWaitingFor", Representation.REF); + description.addLink("full", ".?v=" + RestConstants.REPRESENTATION_FULL); + } else if (representation instanceof DefaultRepresentation) { + addSharedResourceDescriptionProperties(description); + description.addProperty("uuid"); + description.addProperty("display"); + description.addProperty("queue", Representation.DEFAULT); + description.addProperty("status", Representation.DEFAULT); + description.addProperty("patient", Representation.DEFAULT); + description.addProperty("visit", Representation.DEFAULT); + description.addProperty("priority", Representation.DEFAULT); + description.addProperty("priorityComment"); + description.addProperty("sortWeight"); + description.addProperty("startedAt"); + description.addProperty("endedAt"); + description.addProperty("locationWaitingFor", Representation.DEFAULT); + description.addProperty("queueComingFrom", Representation.DEFAULT); + description.addProperty("providerWaitingFor", Representation.DEFAULT); + description.addLink("full", ".?v=" + RestConstants.REPRESENTATION_FULL); + } else if (representation instanceof FullRepresentation) { + addSharedResourceDescriptionProperties(description); + description.addProperty("uuid"); + description.addProperty("display"); + description.addProperty("queue", Representation.FULL); + description.addProperty("status", Representation.FULL); + description.addProperty("patient", Representation.FULL); + description.addProperty("visit", Representation.FULL); + description.addProperty("priority", Representation.FULL); + description.addProperty("priorityComment"); + description.addProperty("sortWeight"); + description.addProperty("startedAt"); + description.addProperty("endedAt"); + description.addProperty("locationWaitingFor", Representation.FULL); + description.addProperty("queueComingFrom", Representation.FULL); + description.addProperty("providerWaitingFor", Representation.FULL); + description.addProperty("voided"); + description.addProperty("voidReason"); + description.addProperty("auditInfo"); + } else if (representation instanceof CustomRepresentation) { + description = null; + } + return description; + } + + private void addSharedResourceDescriptionProperties(DelegatingResourceDescription resourceDescription) { + resourceDescription.addSelfLink(); + resourceDescription.addProperty("uuid"); + } + + @Override + public String getResourceVersion() { + return "2.3"; + } +} 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 885fbd4..82f6cc0 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 @@ -115,9 +115,9 @@ public DelegatingResourceDescription getRepresentationDescription(Representation DelegatingResourceDescription resourceDescription = new DelegatingResourceDescription(); if (representation instanceof RefRepresentation) { this.addSharedResourceDescriptionProperties(resourceDescription); - resourceDescription.addProperty("queue", Representation.REF); resourceDescription.addProperty("status", Representation.REF); resourceDescription.addProperty("patient", Representation.REF); + resourceDescription.addProperty("patient", Representation.REF); resourceDescription.addProperty("priority", Representation.REF); resourceDescription.addProperty("locationWaitingFor", Representation.REF); resourceDescription.addProperty("queueComingFrom", Representation.REF); @@ -125,9 +125,9 @@ public DelegatingResourceDescription getRepresentationDescription(Representation resourceDescription.addLink("full", ".?v=" + RestConstants.REPRESENTATION_FULL); } else if (representation instanceof DefaultRepresentation) { this.addSharedResourceDescriptionProperties(resourceDescription); - resourceDescription.addProperty("queue", Representation.DEFAULT); resourceDescription.addProperty("status", Representation.DEFAULT); resourceDescription.addProperty("patient", Representation.DEFAULT); + resourceDescription.addProperty("visit", Representation.DEFAULT); resourceDescription.addProperty("priority", Representation.DEFAULT); resourceDescription.addProperty("locationWaitingFor", Representation.DEFAULT); resourceDescription.addProperty("queueComingFrom", Representation.DEFAULT); @@ -138,9 +138,9 @@ public DelegatingResourceDescription getRepresentationDescription(Representation resourceDescription.addProperty("voided"); resourceDescription.addProperty("voidReason"); resourceDescription.addProperty("auditInfo"); - resourceDescription.addProperty("queue", Representation.FULL); resourceDescription.addProperty("status", Representation.FULL); resourceDescription.addProperty("patient", Representation.FULL); + resourceDescription.addProperty("visit", Representation.FULL); resourceDescription.addProperty("priority", Representation.FULL); resourceDescription.addProperty("locationWaitingFor", Representation.FULL); resourceDescription.addProperty("queueComingFrom", Representation.FULL); diff --git a/omod/src/main/java/org/openmrs/module/queue/web/resources/VisitQueueEntryResource.java b/omod/src/main/java/org/openmrs/module/queue/web/resources/VisitQueueEntryResource.java deleted file mode 100644 index b3711da..0000000 --- a/omod/src/main/java/org/openmrs/module/queue/web/resources/VisitQueueEntryResource.java +++ /dev/null @@ -1,142 +0,0 @@ -/* - * 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.web.resources; - -import javax.validation.constraints.NotNull; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Optional; - -import lombok.extern.slf4j.Slf4j; -import org.openmrs.api.context.Context; -import org.openmrs.module.queue.api.VisitQueueEntryService; -import org.openmrs.module.queue.model.VisitQueueEntry; -import org.openmrs.module.webservices.rest.web.RequestContext; -import org.openmrs.module.webservices.rest.web.RestConstants; -import org.openmrs.module.webservices.rest.web.annotation.Resource; -import org.openmrs.module.webservices.rest.web.representation.CustomRepresentation; -import org.openmrs.module.webservices.rest.web.representation.DefaultRepresentation; -import org.openmrs.module.webservices.rest.web.representation.FullRepresentation; -import org.openmrs.module.webservices.rest.web.representation.RefRepresentation; -import org.openmrs.module.webservices.rest.web.representation.Representation; -import org.openmrs.module.webservices.rest.web.resource.api.PageableResult; -import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource; -import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription; -import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging; -import org.openmrs.module.webservices.rest.web.response.ObjectNotFoundException; -import org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException; -import org.openmrs.module.webservices.rest.web.response.ResponseException; - -/** - * By convention, resource names should use exclusively lowercase letters. Similarly, dashes (-) are - * conventionally used in place of underscores (_). - */ -@Slf4j -@SuppressWarnings("unused") -@Resource(name = RestConstants.VERSION_1 - + "/visit-queue-entry", supportedClass = VisitQueueEntry.class, supportedOpenmrsVersions = { "2.3 - 9.*" }) -public class VisitQueueEntryResource extends DelegatingCrudResource { - - private final VisitQueueEntryService visitQueueEntryService; - - public VisitQueueEntryResource() { - this.visitQueueEntryService = Context.getService(VisitQueueEntryService.class); - } - - @Override - public VisitQueueEntry getByUniqueId(@NotNull String uuid) { - Optional visitQueueEntryOptional = this.visitQueueEntryService.getVisitQueueEntryByUuid(uuid); - if (!visitQueueEntryOptional.isPresent()) { - throw new ObjectNotFoundException("Could not find visit queue entry with uuid " + uuid); - } - return visitQueueEntryOptional.get(); - } - - @Override - protected void delete(VisitQueueEntry visitQueueEntry, String voidReason, RequestContext requestContext) - throws ResponseException { - this.visitQueueEntryService.voidVisitQueueEntry(visitQueueEntry.getUuid(), voidReason); - } - - @Override - public VisitQueueEntry newDelegate() { - return new VisitQueueEntry(); - } - - @Override - public VisitQueueEntry save(VisitQueueEntry visitQueueEntry) { - return this.visitQueueEntryService.createVisitQueueEntry(visitQueueEntry); - } - - @Override - public void purge(VisitQueueEntry visitQueueEntry, RequestContext requestContext) throws ResponseException { - this.visitQueueEntryService.purgeQueueEntry(visitQueueEntry); - } - - @Override - protected PageableResult doGetAll(RequestContext requestContext) throws ResponseException { - return new NeedsPaging<>(new ArrayList<>(this.visitQueueEntryService.getActiveVisitQueueEntries()), requestContext); - } - - @Override - protected PageableResult doSearch(RequestContext requestContext) { - String status = requestContext.getParameter("status"); - String service = requestContext.getParameter("service"); - String location = requestContext.getParameter("location"); - String patient = requestContext.getParameter("patient"); - //Both status,location,patient & service are nullable - Collection visitQueueEntries = this.visitQueueEntryService.findVisitQueueEntries(status, service, - location, patient); - return new NeedsPaging<>(new ArrayList<>(visitQueueEntries), requestContext); - } - - @Override - public DelegatingResourceDescription getCreatableProperties() throws ResourceDoesNotSupportOperationException { - DelegatingResourceDescription resourceDescription = new DelegatingResourceDescription(); - resourceDescription.addProperty("visit"); - resourceDescription.addProperty("queueEntry"); - return resourceDescription; - } - - @Override - public DelegatingResourceDescription getRepresentationDescription(Representation representation) { - DelegatingResourceDescription description = new DelegatingResourceDescription(); - if (representation instanceof RefRepresentation) { - this.addSharedResourceDescriptionProperties(description); - description.addProperty("visit", Representation.REF); - description.addProperty("queueEntry", Representation.REF); - description.addLink("full", ".?v=" + RestConstants.REPRESENTATION_FULL); - } else if (representation instanceof DefaultRepresentation) { - this.addSharedResourceDescriptionProperties(description); - description.addProperty("visit", Representation.DEFAULT); - description.addProperty("queueEntry", Representation.DEFAULT); - description.addLink("full", ".?v=" + RestConstants.REPRESENTATION_FULL); - } else if (representation instanceof FullRepresentation) { - this.addSharedResourceDescriptionProperties(description); - description.addProperty("visit", Representation.FULL); - description.addProperty("queueEntry", Representation.FULL); - description.addProperty("auditInfo"); - } else if (representation instanceof CustomRepresentation) { - description = null; - } - return description; - } - - private void addSharedResourceDescriptionProperties(DelegatingResourceDescription resourceDescription) { - resourceDescription.addSelfLink(); - resourceDescription.addProperty("uuid"); - } - - @Override - public String getResourceVersion() { - return "2.3"; - } -} diff --git a/omod/src/main/java/org/openmrs/module/queue/web/resources/search/handlers/InActiveQueueEntrySearchHandler.java b/omod/src/main/java/org/openmrs/module/queue/web/resources/search/handlers/InActiveQueueEntrySearchHandler.java deleted file mode 100644 index e3c3d88..0000000 --- a/omod/src/main/java/org/openmrs/module/queue/web/resources/search/handlers/InActiveQueueEntrySearchHandler.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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.web.resources.search.handlers; - -import java.util.ArrayList; -import java.util.Collections; - -import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; -import org.openmrs.api.context.Context; -import org.openmrs.module.queue.api.VisitQueueEntryService; -import org.openmrs.module.webservices.rest.web.RequestContext; -import org.openmrs.module.webservices.rest.web.RestConstants; -import org.openmrs.module.webservices.rest.web.resource.api.PageableResult; -import org.openmrs.module.webservices.rest.web.resource.api.SearchConfig; -import org.openmrs.module.webservices.rest.web.resource.api.SearchHandler; -import org.openmrs.module.webservices.rest.web.resource.api.SearchQuery; -import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging; -import org.openmrs.module.webservices.rest.web.response.ResponseException; -import org.springframework.stereotype.Component; - -@Slf4j -@Component -public class InActiveQueueEntrySearchHandler implements SearchHandler { - - private final static SearchConfig SEARCH_CONFIG = new SearchConfig("default", - RestConstants.VERSION_1 + "/visit-queue-entry", Collections.singletonList("2.3 - 9.*"), - new SearchQuery.Builder("Allows you to include/exclude inactive queue entries") - .withOptionalParameters("includeInactive").build()); - - @Override - public SearchConfig getSearchConfig() { - return SEARCH_CONFIG; - } - - @Override - public PageableResult search(RequestContext requestContext) throws ResponseException { - VisitQueueEntryService visitQueueEntryService = Context.getService(VisitQueueEntryService.class); - String includeInactive = requestContext.getParameter("includeInactive"); - - if (!StringUtils.isBlank(includeInactive)) { - try { - boolean includeInactiveQueueEntries = Boolean.parseBoolean(includeInactive); - if (includeInactiveQueueEntries) { - return new NeedsPaging<>(new ArrayList<>(visitQueueEntryService.findAllVisitQueueEntries()), - requestContext); - } else { - return new NeedsPaging<>(new ArrayList<>(visitQueueEntryService.getActiveVisitQueueEntries()), - requestContext); - } - } - catch (Exception exception) { - log.error("Unable to parse string {} " + includeInactive, exception.getMessage(), exception); - } - } - return new NeedsPaging<>(new ArrayList<>(visitQueueEntryService.getActiveVisitQueueEntries()), requestContext); - } -} diff --git a/omod/src/main/java/org/openmrs/module/queue/web/resources/search/handlers/QueueCountSearchHandler.java b/omod/src/main/java/org/openmrs/module/queue/web/resources/search/handlers/QueueCountSearchHandler.java deleted file mode 100644 index 7b77d1c..0000000 --- a/omod/src/main/java/org/openmrs/module/queue/web/resources/search/handlers/QueueCountSearchHandler.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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.web.resources.search.handlers; - -import java.util.Arrays; -import java.util.Collections; - -import org.apache.commons.lang3.StringUtils; -import org.openmrs.api.context.Context; -import org.openmrs.module.queue.api.QueueEntryService; -import org.openmrs.module.queue.web.resources.custom.response.GenericSingleObjectResult; -import org.openmrs.module.queue.web.resources.custom.response.PropValue; -import org.openmrs.module.webservices.rest.web.RequestContext; -import org.openmrs.module.webservices.rest.web.RestConstants; -import org.openmrs.module.webservices.rest.web.resource.api.PageableResult; -import org.openmrs.module.webservices.rest.web.resource.api.SearchConfig; -import org.openmrs.module.webservices.rest.web.resource.api.SearchQuery; -import org.openmrs.module.webservices.rest.web.resource.api.SubResourceSearchHandler; -import org.openmrs.module.webservices.rest.web.resource.impl.EmptySearchResult; -import org.openmrs.module.webservices.rest.web.response.ResponseException; -import org.springframework.stereotype.Component; - -@Component -public class QueueCountSearchHandler implements SubResourceSearchHandler { - - private final static SearchConfig SEARCH_CONFIG = new SearchConfig("default", RestConstants.VERSION_1 + "/queue/count", - Collections.singletonList("2.3 - 9.*"), - new SearchQuery.Builder("Allows you to find queue entries by status").withOptionalParameters("status").build()); - - @Override - public PageableResult search(String parentUuid, RequestContext requestContext) throws ResponseException { - String queueEntryStatus = requestContext.getParameter("status"); - - if (StringUtils.isBlank(queueEntryStatus) || StringUtils.isBlank(parentUuid)) { - return new EmptySearchResult(); - } - Long queueCount = Context.getService(QueueEntryService.class).getQueueEntriesCountByStatus(queueEntryStatus); - - //Customize results response - return new GenericSingleObjectResult( - Arrays.asList(new PropValue("count", queueCount), new PropValue("queueEntryStatus", queueEntryStatus))); - } - - @Override - public SearchConfig getSearchConfig() { - return SEARCH_CONFIG; - } - - @Override - public PageableResult search(RequestContext requestContext) throws ResponseException { - throw new UnsupportedOperationException("Cannot search for queue entries without parent queue"); - } -} diff --git a/omod/src/main/java/org/openmrs/module/queue/web/resources/search/handlers/QueueEntrySearchHandler.java b/omod/src/main/java/org/openmrs/module/queue/web/resources/search/handlers/QueueEntrySearchHandler.java deleted file mode 100644 index bb9cbb3..0000000 --- a/omod/src/main/java/org/openmrs/module/queue/web/resources/search/handlers/QueueEntrySearchHandler.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * 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.web.resources.search.handlers; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; - -import org.apache.commons.lang3.StringUtils; -import org.openmrs.api.context.Context; -import org.openmrs.module.queue.api.QueueEntryService; -import org.openmrs.module.queue.model.QueueEntry; -import org.openmrs.module.webservices.rest.web.RequestContext; -import org.openmrs.module.webservices.rest.web.RestConstants; -import org.openmrs.module.webservices.rest.web.resource.api.PageableResult; -import org.openmrs.module.webservices.rest.web.resource.api.SearchConfig; -import org.openmrs.module.webservices.rest.web.resource.api.SearchQuery; -import org.openmrs.module.webservices.rest.web.resource.api.SubResourceSearchHandler; -import org.openmrs.module.webservices.rest.web.resource.impl.EmptySearchResult; -import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging; -import org.openmrs.module.webservices.rest.web.response.ResponseException; -import org.springframework.stereotype.Component; - -@Component -public class QueueEntrySearchHandler implements SubResourceSearchHandler { - - private final static SearchConfig SEARCH_CONFIG = new SearchConfig("default", RestConstants.VERSION_1 + "/queue/entry", - Collections.singletonList("2.3 - 9.*"), - new SearchQuery.Builder("Allows you to find queue entries by status").withOptionalParameters("status").build()); - - @Override - public PageableResult search(String parentUuid, RequestContext requestContext) throws ResponseException { - String status = requestContext.getParameter("status"); - - if (StringUtils.isBlank(status) || StringUtils.isBlank(parentUuid)) { - return new EmptySearchResult(); - } - Collection queueEntries = Context.getService(QueueEntryService.class) - .searchQueueEntriesByConceptStatus(status, false); - return new NeedsPaging<>(new ArrayList<>(queueEntries), requestContext); - } - - @Override - public SearchConfig getSearchConfig() { - return SEARCH_CONFIG; - } - - @Override - public PageableResult search(RequestContext requestContext) throws ResponseException { - throw new UnsupportedOperationException("Cannot search for queue entries without parent queue"); - } -} diff --git a/omod/src/test/java/org/openmrs/module/queue/web/resources/QueueEntryMetricsResourceTest.java b/omod/src/test/java/org/openmrs/module/queue/web/resources/QueueEntryMetricsResourceTest.java index 4bd7e11..93952aa 100644 --- a/omod/src/test/java/org/openmrs/module/queue/web/resources/QueueEntryMetricsResourceTest.java +++ b/omod/src/test/java/org/openmrs/module/queue/web/resources/QueueEntryMetricsResourceTest.java @@ -10,22 +10,37 @@ package org.openmrs.module.queue.web.resources; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.notNullValue; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.when; +import static org.mockito.ArgumentMatchers.any; import static org.powermock.api.mockito.PowerMockito.mock; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.when; + +import javax.servlet.http.HttpServletRequest; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.openmrs.Concept; +import org.openmrs.api.ConceptService; +import org.openmrs.api.LocationService; +import org.openmrs.api.PatientService; import org.openmrs.api.context.Context; -import org.openmrs.module.queue.api.VisitQueueEntryService; +import org.openmrs.module.queue.api.QueueEntryService; +import org.openmrs.module.queue.api.QueueRoomService; +import org.openmrs.module.queue.api.QueueService; +import org.openmrs.module.queue.api.QueueServicesWrapper; +import org.openmrs.module.queue.api.RoomProviderMapService; import org.openmrs.module.queue.web.resources.custom.response.GenericSingleObjectResult; import org.openmrs.module.webservices.rest.web.RequestContext; import org.openmrs.module.webservices.rest.web.RestUtil; -import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @@ -35,50 +50,100 @@ public class QueueEntryMetricsResourceTest { private static final String STATUS = "Waiting"; - private static final String SERVICE = "triage"; + private static final String SERVICE = "Triage"; private QueueEntryMetricsResource resource; @Mock - private VisitQueueEntryService visitQueueEntryService; + private QueueService queueService; + + @Mock + private QueueEntryService queueEntryService; + + @Mock + private QueueRoomService queueRoomService; + + @Mock + private RoomProviderMapService roomProviderMapService; + + @Mock + private ConceptService conceptService; + + @Mock + private LocationService locationService; + + @Mock + private PatientService patientService; + + @Mock + private QueueServicesWrapper queueServicesWrapper; @Before public void prepareMocks() { - PowerMockito.mockStatic(RestUtil.class); - PowerMockito.mockStatic(Context.class); + mockStatic(RestUtil.class); + mockStatic(Context.class); + when(queueServicesWrapper.getQueueService()).thenReturn(queueService); + when(queueServicesWrapper.getQueueEntryService()).thenReturn(queueEntryService); + when(queueServicesWrapper.getQueueRoomService()).thenReturn(queueRoomService); + when(queueServicesWrapper.getRoomProviderMapService()).thenReturn(roomProviderMapService); + when(queueServicesWrapper.getConceptService()).thenReturn(conceptService); + when(queueServicesWrapper.getLocationService()).thenReturn(locationService); + when(queueServicesWrapper.getPatientService()).thenReturn(patientService); - resource = new QueueEntryMetricsResource(); //By pass authentication when(Context.isAuthenticated()).thenReturn(true); - when(Context.getService(VisitQueueEntryService.class)).thenReturn(visitQueueEntryService); + when(Context.getRegisteredComponents(QueueServicesWrapper.class)) + .thenReturn(Collections.singletonList(queueServicesWrapper)); + + resource = new QueueEntryMetricsResource(); } @Test public void shouldReturnQueueEntryMetricsByStatus() { + Map parameterMap = new HashMap<>(); + parameterMap.put("status", new String[] { STATUS }); + RequestContext requestContext = mock(RequestContext.class); + HttpServletRequest request = mock(HttpServletRequest.class); + when(requestContext.getRequest()).thenReturn(request); + when(request.getParameterMap()).thenReturn(parameterMap); - when(visitQueueEntryService.getVisitQueueEntriesCountByStatus(STATUS)).thenReturn(0L); - when(requestContext.getParameter("status")).thenReturn(STATUS); + Concept statusConcept = new Concept(); + when(queueServicesWrapper.getConcept(STATUS)).thenReturn(statusConcept); + when(queueEntryService.getCountOfQueueEntries(any())).thenReturn(50L); GenericSingleObjectResult result = (GenericSingleObjectResult) resource.doSearch(requestContext); assertThat(result, notNullValue()); assertThat(result.getPropValues(), hasSize(2)); - assertTrue(result.getPropValues().stream().anyMatch((propValue -> propValue.getValue().equals(0L)))); + assertThat(result.getPropValues().get(0).getProperty(), equalTo("metric")); + assertThat(result.getPropValues().get(0).getValue(), equalTo(STATUS)); + assertThat(result.getPropValues().get(1).getProperty(), equalTo("count")); + assertThat(result.getPropValues().get(1).getValue(), equalTo(50L)); } @Test public void shouldReturnQueueEntryMetricsByService() { + Map parameterMap = new HashMap<>(); + parameterMap.put("service", new String[] { SERVICE }); + RequestContext requestContext = mock(RequestContext.class); + HttpServletRequest request = mock(HttpServletRequest.class); + when(requestContext.getRequest()).thenReturn(request); + when(request.getParameterMap()).thenReturn(parameterMap); - when(visitQueueEntryService.getVisitQueueEntriesCountByStatus(SERVICE)).thenReturn(0L); - when(requestContext.getParameter("service")).thenReturn(SERVICE); + Concept serviceConcept = new Concept(); + when(queueServicesWrapper.getConcept(SERVICE)).thenReturn(serviceConcept); + when(queueEntryService.getCountOfQueueEntries(any())).thenReturn(50L); GenericSingleObjectResult result = (GenericSingleObjectResult) resource.doSearch(requestContext); assertThat(result, notNullValue()); assertThat(result.getPropValues(), hasSize(2)); - assertTrue(result.getPropValues().stream().anyMatch((propValue -> propValue.getValue().equals(0L)))); + assertThat(result.getPropValues().get(0).getProperty(), equalTo("metric")); + assertThat(result.getPropValues().get(0).getValue(), equalTo(SERVICE)); + assertThat(result.getPropValues().get(1).getProperty(), equalTo("count")); + assertThat(result.getPropValues().get(1).getValue(), equalTo(50L)); } } diff --git a/omod/src/test/java/org/openmrs/module/queue/web/resources/QueueEntryResourceTest.java b/omod/src/test/java/org/openmrs/module/queue/web/resources/QueueEntryResourceTest.java index 61697b5..e4887a2 100644 --- a/omod/src/test/java/org/openmrs/module/queue/web/resources/QueueEntryResourceTest.java +++ b/omod/src/test/java/org/openmrs/module/queue/web/resources/QueueEntryResourceTest.java @@ -13,55 +13,96 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; -import static org.powermock.api.mockito.PowerMockito.mock; +import static org.powermock.api.mockito.PowerMockito.mockStatic; import static org.powermock.api.mockito.PowerMockito.when; +import java.util.Collections; import java.util.Optional; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.openmrs.api.ConceptService; +import org.openmrs.api.LocationService; +import org.openmrs.api.PatientService; import org.openmrs.api.context.Context; import org.openmrs.module.queue.api.QueueEntryService; +import org.openmrs.module.queue.api.QueueRoomService; +import org.openmrs.module.queue.api.QueueService; +import org.openmrs.module.queue.api.QueueServicesWrapper; +import org.openmrs.module.queue.api.RoomProviderMapService; import org.openmrs.module.queue.model.QueueEntry; +import org.openmrs.module.webservices.rest.web.RestUtil; import org.openmrs.module.webservices.rest.web.representation.CustomRepresentation; import org.openmrs.module.webservices.rest.web.representation.DefaultRepresentation; import org.openmrs.module.webservices.rest.web.representation.FullRepresentation; import org.openmrs.module.webservices.rest.web.representation.RefRepresentation; +import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) -public class QueueEntryResourceTest extends BaseQueueResourceTest { +@PrepareForTest({ Context.class, RestUtil.class }) +public class QueueEntryResourceTest extends BaseQueueResourceTest { private static final String QUEUE_ENTRY_UUID = "6hje567a-fca0-11e5-9e59-08002719a7"; + private QueueEntryResource resource; + + private QueueEntry queueEntry; + + @Mock + private QueueService queueService; + @Mock private QueueEntryService queueEntryService; - private QueueEntry queueEntry; + @Mock + private QueueRoomService queueRoomService; + + @Mock + private RoomProviderMapService roomProviderMapService; + + @Mock + private ConceptService conceptService; + + @Mock + private LocationService locationService; + + @Mock + private PatientService patientService; + + @Mock + private QueueServicesWrapper queueServicesWrapper; @Before - public void setup() { - this.prepareMocks(); - queueEntry = mock(QueueEntry.class); + public void prepareMocks() { + mockStatic(RestUtil.class); + mockStatic(Context.class); + when(queueServicesWrapper.getQueueService()).thenReturn(queueService); + when(queueServicesWrapper.getQueueEntryService()).thenReturn(queueEntryService); + when(queueServicesWrapper.getQueueRoomService()).thenReturn(queueRoomService); + when(queueServicesWrapper.getRoomProviderMapService()).thenReturn(roomProviderMapService); + when(queueServicesWrapper.getConceptService()).thenReturn(conceptService); + when(queueServicesWrapper.getLocationService()).thenReturn(locationService); + when(queueServicesWrapper.getPatientService()).thenReturn(patientService); - when(queueEntry.getUuid()).thenReturn(QUEUE_ENTRY_UUID); - when(Context.getService(QueueEntryService.class)).thenReturn(queueEntryService); + //By pass authentication + when(Context.isAuthenticated()).thenReturn(true); + when(Context.getRegisteredComponents(QueueServicesWrapper.class)) + .thenReturn(Collections.singletonList(queueServicesWrapper)); - this.setResource(new QueueEntrySubResource()); - this.setObject(queueEntry); - } - - @Test - public void shouldGetQueueEntryService() { - assertThat(queueEntryService, notNullValue()); + resource = new QueueEntryResource(); + setResource(resource); + queueEntry = new QueueEntry(); + queueEntry.setUuid(QUEUE_ENTRY_UUID); + setObject(queueEntry); } @Test public void shouldReturnDefaultRepresentation() { - verifyDefaultRepresentation("uuid", "priority", "priorityComment", "sortWeight", "patient", "locationWaitingFor", - "providerWaitingFor", "startedAt", "endedAt", "display"); + verifyDefaultRepresentation("uuid", "queue", "status", "visit", "priority", "priorityComment", "sortWeight", + "patient", "locationWaitingFor", "providerWaitingFor", "startedAt", "endedAt", "display"); } @Test @@ -94,7 +135,6 @@ public void shouldNOTReturnNullForRefRepresentation() { @Test public void shouldGetResourceByUniqueUuid() { when(queueEntryService.getQueueEntryByUuid(QUEUE_ENTRY_UUID)).thenReturn(Optional.of(queueEntry)); - QueueEntry result = getResource().getByUniqueId(QUEUE_ENTRY_UUID); assertThat(result, notNullValue()); assertThat(result.getUuid(), is(QUEUE_ENTRY_UUID)); diff --git a/omod/src/test/java/org/openmrs/module/queue/web/resources/QueueEntrySubResourceTest.java b/omod/src/test/java/org/openmrs/module/queue/web/resources/QueueEntrySubResourceTest.java new file mode 100644 index 0000000..77f6d6d --- /dev/null +++ b/omod/src/test/java/org/openmrs/module/queue/web/resources/QueueEntrySubResourceTest.java @@ -0,0 +1,151 @@ +/* + * 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.web.resources; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; +import static org.powermock.api.mockito.PowerMockito.mock; +import static org.powermock.api.mockito.PowerMockito.when; + +import java.util.Optional; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.openmrs.api.ConceptService; +import org.openmrs.api.LocationService; +import org.openmrs.api.PatientService; +import org.openmrs.api.context.Context; +import org.openmrs.module.queue.api.QueueEntryService; +import org.openmrs.module.queue.api.QueueRoomService; +import org.openmrs.module.queue.api.QueueService; +import org.openmrs.module.queue.api.QueueServicesWrapper; +import org.openmrs.module.queue.api.RoomProviderMapService; +import org.openmrs.module.queue.model.QueueEntry; +import org.openmrs.module.webservices.rest.web.representation.CustomRepresentation; +import org.openmrs.module.webservices.rest.web.representation.DefaultRepresentation; +import org.openmrs.module.webservices.rest.web.representation.FullRepresentation; +import org.openmrs.module.webservices.rest.web.representation.RefRepresentation; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +public class QueueEntrySubResourceTest extends BaseQueueResourceTest { + + private static final String QUEUE_ENTRY_UUID = "6hje567a-fca0-11e5-9e59-08002719a7"; + + @Mock + private QueueService queueService; + + @Mock + private QueueEntryService queueEntryService; + + @Mock + private QueueRoomService queueRoomService; + + @Mock + private RoomProviderMapService roomProviderMapService; + + @Mock + private ConceptService conceptService; + + @Mock + private LocationService locationService; + + @Mock + private PatientService patientService; + + @Mock + private QueueServicesWrapper queueServicesWrapper; + + private QueueEntry queueEntry; + + @Before + public void setup() { + this.prepareMocks(); + queueEntry = mock(QueueEntry.class); + when(queueServicesWrapper.getQueueService()).thenReturn(queueService); + when(queueServicesWrapper.getQueueEntryService()).thenReturn(queueEntryService); + when(queueServicesWrapper.getQueueRoomService()).thenReturn(queueRoomService); + when(queueServicesWrapper.getRoomProviderMapService()).thenReturn(roomProviderMapService); + when(queueServicesWrapper.getConceptService()).thenReturn(conceptService); + when(queueServicesWrapper.getLocationService()).thenReturn(locationService); + when(queueServicesWrapper.getPatientService()).thenReturn(patientService); + + when(queueEntry.getUuid()).thenReturn(QUEUE_ENTRY_UUID); + when(Context.getService(QueueEntryService.class)).thenReturn(queueEntryService); + + this.setResource(new QueueEntrySubResource()); + this.setObject(queueEntry); + } + + @Test + public void shouldReturnDefaultRepresentation() { + verifyDefaultRepresentation("uuid", "status", "visit", "priority", "priorityComment", "sortWeight", "patient", + "locationWaitingFor", "providerWaitingFor", "startedAt", "endedAt", "display"); + } + + @Test + public void shouldReturnFullRepresentation() { + verifyFullRepresentation("status", "priority", "priorityComment", "sortWeight", "patient", "locationWaitingFor", + "providerWaitingFor", "startedAt", "endedAt", "display", "uuid", "display", "auditInfo"); + } + + @Test + public void shouldReturnNullForCustomRepresentation() { + CustomRepresentation customRepresentation = new CustomRepresentation("custom-representation"); + assertThat(getResource().getRepresentationDescription(customRepresentation), is(nullValue())); + } + + @Test + public void shouldNOTReturnNullForDefaultRepresentation() { + assertThat(getResource().getRepresentationDescription(new DefaultRepresentation()), is(notNullValue())); + } + + @Test + public void shouldNOTReturnNullForFullRepresentation() { + assertThat(getResource().getRepresentationDescription(new FullRepresentation()), is(notNullValue())); + } + + @Test + public void shouldNOTReturnNullForRefRepresentation() { + assertThat(getResource().getRepresentationDescription(new RefRepresentation()), is(notNullValue())); + } + + @Test + public void shouldGetResourceByUniqueUuid() { + when(queueEntryService.getQueueEntryByUuid(QUEUE_ENTRY_UUID)).thenReturn(Optional.of(queueEntry)); + + QueueEntry result = getResource().getByUniqueId(QUEUE_ENTRY_UUID); + assertThat(result, notNullValue()); + assertThat(result.getUuid(), is(QUEUE_ENTRY_UUID)); + } + + @Test + public void shouldCreateNewResource() { + when(queueEntryService.createQueueEntry(getObject())).thenReturn(getObject()); + + QueueEntry newlyCreatedObject = getResource().save(getObject()); + assertThat(newlyCreatedObject, notNullValue()); + assertThat(newlyCreatedObject.getUuid(), is(QUEUE_ENTRY_UUID)); + } + + @Test + public void shouldInstantiateNewDelegate() { + assertThat(getResource().newDelegate(), notNullValue()); + } + + @Test + public void verifyResourceVersion() { + assertThat(getResource().getResourceVersion(), is("2.3")); + } +} diff --git a/omod/src/test/java/org/openmrs/module/queue/web/resources/VisitQueueEntryResourceTest.java b/omod/src/test/java/org/openmrs/module/queue/web/resources/VisitQueueEntryResourceTest.java deleted file mode 100644 index f010778..0000000 --- a/omod/src/test/java/org/openmrs/module/queue/web/resources/VisitQueueEntryResourceTest.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * 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.web.resources; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.*; -import static org.powermock.api.mockito.PowerMockito.mock; -import static org.powermock.api.mockito.PowerMockito.when; - -import java.util.Collections; -import java.util.Optional; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.openmrs.api.context.Context; -import org.openmrs.module.queue.api.VisitQueueEntryService; -import org.openmrs.module.queue.model.VisitQueueEntry; -import org.openmrs.module.webservices.rest.web.RequestContext; -import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging; -import org.powermock.modules.junit4.PowerMockRunner; - -@SuppressWarnings("unchecked") -@RunWith(PowerMockRunner.class) -public class VisitQueueEntryResourceTest extends BaseQueueResourceTest { - - private static final String VISIT_QUEUE_ENTRY_UUID = "6hje567a-fca0-11e5-9e59-08002719a7"; - - @Mock - private VisitQueueEntryService visitQueueEntryService; - - private VisitQueueEntry visitQueueEntry; - - @Before - public void setup() { - this.prepareMocks(); - visitQueueEntry = mock(VisitQueueEntry.class); - - when(visitQueueEntry.getUuid()).thenReturn(VISIT_QUEUE_ENTRY_UUID); - when(Context.getService(VisitQueueEntryService.class)).thenReturn(visitQueueEntryService); - - this.setResource(new VisitQueueEntryResource()); - this.setObject(visitQueueEntry); - } - - @Test - public void shouldGetQueueEntryService() { - assertThat(visitQueueEntryService, notNullValue()); - } - - @Test - public void shouldGetResourceByUniqueUuid() { - when(visitQueueEntryService.getVisitQueueEntryByUuid(VISIT_QUEUE_ENTRY_UUID)) - .thenReturn(Optional.of(visitQueueEntry)); - - VisitQueueEntry result = getResource().getByUniqueId(VISIT_QUEUE_ENTRY_UUID); - assertThat(result, notNullValue()); - assertThat(result.getUuid(), is(VISIT_QUEUE_ENTRY_UUID)); - } - - @Test - public void shouldCreateNewResource() { - when(visitQueueEntryService.createVisitQueueEntry(getObject())).thenReturn(getObject()); - - VisitQueueEntry newlyCreatedObject = getResource().save(getObject()); - assertThat(newlyCreatedObject, notNullValue()); - assertThat(newlyCreatedObject.getUuid(), is(VISIT_QUEUE_ENTRY_UUID)); - } - - @Test - public void shouldInstantiateNewDelegate() { - assertThat(getResource().newDelegate(), notNullValue()); - } - - @Test - public void shouldReturnAllVisitQueueEntriesFromDb() { - RequestContext requestContext = mock(RequestContext.class); - when(visitQueueEntryService.getActiveVisitQueueEntries()).thenReturn(Collections.singletonList(getObject())); - - NeedsPaging result = (NeedsPaging) getResource().doGetAll(requestContext); - - assertThat(result, notNullValue()); - assertThat(result.getTotalCount(), is(1L)); - } -}