diff --git a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityQueryParams.java b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityQueryParams.java index 0ef00f3c51e6..5ebab93624f2 100644 --- a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityQueryParams.java +++ b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentity/TrackedEntityQueryParams.java @@ -469,6 +469,11 @@ public boolean hasTrackedEntityType() { return trackedEntityType != null; } + /** Indicates whether this parameters specifies a max TE limit. */ + public boolean hasMaxTeLimit() { + return maxTeLimit > 0; + } + /** Indicates whether this parameters is of the given organisation unit mode. */ public boolean isOrganisationUnitMode(OrganisationUnitSelectionMode mode) { return orgUnitMode != null && orgUnitMode.equals(mode); diff --git a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityService.java b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityService.java index fa21cf0b6c4c..cfa8e1a964c8 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityService.java +++ b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/DefaultTrackedEntityService.java @@ -679,16 +679,16 @@ public void validateSearchScope(TrackedEntityQueryParams params, boolean isGridS } } - checkIfMaxTeiLimitIsReached(params, maxTeiLimit); params.setMaxTeLimit(maxTeiLimit); + checkIfMaxTeiLimitIsReached(params); } } - private void checkIfMaxTeiLimitIsReached(TrackedEntityQueryParams params, int maxTeiLimit) { - if (maxTeiLimit > 0) { + private void checkIfMaxTeiLimitIsReached(TrackedEntityQueryParams params) { + if (params.hasMaxTeLimit()) { int teCount = trackedEntityStore.getTrackedEntityCountForGridWithMaxTeiLimit(params); - if (teCount > maxTeiLimit) { + if (teCount > params.getMaxTeLimit()) { throw new IllegalQueryException("maxteicountreached"); } } diff --git a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityStore.java b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityStore.java index 49b66cc0b9f0..2910194dd9b5 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityStore.java +++ b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityStore.java @@ -407,8 +407,6 @@ private String getCountQuery(TrackedEntityQueryParams params) { * @return a count SQL query */ private String getCountQueryWithMaxTeiLimit(TrackedEntityQueryParams params) { - boolean hasMaxTeiCountToReturn = - params.hasProgram() && params.getProgram().hasMaxTeiCountToReturn(); return new StringBuilder() .append(getQueryCountSelect(params)) .append(getQuerySelect(params)) @@ -416,10 +414,7 @@ private String getCountQueryWithMaxTeiLimit(TrackedEntityQueryParams params) { .append(getFromSubQuery(params, true, true)) .append(getQueryRelatedTables(params)) .append(getQueryGroupBy(params)) - .append( - hasMaxTeiCountToReturn - ? getLimitClause(params.getProgram().getMaxTeiCountToReturn() + 1) - : "") + .append(params.hasMaxTeLimit() ? getLimitClause(params.getMaxTeLimit() + 1) : "") .append(" ) tecount") .toString(); }