Skip to content

Commit

Permalink
chore: Remove method getByPrograms(List<Program>) [DHIS2-17712] (#18139)
Browse files Browse the repository at this point in the history
* chore: Remove method getByPrograms(List<Program>) [DHIS2-17712]

* chore: Remove spy annotation [DHIS2-17712]

* chore: Address PR suggestions [DHIS2-17712]

* chore: Rename to getEventProgramEnrollments [DHIS2-17712]

* chore: Rename to EnrollmentSupplier [DHIS2-17712]

* chore: Fix javadoc [DHIS2-17712]
  • Loading branch information
muilpp authored Jul 19, 2024
1 parent 2217b95 commit 1cecdf3
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,6 @@ public interface EnrollmentStore extends IdentifiableObjectStore<Enrollment> {
List<Enrollment> 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<Enrollment> getByPrograms(List<Program> programs);

/**
* Hard deletes a {@link Enrollment}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,6 @@ public List<Enrollment> getWithScheduledNotifications(
.list();
}

@Override
public List<Enrollment> getByPrograms(List<Program> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,6 @@
public interface EnrollmentStore extends IdentifiableObjectStore<Enrollment> {
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<Enrollment> getEnrollments(EnrollmentQueryParams params);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long> query = getTypedQuery(hql);

return query.getSingleResult().longValue();
}

private String buildCountEnrollmentHql(EnrollmentQueryParams params) {
return buildEnrollmentHql(params)
.getQuery()
Expand Down Expand Up @@ -127,6 +118,14 @@ public Page<Enrollment> getEnrollments(EnrollmentQueryParams params, PageParams
return getPage(pageParams, query.list(), enrollmentCount);
}

private long countEnrollments(EnrollmentQueryParams params) {
String hql = buildCountEnrollmentHql(params);

Query<Long> query = getTypedQuery(hql);

return query.getSingleResult().longValue();
}

private Page<Enrollment> getPage(
PageParams pageParams, List<Enrollment> enrollments, LongSupplier enrollmentCount) {
if (pageParams.isPageTotal()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -58,7 +58,7 @@ public class TrackerPreheatConfig {
ClassBasedSupplier.class,
DefaultsSupplier.class,
TrackedEntityEnrollmentSupplier.class,
EnrollmentSupplier.class,
EventProgramEnrollmentSupplier.class,
EnrollmentsWithAtLeastOneEventSupplier.class,
EventProgramStageMapSupplier.class,
ProgramOrgUnitsSupplier.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Program> 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<Enrollment> enrollments =
DetachUtils.detach(
EnrollmentMapper.INSTANCE,
enrollmentStore.getByPrograms(programsWithoutRegistration));
List<Enrollment> 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<Enrollment> getEventProgramEnrollments() {
TypedQuery<Enrollment> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) {
if (trackerObjects.getEnrollments().isEmpty()) return;

for (List<String> trackedEntityListSubList : trackedEntities) {
queryTeiAndAddToMap(trackedEntityToEnrollmentMap, trackedEntityListSubList, programList);
queryTeAndAddToMap(trackedEntityToEnrollmentMap, trackedEntityListSubList, programList);
}

preheat.setTrackedEntityToEnrollmentMap(trackedEntityToEnrollmentMap);
}

private void queryTeiAndAddToMap(
private void queryTeAndAddToMap(
Map<String, List<Enrollment>> trackedEntityToEnrollmentMap,
List<String> trackedEntityListSubList,
List<String> programList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Enrollment> enrollments;

Expand All @@ -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());
Expand All @@ -96,62 +87,29 @@ 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<String> 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);

assertEnrollmentInPreheat(
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());
Expand Down

0 comments on commit 1cecdf3

Please sign in to comment.