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: use EnrollmentService in TE service to get enrollements/events DHIS2-18541 #19723

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ public List<Enrollment> getEnrollments(@Nonnull Set<UID> uids) throws ForbiddenE
.toList();
}

@Nonnull
@Override
public List<Enrollment> getEnrollments(@Nonnull EnrollmentOperationParams params)
throws ForbiddenException, BadRequestException {
Expand All @@ -255,6 +256,7 @@ public List<Enrollment> getEnrollments(@Nonnull EnrollmentOperationParams params
queryParams.getOrganisationUnitMode());
}

@Nonnull
@Override
public Page<Enrollment> getEnrollments(
@Nonnull EnrollmentOperationParams params, PageParams pageParams)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ public class EnrollmentOperationParams {
@Builder.Default private final Set<UID> orgUnits = new HashSet<>();

/** Selection mode for the specified organisation units. */
private final OrganisationUnitSelectionMode orgUnitMode;
@Builder.Default
private final OrganisationUnitSelectionMode orgUnitMode =
OrganisationUnitSelectionMode.ACCESSIBLE;

/** Enrollments must be enrolled into this program. */
private final UID program;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import static org.hisp.dhis.system.util.SqlUtils.lower;
import static org.hisp.dhis.system.util.SqlUtils.quote;
import static org.hisp.dhis.system.util.SqlUtils.singleQuote;
import static org.hisp.dhis.user.CurrentUserUtil.getCurrentUserDetails;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -94,6 +95,7 @@
import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
import org.hisp.dhis.tracker.TrackerIdScheme;
import org.hisp.dhis.tracker.TrackerIdSchemeParam;
import org.hisp.dhis.tracker.acl.TrackerAccessManager;
import org.hisp.dhis.tracker.export.Order;
import org.hisp.dhis.tracker.export.Page;
import org.hisp.dhis.tracker.export.PageParams;
Expand Down Expand Up @@ -251,6 +253,8 @@ class JdbcEventStore {

private final RelationshipStore relationshipStore;

private final TrackerAccessManager trackerAccessManager;

public List<Event> getEvents(EventQueryParams queryParams) {
return fetchEvents(queryParams, null);
}
Expand Down Expand Up @@ -474,8 +478,11 @@ private List<Event> fetchEvents(EventQueryParams queryParams, PageParams pagePar
List<Relationship> relationships = relationshipStore.getById(relationshipIds);

Multimap<String, RelationshipItem> map = LinkedListMultimap.create();

for (Relationship relationship : relationships) {
if (!trackerAccessManager.canRead(getCurrentUserDetails(), relationship).isEmpty()) {
continue;
}

if (relationship.getFrom().getEvent() != null) {
map.put(relationship.getFrom().getEvent().getUid(), relationship.getFrom());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@
import org.hisp.dhis.trackedentity.TrackedEntityAttributeService;
import org.hisp.dhis.trackedentity.TrackedEntityProgramOwner;
import org.hisp.dhis.trackedentity.TrackedEntityType;
import org.hisp.dhis.trackedentity.TrackedEntityTypeService;
import org.hisp.dhis.trackedentity.TrackedEntityTypeStore;
import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue;
import org.hisp.dhis.tracker.acl.TrackerAccessManager;
import org.hisp.dhis.tracker.audit.TrackedEntityAuditService;
import org.hisp.dhis.tracker.export.FileResourceStream;
import org.hisp.dhis.tracker.export.Page;
import org.hisp.dhis.tracker.export.PageParams;
import org.hisp.dhis.tracker.export.enrollment.EnrollmentOperationParams;
import org.hisp.dhis.tracker.export.enrollment.EnrollmentService;
import org.hisp.dhis.tracker.export.event.EventParams;
import org.hisp.dhis.tracker.export.event.EventService;
Expand All @@ -86,7 +86,7 @@ class DefaultTrackedEntityService implements TrackedEntityService {
private final TrackedEntityAttributeService trackedEntityAttributeService;

private final TrackedEntityTypeStore trackedEntityTypeStore;
private final TrackedEntityTypeService trackedEntityTypeService;

private final AclService aclService;

private final TrackedEntityAuditService trackedEntityAuditService;
Expand Down Expand Up @@ -212,7 +212,7 @@ private static TrackedEntityAttribute getAttribute(
@Nonnull
@Override
public TrackedEntity getTrackedEntity(@Nonnull UID uid)
throws NotFoundException, ForbiddenException {
throws NotFoundException, ForbiddenException, BadRequestException {
return getTrackedEntity(uid, null, TrackedEntityParams.FALSE);
}

Expand All @@ -222,7 +222,7 @@ public TrackedEntity getTrackedEntity(
@Nonnull UID trackedEntityUid,
@CheckForNull UID programIdentifier,
@Nonnull TrackedEntityParams params)
throws NotFoundException, ForbiddenException {
throws NotFoundException, ForbiddenException, BadRequestException {
Program program = null;
if (programIdentifier != null) {
program = programService.getProgram(programIdentifier.getValue());
Expand All @@ -243,7 +243,7 @@ public TrackedEntity getTrackedEntity(
*/
private TrackedEntity getTrackedEntity(
UID uid, Program program, TrackedEntityParams params, UserDetails user)
throws NotFoundException, ForbiddenException {
throws NotFoundException, ForbiddenException, BadRequestException {
TrackedEntity trackedEntity = trackedEntityStore.getByUid(uid.getValue());
if (trackedEntity == null) {
throw new NotFoundException(TrackedEntity.class, uid);
Expand All @@ -270,7 +270,10 @@ private TrackedEntity getTrackedEntity(
}

if (params.isIncludeEnrollments()) {
trackedEntity.setEnrollments(getEnrollments(trackedEntity, user, false, program));
EnrollmentOperationParams enrollmentOperationParams =
mapToEnrollmentParams(uid, program, params);
List<Enrollment> enrollments = enrollmentService.getEnrollments(enrollmentOperationParams);
trackedEntity.setEnrollments(new HashSet<>(enrollments));
}
setRelationshipItems(trackedEntity, trackedEntity, params, false);
if (params.isIncludeProgramOwners()) {
Expand All @@ -281,25 +284,13 @@ private TrackedEntity getTrackedEntity(
return trackedEntity;
}

private Set<Enrollment> getEnrollments(
TrackedEntity trackedEntity, UserDetails user, boolean includeDeleted, Program program) {
return trackedEntity.getEnrollments().stream()
.filter(e -> program == null || program.getUid().equals(e.getProgram().getUid()))
.filter(e -> includeDeleted || !e.isDeleted())
.filter(e -> trackerAccessManager.canRead(user, e, false).isEmpty())
.map(
e -> {
Set<Event> filteredEvents =
e.getEvents().stream()
.filter(
event ->
(includeDeleted || !event.isDeleted())
&& trackerAccessManager.canRead(user, event, false).isEmpty())
.collect(Collectors.toSet());
e.setEvents(filteredEvents);
return e;
})
.collect(Collectors.toSet());
private EnrollmentOperationParams mapToEnrollmentParams(
UID trackedEntity, Program program, TrackedEntityParams params) {
return EnrollmentOperationParams.builder()
.trackedEntity(trackedEntity)
.program(program)
.enrollmentParams(params.getEnrollmentParams())
.build();
}

private static Set<TrackedEntityProgramOwner> getTrackedEntityProgramOwners(
Expand Down Expand Up @@ -346,7 +337,7 @@ public List<TrackedEntity> getTrackedEntities(
throws ForbiddenException, NotFoundException, BadRequestException {
UserDetails user = getCurrentUserDetails();
TrackedEntityQueryParams queryParams = mapper.map(operationParams, user);
final List<Long> ids = trackedEntityStore.getTrackedEntityIds(queryParams);
final List<TrackedEntityIdentifiers> ids = trackedEntityStore.getTrackedEntityIds(queryParams);

return getTrackedEntities(ids, operationParams, queryParams, user);
}
Expand All @@ -357,15 +348,16 @@ public List<TrackedEntity> getTrackedEntities(
throws BadRequestException, ForbiddenException, NotFoundException {
UserDetails user = getCurrentUserDetails();
TrackedEntityQueryParams queryParams = mapper.map(operationParams, user);
final Page<Long> ids = trackedEntityStore.getTrackedEntityIds(queryParams, pageParams);
final Page<TrackedEntityIdentifiers> ids =
trackedEntityStore.getTrackedEntityIds(queryParams, pageParams);

List<TrackedEntity> trackedEntities =
getTrackedEntities(ids.getItems(), operationParams, queryParams, user);
return ids.withItems(trackedEntities);
}

private List<TrackedEntity> getTrackedEntities(
List<Long> ids,
List<TrackedEntityIdentifiers> ids,
TrackedEntityOperationParams operationParams,
TrackedEntityQueryParams queryParams,
UserDetails user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,46 +149,46 @@ public HibernateTrackedEntityStore(
}

@Override
public List<Long> getTrackedEntityIds(TrackedEntityQueryParams params) {
public List<TrackedEntityIdentifiers> getTrackedEntityIds(TrackedEntityQueryParams params) {
String sql = getQuery(params, null);
SqlRowSet rowSet = jdbcTemplate.queryForRowSet(sql);

checkMaxTrackedEntityCountReached(params, rowSet);

List<Long> ids = new ArrayList<>();

List<TrackedEntityIdentifiers> ids = new ArrayList<>();
while (rowSet.next()) {
ids.add(rowSet.getLong("trackedentityid"));
ids.add(
new TrackedEntityIdentifiers(rowSet.getLong("trackedentityid"), rowSet.getString("uid")));
}

return ids;
}

@Override
public Page<Long> getTrackedEntityIds(TrackedEntityQueryParams params, PageParams pageParams) {
public Page<TrackedEntityIdentifiers> getTrackedEntityIds(
TrackedEntityQueryParams params, PageParams pageParams) {
String sql = getQuery(params, pageParams);
SqlRowSet rowSet = jdbcTemplate.queryForRowSet(sql);

checkMaxTrackedEntityCountReached(params, rowSet);

List<Long> ids = new ArrayList<>();

List<TrackedEntityIdentifiers> ids = new ArrayList<>();
while (rowSet.next()) {
ids.add(rowSet.getLong("trackedentityid"));
ids.add(
new TrackedEntityIdentifiers(rowSet.getLong("trackedentityid"), rowSet.getString("uid")));
}

LongSupplier teCount = () -> getTrackedEntityCount(params);
return getPage(pageParams, ids, teCount);
}

private Page<Long> getPage(
PageParams pageParams, List<Long> teIds, LongSupplier enrollmentCount) {
private Page<TrackedEntityIdentifiers> getPage(
PageParams pageParams, List<TrackedEntityIdentifiers> ids, LongSupplier enrollmentCount) {
if (pageParams.isPageTotal()) {
return Page.withTotals(
teIds, pageParams.getPage(), pageParams.getPageSize(), enrollmentCount.getAsLong());
ids, pageParams.getPage(), pageParams.getPageSize(), enrollmentCount.getAsLong());
}

return Page.withoutTotals(teIds, pageParams.getPage(), pageParams.getPageSize());
return Page.withoutTotals(ids, pageParams.getPage(), pageParams.getPageSize());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2004-2022, University of Oslo
* Copyright (c) 2004-2025, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand All @@ -25,28 +25,11 @@
* (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.aggregates.query;

import lombok.AllArgsConstructor;
import lombok.Getter;
package org.hisp.dhis.tracker.export.trackedentity;

/**
* @author Luciano Fiandesio
* Temporary solution: pair of primary key and uid needed by the aggregate store.
*
* @deprecated do not use this class! This is a temporary solution that will be removed.
*/
@Getter
@AllArgsConstructor
class Subselect implements QueryElement {
private String query;

private String alias;

@Override
public String useInSelect() {
return query + " as " + alias;
}

@Override
public String getResultsetValue() {
return alias;
}
}
public record TrackedEntityIdentifiers(Long id, String uid) {}
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ public interface TrackedEntityStore extends IdentifiableObjectStore<TrackedEntit
String ID = TrackedEntityStore.class.getName();

/** Get all tracked entity ids matching given params. */
List<Long> getTrackedEntityIds(TrackedEntityQueryParams params);
List<TrackedEntityIdentifiers> getTrackedEntityIds(TrackedEntityQueryParams params);

/** Get a page of tracked entities matching given params. */
Page<Long> getTrackedEntityIds(TrackedEntityQueryParams params, PageParams pageParams);
Page<TrackedEntityIdentifiers> getTrackedEntityIds(
TrackedEntityQueryParams params, PageParams pageParams);

/**
* Fields the {@link #getTrackedEntityIds(TrackedEntityQueryParams)})} can order tracked entities
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,4 @@ static <T> CompletableFuture<Multimap<String, T>> conditionalAsyncFetch(
? supplyAsync(supplier, executor)
: supplyAsync(ArrayListMultimap::create, executor));
}

/**
* Executes the Supplier asynchronously using the thread pool from the provided {@see Executor}
*
* @param supplier The Supplier to execute
* @return A CompletableFuture with the result of the Supplier
*/
static <T> CompletableFuture<Multimap<String, T>> asyncFetch(
Supplier<Multimap<String, T>> supplier, Executor executor) {
return supplyAsync(supplier, executor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import lombok.Value;
import org.hisp.dhis.tracker.export.trackedentity.TrackedEntityParams;
import org.hisp.dhis.tracker.export.trackedentity.TrackedEntityQueryParams;
import org.hisp.dhis.user.UserDetails;

/**
* @author Luciano Fiandesio
Expand All @@ -48,6 +49,8 @@ class Context {
/** The current user uid */
String userUid;

UserDetails userDetails;

/** A list of group ID to which the user belongs */
List<String> userGroups;

Expand Down
Loading
Loading