diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/tei/TeiQueryParamPostProcessor.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/tei/TeiQueryParamPostProcessor.java index 49970ad8f2fb..715eefd6ade9 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/tei/TeiQueryParamPostProcessor.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/tei/TeiQueryParamPostProcessor.java @@ -27,52 +27,26 @@ */ package org.hisp.dhis.analytics.tei; -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.Stream; import lombok.RequiredArgsConstructor; -import org.hisp.dhis.analytics.common.params.dimension.DimensionIdentifier; -import org.hisp.dhis.analytics.common.params.dimension.DimensionParam; -import org.hisp.dhis.analytics.common.params.dimension.DimensionParamType; -import org.hisp.dhis.analytics.common.processing.CommonQueryRequestMapper; import org.hisp.dhis.analytics.common.processing.Processor; import org.springframework.stereotype.Component; +/** + * This component is a hook to allow for post-processing of the {@link TeiQueryParams} before it is + * used to build the query context. + */ @Component @RequiredArgsConstructor public class TeiQueryParamPostProcessor implements Processor { - private static final String DEFAULT_PERIOD = "pe:LAST_YEAR:LAST_UPDATED"; - - private final CommonQueryRequestMapper commonQueryRequestMapper; + /** + * Processes the given {@link TeiQueryParams} and returns the processed version. + * + * @param params to be processed. + * @return the processed {@link TeiQueryParams}. + */ public TeiQueryParams process(TeiQueryParams params) { - boolean existsPeriodRestriction = - params.getCommonParams().getDimensionIdentifiers().stream() - .filter(d -> d.getDimension().isPeriodDimension()) - .anyMatch(d -> d.getDimension().hasRestrictions()); - - if (existsPeriodRestriction) { - return params; - } - - DimensionIdentifier defaultPeriod = - commonQueryRequestMapper.toDimensionIdentifier( - DEFAULT_PERIOD, - DimensionParamType.DATE_FILTERS, - params.getCommonParams().getRelativePeriodDate(), - params.getCommonParams().getDisplayProperty(), - params.getCommonParams().getPrograms(), - params.getCommonParams().getUserOrgUnit()); - - List> dimensionIdentifiers = - Stream.concat( - params.getCommonParams().getDimensionIdentifiers().stream(), - Stream.of(defaultPeriod)) - .collect(Collectors.toList()); - - return params.toBuilder() - .commonParams( - params.getCommonParams().toBuilder().dimensionIdentifiers(dimensionIdentifiers).build()) - .build(); + // do nothing for now - implement required TeiQueryParams processing here. + return params; } } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/tei/TeiQueryParamPostProcessorTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/tei/TeiQueryParamPostProcessorTest.java deleted file mode 100644 index 05d9cf81e75c..000000000000 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/tei/TeiQueryParamPostProcessorTest.java +++ /dev/null @@ -1,151 +0,0 @@ -/* - * Copyright (c) 2004-2023, University of Oslo - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * Neither the name of the HISP project nor the names of its contributors may - * be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package org.hisp.dhis.analytics.tei; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyBoolean; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.mock; - -import java.util.Collections; -import java.util.List; -import javax.annotation.Nonnull; -import org.hisp.dhis.analytics.common.params.CommonParams; -import org.hisp.dhis.analytics.common.params.dimension.DimensionIdentifier; -import org.hisp.dhis.analytics.common.params.dimension.DimensionParam; -import org.hisp.dhis.analytics.common.params.dimension.DimensionParamType; -import org.hisp.dhis.analytics.common.params.dimension.ElementWithOffset; -import org.hisp.dhis.analytics.common.processing.CommonQueryRequestMapper; -import org.hisp.dhis.analytics.common.processing.DimensionIdentifierConverter; -import org.hisp.dhis.analytics.data.DefaultDataQueryService; -import org.hisp.dhis.analytics.event.data.DefaultEventDataQueryService; -import org.hisp.dhis.common.BaseDimensionalObject; -import org.hisp.dhis.common.DimensionType; -import org.hisp.dhis.common.DimensionalObject; -import org.hisp.dhis.period.Period; -import org.hisp.dhis.program.DefaultProgramService; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -/** Unit tests for {@link TeiQueryParamPostProcessor}. */ -class TeiQueryParamPostProcessorTest { - private TeiQueryParamPostProcessor teiQueryParamPostProcessor; - - @BeforeEach - void setup() { - DefaultDataQueryService dataQueryService = mock(DefaultDataQueryService.class); - DefaultEventDataQueryService eventDataQueryService = mock(DefaultEventDataQueryService.class); - DefaultProgramService programService = mock(DefaultProgramService.class); - - doAnswer(invocationOnMock -> mockPeriod()) - .when(dataQueryService) - .getDimension(any(), any(), any(), any(), anyBoolean(), any(), any()); - - CommonQueryRequestMapper commonQueryRequestMapper = - new CommonQueryRequestMapper( - dataQueryService, - eventDataQueryService, - programService, - new DimensionIdentifierConverter()); - - teiQueryParamPostProcessor = new TeiQueryParamPostProcessor(commonQueryRequestMapper); - } - - @Nonnull - private static DimensionalObject mockPeriod() { - return new BaseDimensionalObject("pe", DimensionType.PERIOD, List.of(new Period())); - } - - @Test - void verifyDefaultPeriodIsAddedIfPeriodIsMissing() { - // Given - TeiQueryParams params = - TeiQueryParams.builder() - .commonParams(CommonParams.builder().dimensionIdentifiers(List.of()).build()) - .build(); - - // When - params = teiQueryParamPostProcessor.process(params); - - // Then - assertEquals(1, params.getCommonParams().getDimensionIdentifiers().size()); - } - - @Test - void verifyDefaultPeriodIsNotAddedIfPeriodIsPresent() { - // Given - TeiQueryParams params = - TeiQueryParams.builder() - .commonParams( - CommonParams.builder() - .dimensionIdentifiers( - List.of( - DimensionIdentifier.of( - ElementWithOffset.of(null, null), - ElementWithOffset.of(null, null), - DimensionParam.ofObject( - mockPeriod(), - DimensionParamType.DIMENSIONS, - List.of("LAST_12_MONTHS"))))) - .build()) - .build(); - - // When - params = teiQueryParamPostProcessor.process(params); - - // Then - assertEquals(1, params.getCommonParams().getDimensionIdentifiers().size()); - } - - @Test - void verifyDefaultPeriodIsAddedIfPeriodIsPresentWithNoItems() { - // Given - TeiQueryParams params = - TeiQueryParams.builder() - .commonParams( - CommonParams.builder() - .dimensionIdentifiers( - List.of( - DimensionIdentifier.of( - ElementWithOffset.of(null, null), - ElementWithOffset.of(null, null), - DimensionParam.ofObject( - mockPeriod(), - DimensionParamType.DIMENSIONS, - Collections.emptyList())))) - .build()) - .build(); - - // When - params = teiQueryParamPostProcessor.process(params); - - // Then - assertEquals(2, params.getCommonParams().getDimensionIdentifiers().size()); - } -} diff --git a/dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/analytics/tei/TrackedEntityQueryTest.java b/dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/analytics/tei/TrackedEntityQueryTest.java index 02e0fe16647b..32f3932eb00a 100644 --- a/dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/analytics/tei/TrackedEntityQueryTest.java +++ b/dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/analytics/tei/TrackedEntityQueryTest.java @@ -60,6 +60,7 @@ public void queryWithProgramAndProgramStageWhenTotalPagesIsFalse() { .add("dimension=ou:ImspTQPwCqd") .add("program=IpHINAT79UW") .add("asc=IpHINAT79UW.w75KJ2mc4zz") + .add("lastUpdated=LAST_YEAR") .add("totalPages=false") .add("pageSize=100") .add("page=1") @@ -2577,6 +2578,7 @@ public void queryWithProgramStatus() { new QueryParamsBuilder() .add("program=IpHINAT79UW") .add("programStatus=IpHINAT79UW.COMPLETED") + .add("lastUpdated=LAST_YEAR") .add("desc=lastupdated") .add("relativePeriodDate=2018-01-01"); @@ -2630,6 +2632,7 @@ public void queryWithEnrollmentStatus() { new QueryParamsBuilder() .add("program=IpHINAT79UW") .add("enrollmentStatus=IpHINAT79UW.COMPLETED") + .add("lastUpdated=LAST_YEAR") .add("desc=lastupdated") .add("relativePeriodDate=2018-01-01"); @@ -2683,6 +2686,7 @@ public void queryWithEventStatus() { new QueryParamsBuilder() .add("program=IpHINAT79UW") .add("eventStatus=IpHINAT79UW.A03MvHHogjR.COMPLETED") + .add("lastUpdated=LAST_YEAR") .add("desc=lastupdated") .add("relativePeriodDate=2018-01-01");