Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: move handling of total count and pagination to store [TECH-1664] #15486

Merged
merged 4 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

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 All @@ -47,6 +40,8 @@
import org.hisp.dhis.relationship.RelationshipType;
import org.hisp.dhis.trackedentity.TrackedEntity;
import org.hisp.dhis.trackedentity.TrackerAccessManager;
import org.hisp.dhis.tracker.export.Page;
import org.hisp.dhis.tracker.export.PageParams;
import org.hisp.dhis.user.CurrentUserService;
import org.hisp.dhis.user.User;
import org.springframework.stereotype.Service;
Expand All @@ -66,42 +61,20 @@ public class DefaultRelationshipService implements RelationshipService {
private final RelationshipOperationParamsMapper mapper;

@Override
public Relationships getRelationships(RelationshipOperationParams params)
public List<Relationship> getRelationships(RelationshipOperationParams params)
throws ForbiddenException, NotFoundException {
RelationshipQueryParams queryParams = mapper.map(params);

Pager pager;
List<Relationship> relationships = getRelationships(queryParams);

if (queryParams.isSkipPaging()) {
return Relationships.withoutPagination(relationships);
}

if (queryParams.isTotalPages()) {
int count = countRelationships(queryParams);
pager =
new Pager(queryParams.getPageWithDefault(), count, queryParams.getPageSizeWithDefault());
} else {
pager = handleLastPageFlag(params, relationships);
}

return Relationships.of(relationships, pager);
return getRelationships(queryParams);
}

private int countRelationships(RelationshipQueryParams queryParams) {
if (queryParams.getEntity() instanceof TrackedEntity te) {
return getRelationshipsByTrackedEntity(te, null).size();
}

if (queryParams.getEntity() instanceof Enrollment en) {
return getRelationshipsByEnrollment(en, null).size();
}

if (queryParams.getEntity() instanceof Event ev) {
return getRelationshipsByEvent(ev, null).size();
}
@Override
public Page<Relationship> getRelationships(
RelationshipOperationParams params, PageParams pageParams)
throws ForbiddenException, NotFoundException {
RelationshipQueryParams queryParams = mapper.map(params);

throw new IllegalArgumentException("Unkown type");
return getRelationships(queryParams, pageParams);
}

@Override
Expand Down Expand Up @@ -131,6 +104,18 @@ public List<Relationship> getRelationshipsByTrackedEntity(
return map(relationships);
}

public Page<Relationship> getRelationshipsByTrackedEntity(
TrackedEntity trackedEntity, RelationshipQueryParams queryParams, PageParams pageParams) {
Page<Relationship> relationshipPage =
relationshipStore.getByTrackedEntity(trackedEntity, queryParams, pageParams);
List<Relationship> relationships =
relationshipPage.getItems().stream()
.filter(
r -> trackerAccessManager.canRead(currentUserService.getCurrentUser(), r).isEmpty())
.toList();
return Page.of(map(relationships), relationshipPage.getPager());
}

public List<Relationship> getRelationshipsByEnrollment(
Enrollment enrollment, RelationshipQueryParams queryParams) {
List<Relationship> relationships =
Expand All @@ -141,6 +126,18 @@ public List<Relationship> getRelationshipsByEnrollment(
return map(relationships);
}

public Page<Relationship> getRelationshipsByEnrollment(
Enrollment enrollment, RelationshipQueryParams queryParams, PageParams pageParams) {
Page<Relationship> relationshipPage =
relationshipStore.getByEnrollment(enrollment, queryParams, pageParams);
List<Relationship> relationships =
relationshipPage.getItems().stream()
.filter(
r -> trackerAccessManager.canRead(currentUserService.getCurrentUser(), r).isEmpty())
.toList();
return Page.of(map(relationships), relationshipPage.getPager());
}

public List<Relationship> getRelationshipsByEvent(
Event event, RelationshipQueryParams queryParams) {
List<Relationship> relationships =
Expand All @@ -151,6 +148,18 @@ public List<Relationship> getRelationshipsByEvent(
return map(relationships);
}

public Page<Relationship> getRelationshipsByEvent(
Event event, RelationshipQueryParams queryParams, PageParams pageParams) {
Page<Relationship> relationshipPage =
relationshipStore.getByEvent(event, queryParams, pageParams);
List<Relationship> relationships =
relationshipPage.getItems().stream()
.filter(
r -> trackerAccessManager.canRead(currentUserService.getCurrentUser(), r).isEmpty())
.toList();
return Page.of(map(relationships), relationshipPage.getPager());
}

private List<Relationship> getRelationships(RelationshipQueryParams queryParams) {
if (queryParams.getEntity() instanceof TrackedEntity te) {
return getRelationshipsByTrackedEntity(te, queryParams);
Expand All @@ -167,6 +176,23 @@ private List<Relationship> getRelationships(RelationshipQueryParams queryParams)
throw new IllegalArgumentException("Unkown type");
}

private Page<Relationship> getRelationships(
RelationshipQueryParams queryParams, PageParams pageParams) {
if (queryParams.getEntity() instanceof TrackedEntity te) {
return getRelationshipsByTrackedEntity(te, queryParams, pageParams);
}

if (queryParams.getEntity() instanceof Enrollment en) {
return getRelationshipsByEnrollment(en, queryParams, pageParams);
}

if (queryParams.getEntity() instanceof Event ev) {
return getRelationshipsByEvent(ev, queryParams, pageParams);
}

throw new IllegalArgumentException("Unkown type");
}

/** Map to a non-proxied Relationship to prevent hibernate exceptions. */
private List<Relationship> map(List<Relationship> relationships) {
List<Relationship> result = new ArrayList<>(relationships.size());
Expand Down Expand Up @@ -214,37 +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 params the request params
* @param relationships the reference to the list of Relationships
* @return the populated SlimPager instance
*/
private Pager handleLastPageFlag(
RelationshipOperationParams params, List<Relationship> relationships) {
Integer originalPage = defaultIfNull(params.getPage(), FIRST_PAGE);
Integer originalPageSize = defaultIfNull(params.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
Loading
Loading