From aca2b85e99f05a45172d70e9c20feee95fc03efb Mon Sep 17 00:00:00 2001 From: druchniewicz Date: Thu, 21 Dec 2023 15:10:58 +0100 Subject: [PATCH 1/5] O3-1494: Added method to get report requests with pagination --- .../reporting/report/ReportRequestDTO.java | 31 ++++++++++++++ .../report/service/ReportService.java | 7 ++++ .../report/service/ReportServiceImpl.java | 10 +++++ .../report/service/db/HibernateReportDAO.java | 41 ++++++++++++++++++- .../report/service/db/ReportDAO.java | 6 +++ 5 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 api/src/main/java/org/openmrs/module/reporting/report/ReportRequestDTO.java diff --git a/api/src/main/java/org/openmrs/module/reporting/report/ReportRequestDTO.java b/api/src/main/java/org/openmrs/module/reporting/report/ReportRequestDTO.java new file mode 100644 index 0000000000..f7a3cebb3d --- /dev/null +++ b/api/src/main/java/org/openmrs/module/reporting/report/ReportRequestDTO.java @@ -0,0 +1,31 @@ +package org.openmrs.module.reporting.report; + +import java.util.List; + +public class ReportRequestDTO { + + private List reportRequests; + + private Long reportRequestCount; + + public ReportRequestDTO(List reportRequests, Long reportRequestCount) { + this.reportRequests = reportRequests; + this.reportRequestCount = reportRequestCount; + } + + public List getReportRequests() { + return reportRequests; + } + + public void setReportRequests(List reportRequests) { + this.reportRequests = reportRequests; + } + + public Long getReportRequestCount() { + return reportRequestCount; + } + + public void setReportRequestCount(Long reportRequestCount) { + this.reportRequestCount = reportRequestCount; + } +} diff --git a/api/src/main/java/org/openmrs/module/reporting/report/service/ReportService.java b/api/src/main/java/org/openmrs/module/reporting/report/service/ReportService.java index fac343a05a..d50b16ffcc 100644 --- a/api/src/main/java/org/openmrs/module/reporting/report/service/ReportService.java +++ b/api/src/main/java/org/openmrs/module/reporting/report/service/ReportService.java @@ -17,6 +17,7 @@ import org.openmrs.module.reporting.report.ReportProcessorConfiguration; import org.openmrs.module.reporting.report.ReportRequest; import org.openmrs.module.reporting.report.ReportRequest.Status; +import org.openmrs.module.reporting.report.ReportRequestDTO; import org.openmrs.module.reporting.report.definition.ReportDefinition; import org.openmrs.module.reporting.report.processor.ReportProcessor; import org.openmrs.module.reporting.report.renderer.RenderingMode; @@ -140,6 +141,12 @@ public interface ReportService extends OpenmrsService { @Transactional(readOnly = true) public List getReportRequests(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer mostRecentNum, Status...statuses); + /** + * @return {@link ReportRequestDTO} object which contains report requests and total count data + */ + @Transactional(readOnly = true) + ReportRequestDTO getReportsWithPagination(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer pageNumber, Integer pageSize, Status...statuses); + /** * Deletes the passed {@link ReportRequest} */ diff --git a/api/src/main/java/org/openmrs/module/reporting/report/service/ReportServiceImpl.java b/api/src/main/java/org/openmrs/module/reporting/report/service/ReportServiceImpl.java index 41d9602c06..ba2d10bac9 100644 --- a/api/src/main/java/org/openmrs/module/reporting/report/service/ReportServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/reporting/report/service/ReportServiceImpl.java @@ -33,6 +33,7 @@ import org.openmrs.module.reporting.report.ReportRequest.Priority; import org.openmrs.module.reporting.report.ReportRequest.PriorityComparator; import org.openmrs.module.reporting.report.ReportRequest.Status; +import org.openmrs.module.reporting.report.ReportRequestDTO; import org.openmrs.module.reporting.report.definition.ReportDefinition; import org.openmrs.module.reporting.report.definition.service.ReportDefinitionService; import org.openmrs.module.reporting.report.processor.ReportProcessor; @@ -223,6 +224,15 @@ public List getReportRequests(ReportDefinition reportDefinition, return reportDAO.getReportRequests(reportDefinition, requestOnOrAfter, requestOnOrBefore, mostRecentNum, statuses); } + /** + * @see ReportService#getReportsWithPagination(ReportDefinition, Date, Date, Integer, Integer, Status...) + */ + @Override + @Transactional(readOnly = true) + public ReportRequestDTO getReportsWithPagination(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer pageNumber, Integer pageSize, Status... statuses) { + return reportDAO.getReportsWithPagination(reportDefinition, requestOnOrAfter, requestOnOrBefore, pageNumber, pageSize, statuses); + } + /** * @see ReportService#purgeReportRequest(ReportRequest) */ diff --git a/api/src/main/java/org/openmrs/module/reporting/report/service/db/HibernateReportDAO.java b/api/src/main/java/org/openmrs/module/reporting/report/service/db/HibernateReportDAO.java index aabed07c71..e9d9d6b1e0 100644 --- a/api/src/main/java/org/openmrs/module/reporting/report/service/db/HibernateReportDAO.java +++ b/api/src/main/java/org/openmrs/module/reporting/report/service/db/HibernateReportDAO.java @@ -15,6 +15,7 @@ import org.hibernate.Query; import org.hibernate.criterion.Expression; import org.hibernate.criterion.Order; +import org.hibernate.criterion.Projections; import org.hibernate.criterion.Restrictions; import org.openmrs.api.db.DAOException; import org.openmrs.api.db.hibernate.DbSessionFactory; @@ -22,6 +23,7 @@ import org.openmrs.module.reporting.report.ReportProcessorConfiguration; import org.openmrs.module.reporting.report.ReportRequest; import org.openmrs.module.reporting.report.ReportRequest.Status; +import org.openmrs.module.reporting.report.ReportRequestDTO; import org.openmrs.module.reporting.report.definition.ReportDefinition; import org.openmrs.module.reporting.report.renderer.ReportRenderer; @@ -214,7 +216,44 @@ public List getReportRequests(ReportDefinition reportDefinition, } return c.list(); } - + + /** + * @see ReportDAO#getReportsWithPagination(ReportDefinition, Date, Date, Integer, Integer, Status...) + */ + public ReportRequestDTO getReportsWithPagination(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer pageNumber, Integer pageSize, Status...statuses) { + Criteria c = sessionFactory.getCurrentSession().createCriteria(ReportRequest.class); + + if (reportDefinition != null) { + c.add(Restrictions.eq("reportDefinition.definition", reportDefinition.getUuid())); + } + if (requestOnOrAfter != null) { + c.add(Restrictions.ge("requestDate", requestOnOrAfter)); + } + if (requestOnOrBefore != null) { + c.add(Restrictions.le("requestDate", requestOnOrBefore)); + } + if (statuses != null && statuses.length > 0) { + c.add(Restrictions.in("status", statuses)); + } + c.addOrder(Order.desc("evaluateCompleteDatetime")); + c.addOrder(Order.desc("evaluateStartDatetime")); + c.addOrder(Order.desc("priority")); + c.addOrder(Order.desc("requestDate")); + + c.setProjection(Projections.rowCount()); + Long count = (Long) c.uniqueResult(); + c.setProjection(null); + + if (pageNumber != null && pageSize != null) { + c.setFirstResult((pageNumber - 1) * pageSize); + c.setMaxResults(pageSize); + } + + List reportRequests = c.list(); + + return new ReportRequestDTO(reportRequests, count); + } + /** * @see ReportDAO#purgeReportRequest(ReportRequest) */ diff --git a/api/src/main/java/org/openmrs/module/reporting/report/service/db/ReportDAO.java b/api/src/main/java/org/openmrs/module/reporting/report/service/db/ReportDAO.java index c36e465c07..19138b389f 100644 --- a/api/src/main/java/org/openmrs/module/reporting/report/service/db/ReportDAO.java +++ b/api/src/main/java/org/openmrs/module/reporting/report/service/db/ReportDAO.java @@ -14,6 +14,7 @@ import org.openmrs.module.reporting.report.ReportProcessorConfiguration; import org.openmrs.module.reporting.report.ReportRequest; import org.openmrs.module.reporting.report.ReportRequest.Status; +import org.openmrs.module.reporting.report.ReportRequestDTO; import org.openmrs.module.reporting.report.definition.ReportDefinition; import org.openmrs.module.reporting.report.renderer.ReportRenderer; @@ -123,6 +124,11 @@ public List getReportDesigns(ReportDefinition reportDefinition, Cl */ public List getReportRequests(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer mostRecentNum, Status...statuses); + /** + * @return {@link ReportRequestDTO} object which contains report requests and total count data + */ + ReportRequestDTO getReportsWithPagination(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer pageNumber, Integer pageSize, Status...statuses); + /** * Deletes the passed {@link ReportRequest} */ From 533d2b525edcc1af93e6e3b3b70f16deaac20d60 Mon Sep 17 00:00:00 2001 From: Piotr Wargulak Date: Wed, 21 Feb 2024 17:26:55 +0100 Subject: [PATCH 2/5] O3-1494: Changes after 1st code review --- .../report/service/ReportServiceTest.java | 78 ++++++++++++++++++- .../ReportTestDataset-openmrs-1.10.xml | 26 +++++-- .../ReportTestDataset-openmrs-1.11.xml | 24 ++++-- .../ReportTestDataset-openmrs-1.12.xml | 24 ++++-- .../include/ReportTestDataset-openmrs-1.9.xml | 22 ++++-- .../include/ReportTestDataset-openmrs-2.0.xml | 26 +++++-- .../include/ReportTestDataset-openmrs-2.1.xml | 28 ++++--- .../include/ReportTestDataset-openmrs-2.2.xml | 22 ++++-- .../include/ReportTestDataset-openmrs-2.3.xml | 22 ++++-- .../include/ReportTestDataset-openmrs-2.4.xml | 22 ++++-- .../reporting/report/ReportRequestDTO.java | 17 +++- .../report/service/ReportService.java | 40 +++++++++- .../report/service/ReportServiceImpl.java | 8 +- .../report/service/db/HibernateReportDAO.java | 13 ++-- .../report/service/db/ReportDAO.java | 2 +- 15 files changed, 293 insertions(+), 81 deletions(-) diff --git a/api-tests/src/test/java/org/openmrs/module/reporting/report/service/ReportServiceTest.java b/api-tests/src/test/java/org/openmrs/module/reporting/report/service/ReportServiceTest.java index fcd90d4f4b..37724baa22 100644 --- a/api-tests/src/test/java/org/openmrs/module/reporting/report/service/ReportServiceTest.java +++ b/api-tests/src/test/java/org/openmrs/module/reporting/report/service/ReportServiceTest.java @@ -27,6 +27,7 @@ import org.openmrs.module.reporting.report.ReportProcessorConfiguration; import org.openmrs.module.reporting.report.ReportRequest; import org.openmrs.module.reporting.report.ReportRequest.Priority; +import org.openmrs.module.reporting.report.ReportRequestDTO; import org.openmrs.module.reporting.report.definition.ReportDefinition; import org.openmrs.module.reporting.report.definition.service.ReportDefinitionService; import org.openmrs.module.reporting.report.processor.LoggingReportProcessor; @@ -42,14 +43,17 @@ import java.io.File; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Properties; import java.util.UUID; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; public class ReportServiceTest extends BaseModuleContextSensitiveTest { @@ -521,4 +525,76 @@ public void purgeReportDesignsForReportDefinition_shouldDeleteAllAssociatedRepor assertNull(rs.getReportDesignByUuid("d7a82b63-1066-4c1d-9b43-b405851fc467")); assertNull(rs.getReportDesignByUuid("e7a82b63-1066-4c1d-9b43-b405851fc467")); } -} \ No newline at end of file + + @Test + public void getReportRequestsWithPagination_shouldReturnAllReportRequestsPagedWithCorrectTotalCount() { + final ReportService rs = Context.getService(ReportService.class); + final ReportRequestDTO result = rs.getReportRequestsWithPagination(null, null, null, 1,2); + + assertEquals(4, result.getReportRequestCount()); + assertEquals(2, result.getReportRequests().size()); + + final List resultUuids = mapToReportRequestUuids(result.getReportRequests()); + assertTrue(resultUuids.contains("fce15a1b-4618-4f65-bfe9-8bb60a85c110")); + assertTrue(resultUuids.contains("b0a82b63-1066-4c1d-9b43-b405851fc467")); + } + + @Test + public void getReportRequestsWithPagination_shouldReturnReportRequestsForGivenReportDefinitionPaged() { + final ReportService rs = Context.getService(ReportService.class); + final ReportDefinition testReportDefinition = + rs.getReportDesignByUuid("d7a82b63-1066-4c1d-9b43-b405851fc467").getReportDefinition(); + final ReportRequestDTO result = rs.getReportRequestsWithPagination(testReportDefinition, null, null, 1,2); + + assertEquals(2, result.getReportRequestCount()); + assertEquals(2, result.getReportRequests().size()); + + final List resultUuids = mapToReportRequestUuids(result.getReportRequests()); + assertTrue(resultUuids.contains("h8a82b63-1066-4c1d-9b43-b405851fc467")); + assertTrue(resultUuids.contains("b0a82b63-1066-4c1d-9b43-b405851fc467")); + } + + @Test + public void getReportRequestsWithPagination_shouldReturnReportRequestsForRequestedWithinDatesPaged() { + final ReportService rs = Context.getService(ReportService.class); + final Date from = newDate(2013, Calendar.JANUARY, 21, 14, 8, 48); + final Date to = newDate(2013, Calendar.JANUARY, 21, 14, 8, 49); + final ReportRequestDTO result = rs.getReportRequestsWithPagination(null, from, to, 1,2); + + assertEquals(2, result.getReportRequestCount()); + assertEquals(2, result.getReportRequests().size()); + + final List resultUuids = mapToReportRequestUuids(result.getReportRequests()); + assertTrue(resultUuids.contains("b0a82b63-1066-4c1d-9b43-b405851fc467")); + assertTrue(resultUuids.contains("d9a82b63-1066-4c1d-9b43-b405851fc467")); + } + + @Test + public void getReportRequestsWithPagination_shouldReturnAPartialPageOfReportRequests() { + final ReportService rs = Context.getService(ReportService.class); + final ReportRequestDTO result = rs.getReportRequestsWithPagination(null, null, null, 1, 2, ReportRequest.Status.FAILED); + + assertEquals(1, result.getReportRequestCount()); + assertEquals(1, result.getReportRequests().size()); + + final List resultUuids = mapToReportRequestUuids(result.getReportRequests()); + assertTrue(resultUuids.contains("fce15a1b-4618-4f65-bfe9-8bb60a85c110")); + } + + private List mapToReportRequestUuids(List reportRequests) { + List reportRequestUuids = new ArrayList(); + + for (ReportRequest reportRequest : reportRequests) { + reportRequestUuids.add(reportRequest.getUuid()); + } + + return reportRequestUuids; + } + + private Date newDate(int year, int month, int day, int hour, int minute, int second) { + final Calendar cal = Calendar.getInstance(); + cal.clear(); + cal.set(year, month, day, hour, minute, second); + return cal.getTime(); + } +} diff --git a/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-1.10.xml b/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-1.10.xml index 9563778ab6..0504594c94 100644 --- a/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-1.10.xml +++ b/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-1.10.xml @@ -523,11 +523,21 @@ properties="#Mon Jan 21 14:08:47 CET 2013" creator="1" date_created="2013-01-21 14:08:47" retired="0" /> - - - - - \ No newline at end of file + + + + + + diff --git a/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-1.11.xml b/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-1.11.xml index e66c1fd330..ebe5f05ab8 100644 --- a/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-1.11.xml +++ b/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-1.11.xml @@ -545,11 +545,21 @@ properties="#Mon Jan 21 14:08:47 CET 2013" creator="1" date_created="2013-01-21 14:08:47" retired="0" /> - - - + + + + - \ No newline at end of file + diff --git a/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-1.12.xml b/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-1.12.xml index e66c1fd330..ebe5f05ab8 100644 --- a/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-1.12.xml +++ b/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-1.12.xml @@ -545,11 +545,21 @@ properties="#Mon Jan 21 14:08:47 CET 2013" creator="1" date_created="2013-01-21 14:08:47" retired="0" /> - - - + + + + - \ No newline at end of file + diff --git a/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-1.9.xml b/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-1.9.xml index 434acb502a..236aadb7ea 100644 --- a/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-1.9.xml +++ b/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-1.9.xml @@ -529,11 +529,21 @@ properties="#Mon Jan 21 14:08:47 CET 2013" creator="1" date_created="2013-01-21 14:08:47" retired="0" /> - - - + + + + diff --git a/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-2.0.xml b/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-2.0.xml index 6da1c292fd..7d6f1e489a 100644 --- a/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-2.0.xml +++ b/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-2.0.xml @@ -553,11 +553,21 @@ properties="#Mon Jan 21 14:08:47 CET 2013" creator="1" date_created="2013-01-21 14:08:47" retired="0" /> - - - - - \ No newline at end of file + + + + + + diff --git a/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-2.1.xml b/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-2.1.xml index 2311993e7e..60139af0ae 100644 --- a/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-2.1.xml +++ b/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-2.1.xml @@ -553,12 +553,22 @@ properties="#Mon Jan 21 14:08:47 CET 2013" creator="1" date_created="2013-01-21 14:08:47" retired="0" /> - - - - - - \ No newline at end of file + + + + + + + diff --git a/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-2.2.xml b/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-2.2.xml index bb308ce0b9..e89f14375f 100644 --- a/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-2.2.xml +++ b/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-2.2.xml @@ -553,12 +553,22 @@ properties="#Mon Jan 21 14:08:47 CET 2013" creator="1" date_created="2013-01-21 14:08:47" retired="0" /> - - - + + + + diff --git a/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-2.3.xml b/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-2.3.xml index bb308ce0b9..e89f14375f 100644 --- a/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-2.3.xml +++ b/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-2.3.xml @@ -553,12 +553,22 @@ properties="#Mon Jan 21 14:08:47 CET 2013" creator="1" date_created="2013-01-21 14:08:47" retired="0" /> - - - + + + + diff --git a/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-2.4.xml b/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-2.4.xml index bb308ce0b9..e89f14375f 100644 --- a/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-2.4.xml +++ b/api-tests/src/test/resources/org/openmrs/module/reporting/include/ReportTestDataset-openmrs-2.4.xml @@ -553,12 +553,22 @@ properties="#Mon Jan 21 14:08:47 CET 2013" creator="1" date_created="2013-01-21 14:08:47" retired="0" /> - - - + + + + diff --git a/api/src/main/java/org/openmrs/module/reporting/report/ReportRequestDTO.java b/api/src/main/java/org/openmrs/module/reporting/report/ReportRequestDTO.java index f7a3cebb3d..014a74c8d6 100644 --- a/api/src/main/java/org/openmrs/module/reporting/report/ReportRequestDTO.java +++ b/api/src/main/java/org/openmrs/module/reporting/report/ReportRequestDTO.java @@ -1,3 +1,12 @@ +/** + * 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.reporting.report; import java.util.List; @@ -6,9 +15,9 @@ public class ReportRequestDTO { private List reportRequests; - private Long reportRequestCount; + private long reportRequestCount; - public ReportRequestDTO(List reportRequests, Long reportRequestCount) { + public ReportRequestDTO(List reportRequests, long reportRequestCount) { this.reportRequests = reportRequests; this.reportRequestCount = reportRequestCount; } @@ -21,11 +30,11 @@ public void setReportRequests(List reportRequests) { this.reportRequests = reportRequests; } - public Long getReportRequestCount() { + public long getReportRequestCount() { return reportRequestCount; } - public void setReportRequestCount(Long reportRequestCount) { + public void setReportRequestCount(long reportRequestCount) { this.reportRequestCount = reportRequestCount; } } diff --git a/api/src/main/java/org/openmrs/module/reporting/report/service/ReportService.java b/api/src/main/java/org/openmrs/module/reporting/report/service/ReportService.java index d50b16ffcc..8321198d13 100644 --- a/api/src/main/java/org/openmrs/module/reporting/report/service/ReportService.java +++ b/api/src/main/java/org/openmrs/module/reporting/report/service/ReportService.java @@ -128,6 +128,16 @@ public interface ReportService extends OpenmrsService { public ReportRequest getReportRequestByUuid(String uuid); /** + * Get Report Requests by Report Definition, request date, having given status. + * The list is sorted descending by evaluateCompleteDatetime, evaluateStartDatetime, priority, requestDate. + * + * @param reportDefinition a Report Definition filter, nullable + * @param requestOnOrAfter a Date used to limit result to ReportRequests which ware requested on or after + * (greater or equal filter), nullable + * @param requestOnOrBefore a Date used to limit result to ReportRequests which ware requested on or before + * (lower or equal filter), nullable + * @param statuses an array of Status, used to limit result to ReportRequests with status included in the array, null + * or empty array means that all statuses are included, nullable * @return all {@link ReportRequest} in the system that match the passed parameters * @should retrieve report requests by definition */ @@ -135,6 +145,18 @@ public interface ReportService extends OpenmrsService { public List getReportRequests(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Status...statuses); /** + * Get Report Requests by Report Definition, request date, having given status, limit result to at most {@code + * mostRecentNum} elements. + * The list is sorted descending by evaluateCompleteDatetime, evaluateStartDatetime, priority, requestDate. + * + * @param reportDefinition a Report Definition filter, nullable + * @param requestOnOrAfter a Date used to limit result to ReportRequests which ware requested on or after + * (greater or equal filter), nullable + * @param requestOnOrBefore a Date used to limit result to ReportRequests which ware requested on or before + * (lower or equal filter), nullable + * @param mostRecentNum maximum number of results, not null + * @param statuses an array of Status, used to limit result to ReportRequests with status included in the array, null + * or empty array means that all statuses are included, nullable * @return all {@link ReportRequest} in the system that match the passed parameters * @should retrieve report requests by definition */ @@ -142,10 +164,24 @@ public interface ReportService extends OpenmrsService { public List getReportRequests(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer mostRecentNum, Status...statuses); /** - * @return {@link ReportRequestDTO} object which contains report requests and total count data + * Get paginated Report Requests by Report Definition, request date, having given status. + * The list is sorted descending by evaluateCompleteDatetime, evaluateStartDatetime, priority, requestDate. + * + * @param reportDefinition a Report Definition filter, nullable + * @param requestOnOrAfter a Date used to limit result to ReportRequests which ware requested on or after + * (greater or equal filter), nullable + * @param requestOnOrBefore a Date used to limit result to ReportRequests which ware requested on or before + * (lower or equal filter), nullable + * @param pageNumber a number of a page to return, starting from 1, not null + * @param pageSize page size, not null + * @param statuses an array of Status, used to limit result to ReportRequests with status included in the array, null + * or empty array means that all statuses are included, nullable + * @return {@link ReportRequestDTO} object which contains report requests and total count data, never null + * @since 1.27.0 */ @Transactional(readOnly = true) - ReportRequestDTO getReportsWithPagination(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer pageNumber, Integer pageSize, Status...statuses); + public ReportRequestDTO getReportRequestsWithPagination(ReportDefinition reportDefinition, Date requestOnOrAfter, + Date requestOnOrBefore, Integer pageNumber, Integer pageSize, Status...statuses); /** * Deletes the passed {@link ReportRequest} diff --git a/api/src/main/java/org/openmrs/module/reporting/report/service/ReportServiceImpl.java b/api/src/main/java/org/openmrs/module/reporting/report/service/ReportServiceImpl.java index ba2d10bac9..a909e8f913 100644 --- a/api/src/main/java/org/openmrs/module/reporting/report/service/ReportServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/reporting/report/service/ReportServiceImpl.java @@ -225,12 +225,12 @@ public List getReportRequests(ReportDefinition reportDefinition, } /** - * @see ReportService#getReportsWithPagination(ReportDefinition, Date, Date, Integer, Integer, Status...) + * @see ReportService#getReportRequestsWithPagination(ReportDefinition, Date, Date, Integer, Integer, Status...) */ @Override @Transactional(readOnly = true) - public ReportRequestDTO getReportsWithPagination(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer pageNumber, Integer pageSize, Status... statuses) { - return reportDAO.getReportsWithPagination(reportDefinition, requestOnOrAfter, requestOnOrBefore, pageNumber, pageSize, statuses); + public ReportRequestDTO getReportRequestsWithPagination(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer pageNumber, Integer pageSize, Status... statuses) { + return reportDAO.getReportRequestsWithPagination(reportDefinition, requestOnOrAfter, requestOnOrBefore, pageNumber, pageSize, statuses); } /** @@ -848,4 +848,4 @@ public void setReportData(ReportData reportData) { this.reportData = reportData; } } -} \ No newline at end of file +} diff --git a/api/src/main/java/org/openmrs/module/reporting/report/service/db/HibernateReportDAO.java b/api/src/main/java/org/openmrs/module/reporting/report/service/db/HibernateReportDAO.java index e9d9d6b1e0..3bbe6c80d7 100644 --- a/api/src/main/java/org/openmrs/module/reporting/report/service/db/HibernateReportDAO.java +++ b/api/src/main/java/org/openmrs/module/reporting/report/service/db/HibernateReportDAO.java @@ -218,9 +218,9 @@ public List getReportRequests(ReportDefinition reportDefinition, } /** - * @see ReportDAO#getReportsWithPagination(ReportDefinition, Date, Date, Integer, Integer, Status...) + * @see ReportDAO#getReportRequestsWithPagination(ReportDefinition, Date, Date, Integer, Integer, Status...) */ - public ReportRequestDTO getReportsWithPagination(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer pageNumber, Integer pageSize, Status...statuses) { + public ReportRequestDTO getReportRequestsWithPagination(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer pageNumber, Integer pageSize, Status...statuses) { Criteria c = sessionFactory.getCurrentSession().createCriteria(ReportRequest.class); if (reportDefinition != null) { @@ -235,15 +235,16 @@ public ReportRequestDTO getReportsWithPagination(ReportDefinition reportDefiniti if (statuses != null && statuses.length > 0) { c.add(Restrictions.in("status", statuses)); } - c.addOrder(Order.desc("evaluateCompleteDatetime")); - c.addOrder(Order.desc("evaluateStartDatetime")); - c.addOrder(Order.desc("priority")); - c.addOrder(Order.desc("requestDate")); c.setProjection(Projections.rowCount()); Long count = (Long) c.uniqueResult(); c.setProjection(null); + c.addOrder(Order.desc("evaluateCompleteDatetime")); + c.addOrder(Order.desc("evaluateStartDatetime")); + c.addOrder(Order.desc("priority")); + c.addOrder(Order.desc("requestDate")); + if (pageNumber != null && pageSize != null) { c.setFirstResult((pageNumber - 1) * pageSize); c.setMaxResults(pageSize); diff --git a/api/src/main/java/org/openmrs/module/reporting/report/service/db/ReportDAO.java b/api/src/main/java/org/openmrs/module/reporting/report/service/db/ReportDAO.java index 19138b389f..7cb6a0d52b 100644 --- a/api/src/main/java/org/openmrs/module/reporting/report/service/db/ReportDAO.java +++ b/api/src/main/java/org/openmrs/module/reporting/report/service/db/ReportDAO.java @@ -127,7 +127,7 @@ public List getReportDesigns(ReportDefinition reportDefinition, Cl /** * @return {@link ReportRequestDTO} object which contains report requests and total count data */ - ReportRequestDTO getReportsWithPagination(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer pageNumber, Integer pageSize, Status...statuses); + ReportRequestDTO getReportRequestsWithPagination(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer pageNumber, Integer pageSize, Status...statuses); /** * Deletes the passed {@link ReportRequest} From 28d54b4820c19bcbd305f396227bf1df14131a77 Mon Sep 17 00:00:00 2001 From: Piotr Wargulak Date: Thu, 22 Feb 2024 17:22:56 +0100 Subject: [PATCH 3/5] O3-1494: Changes after 2nd code review --- .../report/service/ReportServiceTest.java | 18 +++++++++--------- ...questDTO.java => ReportRequestPageDTO.java} | 16 ++++++++-------- .../report/service/ReportService.java | 7 +++---- .../report/service/ReportServiceImpl.java | 4 ++-- .../report/service/db/HibernateReportDAO.java | 6 +++--- .../reporting/report/service/db/ReportDAO.java | 7 ++++--- 6 files changed, 29 insertions(+), 29 deletions(-) rename api/src/main/java/org/openmrs/module/reporting/report/{ReportRequestDTO.java => ReportRequestPageDTO.java} (68%) diff --git a/api-tests/src/test/java/org/openmrs/module/reporting/report/service/ReportServiceTest.java b/api-tests/src/test/java/org/openmrs/module/reporting/report/service/ReportServiceTest.java index 37724baa22..a828d37a36 100644 --- a/api-tests/src/test/java/org/openmrs/module/reporting/report/service/ReportServiceTest.java +++ b/api-tests/src/test/java/org/openmrs/module/reporting/report/service/ReportServiceTest.java @@ -27,7 +27,7 @@ import org.openmrs.module.reporting.report.ReportProcessorConfiguration; import org.openmrs.module.reporting.report.ReportRequest; import org.openmrs.module.reporting.report.ReportRequest.Priority; -import org.openmrs.module.reporting.report.ReportRequestDTO; +import org.openmrs.module.reporting.report.ReportRequestPageDTO; import org.openmrs.module.reporting.report.definition.ReportDefinition; import org.openmrs.module.reporting.report.definition.service.ReportDefinitionService; import org.openmrs.module.reporting.report.processor.LoggingReportProcessor; @@ -529,9 +529,9 @@ public void purgeReportDesignsForReportDefinition_shouldDeleteAllAssociatedRepor @Test public void getReportRequestsWithPagination_shouldReturnAllReportRequestsPagedWithCorrectTotalCount() { final ReportService rs = Context.getService(ReportService.class); - final ReportRequestDTO result = rs.getReportRequestsWithPagination(null, null, null, 1,2); + final ReportRequestPageDTO result = rs.getReportRequestsWithPagination(null, null, null, 1,2); - assertEquals(4, result.getReportRequestCount()); + assertEquals(4, result.getTotalCount()); assertEquals(2, result.getReportRequests().size()); final List resultUuids = mapToReportRequestUuids(result.getReportRequests()); @@ -544,9 +544,9 @@ public void getReportRequestsWithPagination_shouldReturnReportRequestsForGivenRe final ReportService rs = Context.getService(ReportService.class); final ReportDefinition testReportDefinition = rs.getReportDesignByUuid("d7a82b63-1066-4c1d-9b43-b405851fc467").getReportDefinition(); - final ReportRequestDTO result = rs.getReportRequestsWithPagination(testReportDefinition, null, null, 1,2); + final ReportRequestPageDTO result = rs.getReportRequestsWithPagination(testReportDefinition, null, null, 1,2); - assertEquals(2, result.getReportRequestCount()); + assertEquals(2, result.getTotalCount()); assertEquals(2, result.getReportRequests().size()); final List resultUuids = mapToReportRequestUuids(result.getReportRequests()); @@ -559,9 +559,9 @@ public void getReportRequestsWithPagination_shouldReturnReportRequestsForRequest final ReportService rs = Context.getService(ReportService.class); final Date from = newDate(2013, Calendar.JANUARY, 21, 14, 8, 48); final Date to = newDate(2013, Calendar.JANUARY, 21, 14, 8, 49); - final ReportRequestDTO result = rs.getReportRequestsWithPagination(null, from, to, 1,2); + final ReportRequestPageDTO result = rs.getReportRequestsWithPagination(null, from, to, 1,2); - assertEquals(2, result.getReportRequestCount()); + assertEquals(2, result.getTotalCount()); assertEquals(2, result.getReportRequests().size()); final List resultUuids = mapToReportRequestUuids(result.getReportRequests()); @@ -572,9 +572,9 @@ public void getReportRequestsWithPagination_shouldReturnReportRequestsForRequest @Test public void getReportRequestsWithPagination_shouldReturnAPartialPageOfReportRequests() { final ReportService rs = Context.getService(ReportService.class); - final ReportRequestDTO result = rs.getReportRequestsWithPagination(null, null, null, 1, 2, ReportRequest.Status.FAILED); + final ReportRequestPageDTO result = rs.getReportRequestsWithPagination(null, null, null, 1, 2, ReportRequest.Status.FAILED); - assertEquals(1, result.getReportRequestCount()); + assertEquals(1, result.getTotalCount()); assertEquals(1, result.getReportRequests().size()); final List resultUuids = mapToReportRequestUuids(result.getReportRequests()); diff --git a/api/src/main/java/org/openmrs/module/reporting/report/ReportRequestDTO.java b/api/src/main/java/org/openmrs/module/reporting/report/ReportRequestPageDTO.java similarity index 68% rename from api/src/main/java/org/openmrs/module/reporting/report/ReportRequestDTO.java rename to api/src/main/java/org/openmrs/module/reporting/report/ReportRequestPageDTO.java index 014a74c8d6..c7ae333152 100644 --- a/api/src/main/java/org/openmrs/module/reporting/report/ReportRequestDTO.java +++ b/api/src/main/java/org/openmrs/module/reporting/report/ReportRequestPageDTO.java @@ -11,15 +11,15 @@ import java.util.List; -public class ReportRequestDTO { +public class ReportRequestPageDTO { private List reportRequests; - private long reportRequestCount; + private long totalCount; - public ReportRequestDTO(List reportRequests, long reportRequestCount) { + public ReportRequestPageDTO(List reportRequests, long totalCount) { this.reportRequests = reportRequests; - this.reportRequestCount = reportRequestCount; + this.totalCount = totalCount; } public List getReportRequests() { @@ -30,11 +30,11 @@ public void setReportRequests(List reportRequests) { this.reportRequests = reportRequests; } - public long getReportRequestCount() { - return reportRequestCount; + public long getTotalCount() { + return totalCount; } - public void setReportRequestCount(long reportRequestCount) { - this.reportRequestCount = reportRequestCount; + public void setTotalCount(long totalCount) { + this.totalCount = totalCount; } } diff --git a/api/src/main/java/org/openmrs/module/reporting/report/service/ReportService.java b/api/src/main/java/org/openmrs/module/reporting/report/service/ReportService.java index 8321198d13..2d573140e9 100644 --- a/api/src/main/java/org/openmrs/module/reporting/report/service/ReportService.java +++ b/api/src/main/java/org/openmrs/module/reporting/report/service/ReportService.java @@ -17,7 +17,7 @@ import org.openmrs.module.reporting.report.ReportProcessorConfiguration; import org.openmrs.module.reporting.report.ReportRequest; import org.openmrs.module.reporting.report.ReportRequest.Status; -import org.openmrs.module.reporting.report.ReportRequestDTO; +import org.openmrs.module.reporting.report.ReportRequestPageDTO; import org.openmrs.module.reporting.report.definition.ReportDefinition; import org.openmrs.module.reporting.report.processor.ReportProcessor; import org.openmrs.module.reporting.report.renderer.RenderingMode; @@ -176,12 +176,11 @@ public interface ReportService extends OpenmrsService { * @param pageSize page size, not null * @param statuses an array of Status, used to limit result to ReportRequests with status included in the array, null * or empty array means that all statuses are included, nullable - * @return {@link ReportRequestDTO} object which contains report requests and total count data, never null + * @return {@link ReportRequestPageDTO} object which contains report requests and total count data, never null * @since 1.27.0 */ @Transactional(readOnly = true) - public ReportRequestDTO getReportRequestsWithPagination(ReportDefinition reportDefinition, Date requestOnOrAfter, - Date requestOnOrBefore, Integer pageNumber, Integer pageSize, Status...statuses); + public ReportRequestPageDTO getReportRequestsWithPagination(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer pageNumber, Integer pageSize, Status...statuses); /** * Deletes the passed {@link ReportRequest} diff --git a/api/src/main/java/org/openmrs/module/reporting/report/service/ReportServiceImpl.java b/api/src/main/java/org/openmrs/module/reporting/report/service/ReportServiceImpl.java index a909e8f913..151e09bc70 100644 --- a/api/src/main/java/org/openmrs/module/reporting/report/service/ReportServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/reporting/report/service/ReportServiceImpl.java @@ -33,7 +33,7 @@ import org.openmrs.module.reporting.report.ReportRequest.Priority; import org.openmrs.module.reporting.report.ReportRequest.PriorityComparator; import org.openmrs.module.reporting.report.ReportRequest.Status; -import org.openmrs.module.reporting.report.ReportRequestDTO; +import org.openmrs.module.reporting.report.ReportRequestPageDTO; import org.openmrs.module.reporting.report.definition.ReportDefinition; import org.openmrs.module.reporting.report.definition.service.ReportDefinitionService; import org.openmrs.module.reporting.report.processor.ReportProcessor; @@ -229,7 +229,7 @@ public List getReportRequests(ReportDefinition reportDefinition, */ @Override @Transactional(readOnly = true) - public ReportRequestDTO getReportRequestsWithPagination(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer pageNumber, Integer pageSize, Status... statuses) { + public ReportRequestPageDTO getReportRequestsWithPagination(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer pageNumber, Integer pageSize, Status... statuses) { return reportDAO.getReportRequestsWithPagination(reportDefinition, requestOnOrAfter, requestOnOrBefore, pageNumber, pageSize, statuses); } diff --git a/api/src/main/java/org/openmrs/module/reporting/report/service/db/HibernateReportDAO.java b/api/src/main/java/org/openmrs/module/reporting/report/service/db/HibernateReportDAO.java index 3bbe6c80d7..849f718f4e 100644 --- a/api/src/main/java/org/openmrs/module/reporting/report/service/db/HibernateReportDAO.java +++ b/api/src/main/java/org/openmrs/module/reporting/report/service/db/HibernateReportDAO.java @@ -23,7 +23,7 @@ import org.openmrs.module.reporting.report.ReportProcessorConfiguration; import org.openmrs.module.reporting.report.ReportRequest; import org.openmrs.module.reporting.report.ReportRequest.Status; -import org.openmrs.module.reporting.report.ReportRequestDTO; +import org.openmrs.module.reporting.report.ReportRequestPageDTO; import org.openmrs.module.reporting.report.definition.ReportDefinition; import org.openmrs.module.reporting.report.renderer.ReportRenderer; @@ -220,7 +220,7 @@ public List getReportRequests(ReportDefinition reportDefinition, /** * @see ReportDAO#getReportRequestsWithPagination(ReportDefinition, Date, Date, Integer, Integer, Status...) */ - public ReportRequestDTO getReportRequestsWithPagination(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer pageNumber, Integer pageSize, Status...statuses) { + public ReportRequestPageDTO getReportRequestsWithPagination(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer pageNumber, Integer pageSize, Status...statuses) { Criteria c = sessionFactory.getCurrentSession().createCriteria(ReportRequest.class); if (reportDefinition != null) { @@ -252,7 +252,7 @@ public ReportRequestDTO getReportRequestsWithPagination(ReportDefinition reportD List reportRequests = c.list(); - return new ReportRequestDTO(reportRequests, count); + return new ReportRequestPageDTO(reportRequests, count); } /** diff --git a/api/src/main/java/org/openmrs/module/reporting/report/service/db/ReportDAO.java b/api/src/main/java/org/openmrs/module/reporting/report/service/db/ReportDAO.java index 7cb6a0d52b..6837b4ab50 100644 --- a/api/src/main/java/org/openmrs/module/reporting/report/service/db/ReportDAO.java +++ b/api/src/main/java/org/openmrs/module/reporting/report/service/db/ReportDAO.java @@ -14,7 +14,7 @@ import org.openmrs.module.reporting.report.ReportProcessorConfiguration; import org.openmrs.module.reporting.report.ReportRequest; import org.openmrs.module.reporting.report.ReportRequest.Status; -import org.openmrs.module.reporting.report.ReportRequestDTO; +import org.openmrs.module.reporting.report.ReportRequestPageDTO; import org.openmrs.module.reporting.report.definition.ReportDefinition; import org.openmrs.module.reporting.report.renderer.ReportRenderer; @@ -125,9 +125,10 @@ public List getReportDesigns(ReportDefinition reportDefinition, Cl public List getReportRequests(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer mostRecentNum, Status...statuses); /** - * @return {@link ReportRequestDTO} object which contains report requests and total count data + * @return {@link ReportRequestPageDTO} object which contains report requests and total count data + * @see org.openmrs.module.reporting.report.service.ReportService#getReportRequestsWithPagination(ReportDefinition, Date, Date, Integer, Integer, Status...) */ - ReportRequestDTO getReportRequestsWithPagination(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer pageNumber, Integer pageSize, Status...statuses); + ReportRequestPageDTO getReportRequestsWithPagination(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer pageNumber, Integer pageSize, Status...statuses); /** * Deletes the passed {@link ReportRequest} From 7649ee7ee477b49aa4fc5de06605bd2d8083d69b Mon Sep 17 00:00:00 2001 From: Piotr Wargulak Date: Fri, 23 Feb 2024 15:35:45 +0100 Subject: [PATCH 4/5] O3-1494: Removed ReportRequestPageDTO --- .../report/service/ReportServiceTest.java | 73 ++++++++++----- .../report/ReportRequestPageDTO.java | 40 -------- .../report/service/ReportService.java | 26 ++++-- .../report/service/ReportServiceImpl.java | 15 ++- .../report/service/db/HibernateReportDAO.java | 93 ++++++++----------- .../report/service/db/ReportDAO.java | 12 +-- 6 files changed, 125 insertions(+), 134 deletions(-) delete mode 100644 api/src/main/java/org/openmrs/module/reporting/report/ReportRequestPageDTO.java diff --git a/api-tests/src/test/java/org/openmrs/module/reporting/report/service/ReportServiceTest.java b/api-tests/src/test/java/org/openmrs/module/reporting/report/service/ReportServiceTest.java index a828d37a36..1f460db5a4 100644 --- a/api-tests/src/test/java/org/openmrs/module/reporting/report/service/ReportServiceTest.java +++ b/api-tests/src/test/java/org/openmrs/module/reporting/report/service/ReportServiceTest.java @@ -27,7 +27,6 @@ import org.openmrs.module.reporting.report.ReportProcessorConfiguration; import org.openmrs.module.reporting.report.ReportRequest; import org.openmrs.module.reporting.report.ReportRequest.Priority; -import org.openmrs.module.reporting.report.ReportRequestPageDTO; import org.openmrs.module.reporting.report.definition.ReportDefinition; import org.openmrs.module.reporting.report.definition.service.ReportDefinitionService; import org.openmrs.module.reporting.report.processor.LoggingReportProcessor; @@ -527,60 +526,92 @@ public void purgeReportDesignsForReportDefinition_shouldDeleteAllAssociatedRepor } @Test - public void getReportRequestsWithPagination_shouldReturnAllReportRequestsPagedWithCorrectTotalCount() { + public void getReportRequests_shouldReturnCorrectReportRequests() { final ReportService rs = Context.getService(ReportService.class); - final ReportRequestPageDTO result = rs.getReportRequestsWithPagination(null, null, null, 1,2); + final List reportRequests = rs.getReportRequests(null, null, null, 0,2); - assertEquals(4, result.getTotalCount()); - assertEquals(2, result.getReportRequests().size()); + assertEquals(2, reportRequests.size()); - final List resultUuids = mapToReportRequestUuids(result.getReportRequests()); + final List resultUuids = mapToReportRequestUuids(reportRequests); assertTrue(resultUuids.contains("fce15a1b-4618-4f65-bfe9-8bb60a85c110")); assertTrue(resultUuids.contains("b0a82b63-1066-4c1d-9b43-b405851fc467")); } @Test - public void getReportRequestsWithPagination_shouldReturnReportRequestsForGivenReportDefinitionPaged() { + public void getReportRequestsCount_shouldReturnTotalCount() { + final ReportService rs = Context.getService(ReportService.class); + final long totalCount = rs.getReportRequestsCount(null, null, null); + + assertEquals(4, totalCount); + } + + @Test + public void getReportRequests_shouldReturnCorrectReportRequestsForGivenReportDefinition() { final ReportService rs = Context.getService(ReportService.class); final ReportDefinition testReportDefinition = rs.getReportDesignByUuid("d7a82b63-1066-4c1d-9b43-b405851fc467").getReportDefinition(); - final ReportRequestPageDTO result = rs.getReportRequestsWithPagination(testReportDefinition, null, null, 1,2); + final List reportRequests = rs.getReportRequests(testReportDefinition, null, null, 0,2); - assertEquals(2, result.getTotalCount()); - assertEquals(2, result.getReportRequests().size()); + assertEquals(2, reportRequests.size()); - final List resultUuids = mapToReportRequestUuids(result.getReportRequests()); + final List resultUuids = mapToReportRequestUuids(reportRequests); assertTrue(resultUuids.contains("h8a82b63-1066-4c1d-9b43-b405851fc467")); assertTrue(resultUuids.contains("b0a82b63-1066-4c1d-9b43-b405851fc467")); } @Test - public void getReportRequestsWithPagination_shouldReturnReportRequestsForRequestedWithinDatesPaged() { + public void getReportRequestsCount_shouldReturnCorrectTotalCountForReportDefinitionFilter() { + final ReportService rs = Context.getService(ReportService.class); + final ReportDefinition testReportDefinition = + rs.getReportDesignByUuid("d7a82b63-1066-4c1d-9b43-b405851fc467").getReportDefinition(); + final long totalCount = rs.getReportRequestsCount(testReportDefinition, null, null); + + assertEquals(2, totalCount); + } + + @Test + public void getReportRequests_shouldReturnCorrectReportRequestsForRequestedWithinDates() { final ReportService rs = Context.getService(ReportService.class); final Date from = newDate(2013, Calendar.JANUARY, 21, 14, 8, 48); final Date to = newDate(2013, Calendar.JANUARY, 21, 14, 8, 49); - final ReportRequestPageDTO result = rs.getReportRequestsWithPagination(null, from, to, 1,2); + final List reportRequests = rs.getReportRequests(null, from, to, 0,2); - assertEquals(2, result.getTotalCount()); - assertEquals(2, result.getReportRequests().size()); + assertEquals(2, reportRequests.size()); - final List resultUuids = mapToReportRequestUuids(result.getReportRequests()); + final List resultUuids = mapToReportRequestUuids(reportRequests); assertTrue(resultUuids.contains("b0a82b63-1066-4c1d-9b43-b405851fc467")); assertTrue(resultUuids.contains("d9a82b63-1066-4c1d-9b43-b405851fc467")); } @Test - public void getReportRequestsWithPagination_shouldReturnAPartialPageOfReportRequests() { + public void getReportRequestsCount_shouldReturnCorrectTotalCountForRequestedWithinDatesFilter() { final ReportService rs = Context.getService(ReportService.class); - final ReportRequestPageDTO result = rs.getReportRequestsWithPagination(null, null, null, 1, 2, ReportRequest.Status.FAILED); + final Date from = newDate(2013, Calendar.JANUARY, 21, 14, 8, 48); + final Date to = newDate(2013, Calendar.JANUARY, 21, 14, 8, 49); + final long totalCount = rs.getReportRequestsCount(null, from, to); + + assertEquals(2, totalCount); + } + + @Test + public void getReportRequests_shouldReturnAPartialPageOfReportRequests() { + final ReportService rs = Context.getService(ReportService.class); + final List reportRequests = rs.getReportRequests(null, null, null, 0, 2, ReportRequest.Status.FAILED); - assertEquals(1, result.getTotalCount()); - assertEquals(1, result.getReportRequests().size()); + assertEquals(1, reportRequests.size()); - final List resultUuids = mapToReportRequestUuids(result.getReportRequests()); + final List resultUuids = mapToReportRequestUuids(reportRequests); assertTrue(resultUuids.contains("fce15a1b-4618-4f65-bfe9-8bb60a85c110")); } + @Test + public void getReportRequestsCount_shouldReturnCorrectTotalCountForStatusFilter() { + final ReportService rs = Context.getService(ReportService.class); + final long totalCount = rs.getReportRequestsCount(null, null, null, ReportRequest.Status.FAILED); + + assertEquals(1, totalCount); + } + private List mapToReportRequestUuids(List reportRequests) { List reportRequestUuids = new ArrayList(); diff --git a/api/src/main/java/org/openmrs/module/reporting/report/ReportRequestPageDTO.java b/api/src/main/java/org/openmrs/module/reporting/report/ReportRequestPageDTO.java deleted file mode 100644 index c7ae333152..0000000000 --- a/api/src/main/java/org/openmrs/module/reporting/report/ReportRequestPageDTO.java +++ /dev/null @@ -1,40 +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.reporting.report; - -import java.util.List; - -public class ReportRequestPageDTO { - - private List reportRequests; - - private long totalCount; - - public ReportRequestPageDTO(List reportRequests, long totalCount) { - this.reportRequests = reportRequests; - this.totalCount = totalCount; - } - - public List getReportRequests() { - return reportRequests; - } - - public void setReportRequests(List reportRequests) { - this.reportRequests = reportRequests; - } - - public long getTotalCount() { - return totalCount; - } - - public void setTotalCount(long totalCount) { - this.totalCount = totalCount; - } -} diff --git a/api/src/main/java/org/openmrs/module/reporting/report/service/ReportService.java b/api/src/main/java/org/openmrs/module/reporting/report/service/ReportService.java index 2d573140e9..7b9df89302 100644 --- a/api/src/main/java/org/openmrs/module/reporting/report/service/ReportService.java +++ b/api/src/main/java/org/openmrs/module/reporting/report/service/ReportService.java @@ -17,7 +17,6 @@ import org.openmrs.module.reporting.report.ReportProcessorConfiguration; import org.openmrs.module.reporting.report.ReportRequest; import org.openmrs.module.reporting.report.ReportRequest.Status; -import org.openmrs.module.reporting.report.ReportRequestPageDTO; import org.openmrs.module.reporting.report.definition.ReportDefinition; import org.openmrs.module.reporting.report.processor.ReportProcessor; import org.openmrs.module.reporting.report.renderer.RenderingMode; @@ -164,7 +163,7 @@ public interface ReportService extends OpenmrsService { public List getReportRequests(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer mostRecentNum, Status...statuses); /** - * Get paginated Report Requests by Report Definition, request date, having given status. + * Get Report Requests by Report Definition, request date, having given status. * The list is sorted descending by evaluateCompleteDatetime, evaluateStartDatetime, priority, requestDate. * * @param reportDefinition a Report Definition filter, nullable @@ -172,15 +171,30 @@ public interface ReportService extends OpenmrsService { * (greater or equal filter), nullable * @param requestOnOrBefore a Date used to limit result to ReportRequests which ware requested on or before * (lower or equal filter), nullable - * @param pageNumber a number of a page to return, starting from 1, not null - * @param pageSize page size, not null + * @param firstResult the first result to be retrieved, not null + * @param maxResults a limit upon the number of Report Requests to be retrieved, not null * @param statuses an array of Status, used to limit result to ReportRequests with status included in the array, null * or empty array means that all statuses are included, nullable - * @return {@link ReportRequestPageDTO} object which contains report requests and total count data, never null + * @return all {@link ReportRequest} in the system that match the passed parameters * @since 1.27.0 */ @Transactional(readOnly = true) - public ReportRequestPageDTO getReportRequestsWithPagination(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer pageNumber, Integer pageSize, Status...statuses); + public List getReportRequests(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer firstResult, Integer maxResults, Status...statuses); + + /** + * Gets a count of Report Requests that match by Report Definition, request date limits and status(-es). + * + * @param reportDefinition a Report Definition filter, nullable + * @param requestOnOrAfter a Date used to limit result to ReportRequests which ware requested on or after + * (greater or equal filter), nullable + * @param requestOnOrBefore a Date used to limit result to ReportRequests which ware requested on or before + * (lower or equal filter), nullable + * @param statuses an array of Status, used to limit result to ReportRequests with status included in the array, null + * or empty array means that all statuses are included, nullable + * @return the count of Report Requests that match the passed parameters + * @since 1.27.0 + */ + public long getReportRequestsCount(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Status...statuses); /** * Deletes the passed {@link ReportRequest} diff --git a/api/src/main/java/org/openmrs/module/reporting/report/service/ReportServiceImpl.java b/api/src/main/java/org/openmrs/module/reporting/report/service/ReportServiceImpl.java index 151e09bc70..394ab83b0e 100644 --- a/api/src/main/java/org/openmrs/module/reporting/report/service/ReportServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/reporting/report/service/ReportServiceImpl.java @@ -33,7 +33,6 @@ import org.openmrs.module.reporting.report.ReportRequest.Priority; import org.openmrs.module.reporting.report.ReportRequest.PriorityComparator; import org.openmrs.module.reporting.report.ReportRequest.Status; -import org.openmrs.module.reporting.report.ReportRequestPageDTO; import org.openmrs.module.reporting.report.definition.ReportDefinition; import org.openmrs.module.reporting.report.definition.service.ReportDefinitionService; import org.openmrs.module.reporting.report.processor.ReportProcessor; @@ -221,16 +220,22 @@ public List getReportRequests(ReportDefinition reportDefinition, */ @Transactional(readOnly=true) public List getReportRequests(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer mostRecentNum, Status...statuses) { - return reportDAO.getReportRequests(reportDefinition, requestOnOrAfter, requestOnOrBefore, mostRecentNum, statuses); + return getReportRequests(reportDefinition, requestOnOrAfter, requestOnOrBefore, 0, mostRecentNum, statuses); } /** - * @see ReportService#getReportRequestsWithPagination(ReportDefinition, Date, Date, Integer, Integer, Status...) + * @see ReportService#getReportRequests(ReportDefinition, Date, Date, Integer, Integer, Status...) */ @Override @Transactional(readOnly = true) - public ReportRequestPageDTO getReportRequestsWithPagination(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer pageNumber, Integer pageSize, Status... statuses) { - return reportDAO.getReportRequestsWithPagination(reportDefinition, requestOnOrAfter, requestOnOrBefore, pageNumber, pageSize, statuses); + public List getReportRequests(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer firstResult, Integer maxResults, Status... statuses) { + return reportDAO.getReportRequests(reportDefinition, requestOnOrAfter, requestOnOrBefore, firstResult, maxResults, statuses); + } + + @Override + @Transactional(readOnly = true) + public long getReportRequestsCount(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Status... statuses) { + return reportDAO.getReportRequestsCount(reportDefinition, requestOnOrAfter, requestOnOrBefore, statuses); } /** diff --git a/api/src/main/java/org/openmrs/module/reporting/report/service/db/HibernateReportDAO.java b/api/src/main/java/org/openmrs/module/reporting/report/service/db/HibernateReportDAO.java index 849f718f4e..459a2e97fc 100644 --- a/api/src/main/java/org/openmrs/module/reporting/report/service/db/HibernateReportDAO.java +++ b/api/src/main/java/org/openmrs/module/reporting/report/service/db/HibernateReportDAO.java @@ -23,7 +23,6 @@ import org.openmrs.module.reporting.report.ReportProcessorConfiguration; import org.openmrs.module.reporting.report.ReportRequest; import org.openmrs.module.reporting.report.ReportRequest.Status; -import org.openmrs.module.reporting.report.ReportRequestPageDTO; import org.openmrs.module.reporting.report.definition.ReportDefinition; import org.openmrs.module.reporting.report.renderer.ReportRenderer; @@ -189,70 +188,33 @@ public ReportRequest getReportRequestByUuid(String uuid) { } /** - * @see ReportDAO#getReportRequests(ReportDefinition, Date, Date, Integer, Status...) + * @see ReportDAO#getReportRequests(ReportDefinition, Date, Date, Integer, Integer, Status...) */ @SuppressWarnings("unchecked") - public List getReportRequests(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer mostRecentNum, Status...statuses) { - Criteria c = sessionFactory.getCurrentSession().createCriteria(ReportRequest.class); - if (reportDefinition != null) { - c.add(Restrictions.eq("reportDefinition.definition", reportDefinition.getUuid())); - } - if (requestOnOrAfter != null) { - c.add(Restrictions.ge("requestDate", requestOnOrAfter)); - } - if (requestOnOrBefore != null) { - c.add(Restrictions.le("requestDate", requestOnOrBefore)); - } - if (statuses != null && statuses.length > 0) { - c.add(Restrictions.in("status", statuses)); - } - c.addOrder(Order.desc("evaluateCompleteDatetime")); - c.addOrder(Order.desc("evaluateStartDatetime")); - c.addOrder(Order.desc("priority")); - c.addOrder(Order.desc("requestDate")); - - if (mostRecentNum != null) { - c.setMaxResults(mostRecentNum); - } - return c.list(); - } + public List getReportRequests(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer firstResult, Integer maxResults, Status...statuses) { + final Criteria criteria = createReportRequestsBaseCriteria(reportDefinition, requestOnOrAfter, requestOnOrBefore, statuses); - /** - * @see ReportDAO#getReportRequestsWithPagination(ReportDefinition, Date, Date, Integer, Integer, Status...) - */ - public ReportRequestPageDTO getReportRequestsWithPagination(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer pageNumber, Integer pageSize, Status...statuses) { - Criteria c = sessionFactory.getCurrentSession().createCriteria(ReportRequest.class); + criteria.addOrder(Order.desc("evaluateCompleteDatetime")); + criteria.addOrder(Order.desc("evaluateStartDatetime")); + criteria.addOrder(Order.desc("priority")); + criteria.addOrder(Order.desc("requestDate")); - if (reportDefinition != null) { - c.add(Restrictions.eq("reportDefinition.definition", reportDefinition.getUuid())); - } - if (requestOnOrAfter != null) { - c.add(Restrictions.ge("requestDate", requestOnOrAfter)); - } - if (requestOnOrBefore != null) { - c.add(Restrictions.le("requestDate", requestOnOrBefore)); + if (firstResult != null) { + criteria.setFirstResult(firstResult); } - if (statuses != null && statuses.length > 0) { - c.add(Restrictions.in("status", statuses)); - } - - c.setProjection(Projections.rowCount()); - Long count = (Long) c.uniqueResult(); - c.setProjection(null); - c.addOrder(Order.desc("evaluateCompleteDatetime")); - c.addOrder(Order.desc("evaluateStartDatetime")); - c.addOrder(Order.desc("priority")); - c.addOrder(Order.desc("requestDate")); - - if (pageNumber != null && pageSize != null) { - c.setFirstResult((pageNumber - 1) * pageSize); - c.setMaxResults(pageSize); + if(maxResults != null) { + criteria.setMaxResults(maxResults); } - List reportRequests = c.list(); + return criteria.list(); + } - return new ReportRequestPageDTO(reportRequests, count); + @Override + public long getReportRequestsCount(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Status... statuses) { + final Criteria criteria = createReportRequestsBaseCriteria(reportDefinition, requestOnOrAfter, requestOnOrBefore, statuses); + criteria.setProjection(Projections.rowCount()); + return ((Number) criteria.uniqueResult()).longValue(); } /** @@ -311,5 +273,24 @@ public DbSessionFactory getSessionFactory() { public void setSessionFactory(DbSessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } + + private Criteria createReportRequestsBaseCriteria(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Status... statuses) { + Criteria criteria = sessionFactory.getCurrentSession().createCriteria(ReportRequest.class); + + if (reportDefinition != null) { + criteria.add(Restrictions.eq("reportDefinition.definition", reportDefinition.getUuid())); + } + if (requestOnOrAfter != null) { + criteria.add(Restrictions.ge("requestDate", requestOnOrAfter)); + } + if (requestOnOrBefore != null) { + criteria.add(Restrictions.le("requestDate", requestOnOrBefore)); + } + if (statuses != null && statuses.length > 0) { + criteria.add(Restrictions.in("status", statuses)); + } + + return criteria; + } } diff --git a/api/src/main/java/org/openmrs/module/reporting/report/service/db/ReportDAO.java b/api/src/main/java/org/openmrs/module/reporting/report/service/db/ReportDAO.java index 6837b4ab50..142b464831 100644 --- a/api/src/main/java/org/openmrs/module/reporting/report/service/db/ReportDAO.java +++ b/api/src/main/java/org/openmrs/module/reporting/report/service/db/ReportDAO.java @@ -14,7 +14,6 @@ import org.openmrs.module.reporting.report.ReportProcessorConfiguration; import org.openmrs.module.reporting.report.ReportRequest; import org.openmrs.module.reporting.report.ReportRequest.Status; -import org.openmrs.module.reporting.report.ReportRequestPageDTO; import org.openmrs.module.reporting.report.definition.ReportDefinition; import org.openmrs.module.reporting.report.renderer.ReportRenderer; @@ -118,17 +117,18 @@ public List getReportDesigns(ReportDefinition reportDefinition, Cl * @return the {@link ReportRequest} with the passed uuid */ public ReportRequest getReportRequestByUuid(String uuid); - + /** * @return all {@link ReportRequest} in the system that match the passed parameters + * @see org.openmrs.module.reporting.report.service.ReportService#getReportRequests(ReportDefinition, Date, Date, Integer, Integer, Status...) */ - public List getReportRequests(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer mostRecentNum, Status...statuses); + public List getReportRequests(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer firstResult, Integer maxResults, Status...statuses); /** - * @return {@link ReportRequestPageDTO} object which contains report requests and total count data - * @see org.openmrs.module.reporting.report.service.ReportService#getReportRequestsWithPagination(ReportDefinition, Date, Date, Integer, Integer, Status...) + * @return all {@link ReportRequest} in the system that match the passed parameters + * @see org.openmrs.module.reporting.report.service.ReportService#getReportRequestsCount(ReportDefinition, Date, Date, Status...) */ - ReportRequestPageDTO getReportRequestsWithPagination(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer pageNumber, Integer pageSize, Status...statuses); + public long getReportRequestsCount(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Status...statuses); /** * Deletes the passed {@link ReportRequest} From 5b5e77ee5cb77fec118aa823141c6e820373baee Mon Sep 17 00:00:00 2001 From: Piotr Wargulak Date: Tue, 27 Feb 2024 15:23:44 +0100 Subject: [PATCH 5/5] O3-1494: Adjusted Java Docs --- .../module/reporting/report/service/ReportService.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/api/src/main/java/org/openmrs/module/reporting/report/service/ReportService.java b/api/src/main/java/org/openmrs/module/reporting/report/service/ReportService.java index 7b9df89302..5e4b2400c4 100644 --- a/api/src/main/java/org/openmrs/module/reporting/report/service/ReportService.java +++ b/api/src/main/java/org/openmrs/module/reporting/report/service/ReportService.java @@ -153,7 +153,7 @@ public interface ReportService extends OpenmrsService { * (greater or equal filter), nullable * @param requestOnOrBefore a Date used to limit result to ReportRequests which ware requested on or before * (lower or equal filter), nullable - * @param mostRecentNum maximum number of results, not null + * @param mostRecentNum maximum number of results, a null value means all records are to be returned, nullable * @param statuses an array of Status, used to limit result to ReportRequests with status included in the array, null * or empty array means that all statuses are included, nullable * @return all {@link ReportRequest} in the system that match the passed parameters @@ -171,8 +171,8 @@ public interface ReportService extends OpenmrsService { * (greater or equal filter), nullable * @param requestOnOrBefore a Date used to limit result to ReportRequests which ware requested on or before * (lower or equal filter), nullable - * @param firstResult the first result to be retrieved, not null - * @param maxResults a limit upon the number of Report Requests to be retrieved, not null + * @param firstResult the first result to be retrieved, nullable + * @param maxResults a limit upon the number of Report Requests to be retrieved, nullable * @param statuses an array of Status, used to limit result to ReportRequests with status included in the array, null * or empty array means that all statuses are included, nullable * @return all {@link ReportRequest} in the system that match the passed parameters