diff --git a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/EnrollmentStore.java b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/EnrollmentStore.java index d34f403b8c5b..f6321c16f31a 100644 --- a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/EnrollmentStore.java +++ b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/EnrollmentStore.java @@ -67,14 +67,6 @@ public interface EnrollmentStore extends IdentifiableObjectStore { List getWithScheduledNotifications( ProgramNotificationTemplate template, Date notificationDate); - /** - * Return all enrollment linked to programs. - * - * @param programs Programs to fetch by - * @return List of all enrollments that are linked to programs - */ - List getByPrograms(List programs); - /** * Hard deletes a {@link Enrollment}. * diff --git a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/hibernate/HibernateEnrollmentStore.java b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/hibernate/HibernateEnrollmentStore.java index ec203369d677..715ada925a71 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/hibernate/HibernateEnrollmentStore.java +++ b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/hibernate/HibernateEnrollmentStore.java @@ -161,15 +161,6 @@ public List getWithScheduledNotifications( .list(); } - @Override - public List getByPrograms(List programs) { - CriteriaBuilder builder = getCriteriaBuilder(); - - return getList( - builder, - newJpaParameters().addPredicate(root -> builder.in(root.get("program")).value(programs))); - } - @Override public void hardDelete(Enrollment enrollment) { getSession().delete(enrollment); 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 57aa9cf3e941..19bbc5fd6e35 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 @@ -37,14 +37,6 @@ public interface EnrollmentStore extends IdentifiableObjectStore { String ID = EnrollmentStore.class.getName(); - /** - * Count all enrollments by enrollment query params. - * - * @param params EnrollmentQueryParams to use - * @return Count of matching enrollments - */ - long countEnrollments(EnrollmentQueryParams params); - /** Get all enrollments matching given params. */ List getEnrollments(EnrollmentQueryParams params); 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 7f345c3ebae2..97424a8c32bb 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 @@ -91,15 +91,6 @@ public HibernateEnrollmentStore( super(entityManager, jdbcTemplate, publisher, Enrollment.class, aclService, true); } - @Override - public long countEnrollments(EnrollmentQueryParams params) { - String hql = buildCountEnrollmentHql(params); - - Query query = getTypedQuery(hql); - - return query.getSingleResult().longValue(); - } - private String buildCountEnrollmentHql(EnrollmentQueryParams params) { return buildEnrollmentHql(params) .getQuery() @@ -127,6 +118,14 @@ public Page getEnrollments(EnrollmentQueryParams params, PageParams return getPage(pageParams, query.list(), enrollmentCount); } + private long countEnrollments(EnrollmentQueryParams params) { + String hql = buildCountEnrollmentHql(params); + + Query query = getTypedQuery(hql); + + return query.getSingleResult().longValue(); + } + private Page getPage( PageParams pageParams, List enrollments, LongSupplier enrollmentCount) { if (pageParams.isPageTotal()) { diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/config/TrackerPreheatConfig.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/config/TrackerPreheatConfig.java index a626fc62da72..9c333cfcf598 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/config/TrackerPreheatConfig.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/config/TrackerPreheatConfig.java @@ -33,9 +33,9 @@ import org.hisp.dhis.tracker.imports.preheat.supplier.ClassBasedSupplier; import org.hisp.dhis.tracker.imports.preheat.supplier.DefaultsSupplier; import org.hisp.dhis.tracker.imports.preheat.supplier.DuplicateRelationshipSupplier; -import org.hisp.dhis.tracker.imports.preheat.supplier.EnrollmentSupplier; import org.hisp.dhis.tracker.imports.preheat.supplier.EnrollmentsWithAtLeastOneEventSupplier; import org.hisp.dhis.tracker.imports.preheat.supplier.EventCategoryOptionComboSupplier; +import org.hisp.dhis.tracker.imports.preheat.supplier.EventProgramEnrollmentSupplier; import org.hisp.dhis.tracker.imports.preheat.supplier.EventProgramStageMapSupplier; import org.hisp.dhis.tracker.imports.preheat.supplier.FileResourceSupplier; import org.hisp.dhis.tracker.imports.preheat.supplier.OrgUnitValueTypeSupplier; @@ -58,7 +58,7 @@ public class TrackerPreheatConfig { ClassBasedSupplier.class, DefaultsSupplier.class, TrackedEntityEnrollmentSupplier.class, - EnrollmentSupplier.class, + EventProgramEnrollmentSupplier.class, EnrollmentsWithAtLeastOneEventSupplier.class, EventProgramStageMapSupplier.class, ProgramOrgUnitsSupplier.class, diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/EnrollmentSupplier.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/EventProgramEnrollmentSupplier.java similarity index 64% rename from dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/EnrollmentSupplier.java rename to dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/EventProgramEnrollmentSupplier.java index e26b40d99e51..9605d110ba7c 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/EnrollmentSupplier.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/EventProgramEnrollmentSupplier.java @@ -28,49 +28,44 @@ package org.hisp.dhis.tracker.imports.preheat.supplier; import java.util.List; -import java.util.stream.Collectors; import javax.annotation.Nonnull; +import javax.persistence.EntityManager; +import javax.persistence.TypedQuery; import lombok.RequiredArgsConstructor; import org.hisp.dhis.program.Enrollment; -import org.hisp.dhis.program.EnrollmentStore; -import org.hisp.dhis.program.Program; -import org.hisp.dhis.program.ProgramStore; -import org.hisp.dhis.program.ProgramType; import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.hisp.dhis.tracker.imports.preheat.mappers.EnrollmentMapper; import org.springframework.stereotype.Component; /** + * This supplier adds to the pre-heat a list of all enrollments that are part of an event program + * and are not soft-deleted. + * * @author Luciano Fiandesio */ @RequiredArgsConstructor @Component -public class EnrollmentSupplier extends AbstractPreheatSupplier { - @Nonnull private final EnrollmentStore enrollmentStore; - - @Nonnull private final ProgramStore programStore; +public class EventProgramEnrollmentSupplier extends AbstractPreheatSupplier { + @Nonnull private final EntityManager entityManager; @Override public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { - List programsWithoutRegistration = - preheat.getAll(Program.class).stream() - .filter(program -> program.getProgramType().equals(ProgramType.WITHOUT_REGISTRATION)) - .collect(Collectors.toList()); - if (programsWithoutRegistration.isEmpty()) { - programsWithoutRegistration = programStore.getByType(ProgramType.WITHOUT_REGISTRATION); - } - if (!programsWithoutRegistration.isEmpty()) { - List enrollments = - DetachUtils.detach( - EnrollmentMapper.INSTANCE, - enrollmentStore.getByPrograms(programsWithoutRegistration)); + List enrollments = + DetachUtils.detach(EnrollmentMapper.INSTANCE, getEventProgramEnrollments()); + + enrollments.forEach( + e -> { + preheat.putEnrollment(e.getUid(), e); + preheat.putEnrollmentsWithoutRegistration(e.getProgram().getUid(), e); + }); + } - enrollments.forEach( - e -> { - preheat.putEnrollment(e.getUid(), e); - preheat.putEnrollmentsWithoutRegistration(e.getProgram().getUid(), e); - }); - } + private List getEventProgramEnrollments() { + TypedQuery query = + entityManager.createQuery( + "select e from Enrollment e inner join e.program p where e.deleted = false and p.programType ='WITHOUT_REGISTRATION'", + Enrollment.class); + return query.getResultList(); } } diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/TrackedEntityEnrollmentSupplier.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/TrackedEntityEnrollmentSupplier.java index ef201b79f601..d84145e532e7 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/TrackedEntityEnrollmentSupplier.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/TrackedEntityEnrollmentSupplier.java @@ -118,13 +118,13 @@ public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { if (trackerObjects.getEnrollments().isEmpty()) return; for (List trackedEntityListSubList : trackedEntities) { - queryTeiAndAddToMap(trackedEntityToEnrollmentMap, trackedEntityListSubList, programList); + queryTeAndAddToMap(trackedEntityToEnrollmentMap, trackedEntityListSubList, programList); } preheat.setTrackedEntityToEnrollmentMap(trackedEntityToEnrollmentMap); } - private void queryTeiAndAddToMap( + private void queryTeAndAddToMap( Map> trackedEntityToEnrollmentMap, List trackedEntityListSubList, List programList) { diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/EnrollmentSupplierTest.java b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/EventProgramEnrollmentSupplierTest.java similarity index 73% rename from dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/EnrollmentSupplierTest.java rename to dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/EventProgramEnrollmentSupplierTest.java index eb74442224f2..33e590accb55 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/EnrollmentSupplierTest.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/EventProgramEnrollmentSupplierTest.java @@ -30,19 +30,14 @@ import static org.hisp.dhis.program.ProgramType.WITHOUT_REGISTRATION; import static org.hisp.dhis.program.ProgramType.WITH_REGISTRATION; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.mockito.Mockito.when; -import com.google.common.collect.Lists; import java.util.List; import java.util.stream.Collectors; +import javax.persistence.EntityManager; import org.hisp.dhis.program.Enrollment; -import org.hisp.dhis.program.EnrollmentStore; import org.hisp.dhis.program.Program; -import org.hisp.dhis.program.ProgramStore; import org.hisp.dhis.test.DhisConvenienceTest; import org.hisp.dhis.test.random.BeanRandomizer; -import org.hisp.dhis.tracker.imports.TrackerIdSchemeParam; import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.junit.jupiter.api.BeforeEach; @@ -60,13 +55,11 @@ */ @MockitoSettings(strictness = Strictness.LENIENT) @ExtendWith(MockitoExtension.class) -class EnrollmentSupplierTest extends DhisConvenienceTest { +class EventProgramEnrollmentSupplierTest extends DhisConvenienceTest { - @InjectMocks private EnrollmentSupplier supplier; + @InjectMocks private EventProgramEnrollmentSupplier supplier; - @Mock private EnrollmentStore enrollmentStore; - - @Mock private ProgramStore programStore; + @Mock private EntityManager entityManager; private List enrollments; @@ -80,8 +73,6 @@ class EnrollmentSupplierTest extends DhisConvenienceTest { private TrackerPreheat preheat; - private Enrollment enrollmentWithoutRegistration; - @BeforeEach public void setUp() { enrollments = rnd.objects(Enrollment.class, 2).collect(Collectors.toList()); @@ -96,42 +87,22 @@ public void setUp() { programWithoutRegistration = createProgram('B'); programWithoutRegistration.setProgramType(WITHOUT_REGISTRATION); - enrollmentWithoutRegistration = enrollments.get(1); + Enrollment enrollmentWithoutRegistration = enrollments.get(1); enrollmentWithoutRegistration.setProgram(programWithoutRegistration); - when(enrollmentStore.getByPrograms(Lists.newArrayList(programWithoutRegistration))) - .thenReturn(enrollments); - params = TrackerObjects.builder().build(); preheat = new TrackerPreheat(); } - @Test - void verifySupplierWhenNoEventProgramArePresent() { - preheat.put(TrackerIdSchemeParam.UID, programWithRegistration); - - this.supplier.preheatAdd(params, preheat); - - final List programUids = - enrollments.stream().map(enrollment -> enrollment.getProgram().getUid()).toList(); - for (String programUid : programUids) { - assertNull(preheat.getEnrollmentsWithoutRegistration(programUid)); - } - } - // TODO: MAS. Fix this test, it is failing because of the recursive mapping of the OrgUnit @Test @Disabled void verifySupplierWhenNoProgramsArePresent() { - when(programStore.getByType(WITHOUT_REGISTRATION)) - .thenReturn(List.of(programWithoutRegistration)); enrollments = rnd.objects(Enrollment.class, 1).collect(Collectors.toList()); // set the OrgUnit parent to null to avoid recursive errors when mapping enrollments.forEach(p -> p.getOrganisationUnit().setParent(null)); Enrollment enrollment = enrollments.get(0); enrollment.setProgram(programWithoutRegistration); - when(enrollmentStore.getByPrograms(List.of(programWithoutRegistration))) - .thenReturn(enrollments); this.supplier.preheatAdd(params, preheat); @@ -139,19 +110,6 @@ void verifySupplierWhenNoProgramsArePresent() { enrollment, preheat.getEnrollmentsWithoutRegistration(programWithoutRegistration.getUid())); } - @Test - void verifySupplier() { - preheat.put( - TrackerIdSchemeParam.UID, - Lists.newArrayList(programWithRegistration, programWithoutRegistration)); - - this.supplier.preheatAdd(params, preheat); - - assertEnrollmentInPreheat( - enrollmentWithoutRegistration, - preheat.getEnrollmentsWithoutRegistration(programWithoutRegistration.getUid())); - } - private void assertEnrollmentInPreheat(Enrollment expected, Enrollment actual) { assertEquals(expected.getUid(), actual.getUid()); assertEquals(expected.getProgram().getUid(), actual.getProgram().getUid());