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..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 @@ -42,14 +42,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 +524,108 @@ 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 getReportRequests_shouldReturnCorrectReportRequests() { + final ReportService rs = Context.getService(ReportService.class); + final List reportRequests = rs.getReportRequests(null, null, null, 0,2); + + assertEquals(2, reportRequests.size()); + + final List resultUuids = mapToReportRequestUuids(reportRequests); + assertTrue(resultUuids.contains("fce15a1b-4618-4f65-bfe9-8bb60a85c110")); + assertTrue(resultUuids.contains("b0a82b63-1066-4c1d-9b43-b405851fc467")); + } + + @Test + 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 List reportRequests = rs.getReportRequests(testReportDefinition, null, null, 0,2); + + assertEquals(2, reportRequests.size()); + + final List resultUuids = mapToReportRequestUuids(reportRequests); + assertTrue(resultUuids.contains("h8a82b63-1066-4c1d-9b43-b405851fc467")); + assertTrue(resultUuids.contains("b0a82b63-1066-4c1d-9b43-b405851fc467")); + } + + @Test + 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 List reportRequests = rs.getReportRequests(null, from, to, 0,2); + + assertEquals(2, reportRequests.size()); + + final List resultUuids = mapToReportRequestUuids(reportRequests); + assertTrue(resultUuids.contains("b0a82b63-1066-4c1d-9b43-b405851fc467")); + assertTrue(resultUuids.contains("d9a82b63-1066-4c1d-9b43-b405851fc467")); + } + + @Test + public void getReportRequestsCount_shouldReturnCorrectTotalCountForRequestedWithinDatesFilter() { + 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 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, reportRequests.size()); + + 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(); + + 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/service/ReportService.java b/api/src/main/java/org/openmrs/module/reporting/report/service/ReportService.java index fac343a05a..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 @@ -127,6 +127,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 */ @@ -134,12 +144,58 @@ 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, 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 * @should retrieve report requests by definition */ @Transactional(readOnly = true) public List getReportRequests(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer mostRecentNum, Status...statuses); + /** + * 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 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 + * @since 1.27.0 + */ + @Transactional(readOnly = true) + 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 41d9602c06..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 @@ -220,7 +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#getReportRequests(ReportDefinition, Date, Date, Integer, Integer, Status...) + */ + @Override + @Transactional(readOnly = true) + 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); } /** @@ -838,4 +853,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 aabed07c71..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 @@ -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; @@ -187,34 +188,35 @@ 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)); + public List getReportRequests(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer firstResult, Integer maxResults, Status...statuses) { + final Criteria criteria = createReportRequestsBaseCriteria(reportDefinition, requestOnOrAfter, requestOnOrBefore, statuses); + + criteria.addOrder(Order.desc("evaluateCompleteDatetime")); + criteria.addOrder(Order.desc("evaluateStartDatetime")); + criteria.addOrder(Order.desc("priority")); + criteria.addOrder(Order.desc("requestDate")); + + if (firstResult != null) { + criteria.setFirstResult(firstResult); } - 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); + if(maxResults != null) { + criteria.setMaxResults(maxResults); } - return c.list(); + + return criteria.list(); } - + + @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(); + } + /** * @see ReportDAO#purgeReportRequest(ReportRequest) */ @@ -271,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 c36e465c07..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 @@ -117,11 +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 firstResult, Integer maxResults, Status...statuses); + /** * @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...) */ - public List getReportRequests(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Integer mostRecentNum, Status...statuses); + public long getReportRequestsCount(ReportDefinition reportDefinition, Date requestOnOrAfter, Date requestOnOrBefore, Status...statuses); /** * Deletes the passed {@link ReportRequest}