Skip to content

Commit

Permalink
chore: Use UID in enrollment exporter [DHIS2-17790]
Browse files Browse the repository at this point in the history
  • Loading branch information
enricocolasante committed Oct 23, 2024
1 parent e1ef244 commit a695d80
Show file tree
Hide file tree
Showing 29 changed files with 180 additions and 151 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.BasicWebMessage;

@Getter
Expand All @@ -58,6 +59,10 @@ public NotFoundException(Class<?> type, String uid) {
this(type.getSimpleName() + " with id " + uid + " could not be found.");
}

public NotFoundException(Class<?> type, UID uid) {
this(type.getSimpleName() + " with id " + uid + " could not be found.");
}

public NotFoundException(String message) {
super(message);
this.code = ErrorCode.E1005;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import lombok.RequiredArgsConstructor;
import org.hisp.dhis.common.CodeGenerator;
import org.hisp.dhis.common.IdentifiableObject;
import org.hisp.dhis.common.UID;
import org.hisp.dhis.commons.collection.ListUtils;
import org.hisp.dhis.feedback.ForbiddenException;
import org.hisp.dhis.feedback.NotFoundException;
Expand Down Expand Up @@ -281,7 +282,8 @@ public String getUserAccessErrors(
return "Missing data write access to one or more Relationship Types.";
}

List<Enrollment> enrollments = enrollmentService.getEnrollments(mergeObject.getEnrollments());
List<Enrollment> enrollments =
enrollmentService.getEnrollments(UID.of(mergeObject.getEnrollments()));

if (enrollments.stream()
.anyMatch(e -> !aclService.canDataWrite(currentUserDetails, e.getProgram()))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import javax.annotation.Nonnull;
import lombok.RequiredArgsConstructor;
import org.hisp.dhis.common.OrganisationUnitSelectionMode;
import org.hisp.dhis.common.UID;
import org.hisp.dhis.feedback.BadRequestException;
import org.hisp.dhis.feedback.ForbiddenException;
import org.hisp.dhis.feedback.NotFoundException;
Expand Down Expand Up @@ -71,17 +72,16 @@ class DefaultEnrollmentService implements EnrollmentService {
private final EnrollmentOperationParamsMapper paramsMapper;

@Override
public Enrollment getEnrollment(@Nonnull String uid)
throws ForbiddenException, NotFoundException {
public Enrollment getEnrollment(@Nonnull UID uid) throws ForbiddenException, NotFoundException {
return getEnrollment(uid, EnrollmentParams.FALSE, false);
}

@Override
public Enrollment getEnrollment(
@Nonnull String uid, @Nonnull EnrollmentParams params, boolean includeDeleted)
@Nonnull UID uid, @Nonnull EnrollmentParams params, boolean includeDeleted)
throws NotFoundException, ForbiddenException {
UserDetails currentUser = getCurrentUserDetails();
Enrollment enrollment = enrollmentStore.getByUid(uid);
Enrollment enrollment = enrollmentStore.getByUid(uid.getValue());

if (enrollment == null) {
throw new NotFoundException(Enrollment.class, uid);
Expand Down Expand Up @@ -146,11 +146,11 @@ private Enrollment getEnrollment(

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

RelationshipItem relationshipItem = new RelationshipItem();
Enrollment enrollment = enrollmentStore.getByUid(uid);
Enrollment enrollment = enrollmentStore.getByUid(uid.getValue());

if (enrollment == null) {
throw new NotFoundException(Enrollment.class, uid);
Expand Down Expand Up @@ -211,8 +211,8 @@ private Set<TrackedEntityAttributeValue> getTrackedEntityAttributeValues(
}

@Override
public List<Enrollment> getEnrollments(@Nonnull List<String> uids) throws ForbiddenException {
List<Enrollment> enrollments = enrollmentStore.getByUid(uids);
public List<Enrollment> getEnrollments(@Nonnull Set<UID> uids) throws ForbiddenException {
List<Enrollment> enrollments = enrollmentStore.getByUid(UID.toValueList(uids));
UserDetails user = getCurrentUserDetails();
List<String> errors =
enrollments.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import lombok.RequiredArgsConstructor;
import org.hisp.dhis.common.OrganisationUnitSelectionMode;
import org.hisp.dhis.common.SortDirection;
import org.hisp.dhis.common.UID;
import org.hisp.dhis.program.EnrollmentStatus;
import org.hisp.dhis.tracker.export.Order;

Expand All @@ -50,7 +51,7 @@ public class EnrollmentOperationParams {
@Builder.Default private final EnrollmentParams enrollmentParams = EnrollmentParams.FALSE;

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

/** Last updated for enrollment. */
private final Date lastUpdated;
Expand All @@ -62,13 +63,13 @@ public class EnrollmentOperationParams {
* Organisation units for which instances in the response were registered at. Is related to the
* specified OrganisationUnitMode.
*/
@Builder.Default private final Set<String> orgUnitUids = new HashSet<>();
@Builder.Default private final Set<UID> orgUnitUids = new HashSet<>();

/** Selection mode for the specified organisation units. */
private final OrganisationUnitSelectionMode orgUnitMode;

/** Enrollments must be enrolled into this program. */
private final String programUid;
private final UID programUid;

/** Status of a tracked entities enrollment into a given program. */
private final EnrollmentStatus enrollmentStatus;
Expand All @@ -83,10 +84,10 @@ public class EnrollmentOperationParams {
private final Date programEndDate;

/** Tracked entity type of the tracked entity in the response. */
private final String trackedEntityTypeUid;
private final UID trackedEntityTypeUid;

/** Tracked entity. */
private final String trackedEntityUid;
private final UID trackedEntityUid;

/** Indicates whether to include soft-deleted enrollments */
private final boolean includeDeleted;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
import static org.hisp.dhis.common.OrganisationUnitSelectionMode.CAPTURE;
import static org.hisp.dhis.common.OrganisationUnitSelectionMode.DESCENDANTS;
import static org.hisp.dhis.tracker.export.OperationsParamsValidator.validateOrgUnitMode;
import static org.hisp.dhis.util.ObjectUtils.applyIfNotNull;

import java.util.HashSet;
import java.util.Set;
import javax.annotation.Nonnull;
import lombok.RequiredArgsConstructor;
import org.hisp.dhis.common.UID;
import org.hisp.dhis.feedback.BadRequestException;
import org.hisp.dhis.feedback.ForbiddenException;
import org.hisp.dhis.organisationunit.OrganisationUnit;
Expand Down Expand Up @@ -63,14 +65,18 @@ class EnrollmentOperationParamsMapper {
public EnrollmentQueryParams map(
@Nonnull EnrollmentOperationParams operationParams, @Nonnull UserDetails user)
throws BadRequestException, ForbiddenException {
Program program = paramsValidator.validateTrackerProgram(operationParams.getProgramUid(), user);
Program program =
paramsValidator.validateTrackerProgram(
applyIfNotNull(operationParams.getProgramUid(), UID::getValue), user);
TrackedEntityType trackedEntityType =
paramsValidator.validateTrackedEntityType(operationParams.getTrackedEntityTypeUid(), user);
paramsValidator.validateTrackedEntityType(
applyIfNotNull(operationParams.getTrackedEntityTypeUid(), UID::getValue), user);
TrackedEntity trackedEntity =
paramsValidator.validateTrackedEntity(operationParams.getTrackedEntityUid(), user);
paramsValidator.validateTrackedEntity(
applyIfNotNull(operationParams.getTrackedEntityUid(), UID::getValue), user);

Set<OrganisationUnit> orgUnits =
paramsValidator.validateOrgUnits(operationParams.getOrgUnitUids(), user);
paramsValidator.validateOrgUnits(UID.toValueSet(operationParams.getOrgUnitUids()), user);
validateOrgUnitMode(operationParams.getOrgUnitMode(), program, user);

EnrollmentQueryParams params = new EnrollmentQueryParams();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
import lombok.experimental.Accessors;
import org.hisp.dhis.common.OrganisationUnitSelectionMode;
import org.hisp.dhis.common.SortDirection;
import org.hisp.dhis.common.UID;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.program.EnrollmentStatus;
import org.hisp.dhis.program.Program;
import org.hisp.dhis.trackedentity.TrackedEntity;
import org.hisp.dhis.trackedentity.TrackedEntityType;
import org.hisp.dhis.tracker.export.Order;
import org.hisp.dhis.user.User;

/**
* @author Morten Olav Hansen <[email protected]>
Expand All @@ -53,7 +53,7 @@
class EnrollmentQueryParams {

/** Set of enrollment uids to explicitly select. */
private Set<String> enrollmentUids = new HashSet<>();
private Set<UID> enrollmentUids = new HashSet<>();

/** Last updated for enrollment. */
private Date lastUpdated;
Expand Down Expand Up @@ -95,13 +95,6 @@ class EnrollmentQueryParams {

private List<Order> order;

// -------------------------------------------------------------------------
// Transient properties
// -------------------------------------------------------------------------

/** Current user for query. */
private transient User user;

// -------------------------------------------------------------------------
// Constructors
// -------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.List;
import java.util.Set;
import javax.annotation.Nonnull;
import org.hisp.dhis.common.UID;
import org.hisp.dhis.feedback.BadRequestException;
import org.hisp.dhis.feedback.ForbiddenException;
import org.hisp.dhis.feedback.NotFoundException;
Expand All @@ -39,13 +40,13 @@
import org.hisp.dhis.tracker.export.PageParams;

public interface EnrollmentService {
Enrollment getEnrollment(String uid) throws ForbiddenException, NotFoundException;
Enrollment getEnrollment(UID uid) throws ForbiddenException, NotFoundException;

Enrollment getEnrollment(String uid, EnrollmentParams params, boolean includeDeleted)
Enrollment getEnrollment(UID uid, EnrollmentParams params, boolean includeDeleted)
throws NotFoundException, ForbiddenException;

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

/** Get all enrollments matching given params. */
List<Enrollment> getEnrollments(EnrollmentOperationParams params)
Expand All @@ -59,7 +60,7 @@ Page<Enrollment> getEnrollments(EnrollmentOperationParams params, PageParams pag
* Get event matching given {@code UID} under the privileges the user in the context. This method
* does not get the events relationships.
*/
List<Enrollment> getEnrollments(@Nonnull List<String> uids) throws ForbiddenException;
List<Enrollment> getEnrollments(@Nonnull Set<UID> uids) throws ForbiddenException;

/**
* Fields the {@link #getEnrollments(EnrollmentOperationParams)} can order enrollments by.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.apache.commons.lang3.StringUtils;
import org.hibernate.query.Query;
import org.hisp.dhis.common.OrganisationUnitSelectionMode;
import org.hisp.dhis.common.UID;
import org.hisp.dhis.common.hibernate.SoftDeleteHibernateObjectStore;
import org.hisp.dhis.commons.util.SqlHelper;
import org.hisp.dhis.organisationunit.OrganisationUnit;
Expand Down Expand Up @@ -146,7 +147,7 @@ private QueryWithOrderBy buildEnrollmentHql(EnrollmentQueryParams params) {
hql +=
hlp.whereAnd()
+ "en.uid in ("
+ getQuotedCommaDelimitedString(params.getEnrollmentUids())
+ getQuotedCommaDelimitedString(UID.toValueList(params.getEnrollmentUids()))
+ ")";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import lombok.Builder;
import lombok.Getter;
import org.hisp.dhis.common.SortDirection;
import org.hisp.dhis.common.UID;
import org.hisp.dhis.tracker.TrackerType;
import org.hisp.dhis.tracker.export.Order;

Expand All @@ -43,7 +44,7 @@
public class RelationshipOperationParams {
private TrackerType type;

private String identifier;
private UID identifier;

private List<Order> order;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ public RelationshipQueryParams map(@Nonnull RelationshipOperationParams params)

IdentifiableObject entity =
switch (params.getType()) {
case TRACKED_ENTITY -> trackedEntityService.getTrackedEntity(params.getIdentifier());
case TRACKED_ENTITY ->
trackedEntityService.getTrackedEntity(params.getIdentifier().getValue());
case ENROLLMENT -> enrollmentService.getEnrollment(params.getIdentifier());
case EVENT -> eventService.getEvent(UID.of(params.getIdentifier()));
case EVENT -> eventService.getEvent(UID.of(params.getIdentifier().getValue()));
case RELATIONSHIP -> throw new IllegalArgumentException("Unsupported type");
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ private RelationshipItem withNestedEntity(
} else if (item.getEnrollment() != null) {
result =
enrollmentService.getEnrollmentInRelationshipItem(
item.getEnrollment().getUid(),
UID.of(item.getEnrollment().getUid()),
EnrollmentParams.TRUE.withIncludeRelationships(false),
false);
} else if (item.getEvent() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.hisp.dhis.common.OrganisationUnitSelectionMode;
import org.hisp.dhis.common.QueryFilter;
import org.hisp.dhis.common.QueryOperator;
import org.hisp.dhis.common.UID;
import org.hisp.dhis.common.ValueType;
import org.hisp.dhis.feedback.BadRequestException;
import org.hisp.dhis.feedback.ForbiddenException;
Expand Down Expand Up @@ -145,8 +146,8 @@ public void postProcess(
Page<Enrollment> enrollmentPage =
enrollmentService.getEnrollments(
EnrollmentOperationParams.builder()
.trackedEntityUid(trackedEntity.getUid())
.programUid(smsCommand.getProgram().getUid())
.trackedEntityUid(UID.of(trackedEntity))
.programUid(UID.of(smsCommand.getProgram()))
.enrollmentStatus(EnrollmentStatus.ACTIVE)
.orgUnitMode(OrganisationUnitSelectionMode.ACCESSIBLE)
.build(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@

import com.google.common.collect.Lists;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.hisp.dhis.common.CodeGenerator;
import org.hisp.dhis.common.UID;
import org.hisp.dhis.feedback.ForbiddenException;
import org.hisp.dhis.feedback.NotFoundException;
import org.hisp.dhis.organisationunit.OrganisationUnit;
Expand Down Expand Up @@ -106,9 +109,12 @@ class DeduplicationHelperTest extends TestBase {

@BeforeEach
public void setUp() throws ForbiddenException, NotFoundException {
List<String> relationshipUids = Lists.newArrayList("REL_A", "REL_B");
List<String> attributeUids = Lists.newArrayList("ATTR_A", "ATTR_B");
List<String> enrollmentUids = Lists.newArrayList("PI_A", "PI_B");
List<String> relationshipUids =
Lists.newArrayList(CodeGenerator.generateUid(), CodeGenerator.generateUid());
List<String> attributeUids =
Lists.newArrayList(CodeGenerator.generateUid(), CodeGenerator.generateUid());
Set<UID> enrollmentUids =
Set.of(UID.of(CodeGenerator.generateUid()), UID.of(CodeGenerator.generateUid()));

organisationUnitA = createOrganisationUnit('A');
organisationUnitB = createOrganisationUnit('B');
Expand All @@ -122,7 +128,7 @@ public void setUp() throws ForbiddenException, NotFoundException {
MergeObject.builder()
.relationships(relationshipUids)
.trackedEntityAttributes(attributeUids)
.enrollments(enrollmentUids)
.enrollments(UID.toValueList(enrollmentUids))
.build();
user = makeUser("A", Lists.newArrayList("F_TRACKED_ENTITY_MERGE"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.stream.Collectors;
import org.hisp.dhis.common.BaseIdentifiableObject;
import org.hisp.dhis.common.SortDirection;
import org.hisp.dhis.common.UID;
import org.hisp.dhis.feedback.BadRequestException;
import org.hisp.dhis.feedback.ForbiddenException;
import org.hisp.dhis.organisationunit.OrganisationUnit;
Expand Down Expand Up @@ -227,7 +228,7 @@ void shouldMapChildrenOrgUnitModeWhenChildrenProvided()

EnrollmentOperationParams operationParams =
EnrollmentOperationParams.builder()
.orgUnitUids(Set.of(orgUnit1.getUid()))
.orgUnitUids(Set.of(UID.of(orgUnit1)))
.orgUnitMode(CHILDREN)
.build();

Expand Down
Loading

0 comments on commit a695d80

Please sign in to comment.