Skip to content

Commit

Permalink
chore: Use UID for event in tracker importer [DHIS2-17790]
Browse files Browse the repository at this point in the history
  • Loading branch information
enricocolasante committed Nov 1, 2024
1 parent b64c5c7 commit 7becd40
Show file tree
Hide file tree
Showing 97 changed files with 681 additions and 857 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private void merge(DeduplicationMergeParams params)
potentialDuplicateStore.moveRelationships(original, duplicate, mergeObject.getRelationships());
potentialDuplicateStore.moveEnrollments(original, duplicate, mergeObject.getEnrollments());
try {
trackerObjectDeletionService.deleteTrackedEntities(List.of(duplicate.getUid()));
trackerObjectDeletionService.deleteTrackedEntities(List.of(UID.of(duplicate)));
} catch (NotFoundException e) {
throw new RuntimeException("Could not find TrackedEntity: " + duplicate.getUid());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.commons.lang3.StringUtils;
import org.hisp.dhis.category.CategoryOption;
import org.hisp.dhis.category.CategoryOptionCombo;
import org.hisp.dhis.common.UID;
import org.hisp.dhis.dataelement.DataElement;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.program.Program;
Expand Down Expand Up @@ -199,4 +200,13 @@ private <T> void addIdentifier(

identifiers.computeIfAbsent(klass, k -> new HashSet<>()).add(identifier);
}

private <T> void addIdentifier(
Map<Class<?>, Set<String>> identifiers, Class<T> klass, UID identifier) {
if (identifier == null || identifiers == null || klass == null) {
return;
}

identifiers.computeIfAbsent(klass, k -> new HashSet<>()).add(identifier.getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Nonnull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand Down Expand Up @@ -147,16 +148,16 @@ public Optional<Enrollment> findEnrollmentByUid(String uid) {
return findById(this.enrollments, uid);
}

public Optional<Event> findEventByUid(String uid) {
return findById(this.events, uid);
public Optional<Event> findEventByUid(@Nonnull UID uid) {
return findById(this.events, uid.getValue());
}

public Optional<Relationship> findRelationshipByUid(String uid) {
return findById(this.relationships, uid);
}

private static <T extends TrackerDto> Optional<T> findById(List<T> entities, String uid) {
return entities.stream().filter(e -> Objects.equals(e.getUid(), uid)).findFirst();
return entities.stream().filter(e -> Objects.equals(e.getStringUid(), uid)).findFirst();
}

public Map<UID, List<Notification>> getEnrollmentNotifications() {
Expand All @@ -168,11 +169,13 @@ public Map<UID, List<Notification>> getEventNotifications() {
}

public TrackerImportStrategy setStrategy(TrackerDto dto, TrackerImportStrategy strategy) {
return this.getResolvedStrategyMap().get(dto.getTrackerType()).put(dto.getUid(), strategy);
return this.getResolvedStrategyMap()
.get(dto.getTrackerType())
.put(dto.getStringUid(), strategy);
}

public TrackerImportStrategy getStrategy(TrackerDto dto) {
return getResolvedStrategyMap().get(dto.getTrackerType()).get(dto.getUid());
return getResolvedStrategyMap().get(dto.getTrackerType()).get(dto.getStringUid());
}

@SuppressWarnings("unchecked")
Expand All @@ -190,27 +193,4 @@ public <T extends TrackerDto> List<T> get(Class<T> type) {
// only reached if a new TrackerDto implementation is added
throw new IllegalStateException("TrackerType " + type.getName() + " not yet supported.");
}

/** Checks if an entity exists in the payload. */
public <T extends TrackerDto> boolean exists(T entity) {
return exists(entity.getTrackerType(), entity.getUid());
}

/**
* Checks if an entity of given type and UID exists in the payload.
*
* @param type tracker type
* @param uid uid of entity to check
* @return true if an entity of given type and UID exists in the payload
*/
public boolean exists(TrackerType type, String uid) {
Objects.requireNonNull(type);

return switch (type) {
case TRACKED_ENTITY -> findTrackedEntityByUid(uid).isPresent();
case ENROLLMENT -> findEnrollmentByUid(uid).isPresent();
case EVENT -> findEventByUid(uid).isPresent();
case RELATIONSHIP -> findRelationshipByUid(uid).isPresent();
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import org.hisp.dhis.category.CategoryOptionCombo;
import org.hisp.dhis.common.UID;
import org.hisp.dhis.event.EventStatus;
import org.hisp.dhis.note.Note;
import org.hisp.dhis.organisationunit.OrganisationUnit;
Expand Down Expand Up @@ -182,7 +183,7 @@ private TrackerObjectsMapper() {

if (dbEvent == null) {
dbEvent = new Event();
dbEvent.setUid(event.getEvent());
dbEvent.setUid(event.getEvent().getValue());
dbEvent.setCreated(now);
dbEvent.setStoredBy(event.getStoredBy());
dbEvent.setCreatedByUserInfo(UserInfoSnapshot.from(user));
Expand Down Expand Up @@ -276,7 +277,7 @@ private TrackerObjectsMapper() {
case PROGRAM_INSTANCE ->
fromItem.setEnrollment(preheat.getEnrollment(relationship.getFrom().getEnrollment()));
case PROGRAM_STAGE_INSTANCE ->
fromItem.setEvent(preheat.getEvent(relationship.getFrom().getEvent()));
fromItem.setEvent(preheat.getEvent(UID.of(relationship.getFrom().getEvent())));
}
dbRelationship.setFrom(fromItem);

Expand All @@ -290,7 +291,7 @@ private TrackerObjectsMapper() {
case PROGRAM_INSTANCE ->
toItem.setEnrollment(preheat.getEnrollment(relationship.getTo().getEnrollment()));
case PROGRAM_STAGE_INSTANCE ->
toItem.setEvent(preheat.getEvent(relationship.getTo().getEvent()));
toItem.setEvent(preheat.getEvent(UID.of(relationship.getTo().getEvent())));
}
dbRelationship.setTo(toItem);

Expand Down Expand Up @@ -319,10 +320,9 @@ private TrackerObjectsMapper() {
return dbNote;
}

private static Enrollment getEnrollment(
TrackerPreheat preheat, String enrollment, Program program) {
private static Enrollment getEnrollment(TrackerPreheat preheat, UID enrollment, Program program) {
return switch (program.getProgramType()) {
case WITH_REGISTRATION -> preheat.getEnrollment(enrollment);
case WITH_REGISTRATION -> preheat.getEnrollment(enrollment.getValue());
case WITHOUT_REGISTRATION -> preheat.getEnrollmentsWithoutRegistration(program.getUid());
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public TrackerTypeReport persist(EntityManager entityManager, TrackerBundle bund

for (T trackerDto : dtos) {

Entity objectReport = new Entity(getType(), trackerDto.getUid());
Entity objectReport = new Entity(getType(), trackerDto.getStringUid());
List<NotificationTrigger> triggers =
determineNotificationTriggers(bundle.getPreheat(), trackerDto);

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.BaseIdentifiableObject;
import org.hisp.dhis.common.IdentifiableObjectManager;
import org.hisp.dhis.common.UID;
import org.hisp.dhis.feedback.NotFoundException;
import org.hisp.dhis.program.Enrollment;
import org.hisp.dhis.program.Event;
Expand Down Expand Up @@ -79,46 +80,17 @@ public class DefaultTrackerObjectsDeletionService implements TrackerObjectDeleti
private final ProgramNotificationInstanceService programNotificationInstanceService;

@Override
public TrackerTypeReport deleteEnrollments(List<String> enrollments) throws NotFoundException {
UserInfoSnapshot userInfoSnapshot = UserInfoSnapshot.from(getCurrentUserDetails());
TrackerTypeReport typeReport = new TrackerTypeReport(TrackerType.ENROLLMENT);
public TrackerTypeReport deleteRelationships(List<UID> relationships) throws NotFoundException {
TrackerTypeReport typeReport = new TrackerTypeReport(TrackerType.RELATIONSHIP);

for (String uid : enrollments) {
Entity objectReport = new Entity(TrackerType.ENROLLMENT, uid);
for (UID uid : relationships) {
Entity objectReport = new Entity(TrackerType.RELATIONSHIP, uid.getValue());

Enrollment enrollment = manager.get(Enrollment.class, uid);
if (enrollment == null) {
throw new NotFoundException(Enrollment.class, uid);
Relationship relationship = manager.get(Relationship.class, uid.getValue());
if (relationship == null) {
throw new NotFoundException(Relationship.class, uid);
}
enrollment.setLastUpdatedByUserInfo(userInfoSnapshot);

List<String> events =
enrollment.getEvents().stream()
.filter(event -> !event.isDeleted())
.map(BaseIdentifiableObject::getUid)
.toList();
deleteEvents(events);

RelationshipQueryParams params = RelationshipQueryParams.builder().entity(enrollment).build();
List<String> relationships =
relationshipStore.getByEnrollment(enrollment, params).stream()
.map(BaseIdentifiableObject::getUid)
.toList();

deleteRelationships(relationships);

List<ProgramNotificationInstance> notificationInstances =
programNotificationInstanceService.getProgramNotificationInstances(
ProgramNotificationInstanceParam.builder().enrollment(enrollment).build());

notificationInstances.forEach(programNotificationInstanceService::delete);

TrackedEntity te = enrollment.getTrackedEntity();
te.setLastUpdatedByUserInfo(userInfoSnapshot);

manager.delete(enrollment);

manager.update(te);
manager.delete(relationship);

typeReport.getStats().incDeleted();
typeReport.addEntity(objectReport);
Expand All @@ -128,19 +100,20 @@ public TrackerTypeReport deleteEnrollments(List<String> enrollments) throws NotF
}

@Override
public TrackerTypeReport deleteEvents(List<String> events) throws NotFoundException {
public TrackerTypeReport deleteEvents(List<UID> events) throws NotFoundException {
UserInfoSnapshot userInfoSnapshot = UserInfoSnapshot.from(getCurrentUserDetails());
TrackerTypeReport typeReport = new TrackerTypeReport(TrackerType.EVENT);
for (String uid : events) {
Entity objectReport = new Entity(TrackerType.EVENT, uid);
for (UID uid : events) {
Entity objectReport = new Entity(TrackerType.EVENT, uid.getValue());

Event event = manager.get(Event.class, uid);
Event event = manager.get(Event.class, uid.getValue());
event.setLastUpdatedByUserInfo(userInfoSnapshot);

RelationshipQueryParams params = RelationshipQueryParams.builder().entity(event).build();
List<String> relationships =
List<UID> relationships =
relationshipStore.getByEvent(event, params).stream()
.map(BaseIdentifiableObject::getUid)
.map(UID::of)
.toList();

deleteRelationships(relationships);
Expand Down Expand Up @@ -177,15 +150,66 @@ public TrackerTypeReport deleteEvents(List<String> events) throws NotFoundExcept
}

@Override
public TrackerTypeReport deleteTrackedEntities(List<String> trackedEntities)
public TrackerTypeReport deleteEnrollments(List<UID> enrollments) throws NotFoundException {
UserInfoSnapshot userInfoSnapshot = UserInfoSnapshot.from(getCurrentUserDetails());
TrackerTypeReport typeReport = new TrackerTypeReport(TrackerType.ENROLLMENT);

for (UID uid : enrollments) {
Entity objectReport = new Entity(TrackerType.ENROLLMENT, uid.getValue());

Enrollment enrollment = manager.get(Enrollment.class, uid.getValue());
if (enrollment == null) {
throw new NotFoundException(Enrollment.class, uid);
}
enrollment.setLastUpdatedByUserInfo(userInfoSnapshot);

List<UID> events =
enrollment.getEvents().stream()
.filter(event -> !event.isDeleted())
.map(BaseIdentifiableObject::getUid)
.map(UID::of)
.toList();
deleteEvents(events);

RelationshipQueryParams params = RelationshipQueryParams.builder().entity(enrollment).build();
List<UID> relationships =
relationshipStore.getByEnrollment(enrollment, params).stream()
.map(BaseIdentifiableObject::getUid)
.map(UID::of)
.toList();

deleteRelationships(relationships);

List<ProgramNotificationInstance> notificationInstances =
programNotificationInstanceService.getProgramNotificationInstances(
ProgramNotificationInstanceParam.builder().enrollment(enrollment).build());

notificationInstances.forEach(programNotificationInstanceService::delete);

TrackedEntity te = enrollment.getTrackedEntity();
te.setLastUpdatedByUserInfo(userInfoSnapshot);

manager.delete(enrollment);

manager.update(te);

typeReport.getStats().incDeleted();
typeReport.addEntity(objectReport);
}

return typeReport;
}

@Override
public TrackerTypeReport deleteTrackedEntities(List<UID> trackedEntities)
throws NotFoundException {
UserInfoSnapshot userInfoSnapshot = UserInfoSnapshot.from(getCurrentUserDetails());
TrackerTypeReport typeReport = new TrackerTypeReport(TrackerType.TRACKED_ENTITY);

for (String uid : trackedEntities) {
Entity objectReport = new Entity(TrackerType.TRACKED_ENTITY, uid);
for (UID uid : trackedEntities) {
Entity objectReport = new Entity(TrackerType.TRACKED_ENTITY, uid.getValue());

TrackedEntity entity = manager.get(TrackedEntity.class, uid);
TrackedEntity entity = manager.get(TrackedEntity.class, uid.getValue());
if (entity == null) {
throw new NotFoundException(TrackedEntity.class, uid);
}
Expand All @@ -195,17 +219,19 @@ public TrackerTypeReport deleteTrackedEntities(List<String> trackedEntities)

Set<Enrollment> daoEnrollments = entity.getEnrollments();

List<String> enrollments =
List<UID> enrollments =
daoEnrollments.stream()
.filter(enrollment -> !enrollment.isDeleted())
.map(BaseIdentifiableObject::getUid)
.map(UID::of)
.toList();
deleteEnrollments(enrollments);

RelationshipQueryParams params = RelationshipQueryParams.builder().entity(entity).build();
List<String> relationships =
List<UID> relationships =
relationshipStore.getByTrackedEntity(entity, params).stream()
.map(BaseIdentifiableObject::getUid)
.map(UID::of)
.toList();

deleteRelationships(relationships);
Expand All @@ -227,25 +253,4 @@ public TrackerTypeReport deleteTrackedEntities(List<String> trackedEntities)

return typeReport;
}

@Override
public TrackerTypeReport deleteRelationships(List<String> relationships)
throws NotFoundException {
TrackerTypeReport typeReport = new TrackerTypeReport(TrackerType.RELATIONSHIP);

for (String uid : relationships) {
Entity objectReport = new Entity(TrackerType.RELATIONSHIP, uid);

Relationship relationship = manager.get(Relationship.class, uid);
if (relationship == null) {
throw new NotFoundException(Relationship.class, uid);
}
manager.delete(relationship);

typeReport.getStats().incDeleted();
typeReport.addEntity(objectReport);
}

return typeReport;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ protected TrackerNotificationDataBundle handleNotifications(
@Override
protected List<NotificationTrigger> determineNotificationTriggers(
TrackerPreheat preheat, org.hisp.dhis.tracker.imports.domain.Enrollment entity) {
Enrollment persistedEnrollment = preheat.getEnrollment(entity.getUid());
Enrollment persistedEnrollment = preheat.getEnrollment(entity.getStringUid());
List<NotificationTrigger> triggers = new ArrayList<>();

if (persistedEnrollment == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private void handleDataValues(
Event event,
UserDetails user) {
Map<String, EventDataValue> dataValueDBMap =
Optional.ofNullable(preheat.getEvent(event.getUid()))
Optional.ofNullable(preheat.getEvent(UID.of(event)))
.map(
a ->
a.getEventDataValues().stream()
Expand Down
Loading

0 comments on commit 7becd40

Please sign in to comment.