Skip to content

Commit

Permalink
chore: Use UID in trackedEntity exporter [DHIS2-17790] (#18913)
Browse files Browse the repository at this point in the history
  • Loading branch information
enricocolasante authored Oct 25, 2024
1 parent 5e4b6bf commit 97ba4db
Show file tree
Hide file tree
Showing 26 changed files with 527 additions and 485 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import lombok.Getter;
import lombok.experimental.Accessors;
import org.hisp.dhis.common.OpenApi;
import org.hisp.dhis.common.UID;
import org.hisp.dhis.webmessage.WebResponse;

@Getter
Expand Down Expand Up @@ -63,6 +64,10 @@ public ForbiddenException(Class<?> type, String uid) {
this("User has no access to " + type.getSimpleName() + ":" + uid);
}

public ForbiddenException(Class<?> type, UID uid) {
this(type, uid.getValue());
}

public ForbiddenException(ErrorCode code, Object... args) {
super(MessageFormat.format(code.getMessage(), args));
this.code = code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public List<Enrollment> getEnrollments(@Nonnull Set<UID> uids) throws ForbiddenE

@Override
public List<Enrollment> getEnrollments(@Nonnull EnrollmentOperationParams params)
throws ForbiddenException, BadRequestException, NotFoundException {
throws ForbiddenException, BadRequestException {
EnrollmentQueryParams queryParams = paramsMapper.map(params, getCurrentUserDetails());

return getEnrollments(
Expand All @@ -243,7 +243,7 @@ public List<Enrollment> getEnrollments(@Nonnull EnrollmentOperationParams params
@Override
public Page<Enrollment> getEnrollments(
@Nonnull EnrollmentOperationParams params, PageParams pageParams)
throws ForbiddenException, BadRequestException, NotFoundException {
throws ForbiddenException, BadRequestException {
EnrollmentQueryParams queryParams = paramsMapper.map(params, getCurrentUserDetails());

Page<Enrollment> enrollmentsPage = enrollmentStore.getEnrollments(queryParams, pageParams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ RelationshipItem getEnrollmentInRelationshipItem(

/** Get all enrollments matching given params. */
List<Enrollment> getEnrollments(EnrollmentOperationParams params)
throws BadRequestException, ForbiddenException, NotFoundException;
throws BadRequestException, ForbiddenException;

/** Get a page of enrollments matching given params. */
Page<Enrollment> getEnrollments(EnrollmentOperationParams params, PageParams pageParams)
throws BadRequestException, ForbiddenException, NotFoundException;
throws BadRequestException, ForbiddenException;

/**
* Get event matching given {@code UID} under the privileges the user in the context. This method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ public RelationshipQueryParams map(@Nonnull RelationshipOperationParams params)

IdentifiableObject entity =
switch (params.getType()) {
case TRACKED_ENTITY ->
trackedEntityService.getTrackedEntity(params.getIdentifier().getValue());
case TRACKED_ENTITY -> trackedEntityService.getTrackedEntity(params.getIdentifier());
case ENROLLMENT -> enrollmentService.getEnrollment(params.getIdentifier());
case EVENT -> eventService.getEvent(params.getIdentifier());
case RELATIONSHIP -> throw new IllegalArgumentException("Unsupported type");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ public Page<TrackedEntityChangeLog> getTrackedEntityChangeLog(
TrackedEntityChangeLogOperationParams operationParams,
PageParams pageParams)
throws NotFoundException, ForbiddenException, BadRequestException {
TrackedEntity trackedEntity =
trackedEntityService.getTrackedEntity(trackedEntityUid.getValue());
TrackedEntity trackedEntity = trackedEntityService.getTrackedEntity(trackedEntityUid);
if (trackedEntity == null) {
throw new NotFoundException(TrackedEntity.class, trackedEntityUid.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.hisp.dhis.changelog.ChangeLogType;
import org.hisp.dhis.common.BaseIdentifiableObject;
import org.hisp.dhis.common.IdentifiableObject;
Expand Down Expand Up @@ -213,7 +212,7 @@ private static TrackedEntityAttribute getAttribute(
}

@Override
public TrackedEntity getTrackedEntity(@Nonnull String uid)
public TrackedEntity getTrackedEntity(@Nonnull UID uid)
throws NotFoundException, ForbiddenException {
UserDetails currentUser = getCurrentUserDetails();
TrackedEntity trackedEntity =
Expand All @@ -229,35 +228,36 @@ public TrackedEntity getTrackedEntity(@Nonnull String uid)

@Override
public TrackedEntity getTrackedEntity(
@Nonnull String uid,
@CheckForNull String programIdentifier,
@Nonnull UID trackedEntityUid,
@CheckForNull UID programIdentifier,
@Nonnull TrackedEntityParams params)
throws NotFoundException, ForbiddenException {
Program program = null;

if (StringUtils.isNotEmpty(programIdentifier)) {
program = programService.getProgram(programIdentifier);
if (programIdentifier != null) {
program = programService.getProgram(programIdentifier.getValue());
if (program == null) {
throw new NotFoundException(Program.class, programIdentifier);
}
}

TrackedEntity trackedEntity;
if (program != null) {
trackedEntity = getTrackedEntity(uid, program, params);
trackedEntity = getTrackedEntity(trackedEntityUid.getValue(), program, params);

if (params.isIncludeProgramOwners()) {
Set<TrackedEntityProgramOwner> filteredProgramOwners =
trackedEntity.getProgramOwners().stream()
.filter(te -> te.getProgram().getUid().equals(programIdentifier))
.filter(te -> te.getProgram().getUid().equals(programIdentifier.getValue()))
.collect(Collectors.toSet());
trackedEntity.setProgramOwners(filteredProgramOwners);
}
} else {
UserDetails userDetails = getCurrentUserDetails();

trackedEntity =
mapTrackedEntity(getTrackedEntity(uid, userDetails), params, userDetails, null, false);
mapTrackedEntity(
getTrackedEntity(trackedEntityUid, userDetails), params, userDetails, null, false);

mapTrackedEntityTypeAttributes(trackedEntity);
}
Expand Down Expand Up @@ -304,9 +304,9 @@ private TrackedEntity getTrackedEntity(String uid, Program program, TrackedEntit
* @throws NotFoundException if TE does not exist
* @throws ForbiddenException if TE is not accessible
*/
private TrackedEntity getTrackedEntity(String uid, UserDetails userDetails)
private TrackedEntity getTrackedEntity(UID uid, UserDetails userDetails)
throws NotFoundException, ForbiddenException {
TrackedEntity trackedEntity = trackedEntityStore.getByUid(uid);
TrackedEntity trackedEntity = trackedEntityStore.getByUid(uid.getValue());
trackedEntityAuditService.addTrackedEntityAudit(trackedEntity, getCurrentUsername(), READ);
if (trackedEntity == null) {
throw new NotFoundException(TrackedEntity.class, uid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ private String getFromSubQueryTrackedEntityConditions(
trackedEntity
.append(whereAnd.whereAnd())
.append("TE.uid IN (")
.append(encodeAndQuote(params.getTrackedEntityUids()))
.append(encodeAndQuote(UID.toValueSet(params.getTrackedEntities())))
.append(") ");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@
import org.hisp.dhis.common.SortDirection;
import org.hisp.dhis.common.UID;
import org.hisp.dhis.event.EventStatus;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.program.EnrollmentStatus;
import org.hisp.dhis.program.Program;
import org.hisp.dhis.program.ProgramStage;
import org.hisp.dhis.trackedentity.TrackedEntity;
import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
import org.hisp.dhis.trackedentity.TrackedEntityType;
import org.hisp.dhis.tracker.export.Order;

@Getter
Expand All @@ -58,16 +64,16 @@ public class TrackedEntityOperationParams {
@Builder.Default private TrackedEntityParams trackedEntityParams = TrackedEntityParams.FALSE;

/** Tracked entity attribute filters per attribute UID. */
@Builder.Default private Map<String, List<QueryFilter>> filters = new HashMap<>();
@Builder.Default private Map<UID, List<QueryFilter>> filters = new HashMap<>();

/**
* Organisation units for which instances in the response were registered at. Is related to the
* specified OrganisationUnitMode.
*/
@Builder.Default private Set<String> organisationUnits = new HashSet<>();
@Builder.Default private Set<UID> organisationUnits = new HashSet<>();

/** Program for which instances in the response must be enrolled in. */
private String programUid;
private UID program;

/** Status of a tracked entities enrollment into a given program. */
private EnrollmentStatus enrollmentStatus;
Expand Down Expand Up @@ -97,18 +103,18 @@ public class TrackedEntityOperationParams {
private Date programIncidentEndDate;

/** Tracked entity type to fetch. */
private String trackedEntityTypeUid;
private UID trackedEntityType;

private OrganisationUnitSelectionMode orgUnitMode;

@Getter @Builder.Default
private AssignedUserQueryParam assignedUserQueryParam = AssignedUserQueryParam.ALL;

/** Set of te uids to explicitly select. */
@Builder.Default private Set<String> trackedEntityUids = new HashSet<>();
@Builder.Default private Set<UID> trackedEntities = new HashSet<>();

/** ProgramStage to be used in conjunction with eventstatus. */
private String programStageUid;
private UID programStage;

/** Status of any events in the specified program. */
private EventStatus eventStatus;
Expand Down Expand Up @@ -185,5 +191,68 @@ public TrackedEntityOperationParamsBuilder orderBy(UID uid, SortDirection direct
this.order.add(new Order(uid, direction));
return this;
}

public TrackedEntityOperationParamsBuilder program(UID uid) {
this.program = uid;
return this;
}

public TrackedEntityOperationParamsBuilder program(Program program) {
this.program = UID.of(program);
return this;
}

public TrackedEntityOperationParamsBuilder programStage(UID uid) {
this.programStage = uid;
return this;
}

public TrackedEntityOperationParamsBuilder programStage(ProgramStage programStage) {
this.programStage = UID.of(programStage);
return this;
}

public TrackedEntityOperationParamsBuilder trackedEntityType(UID uid) {
this.trackedEntityType = uid;
return this;
}

public TrackedEntityOperationParamsBuilder trackedEntityType(
TrackedEntityType trackedEntityType) {
this.trackedEntityType = UID.of(trackedEntityType);
return this;
}

public TrackedEntityOperationParamsBuilder trackedEntities(Set<UID> uids) {
this.trackedEntities$value = uids;
this.trackedEntities$set = true;
return this;
}

public TrackedEntityOperationParamsBuilder trackedEntities(TrackedEntity... trackedEntities) {
this.trackedEntities$value = UID.of(trackedEntities);
this.trackedEntities$set = true;
return this;
}

public TrackedEntityOperationParamsBuilder organisationUnits(Set<UID> uids) {
this.organisationUnits$value = uids;
this.organisationUnits$set = true;
return this;
}

public TrackedEntityOperationParamsBuilder organisationUnits(
OrganisationUnit... organisationUnits) {
this.organisationUnits$value = UID.of(organisationUnits);
this.organisationUnits$set = true;
return this;
}

public TrackedEntityOperationParamsBuilder filter(
TrackedEntityAttribute attribute, List<QueryFilter> queryFilters) {
this.filters$value = Map.of(UID.of(attribute), queryFilters);
this.filters$set = true;
return this;
}
}
}
Loading

0 comments on commit 97ba4db

Please sign in to comment.