From 5b6282590b00fcc08c3248fe59ee6515b5f4954e Mon Sep 17 00:00:00 2001 From: Enrico Colasante Date: Fri, 27 Oct 2023 10:25:10 +0200 Subject: [PATCH] chore: move handling of total count and pagination to store [TECH-1661] (#15482) * chore: split fetching all TEs from fetching paginated TEs [TECH-1661] Update dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/export/OrderAndPaginationExporterTest.java chore: split fetching all TEs from fetching paginated TEs [TECH-1661] chore: split fetching all TEs from fetching paginated TEs [TECH-1661] * chore: move handling of total count and pagination to store [TECH-1661] * chore: split fetching all relationships from fetching paginated relationships [TECH-1664] * Fix test * Fix code review comments --- .../tracker/export/event/JdbcEventStore.java | 4 - .../export/relationship/Relationships.java | 48 ------------ .../DefaultTrackedEntityService.java | 66 +++-------------- .../HibernateTrackedEntityStore.java | 68 ++++++++++++----- .../export/trackedentity/TrackedEntities.java | 48 ------------ .../TrackedEntityQueryParams.java | 73 ------------------- .../trackedentity/TrackedEntityStore.java | 6 ++ .../TrackedEntitiesExportController.java | 2 + 8 files changed, 69 insertions(+), 246 deletions(-) delete mode 100644 dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/relationship/Relationships.java delete mode 100644 dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/TrackedEntities.java diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/event/JdbcEventStore.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/event/JdbcEventStore.java index d67b27d3c8df..b513c8ef8d51 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/event/JdbcEventStore.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/event/JdbcEventStore.java @@ -266,8 +266,6 @@ private List fetchEvents(EventQueryParams queryParams, PageParams pagePar sql, mapSqlParameterSource, resultSet -> { - log.debug("Event query SQL: " + sql); - Set notes = new HashSet<>(); while (resultSet.next()) { @@ -543,8 +541,6 @@ private int getEventCount(EventQueryParams params) { sql = sql.replaceFirst("limit \\d+ offset \\d+", ""); - log.debug("Event query count SQL: " + sql); - return jdbcTemplate.queryForObject(sql, mapSqlParameterSource, Integer.class); } diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/relationship/Relationships.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/relationship/Relationships.java deleted file mode 100644 index fd469bca5eb2..000000000000 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/relationship/Relationships.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (c) 2004-2022, University of Oslo - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * Neither the name of the HISP project nor the names of its contributors may - * be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package org.hisp.dhis.tracker.export.relationship; - -import java.util.List; -import lombok.EqualsAndHashCode; -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.hisp.dhis.common.Pager; -import org.hisp.dhis.relationship.Relationship; - -@RequiredArgsConstructor(staticName = "of") -@Getter -@EqualsAndHashCode -public class Relationships { - - private final List relationships; - private final Pager pager; - - public static Relationships withoutPagination(List relationships) { - return new Relationships(relationships, null); - } -} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/DefaultTrackedEntityService.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/DefaultTrackedEntityService.java index 8c6e930d9c20..b1b05572139a 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/DefaultTrackedEntityService.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/DefaultTrackedEntityService.java @@ -27,14 +27,10 @@ */ package org.hisp.dhis.tracker.export.trackedentity; -import static org.apache.commons.collections4.CollectionUtils.isNotEmpty; -import static org.apache.commons.lang3.ObjectUtils.defaultIfNull; import static org.hisp.dhis.common.OrganisationUnitSelectionMode.ALL; import static org.hisp.dhis.common.OrganisationUnitSelectionMode.CHILDREN; import static org.hisp.dhis.common.OrganisationUnitSelectionMode.DESCENDANTS; import static org.hisp.dhis.common.OrganisationUnitSelectionMode.SELECTED; -import static org.hisp.dhis.common.Pager.DEFAULT_PAGE_SIZE; -import static org.hisp.dhis.common.SlimPager.FIRST_PAGE; import java.util.ArrayList; import java.util.HashSet; @@ -53,8 +49,6 @@ import org.hisp.dhis.common.AuditType; import org.hisp.dhis.common.BaseIdentifiableObject; import org.hisp.dhis.common.IllegalQueryException; -import org.hisp.dhis.common.Pager; -import org.hisp.dhis.common.SlimPager; import org.hisp.dhis.feedback.BadRequestException; import org.hisp.dhis.feedback.ForbiddenException; import org.hisp.dhis.feedback.NotFoundException; @@ -311,7 +305,6 @@ private RelationshipItem withNestedEntity( public List getTrackedEntities(TrackedEntityOperationParams operationParams) throws ForbiddenException, NotFoundException, BadRequestException { TrackedEntityQueryParams queryParams = mapper.map(operationParams); - queryParams.setSkipPaging(true); final List ids = getTrackedEntityIds(queryParams); List trackedEntities = @@ -333,15 +326,11 @@ public Page getTrackedEntities( TrackedEntityOperationParams operationParams, PageParams pageParams) throws BadRequestException, ForbiddenException, NotFoundException { TrackedEntityQueryParams queryParams = mapper.map(operationParams); - queryParams.setPage(pageParams.getPage()); - queryParams.setPageSize(pageParams.getPageSize()); - queryParams.setTotalPages(pageParams.isPageTotal()); - queryParams.setSkipPaging(false); - final List ids = getTrackedEntityIds(queryParams); + final Page ids = getTrackedEntityIds(queryParams, pageParams); List trackedEntities = this.trackedEntityAggregate.find( - ids, operationParams.getTrackedEntityParams(), queryParams); + ids.getItems(), operationParams.getTrackedEntityParams(), queryParams); mapRelationshipItems( trackedEntities, @@ -350,16 +339,7 @@ public Page getTrackedEntities( addSearchAudit(trackedEntities, queryParams.getUser()); - Pager pager; - - if (pageParams.isPageTotal()) { - int count = getTrackedEntityCount(queryParams, true, true); - pager = new Pager(pageParams.getPage(), count, pageParams.getPageSize()); - } else { - pager = handleLastPageFlag(queryParams, trackedEntities); - } - - return Page.of(trackedEntities, pager); + return Page.of(trackedEntities, ids.getPager()); } public List getTrackedEntityIds(TrackedEntityQueryParams params) { @@ -370,6 +350,14 @@ public List getTrackedEntityIds(TrackedEntityQueryParams params) { return trackedEntityStore.getTrackedEntityIds(params); } + public Page getTrackedEntityIds(TrackedEntityQueryParams params, PageParams pageParams) { + decideAccess(params); + validate(params); + validateSearchScope(params); + + return trackedEntityStore.getTrackedEntityIds(params, pageParams); + } + public void decideAccess(TrackedEntityQueryParams params) { if (params.isOrganisationUnitMode(ALL) && !currentUserService.currentUserIsAuthorized( @@ -769,38 +757,6 @@ private void addTrackedEntityAudit(TrackedEntity trackedEntity, String user) { } } - /** - * This method will apply the logic related to the parameter 'totalPages=false'. This works in - * conjunction with the method: {@link - * TrackedEntityStore#getTrackedEntityIds(TrackedEntityQueryParams)} - * - *

This is needed because we need to query (pageSize + 1) at DB level. The resulting query will - * allow us to evaluate if we are in the last page or not. And this is what his method does, - * returning the respective Pager object. - * - * @param params the request params - * @param trackedEntityList the reference to the list of Tracked Entities - * @return the populated SlimPager instance - */ - private Pager handleLastPageFlag( - TrackedEntityQueryParams params, List trackedEntityList) { - Integer originalPage = defaultIfNull(params.getPage(), FIRST_PAGE); - Integer originalPageSize = defaultIfNull(params.getPageSize(), DEFAULT_PAGE_SIZE); - boolean isLastPage = false; - - if (isNotEmpty(trackedEntityList)) { - isLastPage = trackedEntityList.size() <= originalPageSize; - if (!isLastPage) { - // Get the same number of elements of the pageSize, forcing - // the removal of the last additional element added at querying - // time. - trackedEntityList.retainAll(trackedEntityList.subList(0, originalPageSize)); - } - } - - return new SlimPager(originalPage, originalPageSize, isLastPage); - } - @Override public Set getOrderableFields() { return trackedEntityStore.getOrderableFields(); diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/HibernateTrackedEntityStore.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/HibernateTrackedEntityStore.java index 8a91a35a55f6..b5bd958725d1 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/HibernateTrackedEntityStore.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/HibernateTrackedEntityStore.java @@ -43,6 +43,7 @@ import java.util.Map; import java.util.Set; import java.util.function.Function; +import java.util.function.IntSupplier; import java.util.stream.Collectors; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.Predicate; @@ -54,6 +55,7 @@ import org.hisp.dhis.common.IdentifiableObject; import org.hisp.dhis.common.IllegalQueryException; import org.hisp.dhis.common.OrganisationUnitSelectionMode; +import org.hisp.dhis.common.Pager; import org.hisp.dhis.common.QueryFilter; import org.hisp.dhis.common.hibernate.SoftDeleteHibernateObjectStore; import org.hisp.dhis.commons.collection.CollectionUtils; @@ -68,6 +70,8 @@ import org.hisp.dhis.trackedentity.TrackedEntity; import org.hisp.dhis.trackedentity.TrackedEntityAttribute; import org.hisp.dhis.tracker.export.Order; +import org.hisp.dhis.tracker.export.Page; +import org.hisp.dhis.tracker.export.PageParams; import org.hisp.dhis.user.CurrentUserService; import org.hisp.dhis.util.DateUtils; import org.springframework.context.ApplicationEventPublisher; @@ -161,8 +165,7 @@ public HibernateTrackedEntityStore( @Override public List getTrackedEntityIds(TrackedEntityQueryParams params) { - String sql = getQuery(params); - log.debug("Tracked entity query SQL: " + sql); + String sql = getQuery(params, null); SqlRowSet rowSet = jdbcTemplate.queryForRowSet(sql); checkMaxTrackedEntityCountReached(params, rowSet); @@ -176,6 +179,35 @@ public List getTrackedEntityIds(TrackedEntityQueryParams params) { return ids; } + @Override + public Page getTrackedEntityIds(TrackedEntityQueryParams params, PageParams pageParams) { + String sql = getQuery(params, pageParams); + SqlRowSet rowSet = jdbcTemplate.queryForRowSet(sql); + + checkMaxTrackedEntityCountReached(params, rowSet); + + List ids = new ArrayList<>(); + + while (rowSet.next()) { + ids.add(rowSet.getLong("trackedentityid")); + } + + IntSupplier teCount = () -> getTrackedEntityCount(params); + return getPage(pageParams, ids, teCount); + } + + private Page getPage(PageParams pageParams, List teIds, IntSupplier enrollmentCount) { + if (pageParams.isPageTotal()) { + Pager pager = + new Pager(pageParams.getPage(), enrollmentCount.getAsInt(), pageParams.getPageSize()); + return Page.of(teIds, pager); + } + + Pager pager = new Pager(pageParams.getPage(), 0, pageParams.getPageSize()); + pager.force(pageParams.getPage(), pageParams.getPageSize()); + return Page.of(teIds, pager); + } + @Override public Set getOrderableFields() { return ORDERABLE_FIELDS.keySet(); @@ -201,14 +233,12 @@ private void checkMaxTrackedEntityCountReached( @Override public int getTrackedEntityCount(TrackedEntityQueryParams params) { String sql = getCountQuery(params); - log.debug("Tracked entity count SQL: " + sql); return jdbcTemplate.queryForObject(sql, Integer.class); } @Override public int getTrackedEntityCountWithMaxTrackedEntityLimit(TrackedEntityQueryParams params) { String sql = getCountQueryWithMaxTrackedEntityLimit(params); - log.debug("Tracked entity count SQL: " + sql); return jdbcTemplate.queryForObject(sql, Integer.class); } @@ -266,11 +296,11 @@ public int getTrackedEntityCountWithMaxTrackedEntityLimit(TrackedEntityQueryPara * @param params params defining the query * @return SQL string */ - private String getQuery(TrackedEntityQueryParams params) { + private String getQuery(TrackedEntityQueryParams params, PageParams pageParams) { StringBuilder stringBuilder = new StringBuilder(getQuerySelect(params)); return stringBuilder .append("FROM ") - .append(getFromSubQuery(params, false)) + .append(getFromSubQuery(params, false, pageParams)) .append(getQueryRelatedTables(params)) .append(getQueryOrderBy(params, false)) .toString(); @@ -287,7 +317,7 @@ private String getCountQuery(TrackedEntityQueryParams params) { return SELECT_COUNT_INSTANCE_FROM + getQuerySelect(params) + "FROM " - + getFromSubQuery(params, true) + + getFromSubQuery(params, true, null) + getQueryRelatedTables(params) + " ) tecount"; } @@ -303,7 +333,7 @@ private String getCountQueryWithMaxTrackedEntityLimit(TrackedEntityQueryParams p return SELECT_COUNT_INSTANCE_FROM + getQuerySelect(params) + "FROM " - + getFromSubQuery(params, true) + + getFromSubQuery(params, true, null) + getQueryRelatedTables(params) + (params.getProgram().getMaxTeiCountToReturn() > 0 ? getLimitClause(params.getProgram().getMaxTeiCountToReturn() + 1) @@ -352,7 +382,8 @@ private String getQuerySelect(TrackedEntityQueryParams params) { * * @return an SQL sub-query */ - private String getFromSubQuery(TrackedEntityQueryParams params, boolean isCountQuery) { + private String getFromSubQuery( + TrackedEntityQueryParams params, boolean isCountQuery, PageParams pageParams) { SqlHelper whereAnd = new SqlHelper(true); StringBuilder fromSubQuery = new StringBuilder() @@ -378,7 +409,7 @@ private String getFromSubQuery(TrackedEntityQueryParams params, boolean isCountQ fromSubQuery .append(getQueryOrderBy(params, true)) // LIMIT, OFFSET - .append(getFromSubQueryLimitAndOffset(params)); + .append(getFromSubQueryLimitAndOffset(params, pageParams)); } return fromSubQuery.append(") TE ").toString(); @@ -980,12 +1011,13 @@ private String getQueryOrderBy(TrackedEntityQueryParams params, boolean innerOrd * * @return a SQL LIMIT and OFFSET clause, or empty string if no LIMIT can be deducted. */ - private String getFromSubQueryLimitAndOffset(TrackedEntityQueryParams params) { + private String getFromSubQueryLimitAndOffset( + TrackedEntityQueryParams params, PageParams pageParams) { StringBuilder limitOffset = new StringBuilder(); int limit = params.getMaxTeLimit(); int teQueryLimit = systemSettingManager.getIntSetting(SettingKey.TRACKED_ENTITY_MAX_LIMIT); - if (limit == 0 && !params.isPaging()) { + if (limit == 0 && pageParams == null) { if (teQueryLimit > 0) { return limitOffset .append(LIMIT) @@ -996,26 +1028,26 @@ private String getFromSubQueryLimitAndOffset(TrackedEntityQueryParams params) { } return limitOffset.toString(); - } else if (limit == 0 && params.isPaging()) { + } else if (limit == 0 && pageParams != null) { return limitOffset .append(LIMIT) .append(SPACE) - .append(params.getPageSizeWithDefault()) + .append(pageParams.getPageSize()) .append(SPACE) .append(OFFSET) .append(SPACE) - .append(params.getOffset()) + .append((pageParams.getPage() - 1) * pageParams.getPageSize()) .append(SPACE) .toString(); - } else if (params.isPaging()) { + } else if (pageParams != null) { return limitOffset .append(LIMIT) .append(SPACE) - .append(Math.min(limit + 1, params.getPageSizeWithDefault())) + .append(Math.min(limit + 1, pageParams.getPageSize())) .append(SPACE) .append(OFFSET) .append(SPACE) - .append(params.getOffset()) + .append((pageParams.getPage() - 1) * pageParams.getPageSize()) .append(SPACE) .toString(); } else { diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/TrackedEntities.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/TrackedEntities.java deleted file mode 100644 index 664dc4029030..000000000000 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/TrackedEntities.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (c) 2004-2022, University of Oslo - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * Neither the name of the HISP project nor the names of its contributors may - * be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package org.hisp.dhis.tracker.export.trackedentity; - -import java.util.List; -import lombok.EqualsAndHashCode; -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.hisp.dhis.common.Pager; -import org.hisp.dhis.trackedentity.TrackedEntity; - -@RequiredArgsConstructor(staticName = "of") -@Getter -@EqualsAndHashCode -public class TrackedEntities { - - private final List trackedEntities; - private final Pager pager; - - public static TrackedEntities withoutPagination(List trackedEntities) { - return new TrackedEntities(trackedEntities, null); - } -} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/TrackedEntityQueryParams.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/TrackedEntityQueryParams.java index 6ff1fc60719f..d5ed783e5c1e 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/TrackedEntityQueryParams.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/TrackedEntityQueryParams.java @@ -61,11 +61,6 @@ @ToString public class TrackedEntityQueryParams { - - public static final int DEFAULT_PAGE = 1; - - public static final int DEFAULT_PAGE_SIZE = 50; - /** * Each attribute will affect the final SQL query. Some attributes are filtered on, while * attributes added via {@link #orderBy(TrackedEntityAttribute, SortDirection)} will be ordered @@ -137,18 +132,6 @@ public class TrackedEntityQueryParams { /** End date for event for the given program. */ private Date eventEndDate; - /** Page number. */ - private Integer page; - - /** Page size. */ - private Integer pageSize; - - /** Indicates whether to include the total number of pages in the paging response. */ - private boolean totalPages; - - /** Indicates whether paging should be skipped. */ - private boolean skipPaging; - /** Indicates if there is a maximum te retrieval limit. 0 no limit. */ private int maxTeLimit; @@ -343,26 +326,6 @@ public boolean hasUniqueFilter() { return false; } - /** Indicates whether paging is enabled. */ - public boolean isPaging() { - return !isSkipPaging(); - } - - /** Returns the page number, falls back to default value of 1 if not specified. */ - public int getPageWithDefault() { - return page != null && page > 0 ? page : DEFAULT_PAGE; - } - - /** Returns the page size, falls back to default value of 50 if not specified. */ - public int getPageSizeWithDefault() { - return pageSize != null && pageSize >= 0 ? pageSize : DEFAULT_PAGE_SIZE; - } - - /** Returns the offset based on the page number and page size. */ - public int getOffset() { - return (getPageWithDefault() - 1) * getPageSizeWithDefault(); - } - /** Returns attributes that are either ordered by or present in any filter. */ public Set getAttributes() { return SetUtils.union(filters.keySet(), getOrderAttributes()); @@ -563,42 +526,6 @@ public TrackedEntityQueryParams setEventEndDate(Date eventEndDate) { return this; } - public Integer getPage() { - return page; - } - - public TrackedEntityQueryParams setPage(Integer page) { - this.page = page; - return this; - } - - public Integer getPageSize() { - return pageSize; - } - - public TrackedEntityQueryParams setPageSize(Integer pageSize) { - this.pageSize = pageSize; - return this; - } - - public boolean isTotalPages() { - return totalPages; - } - - public TrackedEntityQueryParams setTotalPages(boolean totalPages) { - this.totalPages = totalPages; - return this; - } - - public boolean isSkipPaging() { - return skipPaging; - } - - public TrackedEntityQueryParams setSkipPaging(boolean skipPaging) { - this.skipPaging = skipPaging; - return this; - } - public int getMaxTeLimit() { return maxTeLimit; } diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/TrackedEntityStore.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/TrackedEntityStore.java index c762028bc1fb..53249d6d933f 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/TrackedEntityStore.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/TrackedEntityStore.java @@ -31,12 +31,18 @@ import java.util.Set; import org.hisp.dhis.common.IdentifiableObjectStore; import org.hisp.dhis.trackedentity.TrackedEntity; +import org.hisp.dhis.tracker.export.Page; +import org.hisp.dhis.tracker.export.PageParams; interface TrackedEntityStore extends IdentifiableObjectStore { String ID = TrackedEntityStore.class.getName(); + /** Get all tracked entity ids matching given params. */ List getTrackedEntityIds(TrackedEntityQueryParams params); + /** Get a page of tracked entities matching given params. */ + Page getTrackedEntityIds(TrackedEntityQueryParams params, PageParams pageParams); + /** * Fields the {@link #getTrackedEntityIds(TrackedEntityQueryParams)})} can order tracked entities * by. Ordering by fields other than these is considered a programmer error. Validation of user diff --git a/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/export/trackedentity/TrackedEntitiesExportController.java b/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/export/trackedentity/TrackedEntitiesExportController.java index 009e8753fb2d..8d65802c7405 100644 --- a/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/export/trackedentity/TrackedEntitiesExportController.java +++ b/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/export/trackedentity/TrackedEntitiesExportController.java @@ -30,6 +30,7 @@ import static org.hisp.dhis.common.OpenApi.Response.Status; import static org.hisp.dhis.webapi.controller.tracker.ControllerSupport.RESOURCE_PATH; import static org.hisp.dhis.webapi.controller.tracker.ControllerSupport.assertUserOrderableFieldsAreSupported; +import static org.hisp.dhis.webapi.controller.tracker.export.RequestParamsValidator.validatePaginationParameters; import static org.hisp.dhis.webapi.controller.tracker.export.trackedentity.RequestParams.DEFAULT_FIELDS_PARAM; import static org.hisp.dhis.webapi.utils.ContextUtils.CONTENT_TYPE_CSV; import static org.hisp.dhis.webapi.utils.ContextUtils.CONTENT_TYPE_CSV_GZIP; @@ -133,6 +134,7 @@ public TrackedEntitiesExportController( PagingWrapper getTrackedEntities( RequestParams requestParams, @CurrentUser User currentUser) throws BadRequestException, ForbiddenException, NotFoundException { + validatePaginationParameters(requestParams); TrackedEntityOperationParams operationParams = paramsMapper.map(requestParams, currentUser); if (requestParams.isPaged()) { PageParams pageParams =