Skip to content

Commit

Permalink
fix: Run most preprocessors only for CREATE and UPDATE [DHIS2-17402] (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
enricocolasante authored Jun 14, 2024
1 parent be61867 commit 70b0155
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
package org.hisp.dhis.tracker.imports.preprocess;

import org.hisp.dhis.tracker.imports.TrackerImportStrategy;
import org.hisp.dhis.tracker.imports.bundle.TrackerBundle;

/**
Expand All @@ -40,4 +41,8 @@
*/
public interface BundlePreProcessor {
void process(TrackerBundle bundle);

default boolean needsToRun(TrackerImportStrategy strategy) {
return !strategy.isDelete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public void setPreProcessors(List<BundlePreProcessor> preProcessors) {
@Override
public TrackerBundle preprocess(TrackerBundle bundle) {
for (BundlePreProcessor preProcessor : preProcessors) {
preProcessor.process(bundle);
if (preProcessor.needsToRun(bundle.getImportStrategy())) {
preProcessor.process(bundle);
}
}

return bundle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
*/
package org.hisp.dhis.tracker.imports.preprocess;

import static java.util.Objects.nonNull;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import org.hisp.dhis.program.Program;
import org.hisp.dhis.program.ProgramStage;
import org.hisp.dhis.tracker.imports.TrackerIdSchemeParams;
Expand All @@ -51,14 +51,19 @@ public class EventProgramPreProcessor implements BundlePreProcessor {
public void process(TrackerBundle bundle) {
List<Event> eventsToPreprocess =
bundle.getEvents().stream()
.filter(e -> e.getProgram().isBlank() || e.getProgramStage().isBlank())
.collect(Collectors.toList());
.filter(
e ->
e.getProgram() == null
|| e.getProgram().isBlank()
|| e.getProgramStage() == null
|| e.getProgramStage().isBlank())
.toList();

for (Event event : eventsToPreprocess) {
// Extract program from program stage
if (event.getProgramStage().isNotBlank()) {
if (nonNull(event.getProgramStage()) && event.getProgramStage().isNotBlank()) {
ProgramStage programStage = bundle.getPreheat().getProgramStage(event.getProgramStage());
if (Objects.nonNull(programStage)) {
if (nonNull(programStage)) {
// TODO remove if once metadata import is fixed
if (programStage.getProgram() == null) {
// Program stages should always have a program! Due to
Expand All @@ -84,9 +89,9 @@ public void process(TrackerBundle bundle) {
}
}
// If it is a program event, extract program stage from program
else if (event.getProgram().isNotBlank()) {
else if (nonNull(event.getProgram()) && event.getProgram().isNotBlank()) {
Program program = bundle.getPreheat().getProgram(event.getProgram());
if (Objects.nonNull(program) && program.isWithoutRegistration()) {
if (nonNull(program) && program.isWithoutRegistration()) {
Optional<ProgramStage> programStage = program.getProgramStages().stream().findFirst();
if (programStage.isPresent()) {
TrackerIdSchemeParams idSchemes = bundle.getPreheat().getIdSchemes();
Expand All @@ -109,7 +114,7 @@ private void setAttributeOptionCombo(TrackerBundle bundle) {
e.getAttributeOptionCombo().isBlank()
&& !e.getAttributeCategoryOptions().isEmpty())
.filter(e -> preheat.getProgram(e.getProgram()) != null)
.collect(Collectors.toList());
.toList();

for (Event e : events) {
Program program = preheat.getProgram(e.getProgram());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,9 @@ public void preProcessRelationships(TrackerBundle bundle) {
}
}
}

@Override
public boolean needsToRun(TrackerImportStrategy strategy) {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static org.hisp.dhis.DhisConvenienceTest.createProgram;
import static org.hisp.dhis.DhisConvenienceTest.createProgramStage;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
Expand Down Expand Up @@ -201,6 +202,20 @@ void testTrackerEventIsNotEnhancedWithProgramStage() {
assertEquals(MetadataIdentifier.EMPTY_UID, bundle.getEvents().get(0).getProgramStage());
}

@Test
void shouldNotProcessEventWhenEventIsInvalidWithNoProgramAndNoProgramStage() {
Event event = invalidProgramEventWithNoProgramAndNoProgramStage();
TrackerBundle bundle =
TrackerBundle.builder().events(Collections.singletonList(event)).preheat(preheat).build();

preprocessor.process(bundle);

verify(preheat, never()).getProgram(PROGRAM_WITH_REGISTRATION);
verify(preheat, never()).getProgramStage(PROGRAM_STAGE_WITH_REGISTRATION);
assertNull(bundle.getEvents().get(0).getProgram());
assertNull(bundle.getEvents().get(0).getProgramStage());
}

@Test
void testProgramEventWithProgramAndProgramStageIsNotProcessed() {
TrackerIdSchemeParams identifierParams = TrackerIdSchemeParams.builder().build();
Expand Down Expand Up @@ -442,6 +457,14 @@ private Program programWithoutRegistration() {
return program;
}

private Event invalidProgramEventWithNoProgramAndNoProgramStage() {
return Event.builder()
.program(null)
.programStage(null)
.attributeOptionCombo(MetadataIdentifier.EMPTY_UID)
.build();
}

private Event programEventWithProgram() {
return Event.builder()
.program(MetadataIdentifier.ofUid(PROGRAM_WITHOUT_REGISTRATION))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,7 @@
"trackedEntities": [],
"enrollments": [
{
"enrollment": "TvctPPhpD8u",
"createdAtClient": "2017-01-26T13:48:13.363",
"trackedEntity": "NCc1rCEOKaY",
"program": {
"idScheme": "UID",
"identifier": "E8o1E9tAppy"
},
"orgUnit": {
"idScheme": "UID",
"identifier": "QfUVllTs6cS"
}
"enrollment": "TvctPPhpD8u"
}
],
"events": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,51 +34,7 @@
"enrollments": [],
"events": [
{
"event": "D9PbzJY8bJ1",
"status": "COMPLETED",
"program": {
"idScheme": "UID",
"identifier": "E8o1E9tAppy"
},
"programStage": {
"idScheme": "UID",
"identifier": "Qmqxq907VNz"
},
"enrollment": "TvctPPhpD8v",
"orgUnit": {
"idScheme": "UID",
"identifier": "QfUVllTs6cZ"
},
"relationships": [],
"occurredAt": "2019-01-28T00:00:00.000",
"scheduledAt": "2019-01-28T12:10:38.100",
"storedBy": "admin",
"deleted": false,
"attributeOptionCombo": {
"idScheme": "UID",
"identifier": "KKKKX50cXC0"
},
"attributeCategoryOptions": [
{
"idScheme": "UID",
"identifier": "xYerKDKCefk"
}
],

"completedAt": "2019-01-28T00:00:00.000",
"dataValues": [
{
"createdAt": "2019-01-28T12:10:38.113",
"storedBy": "admin",
"providedElsewhere": false,
"dataElement": {
"idScheme": "UID",
"identifier": "DSKTW8qFP0z"
},
"value": "2017-01-27T17:00:00.000Z"
}
],
"notes": []
"event": "D9PbzJY8bJ1"
}
],
"relationships": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,7 @@
"trackedEntities": [],
"enrollments": [
{
"enrollment": "TvctPPhpD8w",
"createdAtClient": "2017-01-26T13:48:13.363",
"trackedEntity": "NCc1rCEOKaY",
"program": {
"idScheme": "UID",
"identifier": "E8o1E9tAppy"
},
"status": "ACTIVE",
"orgUnit": {
"idScheme": "UID",
"identifier": "QfUVllTs6cS"
},
"orgUnitName": "Mbokie CHP",
"enrolledAt": "2021-03-28T12:05:00.000",
"occurredAt": "2021-03-28T12:05:00.000",
"followUp": false,
"deleted": false,
"events": [],
"relationships": [],
"attributes": [],
"notes": []
"enrollment": "TvctPPhpD8w"
}
],
"events": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,63 +32,12 @@
"skipRuleEngine": false,
"trackedEntities": [
{
"trackedEntity": "PB9VMezGkwA",
"trackedEntityType": {
"idScheme": "UID",
"identifier": "bPJ0FMtcnEh"
},
"orgUnit": {
"idScheme": "UID",
"identifier": "QfUVllTs6cS"
},
"inactive": false,
"deleted": false,
"potentialDuplicate": false,
"relationships": [],
"attributes": [
{
"attribute": {
"idScheme": "UID",
"identifier": "fmBIpOStKkF"
},
"storedBy": "admin",
"value": "PersonA"
},
{
"attribute": {
"idScheme": "UID",
"identifier": "sTJvSLN7Kcb"
},
"storedBy": "admin",
"value": "AddressA"
}
],
"enrollments": []
"trackedEntity": "PB9VMezGkwA"
}
],
"enrollments": [
{
"enrollment": "TvctPPhpD8u",
"createdAtClient": "2017-01-26T13:48:13.363",
"trackedEntity": "NCc1rCEOKaY",
"program": {
"idScheme": "UID",
"identifier": "E8o1E9tAppy"
},
"status": "ACTIVE",
"orgUnit": {
"idScheme": "UID",
"identifier": "QfUVllTs6cS"
},
"orgUnitName": "Mbokie CHP",
"enrolledAt": "2021-03-28T12:05:00.000",
"occurredAt": "2021-03-28T12:05:00.000",
"followUp": false,
"deleted": false,
"events": [],
"relationships": [],
"attributes": [],
"notes": []
"enrollment": "TvctPPhpD8u"
}
],
"events": [],
Expand Down

0 comments on commit 70b0155

Please sign in to comment.