Skip to content

Commit

Permalink
Merge branch 'master' into DHIS2-15679_fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaCambi77 committed Oct 27, 2023
2 parents a42e309 + d61c516 commit 17473dc
Show file tree
Hide file tree
Showing 16 changed files with 160 additions and 251 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,10 @@ public boolean isUsedInQueue() {
}

public boolean isRunOnce() {
return cronExpression == null && delay == null && queueName == null;
return schedulingType == SchedulingType.ONCE_ASAP
&& cronExpression == null
&& delay == null
&& queueName == null;
}

public boolean isDueBetween(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,6 @@ private List<Event> fetchEvents(EventQueryParams queryParams, PageParams pagePar
sql,
mapSqlParameterSource,
resultSet -> {
log.debug("Event query SQL: " + sql);

Set<String> notes = new HashSet<>();

while (resultSet.next()) {
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,10 @@
*/
package org.hisp.dhis.tracker.export.relationship;

import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;
import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
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.List;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.hisp.dhis.common.Pager;
import org.hisp.dhis.common.SlimPager;
import org.hisp.dhis.feedback.ForbiddenException;
import org.hisp.dhis.feedback.NotFoundException;
import org.hisp.dhis.program.Enrollment;
Expand Down Expand Up @@ -247,35 +240,6 @@ private RelationshipItem withNestedEntity(RelationshipItem item) {
return result;
}

/**
* This method will apply the logic related to the parameter 'totalPages=false'. This works in
* conjunction with methods in : {@link RelationshipService}
*
* <p>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 relationships the reference to the list of Relationships
* @return the populated SlimPager instance
*/
private Pager handleLastPageFlag(List<Relationship> relationships, PageParams pageParams) {
Integer originalPage = defaultIfNull(pageParams.getPage(), FIRST_PAGE);
Integer originalPageSize = defaultIfNull(pageParams.getPageSize(), DEFAULT_PAGE_SIZE);
boolean isLastPage = false;

if (isNotEmpty(relationships)) {
isLastPage = relationships.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.
relationships.retainAll(relationships.subList(0, originalPageSize));
}
}

return new SlimPager(originalPage, originalPageSize, isLastPage);
}

@Override
public Set<String> getOrderableFields() {
return relationshipStore.getOrderableFields();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import java.util.Set;
import java.util.function.Function;
import java.util.function.IntSupplier;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
Expand Down Expand Up @@ -121,7 +121,9 @@ public List<Relationship> getByEvent(Event event, RelationshipQueryParams queryP

@Override
public Page<Relationship> getByTrackedEntity(
TrackedEntity trackedEntity, RelationshipQueryParams queryParams, PageParams pageParams) {
TrackedEntity trackedEntity,
RelationshipQueryParams queryParams,
@Nonnull PageParams pageParams) {
TypedQuery<Relationship> relationshipTypedQuery =
getRelationshipTypedQuery(trackedEntity, queryParams, pageParams);

Expand All @@ -131,7 +133,7 @@ public Page<Relationship> getByTrackedEntity(

@Override
public Page<Relationship> getByEnrollment(
Enrollment enrollment, RelationshipQueryParams queryParams, PageParams pageParams) {
Enrollment enrollment, RelationshipQueryParams queryParams, @Nonnull PageParams pageParams) {
TypedQuery<Relationship> relationshipTypedQuery =
getRelationshipTypedQuery(enrollment, queryParams, pageParams);

Expand All @@ -141,7 +143,7 @@ public Page<Relationship> getByEnrollment(

@Override
public Page<Relationship> getByEvent(
Event event, RelationshipQueryParams queryParams, PageParams pageParams) {
Event event, RelationshipQueryParams queryParams, @Nonnull PageParams pageParams) {
TypedQuery<Relationship> relationshipTypedQuery =
getRelationshipTypedQuery(event, queryParams, pageParams);

Expand Down Expand Up @@ -232,9 +234,7 @@ private TypedQuery<Relationship> getRelationshipTypedQuery(
newJpaParameters(queryParams, pageParams, builder);

relationshipItemCriteriaQuery.orderBy(
jpaQueryParameters.getOrders().stream()
.map(o -> o.apply(root))
.collect(Collectors.toList()));
jpaQueryParameters.getOrders().stream().map(o -> o.apply(root)).toList());

TypedQuery<Relationship> relationshipTypedQuery =
getSession().createQuery(relationshipItemCriteriaQuery);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -311,7 +305,6 @@ private RelationshipItem withNestedEntity(
public List<TrackedEntity> getTrackedEntities(TrackedEntityOperationParams operationParams)
throws ForbiddenException, NotFoundException, BadRequestException {
TrackedEntityQueryParams queryParams = mapper.map(operationParams);
queryParams.setSkipPaging(true);
final List<Long> ids = getTrackedEntityIds(queryParams);

List<TrackedEntity> trackedEntities =
Expand All @@ -333,15 +326,11 @@ public Page<TrackedEntity> 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<Long> ids = getTrackedEntityIds(queryParams);
final Page<Long> ids = getTrackedEntityIds(queryParams, pageParams);

List<TrackedEntity> trackedEntities =
this.trackedEntityAggregate.find(
ids, operationParams.getTrackedEntityParams(), queryParams);
ids.getItems(), operationParams.getTrackedEntityParams(), queryParams);

mapRelationshipItems(
trackedEntities,
Expand All @@ -350,16 +339,7 @@ public Page<TrackedEntity> 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<Long> getTrackedEntityIds(TrackedEntityQueryParams params) {
Expand All @@ -370,6 +350,14 @@ public List<Long> getTrackedEntityIds(TrackedEntityQueryParams params) {
return trackedEntityStore.getTrackedEntityIds(params);
}

public Page<Long> 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(
Expand Down Expand Up @@ -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)}
*
* <p>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<TrackedEntity> 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<String> getOrderableFields() {
return trackedEntityStore.getOrderableFields();
Expand Down
Loading

0 comments on commit 17473dc

Please sign in to comment.