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 UID in enrollment exporter [DHIS2-17790] #18898

Merged
merged 2 commits into from
Oct 23, 2024
Merged
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 @@ -32,9 +32,11 @@
import java.text.MessageFormat;
import java.util.function.Function;
import java.util.function.Supplier;
import javax.annotation.Nonnull;
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 +60,10 @@ public NotFoundException(Class<?> type, String uid) {
this(type.getSimpleName() + " with id " + uid + " could not be found.");
}

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

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> enrollments = 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> orgUnits = 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 program;

/** 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 trackedEntityType;

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

/** 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(
enricocolasante marked this conversation as resolved.
Show resolved Hide resolved
applyIfNotNull(operationParams.getProgram(), UID::getValue), user);
TrackedEntityType trackedEntityType =
paramsValidator.validateTrackedEntityType(operationParams.getTrackedEntityTypeUid(), user);
paramsValidator.validateTrackedEntityType(
applyIfNotNull(operationParams.getTrackedEntityType(), UID::getValue), user);
TrackedEntity trackedEntity =
paramsValidator.validateTrackedEntity(operationParams.getTrackedEntityUid(), user);
paramsValidator.validateTrackedEntity(
applyIfNotNull(operationParams.getTrackedEntity(), UID::getValue), user);

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

EnrollmentQueryParams params = new EnrollmentQueryParams();
Expand All @@ -87,7 +93,7 @@ public EnrollmentQueryParams map(
params.setOrganisationUnitMode(operationParams.getOrgUnitMode());
params.setIncludeDeleted(operationParams.isIncludeDeleted());
params.setOrder(operationParams.getOrder());
params.setEnrollmentUids(operationParams.getEnrollmentUids());
params.setEnrollments(operationParams.getEnrollments());

mergeOrgUnitModes(operationParams, params, user);

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> enrollments = 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 Expand Up @@ -175,7 +168,7 @@ public boolean hasTrackedEntity() {
}

public boolean hasEnrollmentUids() {
return isNotEmpty(this.enrollmentUids);
return isNotEmpty(this.enrollments);
}

/** Indicates whether this params is of the given organisation unit mode. */
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.getEnrollments()))
+ ")";
}

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 @@ -30,7 +30,6 @@
import javax.annotation.Nonnull;
import lombok.RequiredArgsConstructor;
import org.hisp.dhis.common.IdentifiableObject;
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 @@ -60,9 +59,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(params.getIdentifier());
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()),
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())
.trackedEntity(UID.of(trackedEntity))
.program(UID.of(smsCommand.getProgram()))
.enrollmentStatus(EnrollmentStatus.ACTIVE)
.orgUnitMode(OrganisationUnitSelectionMode.ACCESSIBLE)
.build(),
Expand Down
Loading
Loading