From 88777eba5333227ca72377645eab3f07a79208b6 Mon Sep 17 00:00:00 2001 From: marc Date: Wed, 24 Jul 2024 12:02:18 +0200 Subject: [PATCH] chore: Remove deleteEnrollment [DHIS-17712] (#18171) * chore: Remove deleteEnrollment [DHIS-17712] * chore: Use manager.delete [DHIS-17712] * chore: Use store to delete enrollment [DHIS-17712] --- .../hisp/dhis/program/EnrollmentService.java | 7 ----- .../program/DefaultEnrollmentService.java | 8 ----- .../export/enrollment/EnrollmentStore.java | 2 ++ .../enrollment/HibernateEnrollmentStore.java | 7 +++++ .../DefaultTrackerObjectsDeletionService.java | 5 ++-- .../maintenance/MaintenanceServiceTest.java | 9 +++--- .../dhis/program/EnrollmentServiceTest.java | 29 +++++-------------- .../TrackedEntityServiceTest.java | 2 +- 8 files changed, 23 insertions(+), 46 deletions(-) diff --git a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/EnrollmentService.java b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/EnrollmentService.java index dc4096f01d3a..c6ac1876116a 100644 --- a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/EnrollmentService.java +++ b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/EnrollmentService.java @@ -37,13 +37,6 @@ */ public interface EnrollmentService { - /** - * Soft deletes a {@link Enrollment}. - * - * @param enrollment the Enrollment to delete. - */ - void deleteEnrollment(Enrollment enrollment); - /** Get enrollments into a program. */ List getEnrollments(Program program); diff --git a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/DefaultEnrollmentService.java b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/DefaultEnrollmentService.java index 9d5976baa6f8..db1b8e7c843c 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/DefaultEnrollmentService.java +++ b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/DefaultEnrollmentService.java @@ -60,14 +60,6 @@ public class DefaultEnrollmentService implements EnrollmentService { private final IdentifiableObjectManager manager; - @Override - @Transactional - public void deleteEnrollment(Enrollment enrollment) { - enrollment.setStatus(EnrollmentStatus.CANCELLED); - enrollmentStore.update(enrollment); - enrollmentStore.delete(enrollment); - } - @Override @Transactional(readOnly = true) public List getEnrollments(Program program) { diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/enrollment/EnrollmentStore.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/enrollment/EnrollmentStore.java index 19bbc5fd6e35..5f14e575ef4c 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/enrollment/EnrollmentStore.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/enrollment/EnrollmentStore.java @@ -49,4 +49,6 @@ public interface EnrollmentStore extends IdentifiableObjectStore { * names should occur before calling {@link #getEnrollments(EnrollmentQueryParams)}. */ Set getOrderableFields(); + + void deleteEnrollment(Enrollment enrollment); } diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/enrollment/HibernateEnrollmentStore.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/enrollment/HibernateEnrollmentStore.java index 97424a8c32bb..78e340ec2e53 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/enrollment/HibernateEnrollmentStore.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/enrollment/HibernateEnrollmentStore.java @@ -54,6 +54,7 @@ import org.hisp.dhis.commons.util.SqlHelper; import org.hisp.dhis.organisationunit.OrganisationUnit; import org.hisp.dhis.program.Enrollment; +import org.hisp.dhis.program.EnrollmentStatus; import org.hisp.dhis.security.acl.AclService; import org.hisp.dhis.tracker.export.Order; import org.hisp.dhis.tracker.export.Page; @@ -305,4 +306,10 @@ protected Enrollment postProcessObject(Enrollment enrollment) { public Set getOrderableFields() { return ORDERABLE_FIELDS; } + + @Override + public void deleteEnrollment(Enrollment enrollment) { + enrollment.setStatus(EnrollmentStatus.CANCELLED); + super.delete(enrollment); + } } diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/persister/DefaultTrackerObjectsDeletionService.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/persister/DefaultTrackerObjectsDeletionService.java index 88ca421acb94..02445f22a9f5 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/persister/DefaultTrackerObjectsDeletionService.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/persister/DefaultTrackerObjectsDeletionService.java @@ -60,8 +60,6 @@ @Service @RequiredArgsConstructor public class DefaultTrackerObjectsDeletionService implements TrackerObjectDeletionService { - private final org.hisp.dhis.program.EnrollmentService apiEnrollmentService; - private final TrackedEntityService teService; private final IdentifiableObjectManager manager; @@ -113,7 +111,8 @@ public TrackerTypeReport deleteEnrollments(List enrollments) throws NotF TrackedEntity te = enrollment.getTrackedEntity(); te.setLastUpdatedByUserInfo(userInfoSnapshot); - apiEnrollmentService.deleteEnrollment(enrollment); + manager.delete(enrollment); + teService.updateTrackedEntity(te); typeReport.getStats().incDeleted(); diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/maintenance/MaintenanceServiceTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/maintenance/MaintenanceServiceTest.java index 4c5d7046ffd5..883592ea173a 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/maintenance/MaintenanceServiceTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/maintenance/MaintenanceServiceTest.java @@ -91,8 +91,6 @@ * @author Enrico Colasante */ class MaintenanceServiceTest extends PostgresIntegrationTestBase { - @Autowired private org.hisp.dhis.program.EnrollmentService apiEnrollmentService; - @Autowired private EnrollmentService enrollmentService; @Autowired private ProgramMessageService programMessageService; @@ -259,7 +257,8 @@ void testDeleteSoftDeletedEnrollmentWithAProgramMessage() manager.save(enrollment); programMessageService.saveProgramMessage(message); assertNotNull(manager.get(Enrollment.class, enrollment.getUid())); - apiEnrollmentService.deleteEnrollment(enrollment); + + manager.delete(enrollment); assertNull(manager.get(Enrollment.class, enrollment.getUid())); assertTrue(enrollmentExistsIncludingDeleted(enrollment)); @@ -338,7 +337,7 @@ void testDeleteSoftDeletedEnrollmentLinkedToATrackedEntityDataValueAudit() trackedEntityDataValueChangeLog); manager.save(enrollment); assertNotNull(manager.get(Enrollment.class, enrollment.getUid())); - apiEnrollmentService.deleteEnrollment(enrollment); + manager.delete(enrollment); assertNull(manager.get(Enrollment.class, enrollment.getUid())); assertTrue(enrollmentExistsIncludingDeleted(enrollment)); @@ -410,7 +409,7 @@ void testDeleteSoftDeletedEnrollmentLinkedToARelationshipItem() manager.save(r); assertNotNull(manager.get(Enrollment.class, enrollment.getId())); assertNotNull(getRelationship(r.getId())); - apiEnrollmentService.deleteEnrollment(enrollment); + manager.delete(enrollment); assertNull(manager.get(Enrollment.class, enrollment.getId())); manager.delete(r); assertNull(getRelationship(r.getId())); diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/program/EnrollmentServiceTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/program/EnrollmentServiceTest.java index 1637a34e14ea..05a0051f1292 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/program/EnrollmentServiceTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/program/EnrollmentServiceTest.java @@ -51,7 +51,7 @@ import org.hisp.dhis.test.integration.PostgresIntegrationTestBase; import org.hisp.dhis.trackedentity.TrackedEntity; import org.hisp.dhis.trackedentity.TrackedEntityService; -import org.hisp.dhis.tracker.imports.bundle.persister.TrackerObjectDeletionService; +import org.hisp.dhis.tracker.export.enrollment.EnrollmentService; import org.hisp.dhis.user.User; import org.hisp.dhis.user.sharing.Sharing; import org.joda.time.DateTime; @@ -72,11 +72,9 @@ class EnrollmentServiceTest extends PostgresIntegrationTestBase { private static final String ENROLLMENT_D_UID = UID.of(CodeGenerator.generateUid()).getValue(); private static final String EVENT_UID = UID.of(CodeGenerator.generateUid()).getValue(); - @Autowired private EnrollmentService apiEnrollmentService; + @Autowired private org.hisp.dhis.program.EnrollmentService apiEnrollmentService; - @Autowired private TrackerObjectDeletionService trackerObjectDeletionService; - - @Autowired private org.hisp.dhis.tracker.export.enrollment.EnrollmentService enrollmentService; + @Autowired private EnrollmentService enrollmentService; @Autowired private TrackedEntityService trackedEntityService; @@ -197,21 +195,7 @@ void testAddEnrollment() { } @Test - void testDeleteEnrollment() { - manager.save(enrollmentA); - manager.save(enrollmentB); - assertNotNull(manager.get(Enrollment.class, enrollmentA.getUid())); - assertNotNull(manager.get(Enrollment.class, enrollmentB.getUid())); - apiEnrollmentService.deleteEnrollment(enrollmentA); - assertNull(manager.get(Enrollment.class, enrollmentA.getUid())); - assertNotNull(manager.get(Enrollment.class, enrollmentB.getUid())); - apiEnrollmentService.deleteEnrollment(enrollmentB); - assertNull(manager.get(Enrollment.class, enrollmentA.getUid())); - assertNull(manager.get(Enrollment.class, enrollmentB.getUid())); - } - - @Test - void testSoftDeleteEnrollmentAndLinkedEvent() throws NotFoundException { + void testSoftDeleteEnrollmentAndLinkedEvent() { manager.save(enrollmentA); manager.save(eventA); long eventIdA = eventA.getId(); @@ -220,7 +204,8 @@ void testSoftDeleteEnrollmentAndLinkedEvent() throws NotFoundException { assertNotNull(manager.get(Enrollment.class, enrollmentA.getUid())); assertNotNull(manager.get(Event.class, eventIdA)); - trackerObjectDeletionService.deleteEnrollments(List.of(enrollmentA.getUid())); + manager.delete(enrollmentA); + manager.delete(eventA); assertNull(manager.get(Enrollment.class, enrollmentA.getUid())); assertNull(manager.get(Event.class, eventIdA)); @@ -312,7 +297,7 @@ void shouldNotDeleteNoteWhenDeletingEnrollment() throws ForbiddenException, NotF assertNotNull(enrollmentService.getEnrollment(enrollmentA.getUid())); - apiEnrollmentService.deleteEnrollment(enrollmentA); + manager.delete(enrollmentA); Assertions.assertThrows( NotFoundException.class, () -> enrollmentService.getEnrollment(enrollmentA.getUid())); diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/export/trackedentity/TrackedEntityServiceTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/export/trackedentity/TrackedEntityServiceTest.java index 0d4bc6647a74..7b37b527b0f7 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/export/trackedentity/TrackedEntityServiceTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/export/trackedentity/TrackedEntityServiceTest.java @@ -1176,7 +1176,7 @@ void shouldIncludeDeletedEnrollmentAndEvents() .collect(Collectors.toSet()); assertIsEmpty(deletedEvents); - enrollmentService.deleteEnrollment(enrollmentA); + manager.delete(enrollmentA); manager.delete(eventA); trackedEntities = trackedEntityService.getTrackedEntities(operationParams);