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}