Skip to content

Commit

Permalink
chore: Use EventService in EnrollmentService [DHIS2-18791]
Browse files Browse the repository at this point in the history
  • Loading branch information
enricocolasante committed Jan 17, 2025
1 parent 2f1e9ee commit f3e5728
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
import org.hisp.dhis.tracker.acl.TrackerOwnershipManager;
import org.hisp.dhis.tracker.export.Page;
import org.hisp.dhis.tracker.export.PageParams;
import org.hisp.dhis.tracker.export.event.EventOperationParams;
import org.hisp.dhis.tracker.export.event.EventParams;
import org.hisp.dhis.tracker.export.event.EventService;
import org.hisp.dhis.user.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -63,6 +66,8 @@
class DefaultEnrollmentService implements EnrollmentService {
private final EnrollmentStore enrollmentStore;

private final EventService eventService;

private final TrackerOwnershipManager trackerOwnershipAccessManager;

private final TrackedEntityAttributeService trackedEntityAttributeService;
Expand Down Expand Up @@ -130,7 +135,9 @@ private Enrollment getEnrollment(
result.setDeleted(enrollment.isDeleted());
result.setNotes(enrollment.getNotes());
if (params.isIncludeEvents()) {
result.setEvents(getEvents(user, enrollment, includeDeleted));
result.setEvents(
getEvents(
enrollment, params.getEnrollmentEventsParams().getEventParams(), includeDeleted));
}
if (params.isIncludeRelationships()) {
result.setRelationshipItems(getRelationshipItems(user, enrollment, includeDeleted));
Expand All @@ -145,8 +152,7 @@ private Enrollment getEnrollment(
}

@Override
public RelationshipItem getEnrollmentInRelationshipItem(
@Nonnull UID uid, @Nonnull EnrollmentParams params, boolean includeDeleted)
public RelationshipItem getEnrollmentInRelationshipItem(@Nonnull UID uid, boolean includeDeleted)
throws NotFoundException {

RelationshipItem relationshipItem = new RelationshipItem();
Expand All @@ -162,20 +168,29 @@ public RelationshipItem getEnrollmentInRelationshipItem(
return null;
}

relationshipItem.setEnrollment(getEnrollment(enrollment, params, includeDeleted, currentUser));
relationshipItem.setEnrollment(
getEnrollment(
enrollment,
EnrollmentParams.FALSE.withIncludeAttributes(true),
includeDeleted,
currentUser));
return relationshipItem;
}

private Set<Event> getEvents(UserDetails user, Enrollment enrollment, boolean includeDeleted) {
Set<Event> events = new HashSet<>();

for (Event event : enrollment.getEvents()) {
if ((includeDeleted || !event.isDeleted())
&& trackerAccessManager.canRead(user, event, true).isEmpty()) {
events.add(event);
}
private Set<Event> getEvents(
Enrollment enrollment, EventParams eventParams, boolean includeDeleted) {
EventOperationParams eventOperationParams =
EventOperationParams.builder()
.enrollments(Set.of(UID.of(enrollment)))
.eventParams(eventParams)
.includeDeleted(includeDeleted)
.build();
try {
return Set.copyOf(eventService.getEvents(eventOperationParams));
} catch (BadRequestException | ForbiddenException e) {
throw new IllegalArgumentException(
"this must be a bug in how the EventOperationParams are built");
}
return events;
}

private Set<RelationshipItem> getRelationshipItems(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public interface EnrollmentService {
Enrollment getEnrollment(UID uid, EnrollmentParams params, boolean includeDeleted)
throws NotFoundException, ForbiddenException;

RelationshipItem getEnrollmentInRelationshipItem(
UID uid, EnrollmentParams params, boolean includeDeleted) throws NotFoundException;
RelationshipItem getEnrollmentInRelationshipItem(UID uid, boolean includeDeleted)
throws NotFoundException;

/** Get all enrollments matching given params. */
@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,15 @@ private Event getEvent(
return event;
}

@Nonnull
@Override
public List<Event> getEvents(@Nonnull EventOperationParams operationParams)
throws BadRequestException, ForbiddenException {
EventQueryParams queryParams = paramsMapper.map(operationParams, getCurrentUserDetails());
return eventStore.getEvents(queryParams);
}

@Nonnull
@Override
public Page<Event> getEvents(
@Nonnull EventOperationParams operationParams, @Nonnull PageParams pageParams)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public class EventOperationParams {

private UID orgUnit;

private OrganisationUnitSelectionMode orgUnitMode;
@Builder.Default
private OrganisationUnitSelectionMode orgUnitMode = OrganisationUnitSelectionMode.ACCESSIBLE;

private AssignedUserSelectionMode assignedUserMode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
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.EnrollmentParams;
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 Down Expand Up @@ -543,9 +542,7 @@ private RelationshipItem getRelationshipItem(
} else if (item.getEnrollment() != null) {
result =
enrollmentService.getEnrollmentInRelationshipItem(
UID.of(item.getEnrollment()),
EnrollmentParams.TRUE.withIncludeRelationships(false),
includeDeleted);
UID.of(item.getEnrollment()), includeDeleted);
} else if (item.getEvent() != null) {
result =
eventService.getEventInRelationshipItem(
Expand Down

0 comments on commit f3e5728

Please sign in to comment.