diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEnrollmentAnalyticsManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEnrollmentAnalyticsManager.java index e49a6f80932f..2add8cd0def1 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEnrollmentAnalyticsManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEnrollmentAnalyticsManager.java @@ -446,6 +446,13 @@ protected String getWhereClause(EventQueryParams params) { sql += "and ps = '" + params.getProgramStage().getUid() + "' "; } + // --------------------------------------------------------------------- + // Query items and filters + // --------------------------------------------------------------------- + if (!useExperimentalAnalyticsQueryEngine()) { + sql += getQueryItemsAndFiltersWhereClause(params, hlp); + } + // --------------------------------------------------------------------- // Filter expression // --------------------------------------------------------------------- diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/programindicator/DefaultProgramIndicatorSubqueryBuilder.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/programindicator/DefaultProgramIndicatorSubqueryBuilder.java index 916d635d4fb6..e751ef4b73f7 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/programindicator/DefaultProgramIndicatorSubqueryBuilder.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/programindicator/DefaultProgramIndicatorSubqueryBuilder.java @@ -45,6 +45,7 @@ import org.hisp.dhis.program.ProgramIndicator; import org.hisp.dhis.program.ProgramIndicatorService; import org.hisp.dhis.relationship.RelationshipType; +import org.hisp.dhis.setting.SystemSettingsService; import org.springframework.stereotype.Component; @Component @@ -58,6 +59,7 @@ public class DefaultProgramIndicatorSubqueryBuilder implements ProgramIndicatorS private static final String SUBQUERY_TABLE_ALIAS = "subax"; private final ProgramIndicatorService programIndicatorService; + private final SystemSettingsService settingsService; @Override public String getAggregateClauseForProgramIndicator( @@ -247,14 +249,12 @@ private String getWhere( RelationshipTypeJoinGenerator.generate( SUBQUERY_TABLE_ALIAS, relationshipType, programIndicator.getAnalyticsType()); } else { - // Remove the reference to the outer query's enrollment - // We'll handle the join in the main query - if (AnalyticsType.ENROLLMENT == programIndicator.getAnalyticsType()) { - // No condition needed, we'll join on enrollment in the main query - condition = ""; - } else if (AnalyticsType.EVENT == programIndicator.getAnalyticsType()) { - // Handle event type if needed - condition = ""; + if (AnalyticsType.ENROLLMENT == outerSqlEntity) { + condition = useExperimentalAnalyticsQueryEngine() ? "" : "enrollment = ax.enrollment"; + } else { + if (AnalyticsType.EVENT == programIndicator.getAnalyticsType()) { + condition = useExperimentalAnalyticsQueryEngine() ? "" : "event = ax.event"; + } } } @@ -285,4 +285,8 @@ private String getProgramIndicatorSql( latestDate, SUBQUERY_TABLE_ALIAS); } + + protected boolean useExperimentalAnalyticsQueryEngine() { + return this.settingsService.getCurrentSettings().getUseExperimentalAnalyticsQueryEngine(); + } } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/AbstractJdbcEventAnalyticsManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/AbstractJdbcEventAnalyticsManagerTest.java index c78346771352..c74f08b63ce6 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/AbstractJdbcEventAnalyticsManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/AbstractJdbcEventAnalyticsManagerTest.java @@ -104,6 +104,7 @@ import org.hisp.dhis.program.Program; import org.hisp.dhis.program.ProgramIndicator; import org.hisp.dhis.program.ProgramIndicatorService; +import org.hisp.dhis.setting.SystemSettingsService; import org.hisp.dhis.system.grid.ListGrid; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -129,9 +130,11 @@ class AbstractJdbcEventAnalyticsManagerTest extends EventAnalyticsTest { @Mock private OrganisationUnitService organisationUnitService; + @Mock private SystemSettingsService systemSettingsService; + @Spy private ProgramIndicatorSubqueryBuilder programIndicatorSubqueryBuilder = - new DefaultProgramIndicatorSubqueryBuilder(programIndicatorService); + new DefaultProgramIndicatorSubqueryBuilder(programIndicatorService, systemSettingsService); @Spy private SqlBuilder sqlBuilder = new PostgreSqlBuilder(); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EnrollmentAnalyticsManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EnrollmentAnalyticsManagerTest.java index 4417e627a89e..27b7344c906c 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EnrollmentAnalyticsManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EnrollmentAnalyticsManagerTest.java @@ -76,6 +76,7 @@ import org.hisp.dhis.relationship.RelationshipConstraint; import org.hisp.dhis.relationship.RelationshipEntity; import org.hisp.dhis.relationship.RelationshipType; +import org.hisp.dhis.setting.SystemSettings; import org.hisp.dhis.setting.SystemSettingsService; import org.hisp.dhis.system.grid.ListGrid; import org.hisp.dhis.test.random.BeanRandomizer; @@ -117,6 +118,8 @@ class EnrollmentAnalyticsManagerTest extends EventAnalyticsTest { private EnrollmentTimeFieldSqlRenderer enrollmentTimeFieldSqlRenderer = new EnrollmentTimeFieldSqlRenderer(sqlBuilder); + @Spy private SystemSettings systemSettings; + @Captor private ArgumentCaptor sql; private String DEFAULT_COLUMNS = @@ -131,9 +134,9 @@ class EnrollmentAnalyticsManagerTest extends EventAnalyticsTest { @BeforeEach public void setUp() { when(jdbcTemplate.queryForRowSet(anyString())).thenReturn(this.rowSet); - + when(systemSettingsService.getCurrentSettings()).thenReturn(systemSettings); DefaultProgramIndicatorSubqueryBuilder programIndicatorSubqueryBuilder = - new DefaultProgramIndicatorSubqueryBuilder(programIndicatorService); + new DefaultProgramIndicatorSubqueryBuilder(programIndicatorService, systemSettingsService); subject = new JdbcEnrollmentAnalyticsManager( diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventAnalyticsManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventAnalyticsManagerTest.java index e24fc20f598c..f09065537227 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventAnalyticsManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventAnalyticsManagerTest.java @@ -132,7 +132,7 @@ public void setUp() { EventTimeFieldSqlRenderer timeCoordinateSelector = new EventTimeFieldSqlRenderer(sqlBuilder); ProgramIndicatorService programIndicatorService = mock(ProgramIndicatorService.class); DefaultProgramIndicatorSubqueryBuilder programIndicatorSubqueryBuilder = - new DefaultProgramIndicatorSubqueryBuilder(programIndicatorService); + new DefaultProgramIndicatorSubqueryBuilder(programIndicatorService, systemSettingsService); subject = new JdbcEventAnalyticsManager( diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/programindicator/ProgramIndicatorSubqueryBuilderTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/programindicator/ProgramIndicatorSubqueryBuilderTest.java index 0b8057d8ec3c..384d20a5731c 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/programindicator/ProgramIndicatorSubqueryBuilderTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/programindicator/ProgramIndicatorSubqueryBuilderTest.java @@ -43,18 +43,24 @@ import org.hisp.dhis.program.ProgramIndicatorService; import org.hisp.dhis.relationship.RelationshipEntity; import org.hisp.dhis.relationship.RelationshipType; +import org.hisp.dhis.setting.SystemSettings; +import org.hisp.dhis.setting.SystemSettingsService; import org.hisp.dhis.test.random.BeanRandomizer; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.Spy; import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; /** * @author Luciano Fiandesio */ -@ExtendWith(MockitoExtension.class) +@ExtendWith({MockitoExtension.class}) +@MockitoSettings(strictness = Strictness.LENIENT) class ProgramIndicatorSubqueryBuilderTest { private static final String DUMMY_EXPRESSION = "#{1234567}"; @@ -70,13 +76,18 @@ class ProgramIndicatorSubqueryBuilderTest { @Mock private ProgramIndicatorService programIndicatorService; + @Mock private SystemSettingsService systemSettingsService; + @InjectMocks private DefaultProgramIndicatorSubqueryBuilder subject; + @Spy private SystemSettings systemSettings; + @BeforeEach public void setUp() { program = createProgram('A'); startDate = getDate(2018, 1, 1); endDate = getDate(2018, 6, 30); + when(systemSettingsService.getCurrentSettings()).thenReturn(systemSettings); } @Test diff --git a/dhis-2/dhis-services/dhis-service-setting/src/test/java/org/hisp/dhis/setting/SystemSettingsTest.java b/dhis-2/dhis-services/dhis-service-setting/src/test/java/org/hisp/dhis/setting/SystemSettingsTest.java index e630fbe07a28..87447c405042 100644 --- a/dhis-2/dhis-services/dhis-service-setting/src/test/java/org/hisp/dhis/setting/SystemSettingsTest.java +++ b/dhis-2/dhis-services/dhis-service-setting/src/test/java/org/hisp/dhis/setting/SystemSettingsTest.java @@ -93,10 +93,11 @@ void testIsTranslatable() { @Test void testKeysWithDefaults() { Set keys = SystemSettings.keysWithDefaults(); - assertEquals(139, keys.size()); + assertEquals(140, keys.size()); // just check some at random assertTrue(keys.contains("syncSkipSyncForDataChangedBefore")); assertTrue(keys.contains("keyTrackerDashboardLayout")); + assertTrue(keys.contains("experimentalAnalyticsSqlEngineEnabled")); } @Test