Skip to content

Commit

Permalink
fix: use tracker importer in EnrollmentSmsSubmission
Browse files Browse the repository at this point in the history
  • Loading branch information
teleivo committed Sep 13, 2024
1 parent 20d3d59 commit 01938b6
Show file tree
Hide file tree
Showing 12 changed files with 1,286 additions and 851 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dhis-2/projectFilesBackup
coverage
node_modules
**/rebel.xml
.vscode
# ignore generated artifact directory
docker/artifacts
# ignore local docker override files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.apache.commons.lang3.StringUtils;
import org.hisp.dhis.changelog.ChangeLogType;
import org.hisp.dhis.common.BaseIdentifiableObject;
import org.hisp.dhis.common.IdentifiableObject;
import org.hisp.dhis.common.UID;
import org.hisp.dhis.feedback.BadRequestException;
import org.hisp.dhis.feedback.ForbiddenException;
Expand Down Expand Up @@ -386,21 +387,26 @@ private Set<Enrollment> getEnrollments(

private Set<TrackedEntityAttributeValue> getTrackedEntityAttributeValues(
TrackedEntity trackedEntity, Program program) {
Set<TrackedEntityAttribute> readableAttributes =
new HashSet<>(trackedEntity.getTrackedEntityType().getTrackedEntityAttributes());
Set<String> readableAttributes =
trackedEntity.getTrackedEntityType().getTrackedEntityAttributes().stream()
.map(IdentifiableObject::getUid)
.collect(Collectors.toSet());

if (program != null) {
readableAttributes.addAll(program.getTrackedEntityAttributes());
readableAttributes.addAll(
program.getTrackedEntityAttributes().stream()
.map(IdentifiableObject::getUid)
.collect(Collectors.toSet()));
}

return trackedEntity.getTrackedEntityAttributeValues().stream()
.filter(av -> readableAttributes.contains(av.getAttribute()))
.collect(Collectors.toCollection(LinkedHashSet::new));
.filter(av -> readableAttributes.contains(av.getAttribute().getUid()))
.collect(Collectors.toSet());
}

private RelationshipItem withNestedEntity(
TrackedEntity trackedEntity, RelationshipItem item, boolean includeDeleted)
throws ForbiddenException, NotFoundException {
throws NotFoundException {
// relationships of relationship items are not mapped to JSON so there is no need to fetch them
RelationshipItem result = new RelationshipItem();

Expand Down Expand Up @@ -546,7 +552,7 @@ private void mapRelationshipItems(
}

private void mapRelationshipItems(TrackedEntity trackedEntity, boolean includeDeleted)
throws ForbiddenException, NotFoundException {
throws NotFoundException {
Set<RelationshipItem> result = new HashSet<>();

for (RelationshipItem item : trackedEntity.getRelationshipItems()) {
Expand All @@ -562,7 +568,7 @@ private void mapRelationshipItems(TrackedEntity trackedEntity, boolean includeDe

private void mapRelationshipItems(
Enrollment enrollment, TrackedEntity trackedEntity, boolean includeDeleted)
throws ForbiddenException, NotFoundException {
throws NotFoundException {
Set<RelationshipItem> result = new HashSet<>();

for (RelationshipItem item : enrollment.getRelationshipItems()) {
Expand All @@ -573,8 +579,7 @@ private void mapRelationshipItems(
}

private void mapRelationshipItems(
Event event, TrackedEntity trackedEntity, boolean includeDeleted)
throws ForbiddenException, NotFoundException {
Event event, TrackedEntity trackedEntity, boolean includeDeleted) throws NotFoundException {
Set<RelationshipItem> result = new HashSet<>();

for (RelationshipItem item : event.getRelationshipItems()) {
Expand All @@ -589,7 +594,7 @@ private RelationshipItem mapRelationshipItem(
BaseIdentifiableObject itemOwner,
TrackedEntity trackedEntity,
boolean includeDeleted)
throws ForbiddenException, NotFoundException {
throws NotFoundException {
Relationship rel = item.getRelationship();
RelationshipItem from = withNestedEntity(trackedEntity, rel.getFrom(), includeDeleted);
RelationshipItem to = withNestedEntity(trackedEntity, rel.getTo(), includeDeleted);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@
*/
public interface TrackerImportService {
/**
* Import object using provided params. Takes the objects through all phases of the importer from
* preheating to validation, and then finished with a commit (unless its validate only)
* Import objects using provided params. Takes the objects through all phases of the importer from
* preheating to validation, and then finished with a commit (unless its validate only).
*
* <p>{@link TrackerObjects} need to be flat meaning each entity needs to be in the top level
* field like {@link TrackerObjects#getEnrollments()} for an enrollment even though you could put
* it onto its tracked entity in {@link TrackerObjects#getTrackedEntities()}
*
* @param params Parameters for import
* @param trackerObjects the objects to import
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
package org.hisp.dhis.tracker.imports.sms;

import java.util.List;
import javax.annotation.Nonnull;
import org.hisp.dhis.category.CategoryService;
import org.hisp.dhis.common.IdentifiableObjectManager;
import org.hisp.dhis.dataelement.DataElementService;
Expand Down Expand Up @@ -101,10 +102,7 @@ protected SmsResponse postProcess(IncomingSms sms, SmsSubmission submission, Use
// the SMS. We might want to remove the params user in favor of the currentUser set on
// the thread.
.build();
TrackerObjects trackerObjects =
TrackerObjects.builder()
.events(List.of(Event.builder().event(subm.getEvent().getUid()).build()))
.build();
TrackerObjects trackerObjects = map(subm);
ImportReport importReport = trackerImportService.importTracker(params, trackerObjects);

if (Status.OK == importReport.getStatus()) {
Expand All @@ -115,6 +113,13 @@ protected SmsResponse postProcess(IncomingSms sms, SmsSubmission submission, Use
return SmsResponse.INVALID_EVENT.set(subm.getEvent());
}

@Nonnull
private static TrackerObjects map(@Nonnull DeleteSmsSubmission submission) {
return TrackerObjects.builder()
.events(List.of(Event.builder().event(submission.getEvent().getUid()).build()))
.build();
}

@Override
protected boolean handlesType(SubmissionType type) {
return (type == SubmissionType.DELETE);
Expand Down
Loading

0 comments on commit 01938b6

Please sign in to comment.