From 5fdf098d07aa2b8ca9d1554cf569e4aa2f1de1c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Mon, 25 Nov 2024 21:34:57 +0100 Subject: [PATCH 001/110] fix: Update code --- .../table/AbstractEventJdbcTableManager.java | 13 ++++++------- .../JdbcValidationResultTableManager.java | 10 ++++------ .../DimensionIdentifierHelperTest.java | 19 ------------------- .../CommonRequestParamsMapperTest.java | 3 +-- .../query/NotConditionRendererTest.java | 1 - .../AnalyticsServiceReportingRateTest.java | 2 +- 6 files changed, 12 insertions(+), 36 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java index be74348ffd8f..0b07dced4214 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java @@ -286,24 +286,23 @@ protected List getTrackedEntityAttributeColumns(Program pr * The select statement used by the table population. * * @param attribute the {@link TrackedEntityAttribute}. - * @param fromType the sql snippet related to "from" part + * @param columnExpression the column expression. * @param dataClause the data type related clause like "NUMERIC". * @return a select statement. */ protected String selectForInsert( - TrackedEntityAttribute attribute, String fromType, String dataClause) { + TrackedEntityAttribute attribute, String columnExpression, String dataClause) { return replaceQualify( """ - (select ${fromType} from ${trackedentityattributevalue} \ + (select ${columnExpression} from ${trackedentityattributevalue} \ where trackedentityid=en.trackedentityid \ - and trackedentityattributeid=${attributeId}\ - ${dataClause})\ + and trackedentityattributeid=${attributeId}${dataClause})\ ${closingParentheses} as ${attributeUid}""", Map.of( - "fromType", fromType, + "columnExpression", columnExpression, "dataClause", dataClause, "attributeId", String.valueOf(attribute.getId()), - "closingParentheses", getClosingParentheses(fromType), + "closingParentheses", getClosingParentheses(columnExpression), "attributeUid", quote(attribute.getUid()))); } } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcValidationResultTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcValidationResultTableManager.java index 8d7ab2d85e0f..e25bd5756486 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcValidationResultTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcValidationResultTableManager.java @@ -31,7 +31,6 @@ import static org.hisp.dhis.commons.util.TextUtils.emptyIfTrue; import static org.hisp.dhis.commons.util.TextUtils.format; import static org.hisp.dhis.commons.util.TextUtils.removeLastComma; -import static org.hisp.dhis.commons.util.TextUtils.replace; import static org.hisp.dhis.db.model.DataType.CHARACTER_11; import static org.hisp.dhis.db.model.DataType.DATE; import static org.hisp.dhis.db.model.DataType.INTEGER; @@ -213,9 +212,9 @@ private List getDataYears(AnalyticsTableUpdateParams params) { String fromDateClause = params.getFromDate() == null ? "" - : replace( - "and ps.startdate >= '${fromDate}'", - Map.of("fromDate", DateUtils.toMediumDate(params.getFromDate()))); + : String.format( + " and ps.startdate >= '%s'", DateUtils.toMediumDate(params.getFromDate())); + String sql = replaceQualify( """ @@ -223,8 +222,7 @@ select distinct(extract(year from ps.startdate)) \ from ${validationresult} vrs \ inner join analytics_rs_periodstructure ps on vrs.periodid=ps.periodid \ where ps.startdate is not null \ - and vrs.created < '${startTime}' - ${fromDateClause}""", + and vrs.created < '${startTime}'${fromDateClause}""", Map.of( "startTime", toLongDate(params.getStartTime()), "fromDateClause", fromDateClause)); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/common/params/dimension/DimensionIdentifierHelperTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/common/params/dimension/DimensionIdentifierHelperTest.java index f4c5f57ce8b1..e45a6728764d 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/common/params/dimension/DimensionIdentifierHelperTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/common/params/dimension/DimensionIdentifierHelperTest.java @@ -35,17 +35,13 @@ import org.hisp.dhis.common.IllegalQueryException; import org.junit.jupiter.api.Test; -/** Unit tests for {@link DimensionIdentifierHelper} */ class DimensionIdentifierHelperTest { @Test void testFromFullDimensionId() { - // Given String fullDimensionId = "lxAQ7Zs9VYR[0].RaMbOrTys0n[4].bh1Edk21e2n"; - // When StringDimensionIdentifier dimensionIdentifier = fromFullDimensionId(fullDimensionId); - // Then assertEquals( "bh1Edk21e2n", dimensionIdentifier.getDimension().getUid(), @@ -64,12 +60,9 @@ void testFromFullDimensionId() { @Test void testFromDimensionIdWithUnsupportedOffset() { - // Given String dimensionIdWithDashOffset = "lxAQ7Zs9VYR[-].bh1Edk21e2n"; String dimensionIdWithStringOffset = "lxAQ7Zs9VYR[X].bh1Edk21e2n"; - // When - // Then assertThrows(IllegalQueryException.class, () -> fromFullDimensionId(dimensionIdWithDashOffset)); assertThrows( IllegalQueryException.class, () -> fromFullDimensionId(dimensionIdWithStringOffset)); @@ -77,13 +70,10 @@ void testFromDimensionIdWithUnsupportedOffset() { @Test void testFromFullDimensionIdWithSingleDimension() { - // Given String singleDimensionId = "bh1Edk21e2n"; - // When StringDimensionIdentifier dimensionIdentifier = fromFullDimensionId(singleDimensionId); - // Then assertEquals( "bh1Edk21e2n", dimensionIdentifier.getDimension().getUid(), @@ -96,13 +86,10 @@ void testFromFullDimensionIdWithSingleDimension() { @Test void testFromFullDimensionIdWithProgramAndDimension() { - // Given String singleDimensionId = "lxAQ7Zs9VYR.bh1Edk21e2n"; - // When StringDimensionIdentifier dimensionIdentifier = fromFullDimensionId(singleDimensionId); - // Then assertEquals( "bh1Edk21e2n", dimensionIdentifier.getDimension().getUid(), @@ -117,15 +104,12 @@ void testFromFullDimensionIdWithProgramAndDimension() { @Test void testFromFullDimensionIdWhenSingleDimensionHasOffset() { - // Given String singleDimensionWithOffset = "bh1Edk21e2n[2]"; - // When IllegalArgumentException thrown = assertThrows( IllegalArgumentException.class, () -> fromFullDimensionId(singleDimensionWithOffset)); - // Then assertEquals( "Only program and program stage can have offset", thrown.getMessage(), @@ -134,15 +118,12 @@ void testFromFullDimensionIdWhenSingleDimensionHasOffset() { @Test void testFromFullDimensionIdWhenDimensionHasInvalidFormat() { - // Given String invalidFullDimensionId = "lxAQ7Zs9VYR[1].RaMbOrTys0n[4].bh1Edk21e2n.invalid-id"; - // When IllegalArgumentException thrown = assertThrows( IllegalArgumentException.class, () -> fromFullDimensionId(invalidFullDimensionId)); - // Then assertEquals( "Invalid dimension identifier: " + invalidFullDimensionId, thrown.getMessage(), diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/common/processing/CommonRequestParamsMapperTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/common/processing/CommonRequestParamsMapperTest.java index a2657a931d0e..4b1e883c9dae 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/common/processing/CommonRequestParamsMapperTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/common/processing/CommonRequestParamsMapperTest.java @@ -435,8 +435,7 @@ void mapWhenProgramsCannotBeFound() { programService, dimensionIdentifierConverter); - // List has only one Program, but the CommonQueryRequest, below, has - // two. + // List has only one Program, but the CommonQueryRequest, has two List programs = List.of(program1); CommonRequestParams commonRequestParams = diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/common/query/NotConditionRendererTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/common/query/NotConditionRendererTest.java index 20de5f79ff9c..b3ab91830786 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/common/query/NotConditionRendererTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/common/query/NotConditionRendererTest.java @@ -32,7 +32,6 @@ import org.junit.jupiter.api.Test; class NotConditionRendererTest { - @Test void testInWithSingleValueProduceCorrectSql() { Renderable renderable = () -> "test"; diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/AnalyticsServiceReportingRateTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/AnalyticsServiceReportingRateTest.java index 12f852c8e464..975fd51f7844 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/AnalyticsServiceReportingRateTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/AnalyticsServiceReportingRateTest.java @@ -102,7 +102,7 @@ void verifyReportingRatesValueWhenPeriodIsFilter() { DataQueryParams.newBuilder() .withOrganisationUnit(ou) // DATA ELEMENTS - .withDataElements(newArrayList(reportingRateA, reportingRateB, reportingRateC)) + .withDataElements(List.of(reportingRateA, reportingRateB, reportingRateC)) .withIgnoreLimit(true) // FILTERS (OU) .withFilters(List.of(new BaseDimensionalObject("pe", DimensionType.PERIOD, periods))) From 12f1f27f5236331791cf41086173ddb726ba5c28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Mon, 25 Nov 2024 21:41:04 +0100 Subject: [PATCH 002/110] fix: Update code --- .../data/DefaultDataQueryService.java | 2 +- ...er.java => DimensionalObjectProvider.java} | 2 +- .../outlier/data/OutlierQueryParser.java | 4 +-- .../service/AnalyticsOutlierService.java | 4 +-- ...aQueryServiceDimensionItemKeywordTest.java | 2 +- .../analytics/data/DataQueryServiceTest.java | 2 +- ...ava => DimensionalObjectProviderTest.java} | 6 ++--- .../data/OrgUnitTableJoinerTest.java | 24 ++++++++--------- .../analytics/data/QueryItemHelperTest.java | 16 ++++++------ .../SubexpressionPeriodOffsetUtilsTest.java | 26 +++++++++---------- .../data/handler/DataHandlerTest.java | 16 ------------ .../handler/SchemeIdResponseMapperTest.java | 8 +----- ...AbstractJdbcEventAnalyticsManagerTest.java | 1 + .../data/ColumnWithNullIfAndAliasTest.java | 6 ----- .../outlier/data/OutlierQueryParserTest.java | 4 +-- .../controller/AnalyticsControllerTest.java | 4 +-- 16 files changed, 50 insertions(+), 77 deletions(-) rename dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/{DimensionalObjectProducer.java => DimensionalObjectProvider.java} (99%) rename dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/{DimensionalObjectProducerTest.java => DimensionalObjectProviderTest.java} (99%) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultDataQueryService.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultDataQueryService.java index 9d821881af0d..6ed9ff5c5838 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultDataQueryService.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultDataQueryService.java @@ -93,7 +93,7 @@ @Service("org.hisp.dhis.analytics.DataQueryService") @RequiredArgsConstructor public class DefaultDataQueryService implements DataQueryService { - private final DimensionalObjectProducer dimensionalObjectProducer; + private final DimensionalObjectProvider dimensionalObjectProducer; private final IdentifiableObjectManager idObjectManager; diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DimensionalObjectProducer.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DimensionalObjectProvider.java similarity index 99% rename from dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DimensionalObjectProducer.java rename to dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DimensionalObjectProvider.java index c776b35d49f0..c247113b657f 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DimensionalObjectProducer.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DimensionalObjectProvider.java @@ -116,7 +116,7 @@ */ @Component @RequiredArgsConstructor -public class DimensionalObjectProducer { +public class DimensionalObjectProvider { private final IdentifiableObjectManager idObjectManager; private final OrganisationUnitService organisationUnitService; diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/outlier/data/OutlierQueryParser.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/outlier/data/OutlierQueryParser.java index a64dec95202b..65b350d257c9 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/outlier/data/OutlierQueryParser.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/outlier/data/OutlierQueryParser.java @@ -39,7 +39,7 @@ import java.util.UUID; import lombok.AllArgsConstructor; import org.apache.commons.lang3.StringUtils; -import org.hisp.dhis.analytics.data.DimensionalObjectProducer; +import org.hisp.dhis.analytics.data.DimensionalObjectProvider; import org.hisp.dhis.category.CategoryOptionCombo; import org.hisp.dhis.common.BaseDimensionalObject; import org.hisp.dhis.common.DisplayProperty; @@ -60,7 +60,7 @@ @AllArgsConstructor public class OutlierQueryParser { private final IdentifiableObjectManager idObjectManager; - private final DimensionalObjectProducer dimensionalObjectProducer; + private final DimensionalObjectProvider dimensionalObjectProducer; private final UserService userService; /** diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/outlier/service/AnalyticsOutlierService.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/outlier/service/AnalyticsOutlierService.java index 9b7ed5e7a7d1..8dad2acf3de9 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/outlier/service/AnalyticsOutlierService.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/outlier/service/AnalyticsOutlierService.java @@ -63,7 +63,7 @@ import org.hisp.dhis.analytics.cache.OutliersCache; import org.hisp.dhis.analytics.common.ColumnHeader; import org.hisp.dhis.analytics.common.TableInfoReader; -import org.hisp.dhis.analytics.data.DimensionalObjectProducer; +import org.hisp.dhis.analytics.data.DimensionalObjectProvider; import org.hisp.dhis.analytics.outlier.data.Outlier; import org.hisp.dhis.analytics.outlier.data.OutlierRequest; import org.hisp.dhis.category.CategoryOptionCombo; @@ -104,7 +104,7 @@ public class AnalyticsOutlierService { private final IdentifiableObjectManager idObjectManager; - private final DimensionalObjectProducer dimensionalObjectProducer; + private final DimensionalObjectProvider dimensionalObjectProducer; /** * Transform the incoming request into api response (json). diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/DataQueryServiceDimensionItemKeywordTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/DataQueryServiceDimensionItemKeywordTest.java index 399c9c4741fa..622f2b45975f 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/DataQueryServiceDimensionItemKeywordTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/DataQueryServiceDimensionItemKeywordTest.java @@ -122,7 +122,7 @@ class DataQueryServiceDimensionItemKeywordTest { @Mock private I18n i18n; - @InjectMocks private DimensionalObjectProducer dimensionalObjectProducer; + @InjectMocks private DimensionalObjectProvider dimensionalObjectProducer; private DefaultDataQueryService target; diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/DataQueryServiceTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/DataQueryServiceTest.java index 0ea3ea985f43..11e9e08e6ee8 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/DataQueryServiceTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/DataQueryServiceTest.java @@ -52,7 +52,7 @@ @ExtendWith(MockitoExtension.class) class DataQueryServiceTest { - @Mock private DimensionalObjectProducer dimensionalObjectProducer; + @Mock private DimensionalObjectProvider dimensionalObjectProducer; @Mock private IdentifiableObjectManager idObjectManager; diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/DimensionalObjectProducerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/DimensionalObjectProviderTest.java similarity index 99% rename from dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/DimensionalObjectProducerTest.java rename to dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/DimensionalObjectProviderTest.java index 3389f894a488..d0277d6514a5 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/DimensionalObjectProducerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/DimensionalObjectProviderTest.java @@ -110,12 +110,12 @@ import org.mockito.junit.jupiter.MockitoExtension; /** - * Unit tests for {@link DimensionalObjectProducer}. + * Unit tests for {@link DimensionalObjectProvider}. * * @author maikel arabori */ @ExtendWith(MockitoExtension.class) -class DimensionalObjectProducerTest { +class DimensionalObjectProviderTest { @Mock private IdentifiableObjectManager idObjectManager; @@ -135,7 +135,7 @@ class DimensionalObjectProducerTest { @Mock private I18nFormat i18nFormat; - @InjectMocks private DimensionalObjectProducer target; + @InjectMocks private DimensionalObjectProvider target; @BeforeEach public void setUp() { diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/OrgUnitTableJoinerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/OrgUnitTableJoinerTest.java index f9053ebcac55..0ad45adc592f 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/OrgUnitTableJoinerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/OrgUnitTableJoinerTest.java @@ -55,29 +55,29 @@ * @author Jim Grace */ class OrgUnitTableJoinerTest extends TestBase { - private static final OrgUnitField DEFAULT = new OrgUnitField(null); + private final OrgUnitField DEFAULT = new OrgUnitField(null); - private static final OrgUnitField ATTRIBUTE = new OrgUnitField("AttributeId"); + private final OrgUnitField ATTRIBUTE = new OrgUnitField("AttributeId"); - private static final OrgUnitField OWNER_AT_START = new OrgUnitField("OWNER_AT_START"); + private final OrgUnitField OWNER_AT_START = new OrgUnitField("OWNER_AT_START"); - private static final OrgUnitField OWNER_AT_END = new OrgUnitField("OWNER_AT_END"); + private final OrgUnitField OWNER_AT_END = new OrgUnitField("OWNER_AT_END"); - private static final Program programA = createProgram('A'); + private final Program programA = createProgram('A'); - private static final Period periodDaily = PeriodType.getPeriodFromIsoString("20230101"); + private final Period periodDaily = PeriodType.getPeriodFromIsoString("20230101"); - private static final Period periodMonthly = PeriodType.getPeriodFromIsoString("202201"); + private final Period periodMonthly = PeriodType.getPeriodFromIsoString("202201"); - private static final Period periodQuarterly = PeriodType.getPeriodFromIsoString("2022Q1"); + private final Period periodQuarterly = PeriodType.getPeriodFromIsoString("2022Q1"); - private static final Date dateA = new GregorianCalendar(2022, JANUARY, 1).getTime(); + private final Date dateA = new GregorianCalendar(2022, JANUARY, 1).getTime(); - private static final Date dateB = new GregorianCalendar(2023, JANUARY, 1).getTime(); + private final Date dateB = new GregorianCalendar(2023, JANUARY, 1).getTime(); - private static final DimensionalItemObject ouA = createOrganisationUnit('A'); + private final DimensionalItemObject ouA = createOrganisationUnit('A'); - private static final DimensionalObject ouGroupSetA = + private final DimensionalObject ouGroupSetA = new BaseDimensionalObject( "OrgUnitGrSe", DimensionType.ORGANISATION_UNIT_GROUP_SET, emptyList()); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/QueryItemHelperTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/QueryItemHelperTest.java index 1ff002351e6a..370a5198df97 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/QueryItemHelperTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/QueryItemHelperTest.java @@ -60,21 +60,21 @@ * @author Dusan Bernat */ class QueryItemHelperTest extends TestBase { - private String UID_A = CodeGenerator.generateUid(); + private final String UID_A = CodeGenerator.generateUid(); - private String UID_B = CodeGenerator.generateUid(); + private final String UID_B = CodeGenerator.generateUid(); - private String OPTION_NAME_A = "OptionA"; + private final String OPTION_NAME_A = "OptionA"; - private String OPTION_NAME_B = "OptionB"; + private final String OPTION_NAME_B = "OptionB"; - private String LEGEND_NAME_A = "LegendA"; + private final String LEGEND_NAME_A = "LegendA"; - private String LEGEND_NAME_B = "LegendB"; + private final String LEGEND_NAME_B = "LegendB"; - private String LEGEND_CODE_A = "LegendCodeA"; + private final String LEGEND_CODE_A = "LegendCodeA"; - private String LEGEND_CODE_B = "LegendCodeB"; + private final String LEGEND_CODE_B = "LegendCodeB"; @Test void testGeItemOptionValueWithIdSchemeNAME() { diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/SubexpressionPeriodOffsetUtilsTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/SubexpressionPeriodOffsetUtilsTest.java index 5ba3757e7a35..10d80dcab6ee 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/SubexpressionPeriodOffsetUtilsTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/SubexpressionPeriodOffsetUtilsTest.java @@ -55,36 +55,36 @@ */ @TestInstance(TestInstance.Lifecycle.PER_CLASS) class SubexpressionPeriodOffsetUtilsTest { - private DataElement de0 = createDataElement('A'); + private final DataElement de0 = createDataElement('A'); - private DataElement dem1 = createDataElementAWithPeriodOffset(-1); + private final DataElement dem1 = createDataElementAWithPeriodOffset(-1); - private DataElement dem2 = createDataElementAWithPeriodOffset(-2); + private final DataElement dem2 = createDataElementAWithPeriodOffset(-2); - private DataElement dep1 = createDataElementAWithPeriodOffset(1); + private final DataElement dep1 = createDataElementAWithPeriodOffset(1); - private String deAUid = de0.getUid(); + private final String deAUid = de0.getUid(); - private String expression = + private final String expression = format( "subExpression( #{%s} + #{%s}.periodOffset(-1) + #{%s}.periodOffset(-2) + #{%s}.periodOffset(1) )", deAUid, deAUid, deAUid, deAUid); - private List items = List.of(de0, dem1, dem2, dep1); + private final List items = List.of(de0, dem1, dem2, dep1); - private SubexpressionDimensionItem subex = + private final SubexpressionDimensionItem subExpr = new SubexpressionDimensionItem(expression, items, null); - private Period periodA = createPeriod("202309"); + private final Period periodA = createPeriod("202309"); - private Period periodB = createPeriod("202310"); + private final Period periodB = createPeriod("202310"); - private DataQueryParams params = + private final DataQueryParams params = DataQueryParams.newBuilder() .withPeriodType("monthly") .withPeriods(List.of(periodA, periodB)) .addDimension( - new BaseDimensionalObject(DATA_X_DIM_ID, DimensionType.DATA_X, getList(subex))) + new BaseDimensionalObject(DATA_X_DIM_ID, DimensionType.DATA_X, getList(subExpr))) .build(); @Test @@ -120,7 +120,7 @@ void testGetParamsWithDataPeriods() { getPeriodList("202307", "202308", "202309", "202310", "202311"); assertContainsOnly(expectedPeriods, result.getPeriods()); - List expectedData = List.of(subex); + List expectedData = List.of(subExpr); assertContainsOnly(expectedData, result.getAllDataDimensionItems()); } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/handler/DataHandlerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/handler/DataHandlerTest.java index 1b72da84cfc6..79766d139804 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/handler/DataHandlerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/handler/DataHandlerTest.java @@ -43,12 +43,10 @@ import org.hisp.dhis.dataelement.DataElementOperand.TotalType; import org.junit.jupiter.api.Test; -/** Unit tests for {@link DataHandler}. */ class DataHandlerTest { @Test void testOperandDataQueryParamsOnlyOperands() { - // Given DataElement dataElement = new DataElement("NameA"); dataElement.setUid("uid1234567A"); dataElement.setCode("CodeA"); @@ -63,19 +61,16 @@ void testOperandDataQueryParamsOnlyOperands() { TotalType anyTotalType = COC_ONLY; - // When DataHandler dataHandler = withNullDependencies(); DataQueryParams params = dataHandler.getOperandDataQueryParams( stubParams, List.of(dataElementOperand), anyTotalType); - // Then assertEquals(1, params.getDimensions().size()); } @Test void testOperandDataQueryParamsWithCatOptionCombo() { - // Given DataElement dataElement = new DataElement("NameA"); dataElement.setUid("uid1234567A"); dataElement.setCode("CodeA"); @@ -94,18 +89,15 @@ void testOperandDataQueryParamsWithCatOptionCombo() { new BaseDimensionalObject(DATA_X_DIM_ID, DATA_X, List.of(dataElementOperand))) .build(); - // When DataHandler dataHandler = withNullDependencies(); DataQueryParams params = dataHandler.getOperandDataQueryParams(stubParams, List.of(dataElementOperand), COC_ONLY); - // Then assertEquals(2, params.getDimensions().size()); } @Test void testOperandDataQueryParamsWithCatOptionComboInFilter() { - // Given DataElement dataElement = new DataElement("NameA"); dataElement.setUid("uid1234567A"); dataElement.setCode("CodeA"); @@ -124,19 +116,16 @@ void testOperandDataQueryParamsWithCatOptionComboInFilter() { new BaseDimensionalObject(DATA_X_DIM_ID, DATA_X, List.of(dataElementOperand))) .build(); - // When DataHandler dataHandler = withNullDependencies(); DataQueryParams params = dataHandler.getOperandDataQueryParams(stubParams, List.of(dataElementOperand), COC_ONLY); - // Then assertEquals(0, params.getDimensions().size()); assertEquals(3, params.getFilters().size()); } @Test void testOperandDataQueryParamsWithAttrOptionCombo() { - // Given DataElement dataElement = new DataElement("NameA"); dataElement.setUid("uid1234567A"); dataElement.setCode("CodeA"); @@ -155,18 +144,15 @@ void testOperandDataQueryParamsWithAttrOptionCombo() { new BaseDimensionalObject(DATA_X_DIM_ID, DATA_X, List.of(dataElementOperand))) .build(); - // When DataHandler dataHandler = withNullDependencies(); DataQueryParams params = dataHandler.getOperandDataQueryParams(stubParams, List.of(dataElementOperand), AOC_ONLY); - // Then assertEquals(2, params.getDimensions().size()); } @Test void testOperandDataQueryParamsWithAttrOptionComboInFilter() { - // Given DataElement dataElement = new DataElement("NameA"); dataElement.setUid("uid1234567A"); dataElement.setCode("CodeA"); @@ -185,12 +171,10 @@ void testOperandDataQueryParamsWithAttrOptionComboInFilter() { new BaseDimensionalObject(DATA_X_DIM_ID, DATA_X, List.of(dataElementOperand))) .build(); - // When DataHandler dataHandler = withNullDependencies(); DataQueryParams params = dataHandler.getOperandDataQueryParams(stubParams, List.of(dataElementOperand), AOC_ONLY); - // Then assertEquals(0, params.getDimensions().size()); assertEquals(3, params.getFilters().size()); } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/handler/SchemeIdResponseMapperTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/handler/SchemeIdResponseMapperTest.java index 2f2d8008920c..edd44fbee2e6 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/handler/SchemeIdResponseMapperTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/handler/SchemeIdResponseMapperTest.java @@ -69,7 +69,6 @@ import org.hisp.dhis.program.Program; import org.hisp.dhis.program.ProgramIndicator; import org.hisp.dhis.system.grid.ListGrid; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; @@ -79,12 +78,7 @@ */ @ExtendWith(MockitoExtension.class) class SchemeIdResponseMapperTest { - private SchemeIdResponseMapper schemeIdResponseMapper; - - @BeforeEach - public void setUp() { - schemeIdResponseMapper = new SchemeIdResponseMapper(); - } + private final SchemeIdResponseMapper schemeIdResponseMapper = new SchemeIdResponseMapper(); @Test void testGetSchemeIdResponseMapWhenOutputIdSchemeIsSetToName() { 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 5ae3c01b65d9..cfa4d4c09173 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 @@ -123,6 +123,7 @@ class AbstractJdbcEventAnalyticsManagerTest extends EventAnalyticsTest { @Mock private ProgramIndicatorService programIndicatorService; @Mock private ExecutionPlanStore executionPlanStore; + @Mock private OrganisationUnitService organisationUnitService; private final SqlBuilder sqlBuilder = new PostgreSqlBuilder(); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/ColumnWithNullIfAndAliasTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/ColumnWithNullIfAndAliasTest.java index 3f806ce42c14..6b0d7032775c 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/ColumnWithNullIfAndAliasTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/ColumnWithNullIfAndAliasTest.java @@ -38,12 +38,9 @@ class ColumnWithNullIfAndAliasTest { @Test void testAsSqlReturnsRightInstance() { - // given - // when ColumnAndAlias columnAndAlias = ColumnWithNullIfAndAlias.ofColumnWithNullIfAndAlias(COLUMN, ALIAS); - // then assertEquals(COLUMN, columnAndAlias.getColumn()); assertEquals(ALIAS, columnAndAlias.getAlias()); @@ -51,12 +48,9 @@ void testAsSqlReturnsRightInstance() { @Test void testAsSqlReturnsRightSqlSnippetWhenCalled() { - // given - // when ColumnAndAlias columnAndAlias = ColumnWithNullIfAndAlias.ofColumnWithNullIfAndAlias(COLUMN, ALIAS); - // then assertEquals("nullif(" + COLUMN + ",'') as \"" + ALIAS + "\"", columnAndAlias.asSql()); } } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/outlier/data/OutlierQueryParserTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/outlier/data/OutlierQueryParserTest.java index e8fac6ff1c53..686074c0d7aa 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/outlier/data/OutlierQueryParserTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/outlier/data/OutlierQueryParserTest.java @@ -41,7 +41,7 @@ import java.util.List; import java.util.Set; -import org.hisp.dhis.analytics.data.DimensionalObjectProducer; +import org.hisp.dhis.analytics.data.DimensionalObjectProvider; import org.hisp.dhis.common.BaseDimensionalObject; import org.hisp.dhis.common.DisplayProperty; import org.hisp.dhis.common.IdScheme; @@ -61,7 +61,7 @@ @ExtendWith(MockitoExtension.class) class OutlierQueryParserTest { @Mock private IdentifiableObjectManager idObjectManager; - @Mock private DimensionalObjectProducer dimensionalObjectProducer; + @Mock private DimensionalObjectProvider dimensionalObjectProducer; @Mock private UserService userService; private OutlierQueryParser subject; diff --git a/dhis-2/dhis-web-api/src/test/java/org/hisp/dhis/webapi/controller/AnalyticsControllerTest.java b/dhis-2/dhis-web-api/src/test/java/org/hisp/dhis/webapi/controller/AnalyticsControllerTest.java index 7b70ecd98743..bdd79d005960 100644 --- a/dhis-2/dhis-web-api/src/test/java/org/hisp/dhis/webapi/controller/AnalyticsControllerTest.java +++ b/dhis-2/dhis-web-api/src/test/java/org/hisp/dhis/webapi/controller/AnalyticsControllerTest.java @@ -49,7 +49,7 @@ import org.hisp.dhis.analytics.DataQueryParams; import org.hisp.dhis.analytics.DataQueryService; import org.hisp.dhis.analytics.data.DefaultDataQueryService; -import org.hisp.dhis.analytics.data.DimensionalObjectProducer; +import org.hisp.dhis.analytics.data.DimensionalObjectProvider; import org.hisp.dhis.common.BaseDimensionalObject; import org.hisp.dhis.common.DimensionService; import org.hisp.dhis.common.DimensionType; @@ -90,7 +90,7 @@ class AnalyticsControllerTest { @Mock private DimensionService dimensionService; - @Mock private DimensionalObjectProducer dimensionalObjectProducer; + @Mock private DimensionalObjectProvider dimensionalObjectProducer; @Mock private DhisConfigurationProvider dhisConfigurationProvider; From 6647eacf9f88c96df8403cbf9f259ffb8be3370c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Mon, 25 Nov 2024 21:42:35 +0100 Subject: [PATCH 003/110] fix: Update code --- .../DefaultEventCoordinateServiceTest.java | 22 +++---------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/DefaultEventCoordinateServiceTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/DefaultEventCoordinateServiceTest.java index fb665fca2f99..d7bc5b74197e 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/DefaultEventCoordinateServiceTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/DefaultEventCoordinateServiceTest.java @@ -45,6 +45,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; +import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @@ -61,14 +62,13 @@ class DefaultEventCoordinateServiceTest { @Mock private TrackedEntityAttributeService attributeService; + @InjectMocks private DefaultEventCoordinateService service; + @ParameterizedTest @ValueSource(strings = {"eventgeometry", "enrollmentgeometry", "ougeometry"}) void testGetCoordinateFieldOrFail(String geometry) { when(programService.getProgram(any(String.class))).thenReturn(createProgram('A')); - EventCoordinateService service = - new DefaultEventCoordinateService(programService, dataElementService, attributeService); - assertEquals(geometry, service.getCoordinateField("A", geometry, ErrorCode.E7232)); } @@ -76,9 +76,6 @@ void testGetCoordinateFieldOrFail(String geometry) { void testGetFallbackCoordinateFieldsWithFallbackCoordinateFieldParam() { when(programService.getProgram(any(String.class))).thenReturn(createProgram('A')); - EventCoordinateService service = - new DefaultEventCoordinateService(programService, dataElementService, attributeService); - assertEquals(COL_NAME_GEOMETRY_LIST, service.getFallbackCoordinateFields("A", null, true)); } @@ -87,27 +84,18 @@ void testGetFallbackCoordinateFieldsWithFallbackCoordinateFieldParam() { void testGetFallbackCoordinateFieldsWithoutFallbackCoordinateFieldParam(String geometry) { when(programService.getProgram(any(String.class))).thenReturn(createProgram('A')); - EventCoordinateService service = - new DefaultEventCoordinateService(programService, dataElementService, attributeService); - assertEquals(List.of(geometry), service.getFallbackCoordinateFields("A", geometry, true)); } @ParameterizedTest @ValueSource(strings = {"enrollmentgeometry", "eventgeometry", "tegeometry", "ougeometry"}) void testVerifyFallbackCoordinateFieldWithRegistrationProgram(String geometry) { - EventCoordinateService service = - new DefaultEventCoordinateService(programService, dataElementService, attributeService); - assertTrue(service.isFallbackCoordinateFieldValid(true, geometry)); } @ParameterizedTest @ValueSource(strings = {"enrollmentgeometry", "eventgeometry", "ougeometry"}) void testVerifyFallbackCoordinateFieldWithoutRegistrationProgram(String geometry) { - EventCoordinateService service = - new DefaultEventCoordinateService(programService, dataElementService, attributeService); - assertTrue(service.isFallbackCoordinateFieldValid(false, geometry)); } @@ -116,12 +104,8 @@ void testVerifyFallbackCoordinateFieldWithoutRegistrationProgram(String geometry strings = {"badeventgeometry", "badenrollmentgeometry", "badtegeometry", "badougeometry"}) void testVerifyBadFallbackCoordinateField(String geometry) { when(dataElementService.getDataElement(any(String.class))).thenReturn(null); - when(attributeService.getTrackedEntityAttribute(any(String.class))).thenReturn(null); - EventCoordinateService service = - new DefaultEventCoordinateService(programService, dataElementService, attributeService); - assertThrows( IllegalQueryException.class, () -> service.isFallbackCoordinateFieldValid(false, geometry)); } From 50825c4a37a7dcd3da40d1b94209da4b9eed7dde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Mon, 25 Nov 2024 21:45:04 +0100 Subject: [PATCH 004/110] fix: Update code --- .../data/EnrollmentAggregateServiceTest.java | 22 +++++-------------- .../event/data/EventQueryServiceTest.java | 19 +++------------- .../event/data/EventQueryValidatorTest.java | 1 + 3 files changed, 9 insertions(+), 33 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EnrollmentAggregateServiceTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EnrollmentAggregateServiceTest.java index 6987fa94483c..dec8679348ac 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EnrollmentAggregateServiceTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EnrollmentAggregateServiceTest.java @@ -69,9 +69,9 @@ import org.hisp.dhis.program.ProgramStage; import org.hisp.dhis.user.SystemUser; import org.junit.jupiter.api.BeforeAll; -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.junit.jupiter.MockitoExtension; import org.opengis.geometry.primitive.Point; @@ -84,8 +84,6 @@ @ExtendWith(MockitoExtension.class) class EnrollmentAggregateServiceTest { - private EnrollmentAggregateService dummyAnalyticsService; - @Mock private AnalyticsSecurityManager securityManager; @Mock private EnrollmentAnalyticsManager enrollmentAnalyticsManager; @@ -98,23 +96,13 @@ class EnrollmentAggregateServiceTest { @Mock private SchemeIdHandler schemeIdHandler; + @InjectMocks private EnrollmentAggregateService service; + @BeforeAll static void setup() { injectSecurityContextNoSettings(new SystemUser()); } - @BeforeEach - public void setUp() { - dummyAnalyticsService = - new EnrollmentAggregateService( - enrollmentAnalyticsManager, - queryPlanner, - securityManager, - queryValidator, - metadataHandler, - schemeIdHandler); - } - @Test void verifyHeaderCreationBasedOnQueryItemsAndDimensions() { // Given @@ -146,7 +134,7 @@ void verifyHeaderCreationBasedOnQueryItemsAndDimensions() { // When when(securityManager.withUserConstraints(any(EventQueryParams.class))).thenReturn(params); - Grid grid = dummyAnalyticsService.getEnrollments(params); + Grid grid = service.getEnrollments(params); // Then List headers = grid.getHeaders(); @@ -215,7 +203,7 @@ void verifyHeaderCreationBasedOnQueryItemsAndDimensionsWithSameNamesMultiStage() // When when(securityManager.withUserConstraints(any(EventQueryParams.class))).thenReturn(params); - Grid grid = dummyAnalyticsService.getEnrollments(params); + Grid grid = service.getEnrollments(params); // Then List headers = grid.getHeaders(); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventQueryServiceTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventQueryServiceTest.java index 0de1cb91b8cf..4d5b916e4475 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventQueryServiceTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventQueryServiceTest.java @@ -58,9 +58,9 @@ import org.hisp.dhis.system.database.DatabaseInfoProvider; import org.hisp.dhis.user.SystemUser; import org.junit.jupiter.api.BeforeAll; -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.junit.jupiter.MockitoExtension; @@ -71,8 +71,6 @@ */ @ExtendWith(MockitoExtension.class) class EventQueryServiceTest { - @Mock private EventQueryService eventQueryService; - @Mock private EventQueryValidator queryValidator; @Mock private MetadataItemsHandler metadataHandler; @@ -91,24 +89,13 @@ class EventQueryServiceTest { @Mock private SchemeIdResponseMapper schemeIdResponseMapper; + @InjectMocks private EventQueryService eventQueryService; + @BeforeAll static void setup() { injectSecurityContextNoSettings(new SystemUser()); } - @BeforeEach - public void setUp() { - eventQueryService = - new EventQueryService( - securityManager, - queryValidator, - eventAnalyticsManager, - queryPlanner, - databaseInfoProvider, - metadataHandler, - schemeIdHandler); - } - @Test void testOutputSchemeWhenSchemeIsSet() { IdScheme codeScheme = IdScheme.CODE; diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventQueryValidatorTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventQueryValidatorTest.java index f2c27791b050..d67cb83988af 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventQueryValidatorTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EventQueryValidatorTest.java @@ -89,6 +89,7 @@ class EventQueryValidatorTest extends TestBase { private OptionSet osA; @Mock private SystemSettingsProvider settingsProvider; + @Mock private SystemSettings settings; @Mock private QueryValidator queryValidator; From 719655c4e62838b9d41eaad939148b64bd43f659 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Mon, 25 Nov 2024 21:46:46 +0100 Subject: [PATCH 005/110] fix: Update code --- .../ProgramIndicatorSubqueryBuilderTest.java | 8 ++++---- .../event/data/queryItem/QueryItemLocatorTest.java | 14 ++------------ 2 files changed, 6 insertions(+), 16 deletions(-) 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 3bfee00382e5..0b8057d8ec3c 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 @@ -47,6 +47,7 @@ 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.junit.jupiter.MockitoExtension; @@ -61,22 +62,21 @@ class ProgramIndicatorSubqueryBuilderTest { private static final BeanRandomizer rnd = BeanRandomizer.create(); - @Mock private ProgramIndicatorService programIndicatorService; - private Program program; private Date startDate; private Date endDate; - private DefaultProgramIndicatorSubqueryBuilder subject; + @Mock private ProgramIndicatorService programIndicatorService; + + @InjectMocks private DefaultProgramIndicatorSubqueryBuilder subject; @BeforeEach public void setUp() { program = createProgram('A'); startDate = getDate(2018, 1, 1); endDate = getDate(2018, 6, 30); - subject = new DefaultProgramIndicatorSubqueryBuilder(programIndicatorService); } @Test diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/queryItem/QueryItemLocatorTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/queryItem/QueryItemLocatorTest.java index 512baf4b19ad..97f0a0837eaf 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/queryItem/QueryItemLocatorTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/queryItem/QueryItemLocatorTest.java @@ -53,7 +53,6 @@ import java.util.Set; import org.hisp.dhis.analytics.DataQueryService; import org.hisp.dhis.analytics.EventOutputType; -import org.hisp.dhis.analytics.event.QueryItemLocator; import org.hisp.dhis.analytics.event.data.DefaultQueryItemLocator; import org.hisp.dhis.common.BaseDimensionalObject; import org.hisp.dhis.common.CodeGenerator; @@ -78,6 +77,7 @@ 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.junit.jupiter.MockitoExtension; @@ -100,7 +100,7 @@ class QueryItemLocatorTest { @Mock private RelationshipTypeService relationshipTypeService; - private QueryItemLocator subject; + @InjectMocks private DefaultQueryItemLocator subject; private Program programA; @@ -114,16 +114,6 @@ public void setUp() { dimension = CodeGenerator.generateUid(); programStageUid = CodeGenerator.generateUid(); - - subject = - new DefaultQueryItemLocator( - programStageService, - dataElementService, - attributeService, - programIndicatorService, - legendSetService, - relationshipTypeService, - dataQueryService); } @Test From c5863b157123a0322f7e98bd81ce10477a64377b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Mon, 25 Nov 2024 21:47:27 +0100 Subject: [PATCH 006/110] fix: Update code --- .../data/JdbcOrgUnitAnalyticsManagerTest.java | 19 ++++++------------- .../outlier/data/OutlierQueryParserTest.java | 3 +++ 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/orgunit/data/JdbcOrgUnitAnalyticsManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/orgunit/data/JdbcOrgUnitAnalyticsManagerTest.java index 7172035e84d5..8464921c6686 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/orgunit/data/JdbcOrgUnitAnalyticsManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/orgunit/data/JdbcOrgUnitAnalyticsManagerTest.java @@ -43,10 +43,11 @@ import org.hisp.dhis.db.sql.PostgreSqlBuilder; import org.hisp.dhis.db.sql.SqlBuilder; import org.hisp.dhis.organisationunit.OrganisationUnitGroupSet; -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.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.support.rowset.SqlRowSet; @@ -65,15 +66,9 @@ class JdbcOrgUnitAnalyticsManagerTest { @Mock private TableInfoReader tableInfoReader; - private final SqlBuilder sqlBuilder = new PostgreSqlBuilder(); + @Spy private final SqlBuilder sqlBuilder = new PostgreSqlBuilder(); - private JdbcOrgUnitAnalyticsManager jdbcOrgUnitAnalyticsManager; - - @BeforeEach - public void beforeAll() { - jdbcOrgUnitAnalyticsManager = - new JdbcOrgUnitAnalyticsManager(tableInfoReader, sqlBuilder, jdbcTemplate); - } + @InjectMocks private JdbcOrgUnitAnalyticsManager manager; @Test void testGetOrgUnitDataWithSuccess() { @@ -97,7 +92,7 @@ void testGetOrgUnitDataWithSuccess() { "analytics_rs_organisationunitgroupsetstructure", Set.of("abc123", "abc456"))) .thenReturn(Set.of()); when(jdbcTemplate.queryForRowSet(anyString())).thenReturn(sqlRowSet); - Map data = jdbcOrgUnitAnalyticsManager.getOrgUnitData(params); + Map data = manager.getOrgUnitData(params); // Then // Based on the mocked sqlRowSet. @@ -128,9 +123,7 @@ void testGetOrgUnitDataWithInvalidOrgUnitSetDimension() { // Then assertThrows( - QueryRuntimeException.class, - () -> jdbcOrgUnitAnalyticsManager.getOrgUnitData(params), - E7302.getMessage()); + QueryRuntimeException.class, () -> manager.getOrgUnitData(params), E7302.getMessage()); } private void mockSqlRowSet() { diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/outlier/data/OutlierQueryParserTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/outlier/data/OutlierQueryParserTest.java index 686074c0d7aa..de9163de50d4 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/outlier/data/OutlierQueryParserTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/outlier/data/OutlierQueryParserTest.java @@ -61,8 +61,11 @@ @ExtendWith(MockitoExtension.class) class OutlierQueryParserTest { @Mock private IdentifiableObjectManager idObjectManager; + @Mock private DimensionalObjectProvider dimensionalObjectProducer; + @Mock private UserService userService; + private OutlierQueryParser subject; @BeforeEach From d2095b99854d9566255c998e30583b2798bc90b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Mon, 25 Nov 2024 21:50:58 +0100 Subject: [PATCH 007/110] fix: Update code --- .../outlier/data/OutlierQueryParserTest.java | 5 ++--- ...nalyticsZscoreSqlStatementProcessorTest.java | 4 ---- .../CategoryOptionGroupResolverTest.java | 7 ++----- .../resolver/CategoryOptionResolverTest.java | 17 +++++------------ .../resolver/DataElementGroupResolverTest.java | 13 +++---------- 5 files changed, 12 insertions(+), 34 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/outlier/data/OutlierQueryParserTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/outlier/data/OutlierQueryParserTest.java index de9163de50d4..99118eb6e230 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/outlier/data/OutlierQueryParserTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/outlier/data/OutlierQueryParserTest.java @@ -55,6 +55,7 @@ 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.junit.jupiter.MockitoExtension; @@ -66,7 +67,7 @@ class OutlierQueryParserTest { @Mock private UserService userService; - private OutlierQueryParser subject; + @InjectMocks private OutlierQueryParser subject; @BeforeEach void setup() { @@ -91,8 +92,6 @@ void setup() { user.setDataViewOrganisationUnits(Set.of(organisationUnit)); injectSecurityContextNoSettings(UserDetails.fromUser(user)); when(userService.getUserByUsername(anyString())).thenReturn(user); - - subject = new OutlierQueryParser(idObjectManager, dimensionalObjectProducer, userService); } @Test diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/outlier/service/AnalyticsZscoreSqlStatementProcessorTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/outlier/service/AnalyticsZscoreSqlStatementProcessorTest.java index 270d5c3f3c2b..bcc312f02f10 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/outlier/service/AnalyticsZscoreSqlStatementProcessorTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/outlier/service/AnalyticsZscoreSqlStatementProcessorTest.java @@ -57,10 +57,6 @@ class AnalyticsZscoreSqlStatementProcessorTest { private OutlierSqlStatementProcessor subject; - // ------------------------------------------------------------------------- - // Fixture - // ------------------------------------------------------------------------- - private List dataDimensions; private OrganisationUnit ouA; diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/resolver/CategoryOptionGroupResolverTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/resolver/CategoryOptionGroupResolverTest.java index 71c106898d56..e0778c4cd4b5 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/resolver/CategoryOptionGroupResolverTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/resolver/CategoryOptionGroupResolverTest.java @@ -47,6 +47,7 @@ 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.junit.jupiter.MockitoExtension; @@ -62,7 +63,7 @@ class CategoryOptionGroupResolverTest { @Mock private ExpressionService expressionService; - private ExpressionResolver resolver; + @InjectMocks private CategoryOptionGroupResolver resolver; private String uid1; @@ -89,10 +90,6 @@ public void setUp() { coc1 = createCategoryOptionCombo('X'); coc2 = createCategoryOptionCombo('Y'); coc3 = createCategoryOptionCombo('Z'); - - resolver = - new CategoryOptionGroupResolver( - expressionService, categoryOptionGroupStore, categoryOptionComboStore); } @Test diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/resolver/CategoryOptionResolverTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/resolver/CategoryOptionResolverTest.java index d5debfd3cec8..d1a62b9e4d1a 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/resolver/CategoryOptionResolverTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/resolver/CategoryOptionResolverTest.java @@ -46,6 +46,7 @@ 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.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoSettings; @@ -61,7 +62,7 @@ class CategoryOptionResolverTest { @Mock private ExpressionService expressionService; - private ExpressionResolver resolver; + @InjectMocks private CategoryOptionResolver resolver; private String uid1; @@ -75,34 +76,26 @@ class CategoryOptionResolverTest { private CategoryOptionCombo coc3; - DimensionalItemId dimensionalItemId; + private DimensionalItemId dimensionalItemId; private static final String CATEGORY_OPTION_PREFIX = "co:"; @BeforeEach public void setUp() { uid1 = CodeGenerator.generateUid(); - uid2 = CodeGenerator.generateUid(); - uid3 = CodeGenerator.generateUid(); CategoryOption categoryOption = createCategoryOption('A'); coc1 = createCategoryOptionCombo('X'); - - categoryOption.addCategoryOptionCombo(coc1); - coc2 = createCategoryOptionCombo('Y'); - - categoryOption.addCategoryOptionCombo(coc2); - coc3 = createCategoryOptionCombo('Z'); + categoryOption.addCategoryOptionCombo(coc1); + categoryOption.addCategoryOptionCombo(coc2); categoryOption.addCategoryOptionCombo(coc3); - resolver = new CategoryOptionResolver(expressionService, categoryOptionStore); - when(categoryOptionStore.getByUid(anyString())).thenReturn(categoryOption); } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/resolver/DataElementGroupResolverTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/resolver/DataElementGroupResolverTest.java index f5fb99dfcc85..597370b76a0a 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/resolver/DataElementGroupResolverTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/resolver/DataElementGroupResolverTest.java @@ -46,6 +46,7 @@ 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.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoSettings; @@ -61,7 +62,7 @@ class DataElementGroupResolverTest { @Mock private ExpressionService expressionService; - private ExpressionResolver resolver; + @InjectMocks private DataElementGroupResolver resolver; private String uid1; @@ -75,34 +76,26 @@ class DataElementGroupResolverTest { private DataElement de3; - DimensionalItemId dimensionalItemId; + private DimensionalItemId dimensionalItemId; private static final String DATA_ELEMENT_GROUP_PREFIX = "deGroup:"; @BeforeEach public void setUp() { uid1 = CodeGenerator.generateUid(); - uid2 = CodeGenerator.generateUid(); - uid3 = CodeGenerator.generateUid(); de1 = createDataElement('X'); - de2 = createDataElement('Y'); - de3 = createDataElement('Z'); DataElementGroup dataElementGroup = createDataElementGroup('A'); dataElementGroup.addDataElement(de1); - dataElementGroup.addDataElement(de2); - dataElementGroup.addDataElement(de3); - resolver = new DataElementGroupResolver(expressionService, dataElementGroupStore); - when(dataElementGroupStore.getByUid(anyString())).thenReturn(dataElementGroup); } From 18b97820242c318b835d1e05c2795171307e064b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Mon, 25 Nov 2024 21:53:14 +0100 Subject: [PATCH 008/110] fix: Update code --- .../hisp/dhis/analytics/table/AnalyticsTableServiceTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/AnalyticsTableServiceTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/AnalyticsTableServiceTest.java index 5caaa63a71ea..91389524f5de 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/AnalyticsTableServiceTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/AnalyticsTableServiceTest.java @@ -55,8 +55,8 @@ */ @ExtendWith(MockitoExtension.class) class AnalyticsTableServiceTest { - @Mock private SystemSettingsProvider settingsProvider; + @Mock private SystemSettings settings; @Mock private SqlBuilder sqlBuilder; From d1520e375106fee62f9b3e4705e1f10bf2d8d275 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Mon, 25 Nov 2024 21:54:09 +0100 Subject: [PATCH 009/110] fix: Update code --- ...dbcOwnershipAnalyticsTableManagerTest.java | 34 ++++++------------- ...rackedEntityAnalyticsTableManagerTest.java | 1 - 2 files changed, 10 insertions(+), 25 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcOwnershipAnalyticsTableManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcOwnershipAnalyticsTableManagerTest.java index 9ec3e6847827..b399999ff371 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcOwnershipAnalyticsTableManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcOwnershipAnalyticsTableManagerTest.java @@ -87,8 +87,10 @@ 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.MockedStatic; +import org.mockito.Spy; import org.mockito.invocation.Invocation; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.jdbc.core.JdbcTemplate; @@ -135,7 +137,9 @@ class JdbcOwnershipAnalyticsTableManagerTest extends TestBase { @Mock private PeriodDataProvider periodDataProvider; - private final SqlBuilder sqlBuilder = new PostgreSqlBuilder(); + @Spy private final SqlBuilder sqlBuilder = new PostgreSqlBuilder(); + + @InjectMocks private JdbcOwnershipAnalyticsTableManager manager; private static final Program programA = createProgram('A'); @@ -147,27 +151,9 @@ class JdbcOwnershipAnalyticsTableManagerTest extends TestBase { private static AnalyticsTablePartition partitionA; - private JdbcOwnershipAnalyticsTableManager target; - @BeforeEach public void setUp() { lenient().when(settingsProvider.getCurrentSettings()).thenReturn(SystemSettings.of(Map.of())); - target = - new JdbcOwnershipAnalyticsTableManager( - idObjectManager, - organisationUnitService, - categoryService, - settingsProvider, - dataApprovalLevelService, - resourceTableService, - tableHookService, - partitionManager, - databaseInfoProvider, - jdbcTemplate, - jdbcConfiguration, - analyticsTableSettings, - periodDataProvider, - sqlBuilder); tableA = new AnalyticsTable( @@ -188,7 +174,7 @@ public void setUp() { @Test void testGetAnalyticsTableType() { - assertEquals(AnalyticsTableType.OWNERSHIP, target.getAnalyticsTableType()); + assertEquals(AnalyticsTableType.OWNERSHIP, manager.getAnalyticsTableType()); } @Test @@ -197,19 +183,19 @@ void testGetAnalyticsTables() { AnalyticsTableUpdateParams params = AnalyticsTableUpdateParams.newBuilder().build(); - assertEquals(List.of(tableA, tableB), target.getAnalyticsTables(params)); + assertEquals(List.of(tableA, tableB), manager.getAnalyticsTables(params)); params = AnalyticsTableUpdateParams.newBuilder() .lastYears(AnalyticsTablePartition.LATEST_PARTITION) .build(); - assertEquals(emptyList(), target.getAnalyticsTables(params)); + assertEquals(emptyList(), manager.getAnalyticsTables(params)); } @Test void testGetPartitionChecks() { - assertTrue(target.getPartitionChecks(1, new Date()).isEmpty()); + assertTrue(manager.getPartitionChecks(1, new Date()).isEmpty()); } @Test @@ -275,7 +261,7 @@ void testPopulateTable() throws SQLException { try (MockedStatic mocked = mockStatic(JdbcOwnershipWriter.class)) { mocked.when(() -> JdbcOwnershipWriter.getInstance(any())).thenReturn(writer); - target.populateTable(params, partitionA); + manager.populateTable(params, partitionA); } List jdbcInvocations = getInvocations(jdbcTemplate); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManagerTest.java index 5a7eefacbaf4..606d62214bbe 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManagerTest.java @@ -68,7 +68,6 @@ @ExtendWith(MockitoExtension.class) class JdbcTrackedEntityAnalyticsTableManagerTest { - @Mock private JdbcTemplate jdbcTemplate; @Mock private AnalyticsTableSettings analyticsTableSettings; @Mock private PeriodDataProvider periodDataProvider; From 6c0b5a60d57a39a0a5cd1dab570fe996e3e0acd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Mon, 25 Nov 2024 21:56:36 +0100 Subject: [PATCH 010/110] fix: Update code --- .../analytics/orgunit/data/JdbcOrgUnitAnalyticsManagerTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/orgunit/data/JdbcOrgUnitAnalyticsManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/orgunit/data/JdbcOrgUnitAnalyticsManagerTest.java index 8464921c6686..27bdea994a78 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/orgunit/data/JdbcOrgUnitAnalyticsManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/orgunit/data/JdbcOrgUnitAnalyticsManagerTest.java @@ -127,9 +127,7 @@ void testGetOrgUnitDataWithInvalidOrgUnitSetDimension() { } private void mockSqlRowSet() { - // Simulate 2 results. when(sqlRowSet.next()).thenReturn(true).thenReturn(true).thenReturn(false); - when(sqlRowSet.getString("orgunit")).thenReturn("OrgUnit"); when(sqlRowSet.getString("abc123")).thenReturn("Abc123"); when(sqlRowSet.getString("abc456")).thenReturn("Abc456"); From 7d42bb0f963a1ee2b9fc93d69c440a3ffdd37a84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Mon, 25 Nov 2024 21:59:47 +0100 Subject: [PATCH 011/110] fix: Update code --- .../analytics/resolver/CategoryOptionGroupResolverTest.java | 4 ++-- .../dhis/analytics/resolver/CategoryOptionResolverTest.java | 2 +- .../dhis/analytics/resolver/DataElementGroupResolverTest.java | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/resolver/CategoryOptionGroupResolverTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/resolver/CategoryOptionGroupResolverTest.java index e0778c4cd4b5..ae5a6bb91c6f 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/resolver/CategoryOptionGroupResolverTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/resolver/CategoryOptionGroupResolverTest.java @@ -77,9 +77,9 @@ class CategoryOptionGroupResolverTest { private CategoryOptionCombo coc3; - DimensionalItemId dimensionalItemId; + private DimensionalItemId dimensionalItemId; - private static final String CATEGORY_OPTION_GROUP_PREFIX = "coGroup:"; + private final String CATEGORY_OPTION_GROUP_PREFIX = "coGroup:"; @BeforeEach public void setUp() { diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/resolver/CategoryOptionResolverTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/resolver/CategoryOptionResolverTest.java index d1a62b9e4d1a..245cc06733af 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/resolver/CategoryOptionResolverTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/resolver/CategoryOptionResolverTest.java @@ -78,7 +78,7 @@ class CategoryOptionResolverTest { private DimensionalItemId dimensionalItemId; - private static final String CATEGORY_OPTION_PREFIX = "co:"; + private final String CATEGORY_OPTION_PREFIX = "co:"; @BeforeEach public void setUp() { diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/resolver/DataElementGroupResolverTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/resolver/DataElementGroupResolverTest.java index 597370b76a0a..dc47bf1e99a0 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/resolver/DataElementGroupResolverTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/resolver/DataElementGroupResolverTest.java @@ -78,7 +78,7 @@ class DataElementGroupResolverTest { private DimensionalItemId dimensionalItemId; - private static final String DATA_ELEMENT_GROUP_PREFIX = "deGroup:"; + private final String DATA_ELEMENT_GROUP_PREFIX = "deGroup:"; @BeforeEach public void setUp() { From cb3d5237c77829e0769a234666bc39daca61659f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 26 Nov 2024 15:41:24 +0100 Subject: [PATCH 012/110] fix: Update code --- .../hisp/dhis/analytics/common/DimensionServiceCommonTest.java | 3 +-- .../programindicator/ProgramIndicatorSubqueryBuilderTest.java | 1 - .../dhis/analytics/table/model/AnalyticsTableColumnTest.java | 2 -- 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/common/DimensionServiceCommonTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/common/DimensionServiceCommonTest.java index 1a9377fd362d..4df1d8129c22 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/common/DimensionServiceCommonTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/common/DimensionServiceCommonTest.java @@ -30,8 +30,7 @@ import java.util.function.Predicate; import org.hisp.dhis.common.ValueType; -public class DimensionServiceCommonTest { - +class DimensionServiceCommonTest { public static Predicate queryDisallowedValueTypesPredicate() { return DimensionsServiceCommon.QUERY_DISALLOWED_VALUE_TYPES::contains; } 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..30675b3e9b83 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 @@ -35,7 +35,6 @@ import static org.hisp.dhis.test.TestBase.createProgramIndicator; import static org.hisp.dhis.test.TestBase.getDate; import static org.mockito.Mockito.when; - import java.util.Date; import org.hisp.dhis.program.AnalyticsType; import org.hisp.dhis.program.Program; diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/model/AnalyticsTableColumnTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/model/AnalyticsTableColumnTest.java index 3492631a5a36..6ea7c108a985 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/model/AnalyticsTableColumnTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/model/AnalyticsTableColumnTest.java @@ -33,7 +33,6 @@ import static org.hisp.dhis.db.model.constraint.Nullable.NULL; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; - import org.hisp.dhis.db.model.Collation; import org.junit.jupiter.api.Test; @@ -41,7 +40,6 @@ * @author Lars Helge Overland */ class AnalyticsTableColumnTest { - @Test void testIsNotNull() { AnalyticsTableColumn colA = From 9f369e42005ee717f6cda1dd43d0b99d40501868 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 26 Nov 2024 15:41:39 +0100 Subject: [PATCH 013/110] fix: Update code --- .../hisp/dhis/analytics/common/DimensionServiceCommonTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/common/DimensionServiceCommonTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/common/DimensionServiceCommonTest.java index 4df1d8129c22..1a9377fd362d 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/common/DimensionServiceCommonTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/common/DimensionServiceCommonTest.java @@ -30,7 +30,8 @@ import java.util.function.Predicate; import org.hisp.dhis.common.ValueType; -class DimensionServiceCommonTest { +public class DimensionServiceCommonTest { + public static Predicate queryDisallowedValueTypesPredicate() { return DimensionsServiceCommon.QUERY_DISALLOWED_VALUE_TYPES::contains; } From acccd5da5bfe48bc29b0232e3305f42daf736989 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 26 Nov 2024 15:43:59 +0100 Subject: [PATCH 014/110] fix: Update code --- .../table/scheduling/ContinuousAnalyticsTableJobTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/scheduling/ContinuousAnalyticsTableJobTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/scheduling/ContinuousAnalyticsTableJobTest.java index cd4c7b92641a..339100075c5e 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/scheduling/ContinuousAnalyticsTableJobTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/scheduling/ContinuousAnalyticsTableJobTest.java @@ -31,7 +31,6 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.when; - import java.util.Date; import org.hisp.dhis.analytics.AnalyticsTableGenerator; import org.hisp.dhis.analytics.common.TableInfoReader; @@ -48,6 +47,7 @@ class ContinuousAnalyticsTableJobTest { @Mock private AnalyticsTableGenerator analyticsTableGenerator; @Mock private SystemSettingsService settingsService; + @Mock private SystemSettings settings; @Mock private TableInfoReader tableInfoReader; From 97b66974df12af318762e91a80f89116bce63721 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 26 Nov 2024 15:49:37 +0100 Subject: [PATCH 015/110] fix: Update code --- .../ProgramIndicatorSubqueryBuilderTest.java | 1 + .../table/model/AnalyticsTableColumnTest.java | 1 + .../ContinuousAnalyticsTableJobTest.java | 8 +- ...rackedEntityQueryRequestValidatorTest.java | 1 - .../query/DataElementConditionTest.java | 7 - .../query/OrganisationUnitConditionTest.java | 29 --- .../query/RenderableDataValueTest.java | 1 - .../querybuilder/OffsetHelperTest.java | 6 - .../querybuilder/SqlQueryHelperTest.java | 170 +++++++++--------- 9 files changed, 91 insertions(+), 133 deletions(-) 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 30675b3e9b83..0b8057d8ec3c 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 @@ -35,6 +35,7 @@ import static org.hisp.dhis.test.TestBase.createProgramIndicator; import static org.hisp.dhis.test.TestBase.getDate; import static org.mockito.Mockito.when; + import java.util.Date; import org.hisp.dhis.program.AnalyticsType; import org.hisp.dhis.program.Program; diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/model/AnalyticsTableColumnTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/model/AnalyticsTableColumnTest.java index 6ea7c108a985..a016958cacfe 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/model/AnalyticsTableColumnTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/model/AnalyticsTableColumnTest.java @@ -33,6 +33,7 @@ import static org.hisp.dhis.db.model.constraint.Nullable.NULL; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; + import org.hisp.dhis.db.model.Collation; import org.junit.jupiter.api.Test; diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/scheduling/ContinuousAnalyticsTableJobTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/scheduling/ContinuousAnalyticsTableJobTest.java index 339100075c5e..581a9a70d1bc 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/scheduling/ContinuousAnalyticsTableJobTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/scheduling/ContinuousAnalyticsTableJobTest.java @@ -31,6 +31,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.when; + import java.util.Date; import org.hisp.dhis.analytics.AnalyticsTableGenerator; import org.hisp.dhis.analytics.common.TableInfoReader; @@ -39,6 +40,7 @@ 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.junit.jupiter.MockitoExtension; @@ -47,12 +49,12 @@ class ContinuousAnalyticsTableJobTest { @Mock private AnalyticsTableGenerator analyticsTableGenerator; @Mock private SystemSettingsService settingsService; - + @Mock private SystemSettings settings; @Mock private TableInfoReader tableInfoReader; - private ContinuousAnalyticsTableJob job; + @InjectMocks private ContinuousAnalyticsTableJob job; private final Date dateA = getDate(2024, 1, 4, 23, 0); private final Date dateB = getDate(2024, 1, 5, 2, 0); @@ -61,8 +63,6 @@ class ContinuousAnalyticsTableJobTest { @BeforeEach public void beforeEach() { when(settingsService.getCurrentSettings()).thenReturn(settings); - job = - new ContinuousAnalyticsTableJob(analyticsTableGenerator, settingsService, tableInfoReader); } @Test diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/TrackedEntityQueryRequestValidatorTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/TrackedEntityQueryRequestValidatorTest.java index 11e874343ee0..1e1b37f8629f 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/TrackedEntityQueryRequestValidatorTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/TrackedEntityQueryRequestValidatorTest.java @@ -40,7 +40,6 @@ * @author maikel arabori */ class TrackedEntityQueryRequestValidatorTest { - @Test void testValidateWhenTrackedEntityTypeIsInvalid() { String teiUid = CodeGenerator.generateUid() + "invalid"; diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/DataElementConditionTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/DataElementConditionTest.java index 23d66775b561..8047be617731 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/DataElementConditionTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/DataElementConditionTest.java @@ -62,7 +62,6 @@ void testWhenNoLegendSet() { SqlParameterManager sqlParameterManager = new SqlParameterManager(); QueryContext queryContext = QueryContext.of(null, sqlParameterManager); - // SETUP DimensionParam dimensionParam = mock(DimensionParam.class); QueryItem queryItem = mock(QueryItem.class); DimensionParamItem dimensionParamItem = mock(DimensionParamItem.class); @@ -78,13 +77,11 @@ void testWhenNoLegendSet() { when(dimensionParamItem.getOperator()).thenReturn(AnalyticsQueryOperator.of(QueryOperator.EQ)); when(dimensionParamItem.getValues()).thenReturn(List.of("value")); - // CALL DataElementCondition dataElementCondition = DataElementCondition.of(queryContext, dimensionIdentifier); String rendered = dataElementCondition.render(); - // ASSERT assertEquals("(\"eventdatavalues\" -> 'uid' ->> 'value')::TEXT = :1", rendered); assertEquals("value", queryContext.getParametersPlaceHolder().get("1")); } @@ -94,7 +91,6 @@ void testWhenLegendSet() { SqlParameterManager sqlParameterManager = new SqlParameterManager(); QueryContext queryContext = QueryContext.of(null, sqlParameterManager); - // SETUP DimensionParam dimensionParam = mock(DimensionParam.class); QueryItem queryItem = mock(QueryItem.class); DimensionParamItem dimensionParamItem = mock(DimensionParamItem.class); @@ -105,7 +101,6 @@ void testWhenLegendSet() { when(dimensionParam.getUid()).thenReturn("uid"); when(queryItem.hasLegendSet()).thenReturn(true); - // in the assertion we will check that the type is force to DECIMAL when(dimensionParam.getValueType()).thenReturn(ValueType.TEXT); when(dimensionParam.getItems()).thenReturn(List.of(dimensionParamItem)); @@ -121,13 +116,11 @@ void testWhenLegendSet() { when(legend.getStartValue()).thenReturn(1.0); when(legend.getEndValue()).thenReturn(2.0); - // CALL DataElementCondition dataElementCondition = DataElementCondition.of(queryContext, dimensionIdentifier); String rendered = dataElementCondition.render(); - // ASSERT assertEquals( "(\"eventdatavalues\" -> 'uid' ->> 'value')::DECIMAL >= :1 " + "and (\"eventdatavalues\" -> 'uid' ->> 'value')::DECIMAL < :2", diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/OrganisationUnitConditionTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/OrganisationUnitConditionTest.java index 7fac1fb4b9e9..606f35a0cc5a 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/OrganisationUnitConditionTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/OrganisationUnitConditionTest.java @@ -60,11 +60,9 @@ import org.hisp.dhis.trackedentity.TrackedEntityType; import org.junit.jupiter.api.Test; -/** Unit tests for {@link OrganisationUnitCondition}. */ class OrganisationUnitConditionTest { @Test void testTeiOuMultipleOusProduceCorrectSql() { - // Given List ous = List.of("ou1", "ou2"); DimensionIdentifier dimensionIdentifier = stubDimensionIdentifier(ous, null, null); @@ -82,17 +80,14 @@ void testTeiOuMultipleOusProduceCorrectSql() { OrganisationUnitCondition organisationUnitCondition = OrganisationUnitCondition.of(dimensionIdentifier, queryContext); - // When String render = organisationUnitCondition.render(); - // Then assertEquals("t_1.\"ou\" in (:1)", render); assertEquals(ous, queryContext.getParametersPlaceHolder().get("1")); } @Test void testChildrenOuModeProduceCorrectSql() { - // Given List ous = List.of("ou1", "ou2"); DimensionIdentifier dimensionIdentifier = @@ -125,13 +120,11 @@ void testChildrenOuModeProduceCorrectSql() { OrganisationUnitCondition organisationUnitCondition = OrganisationUnitCondition.of(dimensionIdentifier, queryContext); - // When String render = organisationUnitCondition.render(); List expected = ous.stream().flatMap(ouId -> Stream.of(ouId + "_children1", ouId + "_children2")).toList(); - // Then assertEquals("t_1.\"ou\" in (:1)", render); assertTrue( isEqualCollection( @@ -140,7 +133,6 @@ void testChildrenOuModeProduceCorrectSql() { @Test void testDescendantOuModeProduceCorrectSql() { - // Given List ous = List.of("ou1", "ou2"); DimensionIdentifier dimensionIdentifier = @@ -155,7 +147,6 @@ void testDescendantOuModeProduceCorrectSql() { return organisationUnit; }); - // Descendant is the default ouMode. CommonRequestParams requestParams = new CommonRequestParams(); ContextParams contextParams = @@ -169,16 +160,13 @@ void testDescendantOuModeProduceCorrectSql() { OrganisationUnitCondition organisationUnitCondition = OrganisationUnitCondition.of(dimensionIdentifier, queryContext); - // When String render = organisationUnitCondition.render(); - // Then assertEquals("(t_1.\"uidlevel1\" = :1 or t_1.\"uidlevel1\" = :2)", render); } @Test void testEmptyOuProduceFalse() { - // Given List ous = List.of(); DimensionIdentifier dimensionIdentifier = stubDimensionIdentifier(ous, null, null); @@ -197,17 +185,14 @@ void testEmptyOuProduceFalse() { OrganisationUnitCondition organisationUnitCondition = OrganisationUnitCondition.of(dimensionIdentifier, queryContext); - // When String render = organisationUnitCondition.render(); - // Then assertEquals("false", render); assertTrue(queryContext.getParametersPlaceHolder().isEmpty()); } @Test void testTeiOuSingleOusProduceCorrectSql() { - // Given List ous = List.of("ou1"); DimensionIdentifier dimensionIdentifier = @@ -227,17 +212,14 @@ void testTeiOuSingleOusProduceCorrectSql() { OrganisationUnitCondition organisationUnitCondition = OrganisationUnitCondition.of(dimensionIdentifier, queryContext); - // When String render = organisationUnitCondition.render(); - // Then assertEquals("t_1.\"ou\" = :1", render); assertEquals(ous.get(0), queryContext.getParametersPlaceHolder().get("1")); } @Test void testEventOuSingleOusProduceCorrectSql() { - // Given List ous = List.of("ou1"); DimensionIdentifier dimensionIdentifier = @@ -257,17 +239,14 @@ void testEventOuSingleOusProduceCorrectSql() { OrganisationUnitCondition organisationUnitCondition = OrganisationUnitCondition.of(dimensionIdentifier, queryContext); - // When String statement = organisationUnitCondition.render(); - // Then assertEquals("\"Z8z5uu61HAb.tO8L1aBitDm\".\"ou\" = :1", statement); assertEquals(ous.get(0), queryContext.getParametersPlaceHolder().get("1")); } @Test void testEventOuMultipleOusProduceCorrectSql() { - // Given List ous = List.of("ou1", "ou2"); DimensionIdentifier dimensionIdentifier = @@ -291,17 +270,14 @@ void testEventOuMultipleOusProduceCorrectSql() { OrganisationUnitCondition organisationUnitCondition = OrganisationUnitCondition.of(dimensionIdentifier, queryContext); - // When String statement = organisationUnitCondition.render(); - // Then assertEquals("\"Z8z5uu61HAb.tO8L1aBitDm\".\"ou\" in (:1)", statement); assertEquals(ous, queryContext.getParametersPlaceHolder().get("1")); } @Test void testEnrollmentOuSingleOusProduceCorrectSql() { - // Given List ous = List.of("ou1"); DimensionIdentifier dimensionIdentifier = @@ -325,17 +301,14 @@ void testEnrollmentOuSingleOusProduceCorrectSql() { OrganisationUnitCondition organisationUnitCondition = OrganisationUnitCondition.of(dimensionIdentifier, queryContext); - // When String statement = organisationUnitCondition.render(); - // Then assertEquals("\"Z8z5uu61HAb\".\"ou\" = :1", statement); assertEquals(ous.get(0), queryContext.getParametersPlaceHolder().get("1")); } @Test void testEnrollmentOuMultipleOusProduceCorrectSql() { - // Given List ous = List.of("ou1", "ou2"); DimensionIdentifier dimensionIdentifier = @@ -358,10 +331,8 @@ void testEnrollmentOuMultipleOusProduceCorrectSql() { OrganisationUnitCondition organisationUnitCondition = OrganisationUnitCondition.of(dimensionIdentifier, queryContext); - // When String statement = organisationUnitCondition.render(); - // Then assertEquals("\"Z8z5uu61HAb\".\"ou\" in (:1)", statement); assertEquals(ous, queryContext.getParametersPlaceHolder().get("1")); } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/RenderableDataValueTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/RenderableDataValueTest.java index 547005a9ac66..edfe1644c305 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/RenderableDataValueTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/RenderableDataValueTest.java @@ -39,7 +39,6 @@ import org.junit.jupiter.api.Test; class RenderableDataValueTest { - @Test void testRender() { RenderableDataValue renderableDataValue = diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/context/querybuilder/OffsetHelperTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/context/querybuilder/OffsetHelperTest.java index ba1176e86bd4..5ddfb28318ea 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/context/querybuilder/OffsetHelperTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/context/querybuilder/OffsetHelperTest.java @@ -35,21 +35,17 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; -/** Tests for {@link OffsetHelper}. */ class OffsetHelperTest { @ParameterizedTest(name = "testGetItemBasedOnOffset - {index}") @CsvSource({"2,d", "1,e", "0,a", "-1,b", "-2,c"}) void testGetItemBasedOnOffset(String offsetParam, String expectedResponse) { - // Given Stream stream = Stream.of("a", "b", "c", "d", "e"); Comparator comparator = Comparator.naturalOrder(); int offset = Integer.parseInt(offsetParam); - // When Optional result = OffsetHelper.getItemBasedOnOffset(stream, comparator, offset); - // Then Assertions.assertTrue(result.isPresent()); Assertions.assertEquals(expectedResponse, result.get()); } @@ -57,10 +53,8 @@ void testGetItemBasedOnOffset(String offsetParam, String expectedResponse) { @ParameterizedTest @CsvSource({"1,1,asc", "2,2,asc", "0,1,desc", "-1,2,desc", "-2,3,desc"}) void testGetOffset(String offsetParam, String expectedOffset, String expectedDirection) { - // When Offset offset = OffsetHelper.getOffset(Integer.parseInt(offsetParam)); - // Then Assertions.assertEquals(expectedOffset, offset.offset()); Assertions.assertEquals(expectedDirection, offset.direction()); } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/context/querybuilder/SqlQueryHelperTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/context/querybuilder/SqlQueryHelperTest.java index 289140c447d3..286a29c25fee 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/context/querybuilder/SqlQueryHelperTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/context/querybuilder/SqlQueryHelperTest.java @@ -102,26 +102,26 @@ void test_subQuery_enrollment() { assertEquals( """ - (select field - from (select *, - row_number() over ( partition by trackedentity - order by enrollmentdate desc ) as rn - from analytics_te_enrollment_trackedentitytype - where program = 'programUid' - and t_1.trackedentity = trackedentity) en - where en.rn = 1)""", + (select field + from (select *, + row_number() over ( partition by trackedentity + order by enrollmentdate desc ) as rn + from analytics_te_enrollment_trackedentitytype + where program = 'programUid' + and t_1.trackedentity = trackedentity) en + where en.rn = 1)""", SqlQueryHelper.buildOrderSubQuery(testedDimension, () -> "field").render()); assertEquals( """ - exists(select 1 - from (select * - from (select *, row_number() over (partition by trackedentity order by enrollmentdate desc) as rn - from analytics_te_enrollment_trackedentitytype - where program = 'programUid' - and trackedentity = t_1.trackedentity) en - where en.rn = 1) as "prefix" - where field)""", + exists(select 1 + from (select * + from (select *, row_number() over (partition by trackedentity order by enrollmentdate desc) as rn + from analytics_te_enrollment_trackedentitytype + where program = 'programUid' + and trackedentity = t_1.trackedentity) en + where en.rn = 1) as "prefix" + where field)""", SqlQueryHelper.buildExistsValueSubquery(testedDimension, () -> "field").render()); } @@ -148,42 +148,42 @@ void test_subQuery_event() { assertEquals( """ - (select field - from (select *, - row_number() over ( partition by enrollment - order by occurreddate desc ) as rn - from analytics_te_event_trackedentitytype events - where programstage = 'programStageUid' - and enrollment = (select enrollment - from (select *, - row_number() over ( partition by trackedentity - order by enrollmentdate desc ) as rn - from analytics_te_enrollment_trackedentitytype - where program = 'programUid' - and t_1.trackedentity = trackedentity) en - where en.rn = 1) - and status != 'SCHEDULE') ev - where ev.rn = 1)""", + (select field + from (select *, + row_number() over ( partition by enrollment + order by occurreddate desc ) as rn + from analytics_te_event_trackedentitytype events + where programstage = 'programStageUid' + and enrollment = (select enrollment + from (select *, + row_number() over ( partition by trackedentity + order by enrollmentdate desc ) as rn + from analytics_te_enrollment_trackedentitytype + where program = 'programUid' + and t_1.trackedentity = trackedentity) en + where en.rn = 1) + and status != 'SCHEDULE') ev + where ev.rn = 1)""", SqlQueryHelper.buildOrderSubQuery(testedDimension, () -> "field").render()); assertEquals( """ - exists(select 1 - from (select * - from (select *, row_number() over (partition by trackedentity order by enrollmentdate desc) as rn - from analytics_te_enrollment_trackedentitytype - where program = 'programUid' - and trackedentity = t_1.trackedentity) en - where en.rn = 1) as "enrollmentSubqueryAlias" - where exists(select 1 - from (select * - from (select *, row_number() over ( partition by enrollment order by occurreddate desc ) as rn - from analytics_te_event_trackedentitytype - where "enrollmentSubqueryAlias".enrollment = enrollment - and programstage = 'programStageUid' - and status != 'SCHEDULE') ev - where ev.rn = 1) as "prefix" - where field))""", + exists(select 1 + from (select * + from (select *, row_number() over (partition by trackedentity order by enrollmentdate desc) as rn + from analytics_te_enrollment_trackedentitytype + where program = 'programUid' + and trackedentity = t_1.trackedentity) en + where en.rn = 1) as "enrollmentSubqueryAlias" + where exists(select 1 + from (select * + from (select *, row_number() over ( partition by enrollment order by occurreddate desc ) as rn + from analytics_te_event_trackedentitytype + where "enrollmentSubqueryAlias".enrollment = enrollment + and programstage = 'programStageUid' + and status != 'SCHEDULE') ev + where ev.rn = 1) as "prefix" + where field))""", SqlQueryHelper.buildExistsValueSubquery(testedDimension, () -> "field").render()); } @@ -211,47 +211,47 @@ void test_subQuery_data_element() { assertEquals( """ - (select field - from analytics_te_event_trackedentitytype - where event = (select event - from (select *, - row_number() over ( partition by enrollment - order by occurreddate desc ) as rn - from analytics_te_event_trackedentitytype events - where programstage = 'programStageUid' - and enrollment = (select enrollment - from (select *, - row_number() over ( partition by trackedentity - order by enrollmentdate desc ) as rn - from analytics_te_enrollment_trackedentitytype - where program = 'programUid' - and t_1.trackedentity = trackedentity) en - where en.rn = 1) - and status != 'SCHEDULE') ev - where ev.rn = 1))""", + (select field + from analytics_te_event_trackedentitytype + where event = (select event + from (select *, + row_number() over ( partition by enrollment + order by occurreddate desc ) as rn + from analytics_te_event_trackedentitytype events + where programstage = 'programStageUid' + and enrollment = (select enrollment + from (select *, + row_number() over ( partition by trackedentity + order by enrollmentdate desc ) as rn + from analytics_te_enrollment_trackedentitytype + where program = 'programUid' + and t_1.trackedentity = trackedentity) en + where en.rn = 1) + and status != 'SCHEDULE') ev + where ev.rn = 1))""", SqlQueryHelper.buildOrderSubQuery(testedDimension, () -> "field").render()); assertEquals( """ - exists(select 1 - from (select * - from (select *, row_number() over (partition by trackedentity order by enrollmentdate desc) as rn - from analytics_te_enrollment_trackedentitytype - where program = 'programUid' - and trackedentity = t_1.trackedentity) en - where en.rn = 1) as "enrollmentSubqueryAlias" - where exists(select 1 - from (select * - from (select *, row_number() over ( partition by enrollment order by occurreddate desc ) as rn - from analytics_te_event_trackedentitytype - where "enrollmentSubqueryAlias".enrollment = enrollment - and programstage = 'programStageUid' - and status != 'SCHEDULE') ev - where ev.rn = 1) as "prefix" - where exists(select 1 - from analytics_te_event_trackedentitytype - where "prefix".event = event - and field)))""", + exists(select 1 + from (select * + from (select *, row_number() over (partition by trackedentity order by enrollmentdate desc) as rn + from analytics_te_enrollment_trackedentitytype + where program = 'programUid' + and trackedentity = t_1.trackedentity) en + where en.rn = 1) as "enrollmentSubqueryAlias" + where exists(select 1 + from (select * + from (select *, row_number() over ( partition by enrollment order by occurreddate desc ) as rn + from analytics_te_event_trackedentitytype + where "enrollmentSubqueryAlias".enrollment = enrollment + and programstage = 'programStageUid' + and status != 'SCHEDULE') ev + where ev.rn = 1) as "prefix" + where exists(select 1 + from analytics_te_event_trackedentitytype + where "prefix".event = event + and field)))""", SqlQueryHelper.buildExistsValueSubquery(testedDimension, () -> "field").render()); } From 7c725c76e0e71f1b49f766a64dc58667a8c3388c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 26 Nov 2024 15:50:55 +0100 Subject: [PATCH 016/110] fix: Update code --- .../context/sql/SqlQueryCreatorServiceTest.java | 12 ------------ .../dhis/analytics/util/AnalyticsColumnAsserter.java | 1 - .../dhis/common/BaseDimensionalItemObjectTest.java | 6 ------ .../resourcetable/table/UniqueNameContextTest.java | 1 - 4 files changed, 20 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/context/sql/SqlQueryCreatorServiceTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/context/sql/SqlQueryCreatorServiceTest.java index ad9781863681..5be9d61daf2a 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/context/sql/SqlQueryCreatorServiceTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/context/sql/SqlQueryCreatorServiceTest.java @@ -93,7 +93,6 @@ void setUp() { @Test void testSqlQueryRenderingWithOrgUnitNameObject() { - // given TrackedEntityQueryParams trackedEntityQueryParams = TrackedEntityQueryParams.builder().trackedEntityType(createTrackedEntityType('A')).build(); @@ -104,18 +103,15 @@ void testSqlQueryRenderingWithOrgUnitNameObject() { .commonParsed(stubSortingCommonParams(null, 1, "ouname")) .build(); - // when String sql = sqlQueryCreatorService.getSqlQueryCreator(contextParams).createForSelect().getStatement(); - // then assertTrue(sql.contains("ouname")); assertContains("order by t_1.\"ouname\" desc", sql); } @Test void testSqlQueryRenderingWithCommonDimensionalObject() { - // when DimensionalObject dimensionalObject = new BaseDimensionalObject("abc"); TrackedEntityType trackedEntityType = createTrackedEntityType('A'); @@ -132,17 +128,14 @@ void testSqlQueryRenderingWithCommonDimensionalObject() { .commonParsed(stubSortingCommonParams(program, 1, dimensionalObject)) .build(); - // when String sql = sqlQueryCreatorService.getSqlQueryCreator(contextParams).createForSelect().getStatement(); - // then assertTrue(sql.contains(" order by (select (\"eventdatavalues\" -> 'abc' ->> 'value')::TEXT")); } @Test void testEnrolledInProgramWhenSpecifiedInRequest() { - // given CommonParsedParams commonParsed = CommonParsedParams.builder() .programs(List.of(mockProgram("program1"), mockProgram("program2"))) @@ -162,18 +155,15 @@ void testEnrolledInProgramWhenSpecifiedInRequest() { .commonParsed(commonParsed) .build(); - // when String sql = sqlQueryCreatorService.getSqlQueryCreator(contextParams).createForSelect().getStatement(); - // then assertTrue(sql.contains("ouname")); assertContains("t_1.\"program1\" and t_1.\"program2\"", sql); } @Test void testEnrolledInProgramWhenNotSpecifiedInRequest() { - // given CommonParsedParams commonParsed = CommonParsedParams.builder() .programs(List.of(mockProgram("program1"), mockProgram("program2"))) @@ -191,11 +181,9 @@ void testEnrolledInProgramWhenNotSpecifiedInRequest() { .commonRaw(requestParams) .build(); - // when String sql = sqlQueryCreatorService.getSqlQueryCreator(contextParams).createForSelect().getStatement(); - // then assertTrue(sql.contains("ouname")); assertContains("(t_1.\"program1\" or t_1.\"program2\")", sql); } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/util/AnalyticsColumnAsserter.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/util/AnalyticsColumnAsserter.java index ef6e06592070..2c9278516e70 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/util/AnalyticsColumnAsserter.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/util/AnalyticsColumnAsserter.java @@ -39,7 +39,6 @@ */ @Setter public class AnalyticsColumnAsserter { - /** The analytics table column to verify. */ private AnalyticsTableColumn actual; public void verify(AnalyticsTableColumn expected) { diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/common/BaseDimensionalItemObjectTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/common/BaseDimensionalItemObjectTest.java index 3259349deebe..57912122b542 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/common/BaseDimensionalItemObjectTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/common/BaseDimensionalItemObjectTest.java @@ -35,25 +35,19 @@ class BaseDimensionalItemObjectTest { @Test void testWhenBaseDimensionalItemObjectAggregationTypeIsNoneTotalAggregationTypeIsNone() { - // given BaseDimensionalItemObject baseDimensionalItemObject = new BaseDimensionalItemObject(); - // when baseDimensionalItemObject.setAggregationType(AggregationType.NONE); - // then assertSame(TotalAggregationType.NONE, baseDimensionalItemObject.getTotalAggregationType()); } @Test void testWhenBaseDimensionalItemObjectAggregationTypeIsNotNoneTotalAggregationTypeIsSum() { - // given BaseDimensionalItemObject baseDimensionalItemObject = new BaseDimensionalItemObject(); - // when baseDimensionalItemObject.setAggregationType(AggregationType.AVERAGE_SUM_ORG_UNIT); - // then assertSame(TotalAggregationType.SUM, baseDimensionalItemObject.getTotalAggregationType()); } } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/resourcetable/table/UniqueNameContextTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/resourcetable/table/UniqueNameContextTest.java index 0e1bc425adc6..380e698dc9ed 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/resourcetable/table/UniqueNameContextTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/resourcetable/table/UniqueNameContextTest.java @@ -38,7 +38,6 @@ * @author Jan Bernitt */ class UniqueNameContextTest { - private final UniqueNameContext context = new UniqueNameContext(); @Test From 490ea090b88a393e2acd2789b4146e83217c4823 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 26 Nov 2024 16:18:38 +0100 Subject: [PATCH 017/110] fix: Update code --- .../java/org/hisp/dhis/common/DimensionalObjectUtils.java | 8 +++----- .../java/org/hisp/dhis/analytics/common/ColumnHeader.java | 3 +-- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObjectUtils.java b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObjectUtils.java index f0d2ceae6cc3..f4a293029fea 100644 --- a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObjectUtils.java +++ b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObjectUtils.java @@ -38,9 +38,6 @@ import static org.hisp.dhis.common.DimensionalObject.ITEM_SEP; import static org.hisp.dhis.common.DimensionalObject.OPTION_SEP; import static org.hisp.dhis.expression.ExpressionService.SYMBOL_WILDCARD; - -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -53,7 +50,6 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; -import lombok.NoArgsConstructor; import org.apache.commons.collections4.ListUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.Triple; @@ -63,6 +59,9 @@ import org.hisp.dhis.eventvisualization.EventRepetition; import org.hisp.dhis.program.Program; import org.hisp.dhis.program.ProgramStage; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import lombok.NoArgsConstructor; /** * @author Lars Helge Overland @@ -141,7 +140,6 @@ public static DimensionalObject linkAssociations( EventAnalyticalObject eventAnalyticalObject, DimensionalObject dimensionalObject, Attribute parent) { - // Associating event repetitions. List repetitions = eventAnalyticalObject.getEventRepetitions(); if (isNotEmpty(repetitions)) { diff --git a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/analytics/common/ColumnHeader.java b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/analytics/common/ColumnHeader.java index 814be376f4db..1e9e65e1681a 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/analytics/common/ColumnHeader.java +++ b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/analytics/common/ColumnHeader.java @@ -34,9 +34,8 @@ public enum ColumnHeader { GEOMETRY("geometry", "Geometry"), ENROLLMENT_DATE("enrollmentdate", "Enrollment date"), /** - * @deprecated use @Link{#OCCURRED_DATE} instead. + * Deprecated since 2.42. Use @Link{#OCCURRED_DATE} instead. */ - @Deprecated(since = "2.42") INCIDENT_DATE("incidentdate", "Incident date"), OCCURRED_DATE("occurreddate", "Occurred date"), STORED_BY("storedby", "Stored by"), From 781d2e462586495300d7e75c431eb4ab9a7f23b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 26 Nov 2024 16:33:27 +0100 Subject: [PATCH 018/110] fix: Update code --- .../table/AbstractEventJdbcTableManager.java | 6 +++--- .../table/AbstractJdbcTableManager.java | 14 +++++++------- .../table/JdbcEventAnalyticsTableManager.java | 16 ++++++++-------- ...lumnType.java => AnalyticsDimensionType.java} | 2 +- .../table/model/AnalyticsTableColumn.java | 7 ++++--- .../analytics/util/AnalyticsIndexHelper.java | 4 ++-- 6 files changed, 25 insertions(+), 24 deletions(-) rename dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/model/{AnalyticsColumnType.java => AnalyticsDimensionType.java} (97%) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java index 0b07dced4214..225991dcba23 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java @@ -38,7 +38,7 @@ import java.util.Map; import org.hisp.dhis.analytics.AnalyticsTableHookService; import org.hisp.dhis.analytics.partition.PartitionManager; -import org.hisp.dhis.analytics.table.model.AnalyticsColumnType; +import org.hisp.dhis.analytics.table.model.AnalyticsDimensionType; import org.hisp.dhis.analytics.table.model.AnalyticsTableColumn; import org.hisp.dhis.analytics.table.model.AnalyticsTablePartition; import org.hisp.dhis.analytics.table.model.Skip; @@ -259,7 +259,7 @@ protected List getTrackedEntityAttributeColumns(Program pr columns.add( AnalyticsTableColumn.builder() .name(attribute.getUid()) - .columnType(AnalyticsColumnType.DYNAMIC) + .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(dataType) .selectExpression(sql) .skipIndex(skipIndex) @@ -272,7 +272,7 @@ protected List getTrackedEntityAttributeColumns(Program pr columns.add( AnalyticsTableColumn.builder() .name((attribute.getUid() + OU_NAME_COL_SUFFIX)) - .columnType(AnalyticsColumnType.DYNAMIC) + .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(TEXT) .selectExpression(ouNameSql) .skipIndex(SKIP) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java index 1d16314a85cf..94a49a3ea16a 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java @@ -54,7 +54,7 @@ import org.hisp.dhis.analytics.AnalyticsTableType; import org.hisp.dhis.analytics.AnalyticsTableUpdateParams; import org.hisp.dhis.analytics.partition.PartitionManager; -import org.hisp.dhis.analytics.table.model.AnalyticsColumnType; +import org.hisp.dhis.analytics.table.model.AnalyticsDimensionType; import org.hisp.dhis.analytics.table.model.AnalyticsTable; import org.hisp.dhis.analytics.table.model.AnalyticsTableColumn; import org.hisp.dhis.analytics.table.model.AnalyticsTablePartition; @@ -537,7 +537,7 @@ protected List getOrganisationUnitGroupSetColumns() { String name = ougs.getUid(); return AnalyticsTableColumn.builder() .name(name) - .columnType(AnalyticsColumnType.DYNAMIC) + .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(CHARACTER_11) .selectExpression("ougs." + quote(name)) .skipIndex(skipIndex(ougs)) @@ -554,7 +554,7 @@ protected List getDataElementGroupSetColumns() { String name = degs.getUid(); return AnalyticsTableColumn.builder() .name(name) - .columnType(AnalyticsColumnType.DYNAMIC) + .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(CHARACTER_11) .selectExpression("degs." + quote(name)) .skipIndex(skipIndex(degs)) @@ -571,7 +571,7 @@ protected List getDisaggregationCategoryOptionGroupSetColu String name = cogs.getUid(); return AnalyticsTableColumn.builder() .name(name) - .columnType(AnalyticsColumnType.DYNAMIC) + .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(CHARACTER_11) .selectExpression("dcs." + quote(name)) .skipIndex(skipIndex(cogs)) @@ -588,7 +588,7 @@ protected List getAttributeCategoryOptionGroupSetColumns() String name = cogs.getUid(); return AnalyticsTableColumn.builder() .name(name) - .columnType(AnalyticsColumnType.DYNAMIC) + .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(CHARACTER_11) .selectExpression("acs." + quote(name)) .skipIndex(skipIndex(cogs)) @@ -605,7 +605,7 @@ protected List getDisaggregationCategoryColumns() { String name = category.getUid(); return AnalyticsTableColumn.builder() .name(name) - .columnType(AnalyticsColumnType.DYNAMIC) + .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(CHARACTER_11) .selectExpression("dcs." + quote(name)) .skipIndex(skipIndex(category)) @@ -622,7 +622,7 @@ protected List getAttributeCategoryColumns() { String name = category.getUid(); return AnalyticsTableColumn.builder() .name(name) - .columnType(AnalyticsColumnType.DYNAMIC) + .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(CHARACTER_11) .selectExpression("acs." + quote(name)) .skipIndex(skipIndex(category)) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 077b1284393a..1016ca4f3dcf 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -55,7 +55,7 @@ import org.hisp.dhis.analytics.AnalyticsTableType; import org.hisp.dhis.analytics.AnalyticsTableUpdateParams; import org.hisp.dhis.analytics.partition.PartitionManager; -import org.hisp.dhis.analytics.table.model.AnalyticsColumnType; +import org.hisp.dhis.analytics.table.model.AnalyticsDimensionType; import org.hisp.dhis.analytics.table.model.AnalyticsTable; import org.hisp.dhis.analytics.table.model.AnalyticsTableColumn; import org.hisp.dhis.analytics.table.model.AnalyticsTablePartition; @@ -407,7 +407,7 @@ private List getColumns(Program program) { columns.add( AnalyticsTableColumn.builder() .name(category.getUid()) - .columnType(AnalyticsColumnType.DYNAMIC) + .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(CHARACTER_11) .selectExpression("acs." + quote(category.getUid())) .created(category.getCreated()) @@ -496,7 +496,7 @@ private List getColumnForDataElement( columns.add( AnalyticsTableColumn.builder() .name(dataElement.getUid()) - .columnType(AnalyticsColumnType.DYNAMIC) + .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(dataType) .selectExpression(sql) .skipIndex(skipIndex) @@ -523,7 +523,7 @@ private List getColumnForOrgUnitDataElement( columns.add( AnalyticsTableColumn.builder() .name((dataElement.getUid() + OU_GEOMETRY_COL_SUFFIX)) - .columnType(AnalyticsColumnType.DYNAMIC) + .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(GEOMETRY) .selectExpression(geoSql) .indexType(IndexType.GIST) @@ -536,7 +536,7 @@ private List getColumnForOrgUnitDataElement( columns.add( AnalyticsTableColumn.builder() .name((dataElement.getUid() + OU_NAME_COL_SUFFIX)) - .columnType(AnalyticsColumnType.DYNAMIC) + .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(TEXT) .selectExpression(ouNameSql) .skipIndex(SKIP) @@ -562,7 +562,7 @@ private List getColumnForTrackedEntityAttribute( columns.add( AnalyticsTableColumn.builder() .name(attribute.getUid()) - .columnType(AnalyticsColumnType.DYNAMIC) + .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(dataType) .selectExpression(sql) .skipIndex(skipIndex) @@ -633,7 +633,7 @@ private List getColumnsForOrgUnitTrackedEntityAttribute( columns.add( AnalyticsTableColumn.builder() .name((attribute.getUid() + OU_GEOMETRY_COL_SUFFIX)) - .columnType(AnalyticsColumnType.DYNAMIC) + .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(GEOMETRY) .selectExpression(geoSql) .indexType(IndexType.GIST) @@ -646,7 +646,7 @@ private List getColumnsForOrgUnitTrackedEntityAttribute( columns.add( AnalyticsTableColumn.builder() .name((attribute.getUid() + OU_NAME_COL_SUFFIX)) - .columnType(AnalyticsColumnType.DYNAMIC) + .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(TEXT) .selectExpression(ouNameSql) .skipIndex(SKIP) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/model/AnalyticsColumnType.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/model/AnalyticsDimensionType.java similarity index 97% rename from dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/model/AnalyticsColumnType.java rename to dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/model/AnalyticsDimensionType.java index e00267ed3717..cd54f16e4e60 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/model/AnalyticsColumnType.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/model/AnalyticsDimensionType.java @@ -31,7 +31,7 @@ * Represents a type of dimension, either static, meaning fixed, or dynamic, meaning based on a * dimensional configuration entity. */ -public enum AnalyticsColumnType { +public enum AnalyticsDimensionType { STATIC, DYNAMIC; } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/model/AnalyticsTableColumn.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/model/AnalyticsTableColumn.java index 2f04beb6f675..537b473493e5 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/model/AnalyticsTableColumn.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/model/AnalyticsTableColumn.java @@ -74,7 +74,8 @@ public class AnalyticsTableColumn { @Builder.Default private final List indexColumns = List.of(); /** The column type indicates the column origin. */ - @Builder.Default private final AnalyticsColumnType columnType = AnalyticsColumnType.STATIC; + @Builder.Default + private final AnalyticsDimensionType dimensionType = AnalyticsDimensionType.STATIC; /** Date of creation of the underlying data dimension. */ private final Date created; @@ -104,8 +105,8 @@ public boolean isSkipIndex() { } /** Indicates whether the column type is set to a non-default value. */ - public boolean isStatic() { - return AnalyticsColumnType.STATIC == columnType; + public boolean isStaticDimension() { + return AnalyticsDimensionType.STATIC == dimensionType; } @Override diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/util/AnalyticsIndexHelper.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/util/AnalyticsIndexHelper.java index 07d05742ff2e..6d984a71c2b4 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/util/AnalyticsIndexHelper.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/util/AnalyticsIndexHelper.java @@ -154,7 +154,7 @@ private static void maybeAddTextLowerIndex( boolean isSingleColumn = indexColumns.size() == 1; if (column.getDataType() == TEXT - && !column.isStatic() + && !column.isStaticDimension() && isValidUid(columnName) && isSingleColumn) { String name = indexName + "_lower"; @@ -178,7 +178,7 @@ private static void maybeAddDateSortOrderIndex( boolean isSingleColumn = indexColumns.size() == 1; - if (column.getDataType() == TIMESTAMP && column.isStatic() && isSingleColumn) { + if (column.getDataType() == TIMESTAMP && column.isStaticDimension() && isSingleColumn) { indexes.add( Index.builder() .name(indexName + "_desc") From c1b20d73fd638ba6b029ef348c4ecebaa33acabd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 26 Nov 2024 16:35:26 +0100 Subject: [PATCH 019/110] fix: Update code --- .../main/java/org/hisp/dhis/analytics/common/ColumnHeader.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/analytics/common/ColumnHeader.java b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/analytics/common/ColumnHeader.java index 1e9e65e1681a..814be376f4db 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/analytics/common/ColumnHeader.java +++ b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/analytics/common/ColumnHeader.java @@ -34,8 +34,9 @@ public enum ColumnHeader { GEOMETRY("geometry", "Geometry"), ENROLLMENT_DATE("enrollmentdate", "Enrollment date"), /** - * Deprecated since 2.42. Use @Link{#OCCURRED_DATE} instead. + * @deprecated use @Link{#OCCURRED_DATE} instead. */ + @Deprecated(since = "2.42") INCIDENT_DATE("incidentdate", "Incident date"), OCCURRED_DATE("occurreddate", "Occurred date"), STORED_BY("storedby", "Stored by"), From c51fb149780cadde2c72d81080fda8c8c83cd218 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 26 Nov 2024 17:03:00 +0100 Subject: [PATCH 020/110] fix: Update code --- .../dhis/common/DimensionalObjectUtils.java | 7 +- .../table/JdbcEventAnalyticsTableManager.java | 75 +++++++++++++------ 2 files changed, 55 insertions(+), 27 deletions(-) diff --git a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObjectUtils.java b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObjectUtils.java index f4a293029fea..a7786ad46813 100644 --- a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObjectUtils.java +++ b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalObjectUtils.java @@ -38,6 +38,9 @@ import static org.hisp.dhis.common.DimensionalObject.ITEM_SEP; import static org.hisp.dhis.common.DimensionalObject.OPTION_SEP; import static org.hisp.dhis.expression.ExpressionService.SYMBOL_WILDCARD; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -50,6 +53,7 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; +import lombok.NoArgsConstructor; import org.apache.commons.collections4.ListUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.Triple; @@ -59,9 +63,6 @@ import org.hisp.dhis.eventvisualization.EventRepetition; import org.hisp.dhis.program.Program; import org.hisp.dhis.program.ProgramStage; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import lombok.NoArgsConstructor; /** * @author Lars Helge Overland diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 1016ca4f3dcf..766ae26af224 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -421,30 +421,8 @@ private List getColumns(Program program) { columns.addAll(getOrganisationUnitGroupSetColumns()); columns.addAll(getAttributeCategoryOptionGroupSetColumns()); columns.addAll(getPeriodTypeColumns("dps")); - - columns.addAll( - program.getAnalyticsDataElements().stream() - .map(de -> getColumnForDataElement(de, false)) - .flatMap(Collection::stream) - .toList()); - - columns.addAll( - program.getAnalyticsDataElementsWithLegendSet().stream() - .map(de -> getColumnForDataElement(de, true)) - .flatMap(Collection::stream) - .toList()); - - columns.addAll( - program.getNonConfidentialTrackedEntityAttributes().stream() - .map(tea -> getColumnForTrackedEntityAttribute(tea, false)) - .flatMap(Collection::stream) - .toList()); - - columns.addAll( - program.getNonConfidentialTrackedEntityAttributesWithLegendSet().stream() - .map(tea -> getColumnForTrackedEntityAttribute(tea, true)) - .flatMap(Collection::stream) - .toList()); + columns.addAll(getDataElementColumns(program)); + columns.addAll(getAttributeColumns(program)); if (program.isRegistration()) { columns.add(EventAnalyticsColumn.TRACKED_ENTITY); @@ -469,6 +447,27 @@ protected AnalyticsTableColumn getPartitionColumn() { .build(); } + /** + * Returns columns for data elements of the given program. + * + * @param program the {@link Program}. + * @return a list of {@link AnalyticsTableColumn}. + */ + private List getDataElementColumns(Program program) { + List columns = new ArrayList<>(); + columns.addAll( + program.getAnalyticsDataElements().stream() + .map(de -> getColumnForDataElement(de, false)) + .flatMap(Collection::stream) + .toList()); + columns.addAll( + program.getAnalyticsDataElementsWithLegendSet().stream() + .map(de -> getColumnForDataElement(de, true)) + .flatMap(Collection::stream) + .toList()); + return columns; + } + /** * Returns a column for the given data element. If the value type of the data element is {@link * ValueType#ORGANISATION_UNIT}, an extra column will be included. @@ -545,6 +544,34 @@ private List getColumnForOrgUnitDataElement( return columns; } + /** + * Returns columns for attributes of the given program. + * + * @param program the {@link Program}. + * @return a list of {@link AnalyticsTableColumn}. + */ + private List getAttributeColumns(Program program) { + List columns = new ArrayList<>(); + columns.addAll( + program.getNonConfidentialTrackedEntityAttributes().stream() + .map(tea -> getColumnForTrackedEntityAttribute(tea, false)) + .flatMap(Collection::stream) + .toList()); + columns.addAll( + program.getNonConfidentialTrackedEntityAttributesWithLegendSet().stream() + .map(tea -> getColumnForTrackedEntityAttribute(tea, true)) + .flatMap(Collection::stream) + .toList()); + return columns; + } + + /** + * Returns a list of columns based on the given attribute. + * + * @param attribute the {@link TrackedEntityAttribute}. + * @param withLegendSet indicates whether the attribute has a legend set. + * @return a list of {@link AnaylyticsTableColumn}. + */ private List getColumnForTrackedEntityAttribute( TrackedEntityAttribute attribute, boolean withLegendSet) { List columns = new ArrayList<>(); From a84ff584f1ed8b4b09a8f5a4da44c0519ad7bec7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 26 Nov 2024 17:24:54 +0100 Subject: [PATCH 021/110] fix: Update code --- .../org/hisp/dhis/category/CategoryCombo.java | 6 +++ .../table/JdbcEventAnalyticsTableManager.java | 45 +++++++++++-------- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/category/CategoryCombo.java b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/category/CategoryCombo.java index b8b804117d5a..11246bd5533d 100644 --- a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/category/CategoryCombo.java +++ b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/category/CategoryCombo.java @@ -27,6 +27,7 @@ */ package org.hisp.dhis.category; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; @@ -195,6 +196,11 @@ public boolean hasOptionCombos() { return optionCombos != null && !optionCombos.isEmpty(); } + @JsonIgnore + public List getDataDimensionCategories() { + return categories.stream().filter(Category::isDataDimension).toList(); + } + // ------------------------------------------------------------------------- // Logic // ------------------------------------------------------------------------- diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 766ae26af224..5848eaf4ff9e 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -398,24 +398,7 @@ private String getPartitionClause(AnalyticsTablePartition partition) { */ private List getColumns(Program program) { List columns = new ArrayList<>(fixedColumns); - - if (program.hasNonDefaultCategoryCombo()) { - List categories = program.getCategoryCombo().getCategories(); - - for (Category category : categories) { - if (category.isDataDimension()) { - columns.add( - AnalyticsTableColumn.builder() - .name(category.getUid()) - .dimensionType(AnalyticsDimensionType.DYNAMIC) - .dataType(CHARACTER_11) - .selectExpression("acs." + quote(category.getUid())) - .created(category.getCreated()) - .build()); - } - } - } - + columns.addAll(getAttributeCategoryColumns(program)); columns.addAll(getOrganisationUnitLevelColumns()); columns.add(getOrganisationUnitNameHierarchyColumn()); columns.addAll(getOrganisationUnitGroupSetColumns()); @@ -447,6 +430,32 @@ protected AnalyticsTableColumn getPartitionColumn() { .build(); } + /** + * Returns columns for attribute categories of the given program. + * + * @param program the {@link Program}. + * @return a list of {@link AnalyticsTableColumn}. + */ + private List getAttributeCategoryColumns(Program program) { + if (program.hasNonDefaultCategoryCombo()) { + List categories = program.getCategoryCombo().getDataDimensionCategories(); + + return categories.stream() + .map( + category -> + AnalyticsTableColumn.builder() + .name(category.getUid()) + .dimensionType(AnalyticsDimensionType.DYNAMIC) + .dataType(CHARACTER_11) + .selectExpression("acs." + quote(category.getUid())) + .created(category.getCreated()) + .build()) + .toList(); + } + + return List.of(); + } + /** * Returns columns for data elements of the given program. * From e3752118b29277e0b380c8681ec7bae074de7d1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 26 Nov 2024 17:26:00 +0100 Subject: [PATCH 022/110] fix: Update code --- .../dhis/analytics/table/JdbcEventAnalyticsTableManager.java | 1 - 1 file changed, 1 deletion(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 5848eaf4ff9e..08999c31c8ff 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -439,7 +439,6 @@ protected AnalyticsTableColumn getPartitionColumn() { private List getAttributeCategoryColumns(Program program) { if (program.hasNonDefaultCategoryCombo()) { List categories = program.getCategoryCombo().getDataDimensionCategories(); - return categories.stream() .map( category -> From 43ff1026061fc42900d7b95e4302e1eb05b05cab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 26 Nov 2024 18:08:34 +0100 Subject: [PATCH 023/110] fix: Update code --- .../table/AbstractEventJdbcTableManager.java | 63 ------------------ .../JdbcEnrollmentAnalyticsTableManager.java | 64 +++++++++++++++++++ 2 files changed, 64 insertions(+), 63 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java index 225991dcba23..f6ac6eddb560 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java @@ -27,18 +27,13 @@ */ package org.hisp.dhis.analytics.table; -import static org.hisp.dhis.analytics.table.model.Skip.SKIP; import static org.hisp.dhis.analytics.util.AnalyticsUtils.getClosingParentheses; -import static org.hisp.dhis.analytics.util.AnalyticsUtils.getColumnType; -import static org.hisp.dhis.db.model.DataType.TEXT; import static org.hisp.dhis.system.util.MathUtils.NUMERIC_LENIENT_REGEXP; -import java.util.ArrayList; import java.util.List; import java.util.Map; import org.hisp.dhis.analytics.AnalyticsTableHookService; import org.hisp.dhis.analytics.partition.PartitionManager; -import org.hisp.dhis.analytics.table.model.AnalyticsDimensionType; import org.hisp.dhis.analytics.table.model.AnalyticsTableColumn; import org.hisp.dhis.analytics.table.model.AnalyticsTablePartition; import org.hisp.dhis.analytics.table.model.Skip; @@ -48,11 +43,9 @@ import org.hisp.dhis.common.ValueType; import org.hisp.dhis.commons.util.TextUtils; import org.hisp.dhis.dataapproval.DataApprovalLevelService; -import org.hisp.dhis.db.model.DataType; import org.hisp.dhis.db.sql.SqlBuilder; import org.hisp.dhis.organisationunit.OrganisationUnitService; import org.hisp.dhis.period.PeriodDataProvider; -import org.hisp.dhis.program.Program; import org.hisp.dhis.resourcetable.ResourceTableService; import org.hisp.dhis.setting.SystemSettingsProvider; import org.hisp.dhis.system.database.DatabaseInfoProvider; @@ -226,62 +219,6 @@ protected void populateTableInternal(AnalyticsTablePartition partition, String f invokeTimeAndLog(sql, "Populating table: '{}'", tableName); } - protected List getTrackedEntityAttributeColumns(Program program) { - List columns = new ArrayList<>(); - - for (TrackedEntityAttribute attribute : program.getNonConfidentialTrackedEntityAttributes()) { - DataType dataType = getColumnType(attribute.getValueType(), isSpatialSupport()); - String dataClause = - attribute.isNumericType() - ? getNumericClause() - : attribute.isDateType() ? getDateClause() : ""; - String select = getSelectExpressionForAttribute(attribute.getValueType(), "value"); - Skip skipIndex = skipIndex(attribute.getValueType(), attribute.hasOptionSet()); - - String sql = - replaceQualify( - """ - (select ${select} from ${trackedentityattributevalue} \ - where trackedentityid=en.trackedentityid \ - and trackedentityattributeid=${attributeId}\ - ${dataClause})${closingParentheses} as ${attributeUid}""", - Map.of( - "select", - select, - "attributeId", - String.valueOf(attribute.getId()), - "dataClause", - dataClause, - "closingParentheses", - getClosingParentheses(select), - "attributeUid", - quote(attribute.getUid()))); - columns.add( - AnalyticsTableColumn.builder() - .name(attribute.getUid()) - .dimensionType(AnalyticsDimensionType.DYNAMIC) - .dataType(dataType) - .selectExpression(sql) - .skipIndex(skipIndex) - .build()); - - if (attribute.getValueType().isOrganisationUnit()) { - String fromTypeSql = "ou.name from organisationunit ou where ou.uid = (select value"; - String ouNameSql = selectForInsert(attribute, fromTypeSql, dataClause); - - columns.add( - AnalyticsTableColumn.builder() - .name((attribute.getUid() + OU_NAME_COL_SUFFIX)) - .dimensionType(AnalyticsDimensionType.DYNAMIC) - .dataType(TEXT) - .selectExpression(ouNameSql) - .skipIndex(SKIP) - .build()); - } - } - return columns; - } - /** * The select statement used by the table population. * diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java index a6f0841dc2c0..139137fef9cb 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java @@ -27,6 +27,10 @@ */ package org.hisp.dhis.analytics.table; +import static org.hisp.dhis.analytics.table.model.Skip.SKIP; +import static org.hisp.dhis.analytics.util.AnalyticsUtils.getClosingParentheses; +import static org.hisp.dhis.analytics.util.AnalyticsUtils.getColumnType; +import static org.hisp.dhis.db.model.DataType.TEXT; import static org.hisp.dhis.util.DateUtils.toLongDate; import java.util.ArrayList; @@ -37,14 +41,17 @@ import org.hisp.dhis.analytics.AnalyticsTableType; import org.hisp.dhis.analytics.AnalyticsTableUpdateParams; import org.hisp.dhis.analytics.partition.PartitionManager; +import org.hisp.dhis.analytics.table.model.AnalyticsDimensionType; import org.hisp.dhis.analytics.table.model.AnalyticsTable; import org.hisp.dhis.analytics.table.model.AnalyticsTableColumn; import org.hisp.dhis.analytics.table.model.AnalyticsTablePartition; +import org.hisp.dhis.analytics.table.model.Skip; import org.hisp.dhis.analytics.table.setting.AnalyticsTableSettings; import org.hisp.dhis.category.CategoryService; import org.hisp.dhis.common.IdentifiableObjectManager; import org.hisp.dhis.commons.collection.UniqueArrayList; import org.hisp.dhis.dataapproval.DataApprovalLevelService; +import org.hisp.dhis.db.model.DataType; import org.hisp.dhis.db.model.Logged; import org.hisp.dhis.db.sql.SqlBuilder; import org.hisp.dhis.organisationunit.OrganisationUnitService; @@ -53,6 +60,7 @@ import org.hisp.dhis.resourcetable.ResourceTableService; import org.hisp.dhis.setting.SystemSettingsProvider; import org.hisp.dhis.system.database.DatabaseInfoProvider; +import org.hisp.dhis.trackedentity.TrackedEntityAttribute; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; @@ -189,4 +197,60 @@ private List getColumns(Program program) { return filterDimensionColumns(columns); } + + private List getTrackedEntityAttributeColumns(Program program) { + List columns = new ArrayList<>(); + + for (TrackedEntityAttribute attribute : program.getNonConfidentialTrackedEntityAttributes()) { + DataType dataType = getColumnType(attribute.getValueType(), isSpatialSupport()); + String dataClause = + attribute.isNumericType() + ? getNumericClause() + : attribute.isDateType() ? getDateClause() : ""; + String select = getSelectExpressionForAttribute(attribute.getValueType(), "value"); + Skip skipIndex = skipIndex(attribute.getValueType(), attribute.hasOptionSet()); + + String sql = + replaceQualify( + """ + (select ${select} from ${trackedentityattributevalue} \ + where trackedentityid=en.trackedentityid \ + and trackedentityattributeid=${attributeId}\ + ${dataClause})${closingParentheses} as ${attributeUid}""", + Map.of( + "select", + select, + "attributeId", + String.valueOf(attribute.getId()), + "dataClause", + dataClause, + "closingParentheses", + getClosingParentheses(select), + "attributeUid", + quote(attribute.getUid()))); + columns.add( + AnalyticsTableColumn.builder() + .name(attribute.getUid()) + .dimensionType(AnalyticsDimensionType.DYNAMIC) + .dataType(dataType) + .selectExpression(sql) + .skipIndex(skipIndex) + .build()); + + if (attribute.getValueType().isOrganisationUnit()) { + String fromTypeSql = "ou.name from organisationunit ou where ou.uid = (select value"; + String ouNameSql = selectForInsert(attribute, fromTypeSql, dataClause); + + columns.add( + AnalyticsTableColumn.builder() + .name((attribute.getUid() + OU_NAME_COL_SUFFIX)) + .dimensionType(AnalyticsDimensionType.DYNAMIC) + .dataType(TEXT) + .selectExpression(ouNameSql) + .skipIndex(SKIP) + .build()); + } + } + return columns; + } } From 2cab8fce5e176bae188f52e6052b2d8b276baa0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 26 Nov 2024 18:12:17 +0100 Subject: [PATCH 024/110] fix: Update code --- .../JdbcEnrollmentAnalyticsTableManager.java | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java index 139137fef9cb..59be5e960ff0 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java @@ -187,13 +187,7 @@ private List getColumns(Program program) { columns.addAll(getOrganisationUnitGroupSetColumns()); columns.addAll(getPeriodTypeColumns("dps")); columns.addAll(getTrackedEntityAttributeColumns(program)); - - if (program.isRegistration()) { - columns.add(EnrollmentAnalyticsColumn.TRACKED_ENTITY); - if (sqlBuilder.supportsGeospatialData()) { - columns.add(EnrollmentAnalyticsColumn.TRACKED_ENTITY_GEOMETRY); - } - } + columns.addAll(getTrackedEntityColumns(program)); return filterDimensionColumns(columns); } @@ -253,4 +247,23 @@ private List getTrackedEntityAttributeColumns(Program prog } return columns; } + + /** + * Returns a list of tracked entity {@link AnalyticsTableColumn}. + * + * @param program the {@link Program}. + * @return a list of {@link AnalyticsTableColumn}. + */ + private List getTrackedEntityColumns(Program program) { + List columns = new ArrayList<>(); + + if (program.isRegistration()) { + columns.add(EnrollmentAnalyticsColumn.TRACKED_ENTITY); + if (sqlBuilder.supportsGeospatialData()) { + columns.add(EnrollmentAnalyticsColumn.TRACKED_ENTITY_GEOMETRY); + } + } + + return columns; + } } From adc1599bf282ed585ee2606a4eeb1ae2086f1734 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 26 Nov 2024 18:13:05 +0100 Subject: [PATCH 025/110] fix: Update code --- .../table/JdbcEnrollmentAnalyticsTableManager.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java index 59be5e960ff0..7a451dc31086 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java @@ -192,6 +192,12 @@ private List getColumns(Program program) { return filterDimensionColumns(columns); } + /** + * Returns a list of tracked entity attribute {@link AnalyticsTableColumn}. + * + * @param program the {@link Program}. + * @return a list of {@link AnalyticsTableColumn}. + */ private List getTrackedEntityAttributeColumns(Program program) { List columns = new ArrayList<>(); From 793b9fa1fc0079166ad149d4369f4ecaa6fbd7af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 26 Nov 2024 20:12:54 +0100 Subject: [PATCH 026/110] fix: Update code --- .../JdbcTrackedEntityAnalyticsTableManager.java | 8 +++----- ...kedEntityEnrollmentsAnalyticsTableManager.java | 15 +++++++++++++++ ...cTrackedEntityEventsAnalyticsTableManager.java | 11 ++++++++--- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java index bec6c798dbba..b7781c3ea125 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java @@ -237,15 +237,15 @@ private List getColumns( private Stream getAllTrackedEntityAttributes( TrackedEntityType trackedEntityType, Map> programsByTetUid) { - // Given TET has program(s) defined. + // Given TET has program(s) defined if (programsByTetUid.containsKey(trackedEntityType.getUid())) { - // Programs defined for TET -> get attr from program and TET. + // Programs defined for TET -> get attr from program and TET return getAllTrackedEntityAttributesByPrograms( trackedEntityType, programsByTetUid.get(trackedEntityType.getUid())); } - // No programs defined for TET -> get only attributes from TET. + // No programs defined for TET -> get only attributes from TET return getAllTrackedEntityAttributesByEntityType(trackedEntityType); } @@ -297,9 +297,7 @@ private String castBasedOnType(ValueType valueType, String columnName) { private Stream getAllTrackedEntityAttributesByPrograms( TrackedEntityType trackedEntityType, List programs) { return Stream.concat( - /* all attributes of programs */ trackedEntityAttributeService.getProgramTrackedEntityAttributes(programs).stream(), - /* all attributes of the trackedEntityType */ getAllTrackedEntityAttributesByEntityType(trackedEntityType)) .distinct(); } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEnrollmentsAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEnrollmentsAnalyticsTableManager.java index 95a57aaf8168..fc9f6f1b510a 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEnrollmentsAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEnrollmentsAnalyticsTableManager.java @@ -287,6 +287,11 @@ and ev.status in (${statuses})) \ invokeTimeAndLog(sql.toString(), "Populating table: '{}'", tableName); } + /** + * Returns a list of fixed columns. + * + * @return a list of {@link AnalyticsTableColumn}. + */ private List getFixedCols() { List columns = new ArrayList<>(); columns.addAll(FIXED_COLS); @@ -296,6 +301,11 @@ private List getFixedCols() { return columns; } + /** + * Returns a list of geospatial columns. + * + * @return a list of {@link AnalyticsTableColumn}. + */ private List getGeospatialCols() { return List.of( @@ -319,6 +329,11 @@ private List getGeospatialCols() { .build()); } + /** + * Returns a partition column. + * + * @return an {@link AnalyticsTableColumn}. + */ private AnalyticsTableColumn getPartitionColumn() { return AnalyticsTableColumn.builder() .name("year") diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManager.java index 904374494c13..f3055b6db7bc 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManager.java @@ -193,9 +193,8 @@ public class JdbcTrackedEntityEventsAnalyticsTableManager extends AbstractJdbcTa .selectExpression("ous.level") .build()); - private static final String AND = " and ("; - private final TrackedEntityTypeService trackedEntityTypeService; + private final AnalyticsSqlBuilder analyticsSqlBuilder; public JdbcTrackedEntityEventsAnalyticsTableManager( @@ -340,7 +339,8 @@ private List getDataYears(AnalyticsTableUpdateParams params, TrackedEnt "tetId", String.valueOf(tet.getId())))); if (params.getFromDate() != null) { - sql.append(AND + eventDateExpression + ") >= '" + toMediumDate(params.getFromDate()) + "'"); + sql.append( + " and (" + eventDateExpression + ") >= '" + toMediumDate(params.getFromDate()) + "'"); } List availableDataYears = @@ -451,6 +451,11 @@ private String getPartitionClause(AnalyticsTablePartition partition) { : emptyIfTrue(partitionFilter, sqlBuilder.supportsDeclarativePartitioning()); } + /** + * Returns a partition column. + * + * @return an {@link AnalyticsTableColumn}. + */ private AnalyticsTableColumn getPartitionColumn() { return AnalyticsTableColumn.builder() .name("year") From 105b514d465124df0edfe6fb7da6a8e3b203dec9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 26 Nov 2024 20:15:51 +0100 Subject: [PATCH 027/110] fix: Update code --- .../dhis/analytics/table/JdbcEventAnalyticsTableManager.java | 1 - 1 file changed, 1 deletion(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 9182cfc490d5..72e93ba076ee 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -414,7 +414,6 @@ private List getColumns(Program program) { } } if (sqlBuilder.supportsDeclarativePartitioning()) { - // Add the year column required for declarative partitioning columns.add(getPartitionColumn()); } From 45d734e4f9b38b548420d3d67e35779b3caf84ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 26 Nov 2024 20:32:08 +0100 Subject: [PATCH 028/110] fix: Update code --- .../table/JdbcEventAnalyticsTableManager.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 72e93ba076ee..3d7844f9091e 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -50,6 +50,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.stream.Collectors; import lombok.extern.slf4j.Slf4j; import org.hisp.dhis.analytics.AnalyticsTableHookService; import org.hisp.dhis.analytics.AnalyticsTableType; @@ -332,6 +333,7 @@ public void populateTable(AnalyticsTableUpdateParams params, AnalyticsTableParti Integer latestDataYear = availableDataYears.get(availableDataYears.size() - 1); Program program = partition.getMasterTable().getProgram(); String partitionClause = getPartitionClause(partition); + String attributeJoinClause = getAttributeJoinClause(program); String fromClause = replaceQualify( @@ -350,6 +352,7 @@ public void populateTable(AnalyticsTableUpdateParams params, AnalyticsTableParti left join ${organisationunit} enrollmentou on en.organisationunitid=enrollmentou.organisationunitid \ inner join analytics_rs_categorystructure acs on ev.attributeoptioncomboid=acs.categoryoptioncomboid \ left join analytics_rs_dateperiodstructure dps on cast(${eventDateExpression} as date)=dps.dateperiod \ + ${attributeJoinClause}\ where ev.lastupdated < '${startTime}' ${partitionClause} \ and pr.programid=${programId} \ and ev.organisationunitid is not null \ @@ -362,6 +365,7 @@ and ev.status in (${exportableEventStatues}) \ "eventDateMonth", sqlBuilder.dateTrunc("month", eventDateExpression), "eventDateExpression", eventDateExpression, "partitionClause", partitionClause, + "attributeJoinClause", attributeJoinClause, "startTime", toLongDate(params.getStartTime()), "programId", String.valueOf(program.getId()), "firstDataYear", String.valueOf(firstDataYear), @@ -371,6 +375,19 @@ and ev.status in (${exportableEventStatues}) \ populateTableInternal(partition, fromClause); } + private String getAttributeJoinClause(Program program) { + String template = + """ + left join ${trackedentityattributevalue} ${uid} \ + on en.trackedentityid=${uid}.trackedentityid \ + and ${uid}.trackedentityattributeid = ${uid}"""; + + return getAttributeColumns(program).stream() + .map(col -> replaceQualify(template, Map.of("uid", quote(col.getName())))) + .collect(Collectors.joining(" ")) + + " "; + } + /** * Returns a partition SQL clause. * @@ -420,7 +437,7 @@ private List getColumns(Program program) { return filterDimensionColumns(columns); } - protected AnalyticsTableColumn getPartitionColumn() { + private AnalyticsTableColumn getPartitionColumn() { return AnalyticsTableColumn.builder() .name("year") .dataType(INTEGER) @@ -815,6 +832,7 @@ private List getDataYears( "fromDate", toMediumDate(params.getFromDate()))) : EMPTY; + String sql = replaceQualify( """ From 793a55bc4e608ee49af2aba5fbbd0631cc4ec4d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 26 Nov 2024 20:33:14 +0100 Subject: [PATCH 029/110] fix: Update code --- .../analytics/table/JdbcEventAnalyticsTableManager.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 3d7844f9091e..312917e0ff06 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -375,6 +375,12 @@ and ev.status in (${exportableEventStatues}) \ populateTableInternal(partition, fromClause); } + /** + * Returns a join clause for attribute value for every attribute of the given program. + * + * @param program the {@link Program}. + * @return a join clause. + */ private String getAttributeJoinClause(Program program) { String template = """ From 0640b81d059c8f8c0dddbe03569c12d20278428f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 26 Nov 2024 20:34:39 +0100 Subject: [PATCH 030/110] fix: Update code --- .../table/JdbcEventAnalyticsTableManager.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 312917e0ff06..ca676cb4450a 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -41,7 +41,6 @@ import static org.hisp.dhis.db.model.DataType.TEXT; import static org.hisp.dhis.util.DateUtils.toLongDate; import static org.hisp.dhis.util.DateUtils.toMediumDate; - import java.time.Year; import java.util.ArrayList; import java.util.Collection; @@ -51,7 +50,6 @@ import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; -import lombok.extern.slf4j.Slf4j; import org.hisp.dhis.analytics.AnalyticsTableHookService; import org.hisp.dhis.analytics.AnalyticsTableType; import org.hisp.dhis.analytics.AnalyticsTableUpdateParams; @@ -90,6 +88,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; +import lombok.extern.slf4j.Slf4j; /** * @author Lars Helge Overland @@ -388,10 +387,11 @@ private String getAttributeJoinClause(Program program) { on en.trackedentityid=${uid}.trackedentityid \ and ${uid}.trackedentityattributeid = ${uid}"""; - return getAttributeColumns(program).stream() - .map(col -> replaceQualify(template, Map.of("uid", quote(col.getName())))) - .collect(Collectors.joining(" ")) - + " "; + String sql = getAttributeColumns(program).stream() + .map(col -> replaceQualify(template, Map.of("uid", quote(col.getName())))) + .collect(Collectors.joining(" ")); + + return sql + " "; } /** From f33bd881014f02dbba408653ac01fdc64e19a7a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 26 Nov 2024 20:34:52 +0100 Subject: [PATCH 031/110] fix: Update code --- .../table/JdbcEventAnalyticsTableManager.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index ca676cb4450a..bb3ae7df2ae7 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -41,6 +41,7 @@ import static org.hisp.dhis.db.model.DataType.TEXT; import static org.hisp.dhis.util.DateUtils.toLongDate; import static org.hisp.dhis.util.DateUtils.toMediumDate; + import java.time.Year; import java.util.ArrayList; import java.util.Collection; @@ -50,6 +51,7 @@ import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; +import lombok.extern.slf4j.Slf4j; import org.hisp.dhis.analytics.AnalyticsTableHookService; import org.hisp.dhis.analytics.AnalyticsTableType; import org.hisp.dhis.analytics.AnalyticsTableUpdateParams; @@ -88,7 +90,6 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; -import lombok.extern.slf4j.Slf4j; /** * @author Lars Helge Overland @@ -332,7 +333,7 @@ public void populateTable(AnalyticsTableUpdateParams params, AnalyticsTableParti Integer latestDataYear = availableDataYears.get(availableDataYears.size() - 1); Program program = partition.getMasterTable().getProgram(); String partitionClause = getPartitionClause(partition); - String attributeJoinClause = getAttributeJoinClause(program); + String attributeJoinClause = getAttributeValueJoinClause(program); String fromClause = replaceQualify( @@ -380,17 +381,18 @@ and ev.status in (${exportableEventStatues}) \ * @param program the {@link Program}. * @return a join clause. */ - private String getAttributeJoinClause(Program program) { + private String getAttributeValueJoinClause(Program program) { String template = """ left join ${trackedentityattributevalue} ${uid} \ on en.trackedentityid=${uid}.trackedentityid \ and ${uid}.trackedentityattributeid = ${uid}"""; - String sql = getAttributeColumns(program).stream() - .map(col -> replaceQualify(template, Map.of("uid", quote(col.getName())))) - .collect(Collectors.joining(" ")); - + String sql = + getAttributeColumns(program).stream() + .map(col -> replaceQualify(template, Map.of("uid", quote(col.getName())))) + .collect(Collectors.joining(" ")); + return sql + " "; } From 3a6b574f1cd61218e2412bf2ed9bae60f11cbe29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 26 Nov 2024 20:35:09 +0100 Subject: [PATCH 032/110] fix: Update code --- .../table/JdbcEventAnalyticsTableManager.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index bb3ae7df2ae7..9a5e40d58302 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -41,7 +41,6 @@ import static org.hisp.dhis.db.model.DataType.TEXT; import static org.hisp.dhis.util.DateUtils.toLongDate; import static org.hisp.dhis.util.DateUtils.toMediumDate; - import java.time.Year; import java.util.ArrayList; import java.util.Collection; @@ -51,7 +50,6 @@ import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; -import lombok.extern.slf4j.Slf4j; import org.hisp.dhis.analytics.AnalyticsTableHookService; import org.hisp.dhis.analytics.AnalyticsTableType; import org.hisp.dhis.analytics.AnalyticsTableUpdateParams; @@ -90,6 +88,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; +import lombok.extern.slf4j.Slf4j; /** * @author Lars Helge Overland @@ -388,10 +387,9 @@ private String getAttributeValueJoinClause(Program program) { on en.trackedentityid=${uid}.trackedentityid \ and ${uid}.trackedentityattributeid = ${uid}"""; - String sql = - getAttributeColumns(program).stream() - .map(col -> replaceQualify(template, Map.of("uid", quote(col.getName())))) - .collect(Collectors.joining(" ")); + String sql =getAttributeColumns(program).stream() + .map(col -> replaceQualify(template, Map.of("uid", quote(col.getName())))) + .collect(Collectors.joining(" ")); return sql + " "; } From 7ac31de85f9d8b3304fff778c714c86ae815d46f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 26 Nov 2024 21:46:45 +0100 Subject: [PATCH 033/110] fix: Update code --- .../table/JdbcEventAnalyticsTableManager.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 9a5e40d58302..5975f5da5642 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -385,13 +385,12 @@ private String getAttributeValueJoinClause(Program program) { """ left join ${trackedentityattributevalue} ${uid} \ on en.trackedentityid=${uid}.trackedentityid \ - and ${uid}.trackedentityattributeid = ${uid}"""; + and ${uid}.trackedentityattributeid = ${id}\s"""; - String sql =getAttributeColumns(program).stream() - .map(col -> replaceQualify(template, Map.of("uid", quote(col.getName())))) - .collect(Collectors.joining(" ")); - - return sql + " "; + return program.getNonConfidentialTrackedEntityAttributes().stream() + .map(at -> replaceQualify(template, Map.of( + "uid", quote(at.getUid()), "id", String.valueOf(at.getId())))) + .collect(Collectors.joining()); } /** From cd9c2ed9e295935de711db36de415055a54d7847 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 26 Nov 2024 21:47:14 +0100 Subject: [PATCH 034/110] fix: Update code --- .../analytics/table/JdbcEventAnalyticsTableManager.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 5975f5da5642..ed53f54e9aed 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -41,6 +41,7 @@ import static org.hisp.dhis.db.model.DataType.TEXT; import static org.hisp.dhis.util.DateUtils.toLongDate; import static org.hisp.dhis.util.DateUtils.toMediumDate; + import java.time.Year; import java.util.ArrayList; import java.util.Collection; @@ -50,6 +51,7 @@ import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; +import lombok.extern.slf4j.Slf4j; import org.hisp.dhis.analytics.AnalyticsTableHookService; import org.hisp.dhis.analytics.AnalyticsTableType; import org.hisp.dhis.analytics.AnalyticsTableUpdateParams; @@ -88,7 +90,6 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; -import lombok.extern.slf4j.Slf4j; /** * @author Lars Helge Overland @@ -388,8 +389,10 @@ private String getAttributeValueJoinClause(Program program) { and ${uid}.trackedentityattributeid = ${id}\s"""; return program.getNonConfidentialTrackedEntityAttributes().stream() - .map(at -> replaceQualify(template, Map.of( - "uid", quote(at.getUid()), "id", String.valueOf(at.getId())))) + .map( + at -> + replaceQualify( + template, Map.of("uid", quote(at.getUid()), "id", String.valueOf(at.getId())))) .collect(Collectors.joining()); } From 9cf4321ad9f7efc4d4d771261aef7172b14174f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 26 Nov 2024 22:42:47 +0100 Subject: [PATCH 035/110] fix: Update code --- .../table/JdbcEventAnalyticsTableManager.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index b5ca5601adc3..0ab1b5a3b676 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -66,6 +66,7 @@ import org.hisp.dhis.calendar.Calendar; import org.hisp.dhis.category.Category; import org.hisp.dhis.category.CategoryService; +import org.hisp.dhis.common.IdentifiableObject; import org.hisp.dhis.common.IdentifiableObjectManager; import org.hisp.dhis.common.ValueType; import org.hisp.dhis.commons.collection.ListUtils; @@ -389,13 +390,22 @@ private String getAttributeValueJoinClause(Program program) { and ${uid}.trackedentityattributeid = ${id}\s"""; return program.getNonConfidentialTrackedEntityAttributes().stream() - .map( - at -> - replaceQualify( - template, Map.of("uid", quote(at.getUid()), "id", String.valueOf(at.getId())))) + .map(at -> replaceQualify(template, toVariableMap(at))) .collect(Collectors.joining()); } + /** + * Returns a variable map. + * + * @param object the object. + * @return a {@link Map}. + */ + private Map toVariableMap(IdentifiableObject object) { + return Map.of( + "id", String.valueOf(object.getId()), + "uid", quote(object.getUid())); + } + /** * Returns a partition SQL clause. * From df69357740aee75be7d76bb168b07b8913ba3b30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 26 Nov 2024 22:43:27 +0100 Subject: [PATCH 036/110] fix: Update code --- .../dhis/analytics/table/JdbcEventAnalyticsTableManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 0ab1b5a3b676..23270d34231f 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -390,7 +390,7 @@ private String getAttributeValueJoinClause(Program program) { and ${uid}.trackedentityattributeid = ${id}\s"""; return program.getNonConfidentialTrackedEntityAttributes().stream() - .map(at -> replaceQualify(template, toVariableMap(at))) + .map(attribute -> replaceQualify(template, toVariableMap(attribute))) .collect(Collectors.joining()); } From 007fabb4a806cf5f5703d5f5a9a87f6c35662801 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Fri, 29 Nov 2024 09:47:03 +0100 Subject: [PATCH 037/110] fix: Update code --- .../dhis/analytics/event/data/TimeFieldSqlRenderer.java | 6 +++--- .../hisp/dhis/program/function/ProgramCountFunction.java | 2 +- .../hisp/dhis/program/function/ProgramMinMaxFunction.java | 2 +- .../dhis/parser/expression/CommonExpressionVisitor.java | 6 +++--- .../expression/statement/DefaultSqlStatementBuilder.java} | 4 ++-- .../dhis/parser/expression/statement}/StatementBuilder.java | 2 +- 6 files changed, 11 insertions(+), 11 deletions(-) rename dhis-2/dhis-support/{dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/PostgreSqlStatementBuilder.java => dhis-support-expression-parser/src/main/java/org/hisp/dhis/parser/expression/statement/DefaultSqlStatementBuilder.java} (99%) rename dhis-2/dhis-support/{dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc => dhis-support-expression-parser/src/main/java/org/hisp/dhis/parser/expression/statement}/StatementBuilder.java (99%) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/TimeFieldSqlRenderer.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/TimeFieldSqlRenderer.java index 8cccb77fdc90..39029174670b 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/TimeFieldSqlRenderer.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/TimeFieldSqlRenderer.java @@ -51,15 +51,15 @@ import org.hisp.dhis.common.DateRange; import org.hisp.dhis.common.DimensionalItemObject; import org.hisp.dhis.db.sql.SqlBuilder; -import org.hisp.dhis.jdbc.PostgreSqlStatementBuilder; -import org.hisp.dhis.jdbc.StatementBuilder; +import org.hisp.dhis.parser.expression.statement.DefaultSqlStatementBuilder; +import org.hisp.dhis.parser.expression.statement.StatementBuilder; import org.hisp.dhis.period.Period; import org.hisp.dhis.period.PeriodType; /** Provides methods targeting the generation of SQL statements for periods and time fields. */ public abstract class TimeFieldSqlRenderer { protected final SqlBuilder sqlBuilder; - protected final StatementBuilder statementBuilder = new PostgreSqlStatementBuilder(); + protected final StatementBuilder statementBuilder = new DefaultSqlStatementBuilder(); protected TimeFieldSqlRenderer(SqlBuilder sqlBuilder) { this.sqlBuilder = sqlBuilder; diff --git a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/function/ProgramCountFunction.java b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/function/ProgramCountFunction.java index 62a4978b05b7..be852b9cfb3b 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/function/ProgramCountFunction.java +++ b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/function/ProgramCountFunction.java @@ -30,10 +30,10 @@ import java.util.Date; import org.hisp.dhis.analytics.AnalyticsConstants; import org.hisp.dhis.antlr.ParserExceptionWithoutContext; -import org.hisp.dhis.jdbc.StatementBuilder; import org.hisp.dhis.parser.expression.CommonExpressionVisitor; import org.hisp.dhis.parser.expression.ProgramExpressionParams; import org.hisp.dhis.parser.expression.antlr.ExpressionParser.ExprContext; +import org.hisp.dhis.parser.expression.statement.StatementBuilder; import org.hisp.dhis.program.ProgramExpressionItem; import org.hisp.dhis.program.ProgramIndicator; import org.hisp.dhis.program.dataitem.ProgramItemStageElement; diff --git a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/function/ProgramMinMaxFunction.java b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/function/ProgramMinMaxFunction.java index f37ae35271f0..efa538adce0e 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/function/ProgramMinMaxFunction.java +++ b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/function/ProgramMinMaxFunction.java @@ -29,10 +29,10 @@ import java.util.Date; import org.hisp.dhis.analytics.AnalyticsConstants; -import org.hisp.dhis.jdbc.StatementBuilder; import org.hisp.dhis.parser.expression.CommonExpressionVisitor; import org.hisp.dhis.parser.expression.ProgramExpressionParams; import org.hisp.dhis.parser.expression.antlr.ExpressionParser.ExprContext; +import org.hisp.dhis.parser.expression.statement.StatementBuilder; import org.hisp.dhis.program.AnalyticsType; import org.hisp.dhis.program.ProgramExpressionItem; import org.hisp.dhis.program.ProgramIndicator; diff --git a/dhis-2/dhis-support/dhis-support-expression-parser/src/main/java/org/hisp/dhis/parser/expression/CommonExpressionVisitor.java b/dhis-2/dhis-support/dhis-support-expression-parser/src/main/java/org/hisp/dhis/parser/expression/CommonExpressionVisitor.java index 78f832cc96de..684adbb67a8d 100644 --- a/dhis-2/dhis-support/dhis-support-expression-parser/src/main/java/org/hisp/dhis/parser/expression/CommonExpressionVisitor.java +++ b/dhis-2/dhis-support/dhis-support-expression-parser/src/main/java/org/hisp/dhis/parser/expression/CommonExpressionVisitor.java @@ -46,9 +46,9 @@ import org.hisp.dhis.expression.ExpressionInfo; import org.hisp.dhis.expression.ExpressionParams; import org.hisp.dhis.i18n.I18n; -import org.hisp.dhis.jdbc.PostgreSqlStatementBuilder; -import org.hisp.dhis.jdbc.StatementBuilder; import org.hisp.dhis.parser.expression.antlr.ExpressionParser.ExprContext; +import org.hisp.dhis.parser.expression.statement.DefaultSqlStatementBuilder; +import org.hisp.dhis.parser.expression.statement.StatementBuilder; import org.hisp.dhis.program.ProgramIndicatorService; import org.hisp.dhis.program.ProgramStageService; import org.hisp.dhis.trackedentity.TrackedEntityAttributeService; @@ -62,7 +62,7 @@ @Setter @Builder(toBuilder = true) public class CommonExpressionVisitor extends AntlrExpressionVisitor { - private final StatementBuilder statementBuilder = new PostgreSqlStatementBuilder(); + private final StatementBuilder statementBuilder = new DefaultSqlStatementBuilder(); private IdentifiableObjectManager idObjectManager; diff --git a/dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/PostgreSqlStatementBuilder.java b/dhis-2/dhis-support/dhis-support-expression-parser/src/main/java/org/hisp/dhis/parser/expression/statement/DefaultSqlStatementBuilder.java similarity index 99% rename from dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/PostgreSqlStatementBuilder.java rename to dhis-2/dhis-support/dhis-support-expression-parser/src/main/java/org/hisp/dhis/parser/expression/statement/DefaultSqlStatementBuilder.java index a5248d65eed1..f6a6a0e1eef0 100644 --- a/dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/PostgreSqlStatementBuilder.java +++ b/dhis-2/dhis-support/dhis-support-expression-parser/src/main/java/org/hisp/dhis/parser/expression/statement/DefaultSqlStatementBuilder.java @@ -25,7 +25,7 @@ * (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.jdbc; +package org.hisp.dhis.parser.expression.statement; import static java.lang.String.format; import static org.apache.commons.lang3.StringUtils.SPACE; @@ -50,7 +50,7 @@ * @author Lars Helge Overland */ @NoArgsConstructor -public class PostgreSqlStatementBuilder implements StatementBuilder { +public class DefaultSqlStatementBuilder implements StatementBuilder { protected static final String QUOTE = "\""; protected static final String SINGLE_QUOTE = "'"; diff --git a/dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/StatementBuilder.java b/dhis-2/dhis-support/dhis-support-expression-parser/src/main/java/org/hisp/dhis/parser/expression/statement/StatementBuilder.java similarity index 99% rename from dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/StatementBuilder.java rename to dhis-2/dhis-support/dhis-support-expression-parser/src/main/java/org/hisp/dhis/parser/expression/statement/StatementBuilder.java index eb239c2986a3..e51f1ac6b58c 100644 --- a/dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/StatementBuilder.java +++ b/dhis-2/dhis-support/dhis-support-expression-parser/src/main/java/org/hisp/dhis/parser/expression/statement/StatementBuilder.java @@ -25,7 +25,7 @@ * (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.jdbc; +package org.hisp.dhis.parser.expression.statement; import java.util.Date; import org.hisp.dhis.program.AnalyticsPeriodBoundary; From 8af5d48e34c25984d8ff7df2b0d248434ad18be0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Fri, 29 Nov 2024 09:48:37 +0100 Subject: [PATCH 038/110] Revert "fix: Update code" This reverts commit 007fabb4a806cf5f5703d5f5a9a87f6c35662801. --- .../dhis/analytics/event/data/TimeFieldSqlRenderer.java | 6 +++--- .../hisp/dhis/program/function/ProgramCountFunction.java | 2 +- .../hisp/dhis/program/function/ProgramMinMaxFunction.java | 2 +- .../dhis/parser/expression/CommonExpressionVisitor.java | 6 +++--- .../org/hisp/dhis/jdbc/PostgreSqlStatementBuilder.java} | 4 ++-- .../src/main/java/org/hisp/dhis/jdbc}/StatementBuilder.java | 2 +- 6 files changed, 11 insertions(+), 11 deletions(-) rename dhis-2/dhis-support/{dhis-support-expression-parser/src/main/java/org/hisp/dhis/parser/expression/statement/DefaultSqlStatementBuilder.java => dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/PostgreSqlStatementBuilder.java} (99%) rename dhis-2/dhis-support/{dhis-support-expression-parser/src/main/java/org/hisp/dhis/parser/expression/statement => dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc}/StatementBuilder.java (99%) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/TimeFieldSqlRenderer.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/TimeFieldSqlRenderer.java index 39029174670b..8cccb77fdc90 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/TimeFieldSqlRenderer.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/TimeFieldSqlRenderer.java @@ -51,15 +51,15 @@ import org.hisp.dhis.common.DateRange; import org.hisp.dhis.common.DimensionalItemObject; import org.hisp.dhis.db.sql.SqlBuilder; -import org.hisp.dhis.parser.expression.statement.DefaultSqlStatementBuilder; -import org.hisp.dhis.parser.expression.statement.StatementBuilder; +import org.hisp.dhis.jdbc.PostgreSqlStatementBuilder; +import org.hisp.dhis.jdbc.StatementBuilder; import org.hisp.dhis.period.Period; import org.hisp.dhis.period.PeriodType; /** Provides methods targeting the generation of SQL statements for periods and time fields. */ public abstract class TimeFieldSqlRenderer { protected final SqlBuilder sqlBuilder; - protected final StatementBuilder statementBuilder = new DefaultSqlStatementBuilder(); + protected final StatementBuilder statementBuilder = new PostgreSqlStatementBuilder(); protected TimeFieldSqlRenderer(SqlBuilder sqlBuilder) { this.sqlBuilder = sqlBuilder; diff --git a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/function/ProgramCountFunction.java b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/function/ProgramCountFunction.java index be852b9cfb3b..62a4978b05b7 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/function/ProgramCountFunction.java +++ b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/function/ProgramCountFunction.java @@ -30,10 +30,10 @@ import java.util.Date; import org.hisp.dhis.analytics.AnalyticsConstants; import org.hisp.dhis.antlr.ParserExceptionWithoutContext; +import org.hisp.dhis.jdbc.StatementBuilder; import org.hisp.dhis.parser.expression.CommonExpressionVisitor; import org.hisp.dhis.parser.expression.ProgramExpressionParams; import org.hisp.dhis.parser.expression.antlr.ExpressionParser.ExprContext; -import org.hisp.dhis.parser.expression.statement.StatementBuilder; import org.hisp.dhis.program.ProgramExpressionItem; import org.hisp.dhis.program.ProgramIndicator; import org.hisp.dhis.program.dataitem.ProgramItemStageElement; diff --git a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/function/ProgramMinMaxFunction.java b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/function/ProgramMinMaxFunction.java index efa538adce0e..f37ae35271f0 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/function/ProgramMinMaxFunction.java +++ b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/function/ProgramMinMaxFunction.java @@ -29,10 +29,10 @@ import java.util.Date; import org.hisp.dhis.analytics.AnalyticsConstants; +import org.hisp.dhis.jdbc.StatementBuilder; import org.hisp.dhis.parser.expression.CommonExpressionVisitor; import org.hisp.dhis.parser.expression.ProgramExpressionParams; import org.hisp.dhis.parser.expression.antlr.ExpressionParser.ExprContext; -import org.hisp.dhis.parser.expression.statement.StatementBuilder; import org.hisp.dhis.program.AnalyticsType; import org.hisp.dhis.program.ProgramExpressionItem; import org.hisp.dhis.program.ProgramIndicator; diff --git a/dhis-2/dhis-support/dhis-support-expression-parser/src/main/java/org/hisp/dhis/parser/expression/CommonExpressionVisitor.java b/dhis-2/dhis-support/dhis-support-expression-parser/src/main/java/org/hisp/dhis/parser/expression/CommonExpressionVisitor.java index 684adbb67a8d..78f832cc96de 100644 --- a/dhis-2/dhis-support/dhis-support-expression-parser/src/main/java/org/hisp/dhis/parser/expression/CommonExpressionVisitor.java +++ b/dhis-2/dhis-support/dhis-support-expression-parser/src/main/java/org/hisp/dhis/parser/expression/CommonExpressionVisitor.java @@ -46,9 +46,9 @@ import org.hisp.dhis.expression.ExpressionInfo; import org.hisp.dhis.expression.ExpressionParams; import org.hisp.dhis.i18n.I18n; +import org.hisp.dhis.jdbc.PostgreSqlStatementBuilder; +import org.hisp.dhis.jdbc.StatementBuilder; import org.hisp.dhis.parser.expression.antlr.ExpressionParser.ExprContext; -import org.hisp.dhis.parser.expression.statement.DefaultSqlStatementBuilder; -import org.hisp.dhis.parser.expression.statement.StatementBuilder; import org.hisp.dhis.program.ProgramIndicatorService; import org.hisp.dhis.program.ProgramStageService; import org.hisp.dhis.trackedentity.TrackedEntityAttributeService; @@ -62,7 +62,7 @@ @Setter @Builder(toBuilder = true) public class CommonExpressionVisitor extends AntlrExpressionVisitor { - private final StatementBuilder statementBuilder = new DefaultSqlStatementBuilder(); + private final StatementBuilder statementBuilder = new PostgreSqlStatementBuilder(); private IdentifiableObjectManager idObjectManager; diff --git a/dhis-2/dhis-support/dhis-support-expression-parser/src/main/java/org/hisp/dhis/parser/expression/statement/DefaultSqlStatementBuilder.java b/dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/PostgreSqlStatementBuilder.java similarity index 99% rename from dhis-2/dhis-support/dhis-support-expression-parser/src/main/java/org/hisp/dhis/parser/expression/statement/DefaultSqlStatementBuilder.java rename to dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/PostgreSqlStatementBuilder.java index f6a6a0e1eef0..a5248d65eed1 100644 --- a/dhis-2/dhis-support/dhis-support-expression-parser/src/main/java/org/hisp/dhis/parser/expression/statement/DefaultSqlStatementBuilder.java +++ b/dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/PostgreSqlStatementBuilder.java @@ -25,7 +25,7 @@ * (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.parser.expression.statement; +package org.hisp.dhis.jdbc; import static java.lang.String.format; import static org.apache.commons.lang3.StringUtils.SPACE; @@ -50,7 +50,7 @@ * @author Lars Helge Overland */ @NoArgsConstructor -public class DefaultSqlStatementBuilder implements StatementBuilder { +public class PostgreSqlStatementBuilder implements StatementBuilder { protected static final String QUOTE = "\""; protected static final String SINGLE_QUOTE = "'"; diff --git a/dhis-2/dhis-support/dhis-support-expression-parser/src/main/java/org/hisp/dhis/parser/expression/statement/StatementBuilder.java b/dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/StatementBuilder.java similarity index 99% rename from dhis-2/dhis-support/dhis-support-expression-parser/src/main/java/org/hisp/dhis/parser/expression/statement/StatementBuilder.java rename to dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/StatementBuilder.java index e51f1ac6b58c..eb239c2986a3 100644 --- a/dhis-2/dhis-support/dhis-support-expression-parser/src/main/java/org/hisp/dhis/parser/expression/statement/StatementBuilder.java +++ b/dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/StatementBuilder.java @@ -25,7 +25,7 @@ * (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.parser.expression.statement; +package org.hisp.dhis.jdbc; import java.util.Date; import org.hisp.dhis.program.AnalyticsPeriodBoundary; From 4038b0d88f7bdb7fea8c8cbb0269612a9cd87f0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Sat, 30 Nov 2024 22:40:40 +0100 Subject: [PATCH 039/110] fix: Update code --- .../table/AbstractEventJdbcTableManager.java | 9 +++++---- .../table/JdbcEventAnalyticsTableManager.java | 18 +++++++++++++----- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java index f00f880375ec..c7d3e6977a6d 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java @@ -32,6 +32,7 @@ import java.util.List; import java.util.Map; + import org.hisp.dhis.analytics.AnalyticsTableHookService; import org.hisp.dhis.analytics.partition.PartitionManager; import org.hisp.dhis.analytics.table.model.AnalyticsTableColumn; @@ -224,20 +225,20 @@ protected void populateTableInternal(AnalyticsTablePartition partition, String f * * @param attribute the {@link TrackedEntityAttribute}. * @param columnExpression the column expression. - * @param dataClause the data type related clause like "NUMERIC". + * @param dataFilterClause the data filter clause. * @return a select statement. */ protected String getSelectSubquery( - TrackedEntityAttribute attribute, String columnExpression, String dataClause) { + TrackedEntityAttribute attribute, String columnExpression, String dataFilterClause) { return replaceQualify( """ (select ${columnExpression} from ${trackedentityattributevalue} \ where trackedentityid=en.trackedentityid \ - and trackedentityattributeid=${attributeId}${dataClause})\ + and trackedentityattributeid=${attributeId}${dataFilterClause})\ ${closingParentheses} as ${attributeUid}""", Map.of( "columnExpression", columnExpression, - "dataClause", dataClause, + "dataFilterClause", dataFilterClause, "attributeId", String.valueOf(attribute.getId()), "closingParentheses", getClosingParentheses(columnExpression), "attributeUid", quote(attribute.getUid()))); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 7735272ef20e..731207bb5376 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -51,7 +51,7 @@ import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; -import lombok.extern.slf4j.Slf4j; + import org.hisp.dhis.analytics.AnalyticsTableHookService; import org.hisp.dhis.analytics.AnalyticsTableType; import org.hisp.dhis.analytics.AnalyticsTableUpdateParams; @@ -92,6 +92,8 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; +import lombok.extern.slf4j.Slf4j; + /** * @author Lars Helge Overland */ @@ -629,14 +631,16 @@ private List getAttributeColumns(Program program) { private List getColumnForAttribute(TrackedEntityAttribute attribute) { List columns = new ArrayList<>(); + String columnName = String.format("%s.value", quote(attribute.getUid())); DataType dataType = getColumnType(attribute.getValueType(), isSpatialSupport()); - String selectExpression = getSelectExpressionForAttribute(attribute.getValueType(), "value"); - String dataExpression = getDataFilterClause(attribute); - String sql = getSelectSubquery(attribute, selectExpression, dataExpression); + String selectExpression = getSelectExpressionForAttribute(attribute.getValueType(), columnName); + String dataFilterClause = getDataFilterClause(attribute); + String sql = getSelectForInsert(attribute, selectExpression); + //String sql = getSelectSubquery(attribute, selectExpression, dataFilterClause); Skip skipIndex = skipIndex(attribute.getValueType(), attribute.hasOptionSet()); if (attribute.getValueType().isOrganisationUnit()) { - columns.addAll(getColumnsForOrgUnitTrackedEntityAttribute(attribute, dataExpression)); + columns.addAll(getColumnsForOrgUnitTrackedEntityAttribute(attribute, dataFilterClause)); } columns.add( @@ -762,6 +766,10 @@ private String getSelectForInsert( "uid", quote(dataElement.getUid()))); } + + private String getSelectForInsert(TrackedEntityAttribute attribute, String selectExpression) { + return String.format( "%s as %s", selectExpression, quote(attribute.getUid())); + } /** * Returns a list of columns. From b48b83c90e825b84b0e9fc73898d5d9ee9acb2bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Sun, 1 Dec 2024 11:33:21 +0100 Subject: [PATCH 040/110] fix: Update code --- .../table/AbstractEventJdbcTableManager.java | 1 - .../table/AbstractJdbcTableManager.java | 2 +- .../table/JdbcEventAnalyticsTableManager.java | 22 +++++++++++-------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java index c7d3e6977a6d..0a60a2d5042f 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java @@ -32,7 +32,6 @@ import java.util.List; import java.util.Map; - import org.hisp.dhis.analytics.AnalyticsTableHookService; import org.hisp.dhis.analytics.partition.PartitionManager; import org.hisp.dhis.analytics.table.model.AnalyticsTableColumn; diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java index d21a187ed63c..9969f14e6587 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java @@ -431,7 +431,7 @@ protected AnalyticsTable getLatestAnalyticsTable( protected void invokeTimeAndLog(String sql, String logPattern, Object... args) { Timer timer = new SystemTimer().start(); - log.debug("Populate table SQL: '{}'", sql); + log.info("Populate table SQL: '{}'", sql); jdbcTemplate.execute(sql); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 731207bb5376..6c270dd21f7d 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -51,7 +51,7 @@ import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; - +import lombok.extern.slf4j.Slf4j; import org.hisp.dhis.analytics.AnalyticsTableHookService; import org.hisp.dhis.analytics.AnalyticsTableType; import org.hisp.dhis.analytics.AnalyticsTableUpdateParams; @@ -92,8 +92,6 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; -import lombok.extern.slf4j.Slf4j; - /** * @author Lars Helge Overland */ @@ -386,7 +384,7 @@ and ev.status in (${exportableEventStatues}) \ private String getAttributeValueJoinClause(Program program) { String template = """ - left join ${trackedentityattributevalue} ${uid} \ + left join ${trackedentityattributevalue} as ${uid} \ on en.trackedentityid=${uid}.trackedentityid \ and ${uid}.trackedentityattributeid = ${id}\s"""; @@ -636,7 +634,6 @@ private List getColumnForAttribute(TrackedEntityAttribute String selectExpression = getSelectExpressionForAttribute(attribute.getValueType(), columnName); String dataFilterClause = getDataFilterClause(attribute); String sql = getSelectForInsert(attribute, selectExpression); - //String sql = getSelectSubquery(attribute, selectExpression, dataFilterClause); Skip skipIndex = skipIndex(attribute.getValueType(), attribute.hasOptionSet()); if (attribute.getValueType().isOrganisationUnit()) { @@ -740,12 +737,12 @@ private List getColumnsForOrgUnitTrackedEntityAttribute( } /** - * Creates a select statement for the given select expression. + * Returns a select statement. * * @param dataElement the data element to create the select statement for. * @param selectExpression the select expression. * @param dataFilterClause the data filter clause. - * @return A SQL select expression for the data element. + * @return a select expression. */ private String getSelectForInsert( DataElement dataElement, String selectExpression, String dataFilterClause) { @@ -766,9 +763,16 @@ private String getSelectForInsert( "uid", quote(dataElement.getUid()))); } - + + /** + * Returns a select statement. + * + * @param attribute the {@link TrackedEntityAttribute} + * @param selectExpression the select expression. + * @return a select expression. + */ private String getSelectForInsert(TrackedEntityAttribute attribute, String selectExpression) { - return String.format( "%s as %s", selectExpression, quote(attribute.getUid())); + return String.format("%s as %s", selectExpression, quote(attribute.getUid())); } /** From f9ed2a325e8dfa6f08ce88ed6122ac8bb43b7479 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Sun, 1 Dec 2024 13:23:06 +0100 Subject: [PATCH 041/110] fix: Update code --- .../analytics/table/JdbcEventAnalyticsTableManager.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 6c270dd21f7d..edfeec059c38 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -51,7 +51,7 @@ import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; -import lombok.extern.slf4j.Slf4j; + import org.hisp.dhis.analytics.AnalyticsTableHookService; import org.hisp.dhis.analytics.AnalyticsTableType; import org.hisp.dhis.analytics.AnalyticsTableUpdateParams; @@ -92,6 +92,8 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; +import lombok.extern.slf4j.Slf4j; + /** * @author Lars Helge Overland */ @@ -348,7 +350,7 @@ public void populateTable(AnalyticsTableUpdateParams params, AnalyticsTableParti inner join ${organisationunit} ou on ev.organisationunitid=ou.organisationunitid \ left join analytics_rs_orgunitstructure ous on ev.organisationunitid=ous.organisationunitid \ left join analytics_rs_organisationunitgroupsetstructure ougs on ev.organisationunitid=ougs.organisationunitid \ - and (cast(${eventDateMonth} as date)=ougs.startdate or ougs.startdate is null) \ + and (ougs.startdate is null or ps.monthstartdate=ougs.startdate) \ left join ${organisationunit} enrollmentou on en.organisationunitid=enrollmentou.organisationunitid \ inner join analytics_rs_categorystructure acs on ev.attributeoptioncomboid=acs.categoryoptioncomboid \ left join analytics_rs_dateperiodstructure dps on cast(${eventDateExpression} as date)=dps.dateperiod \ @@ -362,7 +364,6 @@ left join analytics_rs_dateperiodstructure dps on cast(${eventDateExpression} as and ev.status in (${exportableEventStatues}) \ and ev.deleted = false""", Map.of( - "eventDateMonth", sqlBuilder.dateTrunc("month", eventDateExpression), "eventDateExpression", eventDateExpression, "partitionClause", partitionClause, "attributeJoinClause", attributeJoinClause, From 9a3afb83bbfc8ebac59008140b2760fef9470099 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Sun, 1 Dec 2024 13:23:31 +0100 Subject: [PATCH 042/110] fix: Update code --- .../dhis/analytics/table/JdbcEventAnalyticsTableManager.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index edfeec059c38..857ac570b760 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -51,7 +51,7 @@ import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; - +import lombok.extern.slf4j.Slf4j; import org.hisp.dhis.analytics.AnalyticsTableHookService; import org.hisp.dhis.analytics.AnalyticsTableType; import org.hisp.dhis.analytics.AnalyticsTableUpdateParams; @@ -92,8 +92,6 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; -import lombok.extern.slf4j.Slf4j; - /** * @author Lars Helge Overland */ From 92be248698adf416f3674fd08cbce7d8a5d209a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Sun, 1 Dec 2024 15:40:58 +0100 Subject: [PATCH 043/110] fix: Update code --- .../table/JdbcEventAnalyticsTableManager.java | 4 ++-- .../resourcetable/table/DatePeriodResourceTable.java | 10 +++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 857ac570b760..568578945f4a 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -346,12 +346,12 @@ public void populateTable(AnalyticsTableUpdateParams params, AnalyticsTableParti left join ${trackedentity} te on en.trackedentityid=te.trackedentityid and te.deleted = false \ left join ${organisationunit} registrationou on te.organisationunitid=registrationou.organisationunitid \ inner join ${organisationunit} ou on ev.organisationunitid=ou.organisationunitid \ + left join analytics_rs_dateperiodstructure dps on cast(${eventDateExpression} as date)=dps.dateperiod \ left join analytics_rs_orgunitstructure ous on ev.organisationunitid=ous.organisationunitid \ left join analytics_rs_organisationunitgroupsetstructure ougs on ev.organisationunitid=ougs.organisationunitid \ - and (ougs.startdate is null or ps.monthstartdate=ougs.startdate) \ + and (ougs.startdate is null or dps.monthstartdate=ougs.startdate) \ left join ${organisationunit} enrollmentou on en.organisationunitid=enrollmentou.organisationunitid \ inner join analytics_rs_categorystructure acs on ev.attributeoptioncomboid=acs.categoryoptioncomboid \ - left join analytics_rs_dateperiodstructure dps on cast(${eventDateExpression} as date)=dps.dateperiod \ ${attributeJoinClause}\ where ev.lastupdated < '${startTime}' ${partitionClause} \ and pr.programid=${programId} \ diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/resourcetable/table/DatePeriodResourceTable.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/resourcetable/table/DatePeriodResourceTable.java index ca86139beedf..444ca2dc4464 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/resourcetable/table/DatePeriodResourceTable.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/resourcetable/table/DatePeriodResourceTable.java @@ -49,6 +49,7 @@ import org.hisp.dhis.period.PeriodType; import org.hisp.dhis.resourcetable.ResourceTable; import org.hisp.dhis.resourcetable.ResourceTableType; +import org.hisp.dhis.util.DateUtils; /** * @author Lars Helge Overland @@ -75,7 +76,8 @@ private List getColumns() { List columns = Lists.newArrayList( new Column("dateperiod", DataType.DATE, Nullable.NOT_NULL), - new Column("year", DataType.INTEGER, Nullable.NOT_NULL)); + new Column("year", DataType.INTEGER, Nullable.NOT_NULL), + new Column("monthstartdate", DataType.DATE, Nullable.NOT_NULL)); for (PeriodType periodType : PeriodType.PERIOD_TYPES) { columns.add(new Column(periodType.getName().toLowerCase(), DataType.VARCHAR_50)); @@ -119,12 +121,14 @@ public Optional> getPopulateTempTableContent() { Calendar calendar = PeriodType.getCalendar(); for (Date day : days) { - List values = new ArrayList<>(); + final int year = PeriodType.getCalendar().fromIso(day).getYear(); + final Date monthStartDate = DateUtils.dateTruncMonth(day); - int year = PeriodType.getCalendar().fromIso(day).getYear(); + List values = new ArrayList<>(); values.add(day); values.add(year); + values.add(monthStartDate); for (PeriodType periodType : periodTypes) { values.add(periodType.createPeriod(day, calendar).getIsoDate()); From 31524cac022257ab15a847391b0a1d8abad18f52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Sun, 1 Dec 2024 19:53:29 +0100 Subject: [PATCH 044/110] fix: Update code --- .../dhis/analytics/table/JdbcEventAnalyticsTableManager.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 622aab8cfee0..c69715b7c7a7 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -736,8 +736,7 @@ private List getColumnsForOrgUnitTrackedEntityAttribute( } /** - * <<<<<<< HEAD Returns a select statement. ======= Retyrns a select statement for the given - * select expression. >>>>>>> master + * Returns a select statement for the given select expression. * * @param dataElement the data element to create the select statement for. * @param selectExpression the select expression. From 4259834a0526db714f2f6718f130d871e877e466 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Mon, 2 Dec 2024 14:55:20 +0100 Subject: [PATCH 045/110] fix: Update code --- .../table/JdbcEventAnalyticsTableManager.java | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index e8adcab5fa1b..8789dd8711b7 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -659,7 +659,7 @@ private List getColumnForAttributeWithLegendSet( } /** - * Retyrns a select statement for the given select expression. + * Returns a select statement for the given select expression. * * @param dataElement the data element to create the select statement for. * @param selectExpression the select expression. @@ -686,17 +686,6 @@ private String getSelectForInsert( quote(dataElement.getUid()))); } - /** - * Returns a select statement. - * - * @param attribute the {@link TrackedEntityAttribute} - * @param selectExpression the select expression. - * @return a select expression. - */ - private String getSelectForInsert(TrackedEntityAttribute attribute, String selectExpression) { - return String.format("%s as %s", selectExpression, quote(attribute.getUid())); - } - /** * Returns a list of columns. * From 25f4feb4323526bb09be7433cbce47630a9c673d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Mon, 2 Dec 2024 16:08:15 +0100 Subject: [PATCH 046/110] fix: Update code --- .../dhis/analytics/table/AbstractEventJdbcTableManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java index 5807a308ae4e..3d45e11185f5 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java @@ -261,7 +261,7 @@ protected List getColumnForAttribute(TrackedEntityAttribut Skip skipIndex = skipIndex(attribute.getValueType(), attribute.hasOptionSet()); if (attribute.getValueType().isOrganisationUnit()) { - columns.addAll(getColumnForOrgUnitTrackedEntityAttribute(attribute, dataFilterClause)); + columns.addAll(getColumnForOrgUnitAttribute(attribute, dataFilterClause)); } columns.add( @@ -283,7 +283,7 @@ protected List getColumnForAttribute(TrackedEntityAttribut * @param dataFilterClause the data filter clause. * @return a list of {@link AnalyticsTableColumn}. */ - private List getColumnForOrgUnitTrackedEntityAttribute( + private List getColumnForOrgUnitAttribute( TrackedEntityAttribute attribute, String dataFilterClause) { List columns = new ArrayList<>(); From df215eccd7acac21c2fd93fdd658d69d0b25d2ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Mon, 2 Dec 2024 16:10:03 +0100 Subject: [PATCH 047/110] fix: Update code --- .../table/JdbcEventAnalyticsTableManager.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 8789dd8711b7..16ff1804e26d 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -673,17 +673,14 @@ private String getSelectForInsert( ? "(select ${selectExpression} ${dataClause})${closingParentheses} as ${uid}" : "${selectExpression}${closingParentheses} as ${uid}"; + String s = getClosingParentheses(selectExpression); return replaceQualify( sqlTemplate, Map.of( - "selectExpression", - selectExpression, - "dataClause", - dataFilterClause, - "closingParentheses", - getClosingParentheses(selectExpression), - "uid", - quote(dataElement.getUid()))); + "selectExpression", selectExpression, + "dataClause", dataFilterClause, + "closingParentheses", s, + "uid", quote(dataElement.getUid()))); } /** From 83b4758bd9efd0f3dbc0e2b5d765cda7b5221095 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Mon, 2 Dec 2024 16:10:40 +0100 Subject: [PATCH 048/110] fix: Update code --- .../dhis/analytics/table/JdbcEventAnalyticsTableManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 16ff1804e26d..fa895ddb66a7 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -668,18 +668,18 @@ private List getColumnForAttributeWithLegendSet( */ private String getSelectForInsert( DataElement dataElement, String selectExpression, String dataFilterClause) { + String parentheses = getClosingParentheses(selectExpression); String sqlTemplate = dataElement.getValueType().isOrganisationUnit() ? "(select ${selectExpression} ${dataClause})${closingParentheses} as ${uid}" : "${selectExpression}${closingParentheses} as ${uid}"; - String s = getClosingParentheses(selectExpression); return replaceQualify( sqlTemplate, Map.of( "selectExpression", selectExpression, "dataClause", dataFilterClause, - "closingParentheses", s, + "closingParentheses", parentheses, "uid", quote(dataElement.getUid()))); } From 7a481d62b185207f926be6d2a66b5fb9e42f3680 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Mon, 2 Dec 2024 16:13:13 +0100 Subject: [PATCH 049/110] fix: Update code --- .../analytics/table/JdbcEventAnalyticsTableManager.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index fa895ddb66a7..04102a4d9d8f 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -758,15 +758,14 @@ private List getDataYears( Program program, Integer firstDataYear, Integer lastDataYear) { + String fromDate = toMediumDate(params.getFromDate()); String fromDateClause = params.getFromDate() != null ? replace( "and (${eventDateExpression}) >= '${fromDate}'", Map.of( - "eventDateExpression", - eventDateExpression, - "fromDate", - toMediumDate(params.getFromDate()))) + "eventDateExpression", eventDateExpression, + "fromDate", fromDate)) : EMPTY; String sql = From bca8df519e9503ed08e8b7a355bc94f7201af75c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Mon, 2 Dec 2024 16:40:43 +0100 Subject: [PATCH 050/110] fix: Update code --- .../table/AbstractEventJdbcTableManager.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java index 3d45e11185f5..1350cc99b9ae 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java @@ -34,7 +34,6 @@ import static org.hisp.dhis.db.model.DataType.GEOMETRY; import static org.hisp.dhis.db.model.DataType.TEXT; import static org.hisp.dhis.system.util.MathUtils.NUMERIC_LENIENT_REGEXP; - import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -172,9 +171,9 @@ private String getSelectExpression(ValueType valueType, String columnExpression, } else if (valueType.isOrganisationUnit()) { String ouClause = isTea - ? "ou.uid from ${organisationunit} ou where ou.uid = (select ${columnName}" - : "ou.uid from ${organisationunit} ou where ou.uid = ${columnName}"; - return replaceQualify(ouClause, Map.of("columnName", columnExpression)); + ? "ou.uid from ${organisationunit} ou where ou.uid = (select ${columnExpression}" + : "ou.uid from ${organisationunit} ou where ou.uid = ${columnExpression}"; + return replaceQualify(ouClause, Map.of("columnExpression", columnExpression)); } else { return columnExpression; } @@ -317,6 +316,13 @@ private List getColumnForOrgUnitAttribute( return columns; } + + private String getSelectForInsert(TrackedEntityAttribute attribute, String selectExpression, String dataFilterClause) { + return + String.format( + """ + %s.%s as %s, """); + } /** * The select subquery statement. From fdc55784bc981ca47b31f8ab9a56918ee5c4fd3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Mon, 2 Dec 2024 16:40:52 +0100 Subject: [PATCH 051/110] fix: Update code --- .../analytics/table/AbstractEventJdbcTableManager.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java index 1350cc99b9ae..c1f8c34172fd 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java @@ -34,6 +34,7 @@ import static org.hisp.dhis.db.model.DataType.GEOMETRY; import static org.hisp.dhis.db.model.DataType.TEXT; import static org.hisp.dhis.system.util.MathUtils.NUMERIC_LENIENT_REGEXP; + import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -316,10 +317,10 @@ private List getColumnForOrgUnitAttribute( return columns; } - - private String getSelectForInsert(TrackedEntityAttribute attribute, String selectExpression, String dataFilterClause) { - return - String.format( + + private String getSelectForInsert( + TrackedEntityAttribute attribute, String selectExpression, String dataFilterClause) { + return String.format( """ %s.%s as %s, """); } From 663386103850260f51ce417c5991002c06276a11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 09:45:35 +0100 Subject: [PATCH 052/110] Revert "feat: skipAnalytics and ConfidentialFlag only for QUERY Analytics [DHIS2-18259] (#19350)" This reverts commit dc0fa5ef4343ad2eba3ddd8c059735edbf7b8d4a. --- .../dhis/common/DimensionalItemObject.java | 4 - .../org/hisp/dhis/feedback/ErrorCode.java | 3 - .../java/org/hisp/dhis/program/Program.java | 34 +++- .../org/hisp/dhis/program/ProgramTest.java | 5 + .../analytics/event/EventQueryParams.java | 5 - ...tEnrollmentAnalyticsDimensionsService.java | 41 ++--- ...efaultEventAnalyticsDimensionsService.java | 15 +- .../data/DefaultEventDataQueryService.java | 69 -------- .../table/AbstractEventJdbcTableManager.java | 2 +- .../JdbcEnrollmentAnalyticsTableManager.java | 2 +- .../table/JdbcEventAnalyticsTableManager.java | 8 +- ...dbcTrackedEntityAnalyticsTableManager.java | 12 +- .../DefaultEventDataQueryServiceTest.java | 151 ------------------ ...rackedEntityAnalyticsTableManagerTest.java | 7 +- .../analytics/AnalyticsDimensionsTest.java | 2 +- 15 files changed, 89 insertions(+), 271 deletions(-) delete mode 100644 dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/DefaultEventDataQueryServiceTest.java diff --git a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalItemObject.java b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalItemObject.java index 4134b7314387..c12c4e954df7 100644 --- a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalItemObject.java +++ b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionalItemObject.java @@ -87,8 +87,4 @@ public interface DimensionalItemObject extends NameableObject { default int getPeriodOffset() { return (getQueryMods() != null) ? getQueryMods().getPeriodOffset() : 0; } - - default boolean isOfType(DimensionItemType type) { - return type == getDimensionItemType(); - } } diff --git a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/feedback/ErrorCode.java b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/feedback/ErrorCode.java index 9dcf3008b561..1c1cf44d0afe 100644 --- a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/feedback/ErrorCode.java +++ b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/feedback/ErrorCode.java @@ -505,9 +505,6 @@ public enum ErrorCode { E7236("Program stage '{0}' is not associated to program '{0}'"), E7237("Sorting must have a valid dimension and a direction"), E7238("Sorting dimension ‘{0}’ is not a column"), - E7239( - "Tracked Entity Attributes marked as 'confidential' can only be used in aggregate analytics: `{0}`"), - E7240("Data Elements marked as 'skipAnalytics' can only be used in aggregate analytics: `{0}`"), /* TE analytics */ E7250("Dimension is not a fully qualified: `{0}`"), diff --git a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/Program.java b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/Program.java index 8d9b8da95009..d6ca3575963d 100644 --- a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/Program.java +++ b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/Program.java @@ -37,9 +37,11 @@ import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; import java.util.ArrayList; +import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; import org.hisp.dhis.category.CategoryCombo; @@ -260,12 +262,26 @@ public Set getDataElements() { return programStages.stream().flatMap(ps -> ps.getDataElements().stream()).collect(toSet()); } + /** + * Returns all data elements which are part of the stages of this program and is not skipped in + * analytics. + */ + public Set getAnalyticsDataElements() { + return programStages.stream() + .map(ProgramStage::getProgramStageDataElements) + .flatMap(Collection::stream) + .filter(Objects::nonNull) + .filter(psde -> !psde.getSkipAnalytics()) + .map(ProgramStageDataElement::getDataElement) + .collect(toSet()); + } + /** * Returns data elements which are part of the stages of this program which have a legend set and * is of numeric value type. */ - public Set getDataElementsWithLegendSet() { - return getDataElements().stream() + public Set getAnalyticsDataElementsWithLegendSet() { + return getAnalyticsDataElements().stream() .filter(de -> de.hasLegendSet() && de.isNumericType()) .collect(toSet()); } @@ -280,13 +296,23 @@ public List getTrackedEntityAttributes() { .collect(Collectors.toList()); } + /** + * Returns non-confidential TrackedEntityAttributes from ProgramTrackedEntityAttributes. Use + * getAttributes() to access the persisted attribute list. + */ + public List getNonConfidentialTrackedEntityAttributes() { + return getTrackedEntityAttributes().stream() + .filter(a -> !a.isConfidentialBool()) + .collect(Collectors.toList()); + } + /** * Returns TrackedEntityAttributes from ProgramTrackedEntityAttributes which have a legend set and * is of numeric value type. */ - public List getTrackedEntityAttributesWithLegendSet() { + public List getNonConfidentialTrackedEntityAttributesWithLegendSet() { return getTrackedEntityAttributes().stream() - .filter(a -> a.hasLegendSet() && a.isNumericType()) + .filter(a -> !a.isConfidentialBool() && a.hasLegendSet() && a.isNumericType()) .collect(Collectors.toList()); } diff --git a/dhis-2/dhis-api/src/test/java/org/hisp/dhis/program/ProgramTest.java b/dhis-2/dhis-api/src/test/java/org/hisp/dhis/program/ProgramTest.java index 7f7cbff8216b..3555686ada7e 100644 --- a/dhis-2/dhis-api/src/test/java/org/hisp/dhis/program/ProgramTest.java +++ b/dhis-2/dhis-api/src/test/java/org/hisp/dhis/program/ProgramTest.java @@ -85,6 +85,7 @@ void testGetAnalyticsDataElements() { assertEquals(2, prA.getDataElements().size()); assertTrue(prA.getDataElements().contains(deA)); assertTrue(prA.getDataElements().contains(deB)); + assertEquals(1, prA.getAnalyticsDataElements().size()); assertTrue(prA.getDataElements().contains(deA)); } @@ -105,6 +106,7 @@ void testCopyOfWithPropertyValuesSet() { // check equal assertEquals(original.getAccess(), copy.getAccess()); assertEquals(original.getAccessLevel(), copy.getAccessLevel()); + assertEquals(original.getAnalyticsDataElements(), copy.getAnalyticsDataElements()); assertEquals(original.getCategoryCombo(), copy.getCategoryCombo()); assertEquals(original.getCompleteEventsExpiryDays(), copy.getCompleteEventsExpiryDays()); assertEquals(original.getDataElements(), copy.getDataElements()); @@ -178,7 +180,10 @@ void testCopyOfWithNulls() { assertEquals("copynull", copy.getName()); assertEquals(original.getAccessLevel(), copy.getAccessLevel()); assertEquals(original.getDescription(), copy.getDescription()); + assertTrue(copy.getAnalyticsDataElements().isEmpty()); assertTrue(copy.getDataElements().isEmpty()); + assertTrue(copy.getNonConfidentialTrackedEntityAttributes().isEmpty()); + assertTrue(copy.getNonConfidentialTrackedEntityAttributesWithLegendSet().isEmpty()); assertTrue(copy.getNotificationTemplates().isEmpty()); assertTrue(copy.getOrganisationUnits().isEmpty()); assertTrue(copy.getProgramAttributes().isEmpty()); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventQueryParams.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventQueryParams.java index 7848156e434d..4d6f0683f691 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventQueryParams.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventQueryParams.java @@ -37,7 +37,6 @@ import static org.hisp.dhis.common.DimensionalObject.PERIOD_DIM_ID; import static org.hisp.dhis.common.DimensionalObjectUtils.asList; import static org.hisp.dhis.common.DimensionalObjectUtils.asTypedList; -import static org.hisp.dhis.common.RequestTypeAware.EndpointAction.AGGREGATE; import static org.hisp.dhis.common.RequestTypeAware.EndpointAction.QUERY; import com.google.common.base.MoreObjects; @@ -999,10 +998,6 @@ public boolean isRowContext() { return rowContext; } - public boolean includeConfidentialOrSkipAnalyticsItems() { - return endpointAction == AGGREGATE; - } - // ------------------------------------------------------------------------- // Builder of immutable instances // ------------------------------------------------------------------------- diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEnrollmentAnalyticsDimensionsService.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEnrollmentAnalyticsDimensionsService.java index c13b40d3d4df..2894d3771eb6 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEnrollmentAnalyticsDimensionsService.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEnrollmentAnalyticsDimensionsService.java @@ -31,26 +31,22 @@ import static org.hisp.dhis.analytics.common.DimensionsServiceCommon.OperationType.QUERY; import static org.hisp.dhis.analytics.common.DimensionsServiceCommon.collectDimensions; import static org.hisp.dhis.analytics.common.DimensionsServiceCommon.filterByValueType; -import static org.hisp.dhis.analytics.event.data.DefaultEventAnalyticsDimensionsService.getTeasIfRegistration; import static org.hisp.dhis.common.PrefixedDimensions.ofItemsWithProgram; import static org.hisp.dhis.common.PrefixedDimensions.ofProgramStageDataElements; import java.util.Collection; import java.util.List; import java.util.Optional; -import java.util.Set; -import java.util.function.Predicate; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import org.hisp.dhis.analytics.common.DimensionsServiceCommon; -import org.hisp.dhis.analytics.common.DimensionsServiceCommon.OperationType; import org.hisp.dhis.analytics.event.EnrollmentAnalyticsDimensionsService; import org.hisp.dhis.common.PrefixedDimension; import org.hisp.dhis.program.Program; import org.hisp.dhis.program.ProgramService; import org.hisp.dhis.program.ProgramStage; -import org.hisp.dhis.program.ProgramStageDataElement; import org.hisp.dhis.security.acl.AclService; +import org.hisp.dhis.trackedentity.TrackedEntityAttribute; import org.hisp.dhis.user.CurrentUserUtil; import org.hisp.dhis.user.UserDetails; import org.springframework.stereotype.Service; @@ -81,7 +77,9 @@ public List getQueryDimensionsByProgramId(String programId) { .collect(Collectors.toSet())), getProgramStageDataElements(QUERY, program), filterByValueType( - QUERY, ofItemsWithProgram(program, getTeasIfRegistration(program)))))) + QUERY, + ofItemsWithProgram( + program, getTeasIfRegistrationAndNotConfidential(program)))))) .orElse(List.of()); } @@ -89,22 +87,14 @@ private Collection getProgramStageDataElements( DimensionsServiceCommon.OperationType operationType, Program program) { return program.getProgramStages().stream() .map(ProgramStage::getProgramStageDataElements) - .map(psdes -> excludeIfSkipAnalytics(operationType, psdes)) - .map(psdes -> filterByValueType(operationType, ofProgramStageDataElements(psdes))) + .map( + programStageDataElements -> + filterByValueType( + operationType, ofProgramStageDataElements(programStageDataElements))) .flatMap(Collection::stream) .collect(Collectors.toList()); } - private Set excludeIfSkipAnalytics( - OperationType operationType, Set programStageDataElements) { - if (operationType == QUERY) { - return programStageDataElements.stream() - .filter(Predicate.not(ProgramStageDataElement::getSkipAnalytics)) - .collect(Collectors.toSet()); - } - return programStageDataElements; - } - @Override public List getAggregateDimensionsByProgramStageId(String programId) { return Optional.of(programId) @@ -119,4 +109,19 @@ public List getAggregateDimensionsByProgramStageId(String pro ofItemsWithProgram(program, program.getTrackedEntityAttributes()))))) .orElse(List.of()); } + + private Collection getTeasIfRegistrationAndNotConfidential( + Program program) { + return Optional.of(program) + .filter(Program::isRegistration) + .map(Program::getTrackedEntityAttributes) + .orElse(List.of()) + .stream() + .filter(this::isNotConfidential) + .collect(Collectors.toList()); + } + + private boolean isNotConfidential(TrackedEntityAttribute trackedEntityAttribute) { + return !trackedEntityAttribute.isConfidentialBool(); + } } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsDimensionsService.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsDimensionsService.java index a67b341b61df..76086a2c4b27 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsDimensionsService.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsDimensionsService.java @@ -135,7 +135,9 @@ private List dimensions(ProgramStage programStage) { .filter(pi -> aclService.canRead(currentUserDetails, pi)) .collect(Collectors.toSet())), filterByValueType(QUERY, ofDataElements(programStage)), - filterByValueType(QUERY, ofItemsWithProgram(p, getTeasIfRegistration(p))), + filterByValueType( + QUERY, + ofItemsWithProgram(p, getTeasIfRegistrationAndNotConfidential(p))), ofItemsWithProgram(p, getCategories(p)), ofItemsWithProgram(p, getAttributeCategoryOptionGroupSetsIfNeeded(p))))) .orElse(List.of()); @@ -185,10 +187,17 @@ private List getCategories(Program program) { .orElse(List.of()); } - static List getTeasIfRegistration(Program program) { + private List getTeasIfRegistrationAndNotConfidential(Program program) { return Optional.of(program) .filter(Program::isRegistration) .map(Program::getTrackedEntityAttributes) - .orElse(List.of()); + .orElse(List.of()) + .stream() + .filter(this::isNotConfidential) + .collect(Collectors.toList()); + } + + private boolean isNotConfidential(TrackedEntityAttribute trackedEntityAttribute) { + return !trackedEntityAttribute.isConfidentialBool(); } } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventDataQueryService.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventDataQueryService.java index 3d20c79d5a61..b08af886985a 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventDataQueryService.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventDataQueryService.java @@ -36,8 +36,6 @@ import static org.hisp.dhis.analytics.event.data.DefaultEventDataQueryService.SortableItems.translateItemIfNecessary; import static org.hisp.dhis.analytics.util.AnalyticsUtils.illegalQueryExSupplier; import static org.hisp.dhis.analytics.util.AnalyticsUtils.throwIllegalQueryEx; -import static org.hisp.dhis.common.DimensionItemType.DATA_ELEMENT; -import static org.hisp.dhis.common.DimensionItemType.PROGRAM_ATTRIBUTE; import static org.hisp.dhis.common.DimensionalObject.DIMENSION_NAME_SEP; import static org.hisp.dhis.common.DimensionalObject.PERIOD_DIM_ID; import static org.hisp.dhis.common.DimensionalObjectUtils.getDimensionFromParam; @@ -54,7 +52,6 @@ import java.util.Set; import java.util.UUID; import java.util.stream.Collectors; -import java.util.stream.Stream; import lombok.Getter; import lombok.RequiredArgsConstructor; import org.apache.commons.lang3.StringUtils; @@ -70,7 +67,6 @@ import org.hisp.dhis.analytics.table.EnrollmentAnalyticsColumnName; import org.hisp.dhis.analytics.table.EventAnalyticsColumnName; import org.hisp.dhis.common.BaseDimensionalItemObject; -import org.hisp.dhis.common.BaseIdentifiableObject; import org.hisp.dhis.common.DimensionalItemObject; import org.hisp.dhis.common.DimensionalObject; import org.hisp.dhis.common.EventAnalyticalObject; @@ -94,7 +90,6 @@ import org.hisp.dhis.program.Program; import org.hisp.dhis.program.ProgramService; import org.hisp.dhis.program.ProgramStage; -import org.hisp.dhis.program.ProgramStageDataElement; import org.hisp.dhis.program.ProgramStageService; import org.hisp.dhis.setting.UserSettings; import org.hisp.dhis.trackedentity.TrackedEntityAttribute; @@ -228,73 +223,9 @@ public EventQueryParams getFromRequest(EventDataQueryRequest request, boolean an eventQueryParams = builder.build(); } - validateQueryParamsForConfidentialAndSkipAnalytics(eventQueryParams); - return eventQueryParams; } - static void validateQueryParamsForConfidentialAndSkipAnalytics( - EventQueryParams eventQueryParams) { - if (eventQueryParams.includeConfidentialOrSkipAnalyticsItems()) { - return; - } - Set confidentialAttributes = - Stream.concat( - eventQueryParams.getItems().stream(), eventQueryParams.getItemFilters().stream()) - .map(QueryItem::getItem) - .filter(Objects::nonNull) - .filter(dimObj -> dimObj.isOfType(PROGRAM_ATTRIBUTE)) - .map(TrackedEntityAttribute.class::cast) - .filter(TrackedEntityAttribute::isConfidentialBool) - .collect(Collectors.toSet()); - - if (!confidentialAttributes.isEmpty()) { - throw new IllegalQueryException( - new ErrorMessage( - ErrorCode.E7239, - confidentialAttributes.stream() - .map(TrackedEntityAttribute::getUid) - .collect(Collectors.joining(", ")))); - } - - Set skipAnalyticsDataElements = - Stream.concat( - eventQueryParams.getItems().stream(), eventQueryParams.getItemFilters().stream()) - .filter(item -> item.getItem().isOfType(DATA_ELEMENT)) - .filter(DefaultEventDataQueryService::isSkipAnalytics) - .map(item -> (DataElement) item.getItem()) - .collect(Collectors.toSet()); - - if (!skipAnalyticsDataElements.isEmpty()) { - throw new IllegalQueryException( - new ErrorMessage( - ErrorCode.E7240, - skipAnalyticsDataElements.stream() - .map(DataElement::getUid) - .collect(Collectors.joining(", ")))); - } - } - - /** - * Checks if the data element is marked as skip analytics by looking at the program stage data - * elements associated with the program stage. If any of the program stage data elements has the - * skip analytics flag set to true, the data element in it is considered to be skipAnalytics. - * - * @param item the query item - * @return true if the data element is marked as skip analytics, false otherwise - */ - static boolean isSkipAnalytics(QueryItem item) { - return Optional.of(item) - .map(QueryItem::getProgramStage) - .map(ProgramStage::getProgramStageDataElements) - .orElse(Set.of()) - .stream() - .filter(ProgramStageDataElement::getSkipAnalytics) - .map(ProgramStageDataElement::getDataElement) - .map(BaseIdentifiableObject::getUid) - .anyMatch(uid -> uid.equals(item.getItem().getUid())); - } - private boolean hasPeriodDimension(EventQueryParams eventQueryParams) { return Objects.nonNull(getPeriodDimension(eventQueryParams)); } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java index 82270c5d2587..60d6c433ad3a 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java @@ -214,7 +214,7 @@ protected void populateTableInternal(AnalyticsTablePartition partition, String f * Returns a list of columns based on the given attribute. * * @param attribute the {@link TrackedEntityAttribute}. - * @return a list of {@link AnalyticsTableColumn}. + * @return a list of {@link AnaylyticsTableColumn}. */ protected List getColumnForAttribute(TrackedEntityAttribute attribute) { List columns = new ArrayList<>(); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java index 59bdf500f0a5..d18fbdffd80b 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java @@ -187,7 +187,7 @@ private List getColumns(Program program) { * @return a list of {@link AnalyticsTableColumn}. */ private List getTrackedEntityAttributeColumns(Program program) { - return program.getTrackedEntityAttributes().stream() + return program.getNonConfidentialTrackedEntityAttributes().stream() .map(this::getColumnForAttribute) .flatMap(Collection::stream) .toList(); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 2da49a036477..74d53d5adb60 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -497,12 +497,12 @@ private List getAttributeCategoryColumns(Program program) private List getDataElementColumns(Program program) { List columns = new ArrayList<>(); columns.addAll( - program.getDataElements().stream() + program.getAnalyticsDataElements().stream() .map(de -> getColumnForDataElement(de, false)) .flatMap(Collection::stream) .toList()); columns.addAll( - program.getDataElementsWithLegendSet().stream() + program.getAnalyticsDataElementsWithLegendSet().stream() .map(de -> getColumnForDataElement(de, true)) .flatMap(Collection::stream) .toList()); @@ -603,12 +603,12 @@ private List getColumnForOrgUnitDataElement( private List getAttributeColumns(Program program) { List columns = new ArrayList<>(); columns.addAll( - program.getTrackedEntityAttributes().stream() + program.getNonConfidentialTrackedEntityAttributes().stream() .map(this::getColumnForAttribute) .flatMap(Collection::stream) .toList()); columns.addAll( - program.getTrackedEntityAttributesWithLegendSet().stream() + program.getNonConfidentialTrackedEntityAttributesWithLegendSet().stream() .map(this::getColumnForAttributeWithLegendSet) .flatMap(Collection::stream) .toList()); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java index e2d0bdb9e5ac..4a483d156d37 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java @@ -88,7 +88,8 @@ public class JdbcTrackedEntityAnalyticsTableManager extends AbstractJdbcTableManager { private static final String PROGRAMS_BY_TET_KEY = "programsByTetUid"; - private static final String ALL_TET_ATTRIBUTES = "allTetAttributes"; + private static final String ALL_NON_CONFIDENTIAL_TET_ATTRIBUTES = + "allNonConfidentialTetAttributes"; private final TrackedEntityTypeService trackedEntityTypeService; @@ -199,9 +200,12 @@ private List getColumns( .build())); List trackedEntityAttributes = - getAllTrackedEntityAttributes(trackedEntityType, programsByTetUid).toList(); + getAllTrackedEntityAttributes(trackedEntityType, programsByTetUid) + .filter(tea -> !tea.isConfidentialBool()) + .toList(); - params.addExtraParam(trackedEntityType.getUid(), ALL_TET_ATTRIBUTES, trackedEntityAttributes); + params.addExtraParam( + trackedEntityType.getUid(), ALL_NON_CONFIDENTIAL_TET_ATTRIBUTES, trackedEntityAttributes); columns.addAll( trackedEntityAttributes.stream() @@ -351,7 +355,7 @@ public void populateTable(AnalyticsTableUpdateParams params, AnalyticsTableParti Map.of())); ((List) - params.getExtraParam(trackedEntityType.getUid(), ALL_TET_ATTRIBUTES)) + params.getExtraParam(trackedEntityType.getUid(), ALL_NON_CONFIDENTIAL_TET_ATTRIBUTES)) .forEach( tea -> sql.append( diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/DefaultEventDataQueryServiceTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/DefaultEventDataQueryServiceTest.java deleted file mode 100644 index 0a54a25dc6aa..000000000000 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/DefaultEventDataQueryServiceTest.java +++ /dev/null @@ -1,151 +0,0 @@ -/* - * Copyright (c) 2004-2022, 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.event.data; - -import static org.hisp.dhis.analytics.event.data.DefaultEventDataQueryService.validateQueryParamsForConfidentialAndSkipAnalytics; -import static org.hisp.dhis.common.DimensionItemType.DATA_ELEMENT; -import static org.hisp.dhis.common.DimensionItemType.PROGRAM_ATTRIBUTE; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.util.List; -import java.util.Set; -import java.util.function.Consumer; -import org.hisp.dhis.analytics.event.EventQueryParams; -import org.hisp.dhis.common.DimensionItemType; -import org.hisp.dhis.common.DimensionalItemObject; -import org.hisp.dhis.common.IllegalQueryException; -import org.hisp.dhis.common.QueryItem; -import org.hisp.dhis.dataelement.DataElement; -import org.hisp.dhis.program.ProgramStage; -import org.hisp.dhis.program.ProgramStageDataElement; -import org.hisp.dhis.trackedentity.TrackedEntityAttribute; -import org.junit.jupiter.api.Test; - -class DefaultEventDataQueryServiceTest { - - private EventQueryParams mockEventQueryParams( - boolean includeConfidentialOrSkipAnalyticsItems, List queryItems) { - EventQueryParams mock = mock(EventQueryParams.class); - when(mock.includeConfidentialOrSkipAnalyticsItems()) - .thenReturn(includeConfidentialOrSkipAnalyticsItems); - when(mock.getItems()).thenReturn(queryItems); - when(mock.getItemFilters()).thenReturn(List.of()); - return mock; - } - - private QueryItem mockDimensionalItemObject( - Class clazz, DimensionItemType type, List> behavious) { - T mock = mock(clazz); - when(mock.isOfType(type)).thenReturn(true); - QueryItem queryItem = mock(QueryItem.class); - when(queryItem.getItem()).thenReturn(mock); - behavious.forEach(c -> c.accept(mock)); - return queryItem; - } - - @Test - void testAggregateDontThrowExceptionForConfidential() { - QueryItem queryItem = - mockDimensionalItemObject( - TrackedEntityAttribute.class, - PROGRAM_ATTRIBUTE, - List.of(t -> when(t.isConfidentialBool()).thenReturn(true))); - EventQueryParams eventQueryParams = mockEventQueryParams(true, List.of(queryItem)); - assertDoesNotThrow(() -> validateQueryParamsForConfidentialAndSkipAnalytics(eventQueryParams)); - } - - @Test - void testAggregateDontThrowExceptionForSkipAnalytics() { - final String dataElementUid = "dataElementUid"; - - ProgramStage programStage = mock(ProgramStage.class); - ProgramStageDataElement programStageDataElement = mock(ProgramStageDataElement.class); - - when(programStage.getProgramStageDataElements()).thenReturn(Set.of(programStageDataElement)); - when(programStageDataElement.getSkipAnalytics()).thenReturn(true); - - DataElement dataElement = mock(DataElement.class); - when(dataElement.getUid()).thenReturn(dataElementUid); - when(programStageDataElement.getDataElement()).thenReturn(dataElement); - - QueryItem queryItem = - mockDimensionalItemObject( - DataElement.class, - DATA_ELEMENT, - List.of(t -> when(t.getUid()).thenReturn(dataElementUid))); - when(queryItem.getProgramStage()).thenReturn(programStage); - - EventQueryParams eventQueryParams = mockEventQueryParams(true, List.of(queryItem)); - - assertDoesNotThrow(() -> validateQueryParamsForConfidentialAndSkipAnalytics(eventQueryParams)); - } - - @Test - void testQueryThrowsForConfidential() { - QueryItem queryItem = - mockDimensionalItemObject( - TrackedEntityAttribute.class, - PROGRAM_ATTRIBUTE, - List.of(t -> when(t.isConfidentialBool()).thenReturn(true))); - EventQueryParams eventQueryParams = mockEventQueryParams(false, List.of(queryItem)); - assertThrows( - IllegalQueryException.class, - () -> validateQueryParamsForConfidentialAndSkipAnalytics(eventQueryParams)); - } - - @Test - void testQueryThrowsForSkipAnalytics() { - final String dataElementUid = "dataElementUid"; - - ProgramStage programStage = mock(ProgramStage.class); - ProgramStageDataElement programStageDataElement = mock(ProgramStageDataElement.class); - - when(programStage.getProgramStageDataElements()).thenReturn(Set.of(programStageDataElement)); - when(programStageDataElement.getSkipAnalytics()).thenReturn(true); - - DataElement dataElement = mock(DataElement.class); - when(dataElement.getUid()).thenReturn(dataElementUid); - when(programStageDataElement.getDataElement()).thenReturn(dataElement); - - QueryItem queryItem = - mockDimensionalItemObject( - DataElement.class, - DATA_ELEMENT, - List.of(t -> when(t.getUid()).thenReturn(dataElementUid))); - when(queryItem.getProgramStage()).thenReturn(programStage); - - EventQueryParams eventQueryParams = mockEventQueryParams(false, List.of(queryItem)); - - assertThrows( - IllegalQueryException.class, - () -> validateQueryParamsForConfidentialAndSkipAnalytics(eventQueryParams)); - } -} diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManagerTest.java index b80504d26b7e..0d537f2e63da 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManagerTest.java @@ -28,6 +28,7 @@ package org.hisp.dhis.analytics.table; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; @@ -143,13 +144,13 @@ void verifyNonConfidentialTeasAreSkipped() { AnalyticsTable analyticsTable = analyticsTables.get(0); assertContainsNonConfidentialTeaColumns(analyticsTable); - assertContainsConfidentialTeaColumns(analyticsTable); + assertDoesntContainConfidentialTeaColumns(analyticsTable); } - private void assertContainsConfidentialTeaColumns(AnalyticsTable analyticsTable) { + private void assertDoesntContainConfidentialTeaColumns(AnalyticsTable analyticsTable) { List columns = analyticsTable.getColumns(); - assertTrue(columns.stream().map(Column::getName).anyMatch("confidentialTeaUid"::equals)); + assertFalse(columns.stream().map(Column::getName).anyMatch("confidentialTeaUid"::equals)); } private void assertContainsNonConfidentialTeaColumns(AnalyticsTable analyticsTable) { diff --git a/dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/analytics/AnalyticsDimensionsTest.java b/dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/analytics/AnalyticsDimensionsTest.java index 821adf4c0230..08811f890abf 100644 --- a/dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/analytics/AnalyticsDimensionsTest.java +++ b/dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/analytics/AnalyticsDimensionsTest.java @@ -183,7 +183,7 @@ public void shouldOnlyReturnConfidentialAttributeInAggregateDimensions() { .query() .getDimensionsByDimensionType(trackerProgram.getUid(), "PROGRAM_ATTRIBUTE") .validate() - .body("dimensions.uid", CoreMatchers.hasItem(confidentialAttribute)); + .body("dimensions.uid", not(CoreMatchers.hasItem(confidentialAttribute))); analyticsEnrollmentsActions .aggregate() From 693ac89091fa07b152bfbbbfa1d4d82a13cfcfb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 09:47:25 +0100 Subject: [PATCH 053/110] fix: Update code --- .../dhis/analytics/table/JdbcEventAnalyticsTableManager.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 74d53d5adb60..9d064681cf64 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -383,8 +383,7 @@ private String getAttributeValueJoinClause(Program program) { on en.trackedentityid=${uid}.trackedentityid \ and ${uid}.trackedentityattributeid = ${id}\s"""; - // TO DO non confidential? - return program.getTrackedEntityAttributes().stream() + return program.getNonConfidentialTrackedEntityAttributes().stream() .map(attribute -> replaceQualify(template, toVariableMap(attribute))) .collect(Collectors.joining()); } From 7b310eba19f76732af4b7e5532a2bf8ad1a66118 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 10:55:06 +0100 Subject: [PATCH 054/110] fix: Update code --- .../dhis/analytics/table/JdbcEventAnalyticsTableManager.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 9d064681cf64..f7cf760b316b 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -657,8 +657,7 @@ private List getColumnForAttributeWithLegendSet( } /** - * <<<<<<< HEAD Returns a select statement for the given select expression. ======= Returns a - * select statement for the given data element with value type org unit. >>>>>>> master + * Returns a select statement for the given select expression. * * @param dataElement the data element to create the select statement for. * @param selectExpression the select expression. From be271e6aa2995c8e29df261759a7b624babdbf5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 10:55:40 +0100 Subject: [PATCH 055/110] fix: Update code --- .../analytics/table/AbstractEventJdbcTableManager.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java index 60d6c433ad3a..9c9e893efd0a 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java @@ -283,13 +283,6 @@ private List getColumnForOrgUnitAttribute( return columns; } - private String getSelectForInsert( - TrackedEntityAttribute attribute, String selectExpression, String dataFilterClause) { - return String.format( - """ - %s.%s as %s, """); - } - /** * The select subquery statement. * From e178384f2fe7117ca49e83872000eacbc454bc2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 10:58:51 +0100 Subject: [PATCH 056/110] fix: Update code --- .../dhis/analytics/table/AbstractEventJdbcTableManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java index 9c9e893efd0a..f309a62dcc0b 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java @@ -118,7 +118,6 @@ protected Skip skipIndex(ValueType valueType, boolean hasOptionSet) { /** * Returns a select expression, potentially with a cast statement, based on the given value type. - * Handles data element and tracked entity attribute select expressions. * * @param valueType the {@link ValueType} to represent as database column type. * @param columnExpression the expression or name of the column to be selected. @@ -138,7 +137,8 @@ protected String getSelectExpression(ValueType valueType, String columnExpressio } else if (valueType.isGeo() && isSpatialSupport()) { return String.format( """ - ST_GeomFromGeoJSON('{"type":"Point", "coordinates":' || (%s) || ', "crs":{"type":"name", "properties":{"name":"EPSG:4326"}}}')""", + ST_GeomFromGeoJSON('{"type":"Point", "coordinates":' || (%s) || \ + ', "crs":{"type":"name", "properties":{"name":"EPSG:4326"}}}')""", columnExpression); } else { return columnExpression; From d3c19579b08e4a2dc21b75ea4864b3bfe53e15b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 11:05:13 +0100 Subject: [PATCH 057/110] fix: Update code --- .../dhis/analytics/table/AbstractEventJdbcTableManager.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java index f309a62dcc0b..8cf2e01dee72 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java @@ -219,10 +219,10 @@ protected void populateTableInternal(AnalyticsTablePartition partition, String f protected List getColumnForAttribute(TrackedEntityAttribute attribute) { List columns = new ArrayList<>(); + String valueColumn = String.format("%s.%s", quote(attribute.getUid()), "value"); DataType dataType = getColumnType(attribute.getValueType(), isSpatialSupport()); - String selectExpression = getSelectExpression(attribute.getValueType(), "value"); + String selectExpression = getSelectExpression(attribute.getValueType(), valueColumn); String dataFilterClause = getDataFilterClause(attribute); - String sql = getSelectSubquery(attribute, selectExpression, dataFilterClause); Skip skipIndex = skipIndex(attribute.getValueType(), attribute.hasOptionSet()); if (attribute.getValueType().isOrganisationUnit()) { @@ -234,7 +234,7 @@ protected List getColumnForAttribute(TrackedEntityAttribut .name(attribute.getUid()) .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(dataType) - .selectExpression(sql) + .selectExpression(selectExpression) .skipIndex(skipIndex) .build()); From 55d8a84f9be5c206dac2d6a3ae128926f537b086 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 11:09:54 +0100 Subject: [PATCH 058/110] fix: Update code --- .../table/AbstractEventJdbcTableManager.java | 13 +++++++++++++ .../table/JdbcEventAnalyticsTableManager.java | 13 ------------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java index 8cf2e01dee72..22973bfe7c73 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java @@ -46,6 +46,7 @@ import org.hisp.dhis.analytics.table.model.Skip; import org.hisp.dhis.analytics.table.setting.AnalyticsTableSettings; import org.hisp.dhis.category.CategoryService; +import org.hisp.dhis.common.IdentifiableObject; import org.hisp.dhis.common.IdentifiableObjectManager; import org.hisp.dhis.common.ValueType; import org.hisp.dhis.commons.util.TextUtils; @@ -306,4 +307,16 @@ private String getSelectSubquery( "closingParentheses", getClosingParentheses(selectExpression), "attributeUid", quote(attribute.getUid()))); } + + /** + * Returns a map of identifiable properties and values. + * + * @param object the {@link IdentifiableObject}. + * @return a {@link Map}. + */ + protected Map toVariableMap(IdentifiableObject object) { + return Map.of( + "id", String.valueOf(object.getId()), + "uid", quote(object.getUid())); + } } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index f7cf760b316b..4983383a6106 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -67,7 +67,6 @@ import org.hisp.dhis.calendar.Calendar; import org.hisp.dhis.category.Category; import org.hisp.dhis.category.CategoryService; -import org.hisp.dhis.common.IdentifiableObject; import org.hisp.dhis.common.IdentifiableObjectManager; import org.hisp.dhis.common.ValueType; import org.hisp.dhis.commons.collection.ListUtils; @@ -388,18 +387,6 @@ private String getAttributeValueJoinClause(Program program) { .collect(Collectors.joining()); } - /** - * Returns a variable map. - * - * @param object the object. - * @return a {@link Map}. - */ - private Map toVariableMap(IdentifiableObject object) { - return Map.of( - "id", String.valueOf(object.getId()), - "uid", quote(object.getUid())); - } - /** * Returns a partition SQL clause. * From 86ef201ef7a9c9dc0b285b4ef134327a1cba806d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 11:19:49 +0100 Subject: [PATCH 059/110] fix: Update code --- .../table/AbstractEventJdbcTableManager.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java index 22973bfe7c73..658f239a30ff 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java @@ -38,6 +38,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import org.hisp.dhis.analytics.AnalyticsTableHookService; import org.hisp.dhis.analytics.partition.PartitionManager; import org.hisp.dhis.analytics.table.model.AnalyticsDimensionType; @@ -56,6 +57,7 @@ import org.hisp.dhis.db.sql.SqlBuilder; import org.hisp.dhis.organisationunit.OrganisationUnitService; import org.hisp.dhis.period.PeriodDataProvider; +import org.hisp.dhis.program.Program; import org.hisp.dhis.resourcetable.ResourceTableService; import org.hisp.dhis.setting.SystemSettingsProvider; import org.hisp.dhis.trackedentity.TrackedEntityAttribute; @@ -308,6 +310,24 @@ private String getSelectSubquery( "attributeUid", quote(attribute.getUid()))); } + /** + * Returns a join clause for attribute value for every attribute of the given program. + * + * @param program the {@link Program}. + * @return a join clause. + */ + protected String getAttributeValueJoinClause(Program program) { + String template = + """ + left join ${trackedentityattributevalue} as ${uid} \ + on en.trackedentityid=${uid}.trackedentityid \ + and ${uid}.trackedentityattributeid = ${id}\s"""; + + return program.getNonConfidentialTrackedEntityAttributes().stream() + .map(attribute -> replaceQualify(template, toVariableMap(attribute))) + .collect(Collectors.joining()); + } + /** * Returns a map of identifiable properties and values. * From f4e76843c7bb8b751b9b5d2963574e7bf4251b1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 11:20:29 +0100 Subject: [PATCH 060/110] fix: Update code --- .../table/JdbcEventAnalyticsTableManager.java | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 4983383a6106..f24e870b24d7 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -50,7 +50,6 @@ import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.stream.Collectors; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.Validate; import org.hisp.dhis.analytics.AnalyticsTableHookService; @@ -369,24 +368,6 @@ and ev.status in (${exportableEventStatues}) \ populateTableInternal(partition, fromClause); } - /** - * Returns a join clause for attribute value for every attribute of the given program. - * - * @param program the {@link Program}. - * @return a join clause. - */ - private String getAttributeValueJoinClause(Program program) { - String template = - """ - left join ${trackedentityattributevalue} as ${uid} \ - on en.trackedentityid=${uid}.trackedentityid \ - and ${uid}.trackedentityattributeid = ${id}\s"""; - - return program.getNonConfidentialTrackedEntityAttributes().stream() - .map(attribute -> replaceQualify(template, toVariableMap(attribute))) - .collect(Collectors.joining()); - } - /** * Returns a partition SQL clause. * From a0320721704126ef96026a886521f2095b662a07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 11:21:58 +0100 Subject: [PATCH 061/110] fix: Update code --- .../analytics/table/JdbcEnrollmentAnalyticsTableManager.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java index d18fbdffd80b..172745e793d3 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java @@ -136,6 +136,7 @@ protected List getPartitionChecks(Integer year, Date endDate) { @Override public void populateTable(AnalyticsTableUpdateParams params, AnalyticsTablePartition partition) { Program program = partition.getMasterTable().getProgram(); + String attributeJoinClause = getAttributeValueJoinClause(program); String fromClause = replaceQualify( @@ -148,6 +149,7 @@ public void populateTable(AnalyticsTableUpdateParams params, AnalyticsTableParti left join analytics_rs_dateperiodstructure dps on cast(en.enrollmentdate as date)=dps.dateperiod \ left join analytics_rs_orgunitstructure ous on en.organisationunitid=ous.organisationunitid \ left join analytics_rs_organisationunitgroupsetstructure ougs on en.organisationunitid=ougs.organisationunitid \ + ${attributeJoinClause}\ where pr.programid=${programId} \ and en.organisationunitid is not null \ and (ougs.startdate is null or dps.monthstartdate=ougs.startdate) \ @@ -155,6 +157,7 @@ left join analytics_rs_dateperiodstructure dps on cast(en.enrollmentdate as date and en.occurreddate is not null \ and en.deleted = false\s""", Map.of( + "attributeJoinClause", attributeJoinClause, "programId", String.valueOf(program.getId()), "startTime", toLongDate(params.getStartTime()))); From 87a0fd313556d548556ad939c99e88343309c892 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 11:25:16 +0100 Subject: [PATCH 062/110] fix: Update code --- .../table/JdbcEventAnalyticsTableManagerTest.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java index b7da2447b4ac..fd650d2adabe 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java @@ -650,12 +650,7 @@ void verifyTeiTypeOrgUnitFetchesOuNameWhenPopulatingEventAnalyticsTable() { subject.populateTable(params, partition); verify(jdbcTemplate).execute(sql.capture()); - String ouUidQuery = - String.format( - """ - (select value from "trackedentityattributevalue" where trackedentityid=en.trackedentityid and \ - trackedentityattributeid=9999) as %s""", - quote(tea.getUid())); + String ouUidQuery = String.format("%s.value", quote(tea.getUid())); String ouNameQuery = String.format( From c93f4ff8d73766fffef4c191adbb1380529b9e0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 11:28:15 +0100 Subject: [PATCH 063/110] fix: Update code --- .../table/JdbcEventAnalyticsTableManagerTest.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java index fd650d2adabe..a1e886cd9001 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java @@ -537,6 +537,7 @@ void verifyGetTableWithTrackedEntityAttribute() { .withTableType(AnalyticsTableType.EVENT) .withColumnSize(59 + OU_NAME_HIERARCHY_COUNT) .addColumns(periodColumns) + // Org unit UID column .addColumn( d1.getUid(), TEXT, @@ -939,12 +940,7 @@ void verifyTeaTypeOrgUnitFetchesOuNameWhenPopulatingEventAnalyticsTable() { verify(jdbcTemplate).execute(sql.capture()); - String ouUidQuery = - String.format( - """ - select value from "trackedentityattributevalue" where trackedentityid=en.trackedentityid \ - and trackedentityattributeid=9999) as %s""", - quote(tea.getUid())); + String ouUidQuery = String.format("%s.value", quote(tea.getUid())); String ouNameQuery = String.format( From 8d9b40f044190ccc855dd0f7eb1ec444f067ac2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 11:35:09 +0100 Subject: [PATCH 064/110] fix: Update code --- .../table/JdbcEventAnalyticsTableManagerTest.java | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java index a1e886cd9001..b2da976c129e 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java @@ -501,10 +501,7 @@ void verifyGetTableWithTrackedEntityAttribute() { String aliasD1 = """ eventdatavalues #>> '{deabcdefghZ, value}' as "deabcdefghZ\""""; - String aliasTeaUid = - """ - (select value from "trackedentityattributevalue" where trackedentityid=en.trackedentityid \ - and trackedentityattributeid=%d) as "%s\""""; + String aliasTeaUid = "%s.value"; String aliasTea1 = """ @@ -537,13 +534,8 @@ void verifyGetTableWithTrackedEntityAttribute() { .withTableType(AnalyticsTableType.EVENT) .withColumnSize(59 + OU_NAME_HIERARCHY_COUNT) .addColumns(periodColumns) - // Org unit UID column - .addColumn( - d1.getUid(), - TEXT, - toSelectExpression(aliasD1, d1.getUid()), - Skip.SKIP) // ValueType.TEXT - .addColumn(tea1.getUid(), TEXT, String.format(aliasTeaUid, tea1.getId(), tea1.getUid())) + .addColumn(d1.getUid(), TEXT, toSelectExpression(aliasD1, d1.getUid()), Skip.SKIP) + .addColumn(tea1.getUid(), TEXT, String.format(aliasTeaUid, quote(tea1.getUid()))) // Org unit geometry column .addColumn( tea1.getUid() + "_geom", From 00536ff9d6bf09af4df48164a162aef69f46bf29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 11:36:03 +0100 Subject: [PATCH 065/110] fix: Update code --- .../table/JdbcEnrollmentAnalyticsTableManagerTest.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManagerTest.java index d1b9f4443599..f2f2d642cf42 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManagerTest.java @@ -137,13 +137,7 @@ void verifyTeiTypeOrgUnitFetchesOuUidWhenPopulatingEventAnalyticsTable() { subject.populateTable(params, partition); verify(jdbcTemplate).execute(sql.capture()); - String ouQuery = - format( - """ - (select value from "trackedentityattributevalue" \ - where trackedentityid=en.trackedentityid and \ - trackedentityattributeid=9999) as %s""", - quote(tea.getUid())); + String ouQuery = format("%s.value", quote(tea.getUid())); assertThat(sql.getValue(), containsString(ouQuery)); } From 15cb92a38989b7cfaff3322d6dbcc8aeb3ef0af2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 11:46:49 +0100 Subject: [PATCH 066/110] fix: Update code --- .../org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java index 966d49573cec..7f735ce1074b 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java @@ -427,7 +427,7 @@ protected AnalyticsTable getLatestAnalyticsTable( protected void invokeTimeAndLog(String sql, String logPattern, Object... args) { Timer timer = new SystemTimer().start(); - log.info("Populate table SQL: '{}'", sql); + log.debug("Populate table SQL: '{}'", sql); jdbcTemplate.execute(sql); From 85c2c92224ebf7606e54a53dc35ddefc03942b3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 11:50:44 +0100 Subject: [PATCH 067/110] fix: Update code --- .../table/AbstractEventJdbcTableManager.java | 6 +++--- .../table/JdbcEventAnalyticsTableManager.java | 19 ++++++++++--------- .../AbstractEventJdbcTableManagerTest.java | 10 +++++----- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java index 658f239a30ff..543c686379ac 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java @@ -120,13 +120,13 @@ protected Skip skipIndex(ValueType valueType, boolean hasOptionSet) { } /** - * Returns a select expression, potentially with a cast statement, based on the given value type. + * Returns a column expression, potentially with a cast statement, based on the given value type. * * @param valueType the {@link ValueType} to represent as database column type. * @param columnExpression the expression or name of the column to be selected. * @return a select expression appropriate for the given value type and context. */ - protected String getSelectExpression(ValueType valueType, String columnExpression) { + protected String getColumnExpression(ValueType valueType, String columnExpression) { if (valueType.isDecimal()) { return getCastExpression(columnExpression, NUMERIC_REGEXP, sqlBuilder.dataTypeDouble()); } else if (valueType.isInteger()) { @@ -224,7 +224,7 @@ protected List getColumnForAttribute(TrackedEntityAttribut String valueColumn = String.format("%s.%s", quote(attribute.getUid()), "value"); DataType dataType = getColumnType(attribute.getValueType(), isSpatialSupport()); - String selectExpression = getSelectExpression(attribute.getValueType(), valueColumn); + String selectExpression = getColumnExpression(attribute.getValueType(), valueColumn); String dataFilterClause = getDataFilterClause(attribute); Skip skipIndex = skipIndex(attribute.getValueType(), attribute.hasOptionSet()); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index f24e870b24d7..95a45283c8a6 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -489,15 +489,16 @@ private List getColumnForDataElement( List columns = new ArrayList<>(); DataType dataType = getColumnType(dataElement.getValueType(), isSpatialSupport()); - String columnExpression = + String jsonExpression = sqlBuilder.jsonExtractNested("eventdatavalues", dataElement.getUid(), "value"); - String selectExpression = getSelectExpression(dataElement.getValueType(), columnExpression); + String columnExpression = getColumnExpression(dataElement.getValueType(), jsonExpression); String dataFilterClause = getDataFilterClause(dataElement); - String sql = String.format("%s as %s", selectExpression, quote(dataElement.getUid())); + String selectExpression = + String.format("%s as %s", columnExpression, quote(dataElement.getUid())); Skip skipIndex = skipIndex(dataElement.getValueType(), dataElement.hasOptionSet()); if (withLegendSet) { - return getColumnFromDataElementWithLegendSet(dataElement, selectExpression, dataFilterClause); + return getColumnFromDataElementWithLegendSet(dataElement, columnExpression, dataFilterClause); } if (dataElement.getValueType().isOrganisationUnit()) { @@ -509,7 +510,7 @@ private List getColumnForDataElement( .name(dataElement.getUid()) .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(dataType) - .selectExpression(sql) + .selectExpression(selectExpression) .skipIndex(skipIndex) .build()); @@ -590,7 +591,7 @@ private List getAttributeColumns(Program program) { */ private List getColumnForAttributeWithLegendSet( TrackedEntityAttribute attribute) { - String selectClause = getSelectExpression(attribute.getValueType(), "value"); + String columnExpression = getColumnExpression(attribute.getValueType(), "value"); String numericClause = getNumericClause(); String query = """ @@ -605,11 +606,11 @@ private List getColumnForAttributeWithLegendSet( .map( ls -> { String column = attribute.getUid() + PartitionUtils.SEP + ls.getUid(); - String sql = + String selectExpression = replaceQualify( query, Map.of( - "selectClause", selectClause, + "selectClause", columnExpression, "legendSetId", String.valueOf(ls.getId()), "column", column, "attributeId", String.valueOf(attribute.getId()), @@ -618,7 +619,7 @@ private List getColumnForAttributeWithLegendSet( return AnalyticsTableColumn.builder() .name(column) .dataType(CHARACTER_11) - .selectExpression(sql) + .selectExpression(selectExpression) .build(); }) .toList(); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManagerTest.java index 05f4337cc10a..ddb386b7bd0b 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManagerTest.java @@ -76,7 +76,7 @@ then cast(eventdatavalues #>> '{GieVkTxp4HH, value}' as double precision) \ else null end"""; String actual = - manager.getSelectExpression(ValueType.NUMBER, "eventdatavalues #>> '{GieVkTxp4HH, value}'"); + manager.getColumnExpression(ValueType.NUMBER, "eventdatavalues #>> '{GieVkTxp4HH, value}'"); assertEquals(expected, actual); } @@ -88,7 +88,7 @@ void testGetSelectExpressionBoolean() { case when eventdatavalues #>> '{Xl3voRRcmpo, value}' = 'true' then 1 when eventdatavalues #>> '{Xl3voRRcmpo, value}' = 'false' then 0 else null end"""; String actual = - manager.getSelectExpression( + manager.getColumnExpression( ValueType.BOOLEAN, "eventdatavalues #>> '{Xl3voRRcmpo, value}'"); assertEquals(expected, actual); @@ -103,7 +103,7 @@ then cast(eventdatavalues #>> '{AL04Wbutskk, value}' as timestamp) \ else null end"""; String actual = - manager.getSelectExpression(ValueType.DATE, "eventdatavalues #>> '{AL04Wbutskk, value}'"); + manager.getColumnExpression(ValueType.DATE, "eventdatavalues #>> '{AL04Wbutskk, value}'"); assertEquals(expected, actual); } @@ -115,7 +115,7 @@ void testGetSelectExpressionText() { eventdatavalues #>> '{FwUzmc49Pcr, value}'"""; String actual = - manager.getSelectExpression(ValueType.TEXT, "eventdatavalues #>> '{FwUzmc49Pcr, value}'"); + manager.getColumnExpression(ValueType.TEXT, "eventdatavalues #>> '{FwUzmc49Pcr, value}'"); assertEquals(expected, actual); } @@ -129,7 +129,7 @@ void testGetSelectExpressionGeometry() { ST_GeomFromGeoJSON('{"type":"Point", "coordinates":' || (eventdatavalues #>> '{C6bh7GevJfH, value}') || ', "crs":{"type":"name", "properties":{"name":"EPSG:4326"}}}')"""; String actual = - manager.getSelectExpression( + manager.getColumnExpression( ValueType.GEOJSON, "eventdatavalues #>> '{C6bh7GevJfH, value}'"); assertEquals(expected, actual); From bec3c7e8b19dcd2511ba4309d4c5edb93ca93658 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 13:33:31 +0100 Subject: [PATCH 068/110] fix: Update code --- .../table/AbstractEventJdbcTableManager.java | 45 ++++--------------- .../table/JdbcEventAnalyticsTableManager.java | 29 +++++++----- 2 files changed, 26 insertions(+), 48 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java index 543c686379ac..f8d9f1bd6f72 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java @@ -27,18 +27,17 @@ */ package org.hisp.dhis.analytics.table; -import static org.apache.commons.lang3.StringUtils.EMPTY; import static org.hisp.dhis.analytics.table.model.Skip.SKIP; import static org.hisp.dhis.analytics.util.AnalyticsUtils.getClosingParentheses; import static org.hisp.dhis.analytics.util.AnalyticsUtils.getColumnType; import static org.hisp.dhis.db.model.DataType.GEOMETRY; import static org.hisp.dhis.db.model.DataType.TEXT; -import static org.hisp.dhis.system.util.MathUtils.NUMERIC_LENIENT_REGEXP; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import org.apache.commons.lang3.Validate; import org.hisp.dhis.analytics.AnalyticsTableHookService; import org.hisp.dhis.analytics.partition.PartitionManager; import org.hisp.dhis.analytics.table.model.AnalyticsDimensionType; @@ -99,14 +98,6 @@ public AbstractEventJdbcTableManager( public static final String OU_NAME_COL_SUFFIX = "_name"; - protected final String getNumericClause() { - return " and " + sqlBuilder.regexpMatch("value", "'" + NUMERIC_LENIENT_REGEXP + "'"); - } - - protected final String getDateClause() { - return " and " + sqlBuilder.regexpMatch("value", DATE_REGEXP); - } - /** * Indicates whether creating an index should be skipped. * @@ -148,22 +139,6 @@ protected String getColumnExpression(ValueType valueType, String columnExpressio } } - /** - * For numeric and date value types, returns a data filter clause for checking whether the value - * is valid according to the value type. For other value types, returns the empty string. - * - * @param attribute the {@link TrackedEntityAttribute}. - * @return a data filter clause. - */ - protected String getDataFilterClause(TrackedEntityAttribute attribute) { - if (attribute.isNumericType()) { - return getNumericClause(); - } else if (attribute.isDateType()) { - return getDateClause(); - } - return EMPTY; - } - /** * Returns a cast expression which includes a value filter for the given value type. * @@ -225,11 +200,10 @@ protected List getColumnForAttribute(TrackedEntityAttribut String valueColumn = String.format("%s.%s", quote(attribute.getUid()), "value"); DataType dataType = getColumnType(attribute.getValueType(), isSpatialSupport()); String selectExpression = getColumnExpression(attribute.getValueType(), valueColumn); - String dataFilterClause = getDataFilterClause(attribute); Skip skipIndex = skipIndex(attribute.getValueType(), attribute.hasOptionSet()); if (attribute.getValueType().isOrganisationUnit()) { - columns.addAll(getColumnForOrgUnitAttribute(attribute, dataFilterClause)); + columns.addAll(getColumnForOrgUnitAttribute(attribute)); } columns.add( @@ -248,11 +222,11 @@ protected List getColumnForAttribute(TrackedEntityAttribut * Returns a list of columns based on the given attribute. * * @param attribute the {@link TrackedEntityAttribute}. - * @param dataFilterClause the data filter clause. * @return a list of {@link AnalyticsTableColumn}. */ private List getColumnForOrgUnitAttribute( - TrackedEntityAttribute attribute, String dataFilterClause) { + TrackedEntityAttribute attribute) { + Validate.isTrue(attribute.getValueType().isOrganisationUnit()); List columns = new ArrayList<>(); String fromClause = @@ -260,7 +234,7 @@ private List getColumnForOrgUnitAttribute( if (isSpatialSupport()) { String selectExpression = "ou.geometry " + fromClause; - String ouGeoSql = getSelectSubquery(attribute, selectExpression, dataFilterClause); + String ouGeoSql = getSelectSubquery(attribute, selectExpression); columns.add( AnalyticsTableColumn.builder() .name((attribute.getUid() + OU_GEOMETRY_COL_SUFFIX)) @@ -272,7 +246,7 @@ private List getColumnForOrgUnitAttribute( } String selectExpression = "ou.name " + fromClause; - String ouNameSql = getSelectSubquery(attribute, selectExpression, dataFilterClause); + String ouNameSql = getSelectSubquery(attribute, selectExpression); columns.add( AnalyticsTableColumn.builder() @@ -291,20 +265,17 @@ private List getColumnForOrgUnitAttribute( * * @param attribute the {@link TrackedEntityAttribute}. * @param selectExpression the select expression. - * @param dataFilterClause the data filter clause. * @return a select statement. */ - private String getSelectSubquery( - TrackedEntityAttribute attribute, String selectExpression, String dataFilterClause) { + private String getSelectSubquery(TrackedEntityAttribute attribute, String selectExpression) { return replaceQualify( """ (select ${selectExpression} from ${trackedentityattributevalue} \ where trackedentityid=en.trackedentityid \ - and trackedentityattributeid=${attributeId}${dataFilterClause})\ + and trackedentityattributeid=${attributeId})\ ${closingParentheses} as ${attributeUid}""", Map.of( "selectExpression", selectExpression, - "dataFilterClause", dataFilterClause, "attributeId", String.valueOf(attribute.getId()), "closingParentheses", getClosingParentheses(selectExpression), "attributeUid", quote(attribute.getUid()))); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 95a45283c8a6..abdccfc70bb4 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -39,6 +39,7 @@ import static org.hisp.dhis.db.model.DataType.GEOMETRY; import static org.hisp.dhis.db.model.DataType.INTEGER; import static org.hisp.dhis.db.model.DataType.TEXT; +import static org.hisp.dhis.system.util.MathUtils.NUMERIC_LENIENT_REGEXP; import static org.hisp.dhis.util.DateUtils.toLongDate; import static org.hisp.dhis.util.DateUtils.toMediumDate; @@ -502,7 +503,7 @@ private List getColumnForDataElement( } if (dataElement.getValueType().isOrganisationUnit()) { - columns.addAll(getColumnForOrgUnitDataElement(dataElement, dataFilterClause)); + columns.addAll(getColumnForOrgUnitDataElement(dataElement)); } columns.add( @@ -524,8 +525,7 @@ private List getColumnForDataElement( * @param dataFilterClause the data filter SQL clause. * @return a list of {@link AnalyticsTableColumn}. */ - private List getColumnForOrgUnitDataElement( - DataElement dataElement, String dataFilterClause) { + private List getColumnForOrgUnitDataElement(DataElement dataElement) { List columns = new ArrayList<>(); String columnExpression = @@ -535,7 +535,7 @@ private List getColumnForOrgUnitDataElement( if (isSpatialSupport()) { String fromType = "ou.geometry " + fromClause; - String geoSql = getOrgUnitSelectExpression(dataElement, fromType, dataFilterClause); + String geoSql = getOrgUnitSelectExpression(dataElement, fromType); columns.add( AnalyticsTableColumn.builder() @@ -548,7 +548,7 @@ private List getColumnForOrgUnitDataElement( } String fromTypeSql = "ou.name " + fromClause; - String ouNameSql = getOrgUnitSelectExpression(dataElement, fromTypeSql, dataFilterClause); + String ouNameSql = getOrgUnitSelectExpression(dataElement, fromTypeSql); columns.add( AnalyticsTableColumn.builder() @@ -592,7 +592,7 @@ private List getAttributeColumns(Program program) { private List getColumnForAttributeWithLegendSet( TrackedEntityAttribute attribute) { String columnExpression = getColumnExpression(attribute.getValueType(), "value"); - String numericClause = getNumericClause(); + String numericClause = getNumericClause("value"); String query = """ \s(select l.uid from ${maplegend} l \ @@ -630,18 +630,15 @@ private List getColumnForAttributeWithLegendSet( * * @param dataElement the data element to create the select statement for. * @param selectExpression the select expression. - * @param dataFilterClause the data filter clause. * @return a select expression. */ - private String getOrgUnitSelectExpression( - DataElement dataElement, String selectExpression, String dataFilterClause) { + private String getOrgUnitSelectExpression(DataElement dataElement, String selectExpression) { Validate.isTrue(dataElement.getValueType().isOrganisationUnit()); String prts = getClosingParentheses(selectExpression); return replaceQualify( - "(select ${selectExpression} ${dataFilterClause})${closingParentheses} as ${uid}", + "(select ${selectExpression})${closingParentheses} as ${uid}", Map.of( "selectExpression", selectExpression, - "dataFilterClause", dataFilterClause, "closingParentheses", prts, "uid", quote(dataElement.getUid()))); } @@ -756,6 +753,16 @@ private List getDataYears( return jdbcTemplate.queryForList(sql, Integer.class); } + /** + * Returns a numeric regexp match expression for the given value. + * + * @param value the value. + * @return a numeric regexp match expression. + */ + private final String getNumericClause(String value) { + return " and " + sqlBuilder.regexpMatch(value, "'" + NUMERIC_LENIENT_REGEXP + "'"); + } + /** * Retrieve years for partition tables. Year will become a partition key. The default return value * is the list with the recent year. From f66b2beadb2dc30b6bb2ac1d8ef9dae29e316c64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 13:39:37 +0100 Subject: [PATCH 069/110] fix: Update code --- .../table/JdbcEventAnalyticsTableManagerDorisTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerDorisTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerDorisTest.java index 9f952e8807b3..5b2bca0f3ec1 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerDorisTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerDorisTest.java @@ -169,7 +169,7 @@ void verifyGetTableWithDataElements() { "case when json_unquote(json_extract(eventdatavalues, '$.%s.value')) regexp '^\\d{4}-\\d{2}-\\d{2}(\\s|T)?((\\d{2}:)(\\d{2}:)?(\\d{2}))?(|.(\\d{3})|.(\\d{3})Z)?$' then cast(json_unquote(json_extract(eventdatavalues, '$.%s.value')) as datetime) else null end as `%s`"; String aliasE = "json_unquote(json_extract(eventdatavalues, '$.%s.value')) as `%s`"; String aliasF = - "(select ou.name from dhis2.public.`organisationunit` ou where ou.uid = json_unquote(json_extract(eventdatavalues, '$.%s.value')) ) as `%s`"; + "(select ou.name from dhis2.public.`organisationunit` ou where ou.uid = json_unquote(json_extract(eventdatavalues, '$.%s.value'))) as `%s`"; String aliasG = "case when json_unquote(json_extract(eventdatavalues, '$.%s.value')) regexp '^(-?[0-9]+)(\\.[0-9]+)?$' then cast(json_unquote(json_extract(eventdatavalues, '$.%s.value')) as bigint) else null end as `%s`"; From 7b92571d30f15226c714a885c8f5dabe5bf76fad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 13:46:00 +0100 Subject: [PATCH 070/110] fix: Update code --- .../analytics/table/JdbcEventAnalyticsTableManagerTest.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java index b2da976c129e..f071d868106b 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java @@ -399,15 +399,13 @@ void verifyGetTableWithDataElements() { String aliasD5_geo = "(select ou.geometry from \"organisationunit\" ou where ou.uid = eventdatavalues #>> '{" + d5.getUid() - + ", value}' " - + ") as \"" + + ", value}') as \"" + d5.getUid() + "\""; String aliasD5_name = "(select ou.name from \"organisationunit\" ou where ou.uid = eventdatavalues #>> '{" + d5.getUid() - + ", value}' " - + ") as \"" + + ", value}') as \"" + d5.getUid() + "\""; AnalyticsTableUpdateParams params = From b400d6b309adfa34c62982ca3c724957cba46d46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 13:46:22 +0100 Subject: [PATCH 071/110] fix: Update code --- .../analytics/table/JdbcEventAnalyticsTableManagerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java index f071d868106b..9a9db09b4f99 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java @@ -597,7 +597,7 @@ void verifyDataElementTypeOrgUnitFetchesOuNameWhenPopulatingEventAnalyticsTable( String.format( """ (select ou.name from "organisationunit" ou where ou.uid = \ - eventdatavalues #>> '{%s, value}' ) as %s""", + eventdatavalues #>> '{%s, value}') as %s""", d5.getUid(), quote(d5.getUid())); assertThat(sql.getValue(), containsString(ouUidQuery)); From f55b90005dc44235b7b8dc50b36a458a9014c4a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 13:47:24 +0100 Subject: [PATCH 072/110] fix: Update code --- .../analytics/table/JdbcEventAnalyticsTableManager.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index abdccfc70bb4..77eba78b4154 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -535,27 +535,27 @@ private List getColumnForOrgUnitDataElement(DataElement da if (isSpatialSupport()) { String fromType = "ou.geometry " + fromClause; - String geoSql = getOrgUnitSelectExpression(dataElement, fromType); + String ouGeoExpression = getOrgUnitSelectExpression(dataElement, fromType); columns.add( AnalyticsTableColumn.builder() .name((dataElement.getUid() + OU_GEOMETRY_COL_SUFFIX)) .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(GEOMETRY) - .selectExpression(geoSql) + .selectExpression(ouGeoExpression) .indexType(IndexType.GIST) .build()); } String fromTypeSql = "ou.name " + fromClause; - String ouNameSql = getOrgUnitSelectExpression(dataElement, fromTypeSql); + String ouNameExpression = getOrgUnitSelectExpression(dataElement, fromTypeSql); columns.add( AnalyticsTableColumn.builder() .name((dataElement.getUid() + OU_NAME_COL_SUFFIX)) .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(TEXT) - .selectExpression(ouNameSql) + .selectExpression(ouNameExpression) .skipIndex(SKIP) .build()); From fb8c59f8f98535674fc4c32ccb477229f3f75e11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 13:48:31 +0100 Subject: [PATCH 073/110] fix: Update code --- .../analytics/util/AnalyticsUtilsTest.java | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/util/AnalyticsUtilsTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/util/AnalyticsUtilsTest.java index 78424fe9c735..dbbb563aa2ac 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/util/AnalyticsUtilsTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/util/AnalyticsUtilsTest.java @@ -93,7 +93,6 @@ import org.hisp.dhis.test.TestBase; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.springframework.jdbc.BadSqlGrammarException; import org.springframework.jdbc.UncategorizedSQLException; /** @@ -763,31 +762,14 @@ void whenUncategorizedSQLException_withTableNotExisting_thenThrowException() { () -> AnalyticsUtils.withExceptionHandling(supplier, false)); } - @Test - void whenBadSqlGrammarException_withMultipleQueries_thenReturnEmpty() { - SQLException sqlException = new SQLException("syntax error"); - BadSqlGrammarException badSqlException = - new BadSqlGrammarException("task", DUMMY_SQL, sqlException); - Supplier supplier = - () -> { - throw badSqlException; - }; - - Optional result = AnalyticsUtils.withExceptionHandling(supplier, true); - - assertFalse(result.isPresent()); - } - @Test void whenQueryRuntimeException_thenRethrow() { - // Arrange QueryRuntimeException queryException = new QueryRuntimeException("Test error"); Supplier supplier = () -> { throw queryException; }; - // Act & Assert QueryRuntimeException thrown = assertThrows( QueryRuntimeException.class, From b4f34ba40f4fd220689370d36053d3f1f157534e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 13:50:36 +0100 Subject: [PATCH 074/110] fix: Update code --- .../analytics/table/JdbcEventAnalyticsTableManager.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 77eba78b4154..4474f2d4c97a 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -535,27 +535,27 @@ private List getColumnForOrgUnitDataElement(DataElement da if (isSpatialSupport()) { String fromType = "ou.geometry " + fromClause; - String ouGeoExpression = getOrgUnitSelectExpression(dataElement, fromType); + String geoExpression = getOrgUnitSelectExpression(dataElement, fromType); columns.add( AnalyticsTableColumn.builder() .name((dataElement.getUid() + OU_GEOMETRY_COL_SUFFIX)) .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(GEOMETRY) - .selectExpression(ouGeoExpression) + .selectExpression(geoExpression) .indexType(IndexType.GIST) .build()); } String fromTypeSql = "ou.name " + fromClause; - String ouNameExpression = getOrgUnitSelectExpression(dataElement, fromTypeSql); + String ouNameSql = getOrgUnitSelectExpression(dataElement, fromTypeSql); columns.add( AnalyticsTableColumn.builder() .name((dataElement.getUid() + OU_NAME_COL_SUFFIX)) .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(TEXT) - .selectExpression(ouNameExpression) + .selectExpression(ouNameSql) .skipIndex(SKIP) .build()); From ddb11611195ad2c52e32877ea81e2ca40dfcdbcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 14:40:32 +0100 Subject: [PATCH 075/110] fix: Update code --- .../analytics/table/JdbcEventAnalyticsTableManager.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 4474f2d4c97a..02eff1c7ca53 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -30,7 +30,6 @@ import static java.util.stream.Collectors.toList; import static org.apache.commons.lang3.StringUtils.EMPTY; import static org.hisp.dhis.analytics.table.model.Skip.SKIP; -import static org.hisp.dhis.analytics.util.AnalyticsUtils.getClosingParentheses; import static org.hisp.dhis.analytics.util.AnalyticsUtils.getColumnType; import static org.hisp.dhis.commons.util.TextUtils.emptyIfTrue; import static org.hisp.dhis.commons.util.TextUtils.format; @@ -634,13 +633,9 @@ private List getColumnForAttributeWithLegendSet( */ private String getOrgUnitSelectExpression(DataElement dataElement, String selectExpression) { Validate.isTrue(dataElement.getValueType().isOrganisationUnit()); - String prts = getClosingParentheses(selectExpression); return replaceQualify( - "(select ${selectExpression})${closingParentheses} as ${uid}", - Map.of( - "selectExpression", selectExpression, - "closingParentheses", prts, - "uid", quote(dataElement.getUid()))); + "(select ${selectExpression}) as ${uid}", + Map.of("selectExpression", selectExpression, "uid", quote(dataElement.getUid()))); } /** From 251e246e4a77953b5941a4e5f9483cbaa2510cd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 15:09:32 +0100 Subject: [PATCH 076/110] fix: Update code --- .../table/JdbcEventAnalyticsTableManager.java | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 02eff1c7ca53..34069bff8466 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -527,34 +527,52 @@ private List getColumnForDataElement( private List getColumnForOrgUnitDataElement(DataElement dataElement) { List columns = new ArrayList<>(); + String format = + """ + (select ou.${field} from ${organisationunit} ou \ + where ou.uid = ${columnExpression}) as ${uid}"""; String columnExpression = sqlBuilder.jsonExtractNested("eventdatavalues", dataElement.getUid(), "value"); - String fromClause = - qualifyVariables("from ${organisationunit} ou where ou.uid = " + columnExpression); if (isSpatialSupport()) { - String fromType = "ou.geometry " + fromClause; - String geoExpression = getOrgUnitSelectExpression(dataElement, fromType); + String geoSelectExpression = + replaceQualify( + format, + Map.of( + "field", + "geometry", + "columnExpression", + columnExpression, + "uid", + quote(dataElement.getUid()))); columns.add( AnalyticsTableColumn.builder() .name((dataElement.getUid() + OU_GEOMETRY_COL_SUFFIX)) .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(GEOMETRY) - .selectExpression(geoExpression) + .selectExpression(geoSelectExpression) .indexType(IndexType.GIST) .build()); } - String fromTypeSql = "ou.name " + fromClause; - String ouNameSql = getOrgUnitSelectExpression(dataElement, fromTypeSql); + String nameSelectExpression = + replaceQualify( + format, + Map.of( + "field", + "name", + "columnExpression", + columnExpression, + "uid", + quote(dataElement.getUid()))); columns.add( AnalyticsTableColumn.builder() .name((dataElement.getUid() + OU_NAME_COL_SUFFIX)) .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(TEXT) - .selectExpression(ouNameSql) + .selectExpression(nameSelectExpression) .skipIndex(SKIP) .build()); From d6c9f0e9af47d7c519d610a585a21dd7b253e7ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 15:09:45 +0100 Subject: [PATCH 077/110] fix: Update code --- .../table/JdbcEventAnalyticsTableManager.java | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 34069bff8466..35a31a623679 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -51,7 +51,6 @@ import java.util.Map; import java.util.Objects; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.Validate; import org.hisp.dhis.analytics.AnalyticsTableHookService; import org.hisp.dhis.analytics.AnalyticsTableType; import org.hisp.dhis.analytics.AnalyticsTableUpdateParams; @@ -642,20 +641,6 @@ private List getColumnForAttributeWithLegendSet( .toList(); } - /** - * Returns a select statement for the given select expression. - * - * @param dataElement the data element to create the select statement for. - * @param selectExpression the select expression. - * @return a select expression. - */ - private String getOrgUnitSelectExpression(DataElement dataElement, String selectExpression) { - Validate.isTrue(dataElement.getValueType().isOrganisationUnit()); - return replaceQualify( - "(select ${selectExpression}) as ${uid}", - Map.of("selectExpression", selectExpression, "uid", quote(dataElement.getUid()))); - } - /** * Returns a list of columns. * From 4bd41ca8c440b160a7f93ddbfc0d458125720fcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 15:11:34 +0100 Subject: [PATCH 078/110] fix: Update code --- .../table/JdbcEventAnalyticsTableManager.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 35a31a623679..9259dac18b64 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -532,18 +532,16 @@ private List getColumnForOrgUnitDataElement(DataElement da where ou.uid = ${columnExpression}) as ${uid}"""; String columnExpression = sqlBuilder.jsonExtractNested("eventdatavalues", dataElement.getUid(), "value"); + String alias = quote(dataElement.getUid()); if (isSpatialSupport()) { String geoSelectExpression = replaceQualify( format, Map.of( - "field", - "geometry", - "columnExpression", - columnExpression, - "uid", - quote(dataElement.getUid()))); + "field", "geometry", + "columnExpression", columnExpression, + "uid", alias)); columns.add( AnalyticsTableColumn.builder() From 427ffbba6103f8222cb688a8cb59055efbdc3f5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 15:13:16 +0100 Subject: [PATCH 079/110] fix: Update code --- .../analytics/table/JdbcEventAnalyticsTableManager.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 9259dac18b64..94f847d455e5 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -557,12 +557,9 @@ private List getColumnForOrgUnitDataElement(DataElement da replaceQualify( format, Map.of( - "field", - "name", - "columnExpression", - columnExpression, - "uid", - quote(dataElement.getUid()))); + "field", "name", + "columnExpression", columnExpression, + "uid", alias)); columns.add( AnalyticsTableColumn.builder() From b0f075191014f3e42820827a69027f10c6ad09d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 16:25:05 +0100 Subject: [PATCH 080/110] fix: Update code --- .../table/JdbcEventAnalyticsTableManager.java | 51 +++++++++---------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 94f847d455e5..ba3baefaf155 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -526,53 +526,52 @@ private List getColumnForDataElement( private List getColumnForOrgUnitDataElement(DataElement dataElement) { List columns = new ArrayList<>(); - String format = - """ - (select ou.${field} from ${organisationunit} ou \ - where ou.uid = ${columnExpression}) as ${uid}"""; - String columnExpression = - sqlBuilder.jsonExtractNested("eventdatavalues", dataElement.getUid(), "value"); - String alias = quote(dataElement.getUid()); - if (isSpatialSupport()) { - String geoSelectExpression = - replaceQualify( - format, - Map.of( - "field", "geometry", - "columnExpression", columnExpression, - "uid", alias)); - columns.add( AnalyticsTableColumn.builder() .name((dataElement.getUid() + OU_GEOMETRY_COL_SUFFIX)) .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(GEOMETRY) - .selectExpression(geoSelectExpression) + .selectExpression(getOrgUnitSelectSubquery("geometry", dataElement)) .indexType(IndexType.GIST) .build()); } - String nameSelectExpression = - replaceQualify( - format, - Map.of( - "field", "name", - "columnExpression", columnExpression, - "uid", alias)); - columns.add( AnalyticsTableColumn.builder() .name((dataElement.getUid() + OU_NAME_COL_SUFFIX)) .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(TEXT) - .selectExpression(nameSelectExpression) + .selectExpression(getOrgUnitSelectSubquery("name", dataElement)) .skipIndex(SKIP) .build()); return columns; } + /** + * Returns a org unit select query. + * + * @param column the column name. + * @param dataElement the {@link DataElement}. + * @return an org unit select query. + */ + private String getOrgUnitSelectSubquery(String column, DataElement dataElement) { + String format = + """ + (select ou.${column} from ${organisationunit} ou \ + where ou.uid = ${columnExpression}) as ${uid}"""; + String columnExpression = + sqlBuilder.jsonExtractNested("eventdatavalues", dataElement.getUid(), "value"); + String alias = quote(dataElement.getUid()); + return replaceQualify( + format, + Map.of( + "column", "name", + "columnExpression", columnExpression, + "uid", alias)); + } + /** * Returns columns for attributes of the given program. * From cce6d852f61d0aa0dda698f9fb622a6f4a29ae66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 16:30:46 +0100 Subject: [PATCH 081/110] fix: Update code --- .../dhis/analytics/table/JdbcEventAnalyticsTableManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index ba3baefaf155..65156116062b 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -567,7 +567,7 @@ private String getOrgUnitSelectSubquery(String column, DataElement dataElement) return replaceQualify( format, Map.of( - "column", "name", + "column", column, "columnExpression", columnExpression, "uid", alias)); } From 63484bd85b43918283e7509f18cf4ebbfae2a7e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 16:31:19 +0100 Subject: [PATCH 082/110] fix: Update code --- .../dhis/analytics/table/JdbcEventAnalyticsTableManager.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 65156116062b..94c8a2805487 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -560,16 +560,17 @@ private String getOrgUnitSelectSubquery(String column, DataElement dataElement) String format = """ (select ou.${column} from ${organisationunit} ou \ - where ou.uid = ${columnExpression}) as ${uid}"""; + where ou.uid = ${columnExpression}) as ${alias}"""; String columnExpression = sqlBuilder.jsonExtractNested("eventdatavalues", dataElement.getUid(), "value"); String alias = quote(dataElement.getUid()); + return replaceQualify( format, Map.of( "column", column, "columnExpression", columnExpression, - "uid", alias)); + "alias", alias)); } /** From 1064844f6aa9dfeeae36e1cae43564c0678cdb6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 20:30:02 +0100 Subject: [PATCH 083/110] fix: Update code --- .../table/AbstractEventJdbcTableManager.java | 1 - .../table/AbstractJdbcTableManager.java | 20 ++++++++++++++++--- ...AbstractJdbcEventAnalyticsManagerTest.java | 18 ++++++++++------- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java index f8d9f1bd6f72..f4f8034cc718 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java @@ -32,7 +32,6 @@ import static org.hisp.dhis.analytics.util.AnalyticsUtils.getColumnType; import static org.hisp.dhis.db.model.DataType.GEOMETRY; import static org.hisp.dhis.db.model.DataType.TEXT; - import java.util.ArrayList; import java.util.List; import java.util.Map; diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java index 7f735ce1074b..449f4dafd43d 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java @@ -34,7 +34,6 @@ import static org.hisp.dhis.db.model.DataType.CHARACTER_11; import static org.hisp.dhis.db.model.DataType.TEXT; import static org.hisp.dhis.util.DateUtils.toLongDate; - import java.util.Collection; import java.util.Collections; import java.util.Date; @@ -42,9 +41,8 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.function.Function; import java.util.stream.Collectors; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; import org.hisp.dhis.analytics.AnalyticsTableHook; import org.hisp.dhis.analytics.AnalyticsTableHookService; import org.hisp.dhis.analytics.AnalyticsTableManager; @@ -88,6 +86,8 @@ import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.util.Assert; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; /** * @author Lars Helge Overland @@ -672,6 +672,20 @@ protected boolean tableIsNotEmpty(String name) { return jdbcTemplate.queryForRowSet(sql).next(); } + /** + * Converts the given list of items to a comma-separated string, using the given mapping function to map the object to string. + * + * @param the type. + * @param list the list. + * @param mapper the mapping function. + * @return a comma-separated string. + */ + protected String toCommaSeparated(List list, Function mapper) { + return list.stream() + .map(mapper) + .collect(Collectors.joining(",")); + } + /** * Quotes the given relation. * 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 cfa4d4c09173..ca2ae3937330 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 @@ -60,7 +60,6 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; - import java.math.BigDecimal; import java.math.RoundingMode; import java.sql.ResultSet; @@ -108,6 +107,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; +import org.mockito.Spy; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.support.rowset.ResultSetWrappingSqlRowSet; @@ -125,9 +125,16 @@ class AbstractJdbcEventAnalyticsManagerTest extends EventAnalyticsTest { @Mock private ExecutionPlanStore executionPlanStore; @Mock private OrganisationUnitService organisationUnitService; + + @Spy private DefaultProgramIndicatorSubqueryBuilder programIndicatorSubqueryBuilder = + new DefaultProgramIndicatorSubqueryBuilder(programIndicatorService); + + @Spy private SqlBuilder sqlBuilder = new PostgreSqlBuilder(); - private final SqlBuilder sqlBuilder = new PostgreSqlBuilder(); + @Spy private EventTimeFieldSqlRenderer eventTimeFieldSqlRenderer = new EventTimeFieldSqlRenderer(sqlBuilder); + @Spy private EnrollmentTimeFieldSqlRenderer enrollmentTimeFieldSqlRenderer = new EnrollmentTimeFieldSqlRenderer(sqlBuilder); + private JdbcEventAnalyticsManager eventSubject; private JdbcEnrollmentAnalyticsManager enrollmentSubject; @@ -146,15 +153,12 @@ class AbstractJdbcEventAnalyticsManagerTest extends EventAnalyticsTest { @BeforeEach public void setUp() { - DefaultProgramIndicatorSubqueryBuilder programIndicatorSubqueryBuilder = - new DefaultProgramIndicatorSubqueryBuilder(programIndicatorService); - eventSubject = new JdbcEventAnalyticsManager( jdbcTemplate, programIndicatorService, programIndicatorSubqueryBuilder, - new EventTimeFieldSqlRenderer(sqlBuilder), + eventTimeFieldSqlRenderer, executionPlanStore, sqlBuilder); @@ -163,7 +167,7 @@ public void setUp() { jdbcTemplate, programIndicatorService, programIndicatorSubqueryBuilder, - new EnrollmentTimeFieldSqlRenderer(sqlBuilder), + enrollmentTimeFieldSqlRenderer, executionPlanStore, sqlBuilder); From 67ef7db28cc2b0abcd61b5defec739ddd893a46d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 20:31:02 +0100 Subject: [PATCH 084/110] fix: Update code --- .../table/AbstractEventJdbcTableManager.java | 1 + .../table/AbstractJdbcTableManager.java | 16 ++++---- ...AbstractJdbcEventAnalyticsManagerTest.java | 40 +++++++------------ 3 files changed, 23 insertions(+), 34 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java index f4f8034cc718..f8d9f1bd6f72 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java @@ -32,6 +32,7 @@ import static org.hisp.dhis.analytics.util.AnalyticsUtils.getColumnType; import static org.hisp.dhis.db.model.DataType.GEOMETRY; import static org.hisp.dhis.db.model.DataType.TEXT; + import java.util.ArrayList; import java.util.List; import java.util.Map; diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java index 449f4dafd43d..c7c63da03aae 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java @@ -34,6 +34,7 @@ import static org.hisp.dhis.db.model.DataType.CHARACTER_11; import static org.hisp.dhis.db.model.DataType.TEXT; import static org.hisp.dhis.util.DateUtils.toLongDate; + import java.util.Collection; import java.util.Collections; import java.util.Date; @@ -43,6 +44,8 @@ import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.hisp.dhis.analytics.AnalyticsTableHook; import org.hisp.dhis.analytics.AnalyticsTableHookService; import org.hisp.dhis.analytics.AnalyticsTableManager; @@ -86,8 +89,6 @@ import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.util.Assert; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; /** * @author Lars Helge Overland @@ -673,19 +674,18 @@ protected boolean tableIsNotEmpty(String name) { } /** - * Converts the given list of items to a comma-separated string, using the given mapping function to map the object to string. - * + * Converts the given list of items to a comma-separated string, using the given mapping function + * to map the object to string. + * * @param the type. * @param list the list. * @param mapper the mapping function. * @return a comma-separated string. */ protected String toCommaSeparated(List list, Function mapper) { - return list.stream() - .map(mapper) - .collect(Collectors.joining(",")); + return list.stream().map(mapper).collect(Collectors.joining(",")); } - + /** * Quotes the given relation. * 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 ca2ae3937330..6e108dbfb48f 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 @@ -106,6 +106,7 @@ 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; @@ -125,19 +126,24 @@ class AbstractJdbcEventAnalyticsManagerTest extends EventAnalyticsTest { @Mock private ExecutionPlanStore executionPlanStore; @Mock private OrganisationUnitService organisationUnitService; - - @Spy private DefaultProgramIndicatorSubqueryBuilder programIndicatorSubqueryBuilder = + + @Spy + private DefaultProgramIndicatorSubqueryBuilder programIndicatorSubqueryBuilder = new DefaultProgramIndicatorSubqueryBuilder(programIndicatorService); - + @Spy private SqlBuilder sqlBuilder = new PostgreSqlBuilder(); - @Spy private EventTimeFieldSqlRenderer eventTimeFieldSqlRenderer = new EventTimeFieldSqlRenderer(sqlBuilder); + @Spy + private EventTimeFieldSqlRenderer eventTimeFieldSqlRenderer = + new EventTimeFieldSqlRenderer(sqlBuilder); - @Spy private EnrollmentTimeFieldSqlRenderer enrollmentTimeFieldSqlRenderer = new EnrollmentTimeFieldSqlRenderer(sqlBuilder); - - private JdbcEventAnalyticsManager eventSubject; + @Spy + private EnrollmentTimeFieldSqlRenderer enrollmentTimeFieldSqlRenderer = + new EnrollmentTimeFieldSqlRenderer(sqlBuilder); - private JdbcEnrollmentAnalyticsManager enrollmentSubject; + @InjectMocks private JdbcEventAnalyticsManager eventSubject; + + @InjectMocks private JdbcEnrollmentAnalyticsManager enrollmentSubject; private Program programA; @@ -153,24 +159,6 @@ class AbstractJdbcEventAnalyticsManagerTest extends EventAnalyticsTest { @BeforeEach public void setUp() { - eventSubject = - new JdbcEventAnalyticsManager( - jdbcTemplate, - programIndicatorService, - programIndicatorSubqueryBuilder, - eventTimeFieldSqlRenderer, - executionPlanStore, - sqlBuilder); - - enrollmentSubject = - new JdbcEnrollmentAnalyticsManager( - jdbcTemplate, - programIndicatorService, - programIndicatorSubqueryBuilder, - enrollmentTimeFieldSqlRenderer, - executionPlanStore, - sqlBuilder); - programA = createProgram('A'); dataElementA = createDataElement('A', ValueType.INTEGER, AggregationType.SUM); From a4084c5ae9799a4974cd373586aa8541cc7bdeb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 20:31:05 +0100 Subject: [PATCH 085/110] fix: Update code --- .../event/data/AbstractJdbcEventAnalyticsManagerTest.java | 1 + 1 file changed, 1 insertion(+) 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 6e108dbfb48f..bbade801c5f3 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 @@ -60,6 +60,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; + import java.math.BigDecimal; import java.math.RoundingMode; import java.sql.ResultSet; From 1ed29a486d3b9064287fdc5d5f48978653a76628 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 20:32:44 +0100 Subject: [PATCH 086/110] fix: Update code --- .../event/data/AbstractJdbcEventAnalyticsManagerTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 bbade801c5f3..538f509a6f67 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 @@ -75,6 +75,7 @@ import org.hisp.dhis.analytics.AnalyticsAggregationType; import org.hisp.dhis.analytics.EventOutputType; import org.hisp.dhis.analytics.analyze.ExecutionPlanStore; +import org.hisp.dhis.analytics.common.ProgramIndicatorSubqueryBuilder; import org.hisp.dhis.analytics.event.EventQueryParams; import org.hisp.dhis.analytics.event.EventQueryParams.Builder; import org.hisp.dhis.analytics.event.data.programindicator.DefaultProgramIndicatorSubqueryBuilder; @@ -129,7 +130,7 @@ class AbstractJdbcEventAnalyticsManagerTest extends EventAnalyticsTest { @Mock private OrganisationUnitService organisationUnitService; @Spy - private DefaultProgramIndicatorSubqueryBuilder programIndicatorSubqueryBuilder = + private ProgramIndicatorSubqueryBuilder programIndicatorSubqueryBuilder = new DefaultProgramIndicatorSubqueryBuilder(programIndicatorService); @Spy private SqlBuilder sqlBuilder = new PostgreSqlBuilder(); @@ -161,7 +162,6 @@ class AbstractJdbcEventAnalyticsManagerTest extends EventAnalyticsTest { @BeforeEach public void setUp() { programA = createProgram('A'); - dataElementA = createDataElement('A', ValueType.INTEGER, AggregationType.SUM); dataElementA.setUid("fWIAEtYVEGk"); } From ee94d6521deb1432348a81e4e84e763067eda20f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 20:33:08 +0100 Subject: [PATCH 087/110] fix: Update code --- .../event/data/AbstractJdbcEventAnalyticsManagerTest.java | 2 -- 1 file changed, 2 deletions(-) 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 538f509a6f67..b37709db626e 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 @@ -234,8 +234,6 @@ void verifyGetCoordinateColumn() { + colName + ")::numeric, 6) || ']' as " + colName)); - - return; } @Test From 7918f3fcabb07f1bc057f4823ab516e84ec3cbee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 20:39:39 +0100 Subject: [PATCH 088/110] fix: Update code --- .../table/AbstractEventJdbcTableManager.java | 15 ++++----------- .../AbstractEventJdbcTableManagerTest.java | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java index f8d9f1bd6f72..9392bfc49806 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java @@ -49,7 +49,6 @@ import org.hisp.dhis.common.IdentifiableObject; import org.hisp.dhis.common.IdentifiableObjectManager; import org.hisp.dhis.common.ValueType; -import org.hisp.dhis.commons.util.TextUtils; import org.hisp.dhis.dataapproval.DataApprovalLevelService; import org.hisp.dhis.db.model.DataType; import org.hisp.dhis.db.model.IndexType; @@ -171,19 +170,13 @@ protected void populateTableInternal(AnalyticsTablePartition partition, String f String sql = "insert into " + tableName + " ("; - for (AnalyticsTableColumn col : columns) { - sql += quote(col.getName()) + ","; - } - - sql = TextUtils.removeLastComma(sql) + ") select "; + sql += toCommaSeparated(columns, col -> quote(col.getName())); - for (AnalyticsTableColumn col : columns) { - sql += col.getSelectExpression() + ","; - } + sql += ") select "; - sql = TextUtils.removeLastComma(sql) + " "; + sql += toCommaSeparated(columns, AnalyticsTableColumn::getSelectExpression); - sql += fromClause; + sql += " " + fromClause; invokeTimeAndLog(sql, "Populating table: '{}'", tableName); } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManagerTest.java index ddb386b7bd0b..a4ea5e5fa65f 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManagerTest.java @@ -30,8 +30,11 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.when; +import java.util.List; import org.hisp.dhis.analytics.table.setting.AnalyticsTableSettings; import org.hisp.dhis.common.ValueType; +import org.hisp.dhis.db.model.Column; +import org.hisp.dhis.db.model.DataType; import org.hisp.dhis.db.sql.PostgreSqlBuilder; import org.hisp.dhis.db.sql.SqlBuilder; import org.junit.jupiter.api.Test; @@ -50,6 +53,21 @@ class AbstractEventJdbcTableManagerTest { @InjectMocks private JdbcEventAnalyticsTableManager manager; + @Test + void testToCommaSeparated() { + List columns = + List.of( + new Column("dx", DataType.VARCHAR_255), + new Column("pe", DataType.VARCHAR_255), + new Column("value", DataType.DOUBLE)); + + String expected = + """ + "dx","pe","value\""""; + + assertEquals(expected, manager.toCommaSeparated(columns, col -> manager.quote(col.getName()))); + } + @Test void testGetCastExpression() { String expected = From 000cc5d01b522961d1ae90dff27c2a52e08ed1bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 20:47:49 +0100 Subject: [PATCH 089/110] fix: Update code --- ...ollmentAnalyticsDimensionsServiceTest.java | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EnrollmentAnalyticsDimensionsServiceTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EnrollmentAnalyticsDimensionsServiceTest.java index 66e7eb8dec26..ea1f01b27015 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EnrollmentAnalyticsDimensionsServiceTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EnrollmentAnalyticsDimensionsServiceTest.java @@ -27,8 +27,6 @@ */ package org.hisp.dhis.analytics.event.data; -import static org.hisp.dhis.analytics.common.AnalyticsDimensionsTestSupport.allValueTypeDataElements; -import static org.hisp.dhis.analytics.common.AnalyticsDimensionsTestSupport.allValueTypeTEAs; import static org.hisp.dhis.analytics.common.DimensionServiceCommonTest.aggregateAllowedValueTypesPredicate; import static org.hisp.dhis.analytics.common.DimensionServiceCommonTest.queryDisallowedValueTypesPredicate; import static org.hisp.dhis.test.TestBase.injectSecurityContextNoSettings; @@ -37,38 +35,35 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import java.util.Collections; import java.util.List; -import org.hisp.dhis.analytics.event.EnrollmentAnalyticsDimensionsService; import org.hisp.dhis.common.BaseIdentifiableObject; import org.hisp.dhis.common.PrefixedDimension; import org.hisp.dhis.dataelement.DataElement; import org.hisp.dhis.program.Program; import org.hisp.dhis.program.ProgramService; -import org.hisp.dhis.security.acl.AclService; import org.hisp.dhis.trackedentity.TrackedEntityAttribute; import org.hisp.dhis.user.SystemUser; 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.junit.jupiter.MockitoExtension; +@ExtendWith(MockitoExtension.class) class EnrollmentAnalyticsDimensionsServiceTest { - private EnrollmentAnalyticsDimensionsService enrollmentAnalyticsDimensionsService; + @Mock private ProgramService programService; + + @InjectMocks + private DefaultEnrollmentAnalyticsDimensionsService enrollmentAnalyticsDimensionsService; @BeforeEach void setup() { injectSecurityContextNoSettings(new SystemUser()); - ProgramService programService = mock(ProgramService.class); - Program program = mock(Program.class); when(programService.getProgram(any())).thenReturn(program); - when(program.getDataElements()).thenReturn(allValueTypeDataElements()); - when(program.getProgramIndicators()).thenReturn(Collections.emptySet()); - when(program.getTrackedEntityAttributes()).thenReturn(allValueTypeTEAs()); - - enrollmentAnalyticsDimensionsService = - new DefaultEnrollmentAnalyticsDimensionsService(programService, mock(AclService.class)); } @Test From 8caf620e6ba995f4d0cdbffc1f877d5911451f66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 20:52:10 +0100 Subject: [PATCH 090/110] fix: Update code --- .../event/data/EnrollmentAnalyticsManagerTest.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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 a389425ecada..6e1491910583 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 @@ -85,6 +85,7 @@ import org.mockito.ArgumentCaptor; import org.mockito.Captor; 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; @@ -108,7 +109,11 @@ class EnrollmentAnalyticsManagerTest extends EventAnalyticsTest { @Mock private ProgramIndicatorService programIndicatorService; - private final SqlBuilder sqlBuilder = new PostgreSqlBuilder(); + @Spy private SqlBuilder sqlBuilder = new PostgreSqlBuilder(); + + @Spy + private EnrollmentTimeFieldSqlRenderer enrollmentTimeFieldSqlRenderer = + new EnrollmentTimeFieldSqlRenderer(sqlBuilder); @Captor private ArgumentCaptor sql; @@ -133,7 +138,7 @@ public void setUp() { jdbcTemplate, programIndicatorService, programIndicatorSubqueryBuilder, - new EnrollmentTimeFieldSqlRenderer(sqlBuilder), + enrollmentTimeFieldSqlRenderer, executionPlanStore, sqlBuilder); } From aeca0bd067e6cfbe9e243744d813ac7613c4c919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 4 Dec 2024 20:53:02 +0100 Subject: [PATCH 091/110] fix: Update code --- .../event/data/EnrollmentQueryHelperTest.java | 23 +------------------ 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EnrollmentQueryHelperTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EnrollmentQueryHelperTest.java index 2b11e9b0cc5c..267fd2d19c94 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EnrollmentQueryHelperTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/event/data/EnrollmentQueryHelperTest.java @@ -35,7 +35,7 @@ import static org.hisp.dhis.common.OrganisationUnitSelectionMode.CHILDREN; import static org.hisp.dhis.common.OrganisationUnitSelectionMode.SELECTED; import static org.hisp.dhis.period.RelativePeriodEnum.LAST_3_DAYS; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.List; import java.util.Set; @@ -52,7 +52,6 @@ class EnrollmentQueryHelperTest { @Test void testGetHeaderColumnsSamePrefixedDimension() { - // Given List headers = List.of( new GridHeader("name0"), @@ -64,10 +63,8 @@ void testGetHeaderColumnsSamePrefixedDimension() { String sql = "select name0, name1, dim.name2, pe, value, ou from table"; - // When Set headerColumns = EnrollmentQueryHelper.getHeaderColumns(headers, sql); - // Then String[] columns = headerColumns.toArray(String[]::new); assertEquals(3, columns.length); assertEquals("t1.\"name0\"", columns[0]); @@ -77,7 +74,6 @@ void testGetHeaderColumnsSamePrefixedDimension() { @Test void testGetHeaderColumnsDifferentPrefixedDimension() { - // Given List headers = List.of( new GridHeader("name0"), @@ -89,10 +85,8 @@ void testGetHeaderColumnsDifferentPrefixedDimension() { String sql = "select name0, name1, ax.name2, pe, value, ou from table"; - // When Set headerColumns = EnrollmentQueryHelper.getHeaderColumns(headers, sql); - // Then String[] columns = headerColumns.toArray(String[]::new); assertEquals(3, columns.length); assertEquals("t1.\"name0\"", columns[0]); @@ -102,7 +96,6 @@ void testGetHeaderColumnsDifferentPrefixedDimension() { @Test void testGetOrgUnitLevelColumnsOuMode() { - // Given OrganisationUnit organisationUnit = new OrganisationUnit("OrgTest"); organisationUnit.setPath("/Level1/OrgTest"); @@ -114,10 +107,8 @@ void testGetOrgUnitLevelColumnsOuMode() { ORGUNIT_DIM_ID, ORGANISATION_UNIT, List.of(organisationUnit))) .build(); - // When Set orgUnitColumns = EnrollmentQueryHelper.getOrgUnitLevelColumns(params); - // Then String[] columns = orgUnitColumns.toArray(String[]::new); assertEquals(1, columns.length); assertEquals("uidlevel2", columns[0]); @@ -125,7 +116,6 @@ void testGetOrgUnitLevelColumnsOuMode() { @Test void testGetOrgUnitLevelColumnsOuModeSelected() { - // Given OrganisationUnit organisationUnit = new OrganisationUnit("/Level1/Level2"); EventQueryParams params = @@ -136,16 +126,13 @@ void testGetOrgUnitLevelColumnsOuModeSelected() { ORGUNIT_DIM_ID, ORGANISATION_UNIT, List.of(organisationUnit))) .build(); - // When Set orgUnitColumns = EnrollmentQueryHelper.getOrgUnitLevelColumns(params); - // Then assertEquals(0, orgUnitColumns.size()); } @Test void testGetOrgUnitLevelColumnsOuModeChildren() { - // Given OrganisationUnit organisationUnit = new OrganisationUnit("/Level1/Level2"); EventQueryParams params = @@ -156,16 +143,13 @@ void testGetOrgUnitLevelColumnsOuModeChildren() { ORGUNIT_DIM_ID, ORGANISATION_UNIT, List.of(organisationUnit))) .build(); - // When Set orgUnitColumns = EnrollmentQueryHelper.getOrgUnitLevelColumns(params); - // Then assertEquals(0, orgUnitColumns.size()); } @Test void testGetPeriodColumns() { - // Given Period period = new Period(LAST_3_DAYS); period.setPeriodType(PeriodType.getPeriodTypeFromIsoString("201101")); @@ -174,10 +158,8 @@ void testGetPeriodColumns() { .addDimension(new BaseDimensionalObject(PERIOD_DIM_ID, PERIOD, List.of(period))) .build(); - // When Set periodColumns = EnrollmentQueryHelper.getPeriodColumns(params); - // Then String[] columns = periodColumns.toArray(String[]::new); assertEquals(1, columns.length); assertEquals("t1.Monthly", columns[0]); @@ -185,16 +167,13 @@ void testGetPeriodColumns() { @Test void testGetPeriodColumnsNoPeriods() { - // Given EventQueryParams params = new EventQueryParams.Builder() .addDimension(new BaseDimensionalObject(ORGUNIT_DIM_ID, ORGANISATION_UNIT, List.of())) .build(); - // When Set periodColumns = EnrollmentQueryHelper.getPeriodColumns(params); - // Then assertEquals(0, periodColumns.size()); } } From 75f1e638b8882a803782387fe11d805a458ea065 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Thu, 5 Dec 2024 11:25:16 +0100 Subject: [PATCH 092/110] fix: Update code --- .../table/AbstractEventJdbcTableManager.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java index 9392bfc49806..658627459339 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java @@ -190,7 +190,7 @@ protected void populateTableInternal(AnalyticsTablePartition partition, String f protected List getColumnForAttribute(TrackedEntityAttribute attribute) { List columns = new ArrayList<>(); - String valueColumn = String.format("%s.%s", quote(attribute.getUid()), "value"); + String valueColumn = getValueColumn(attribute); DataType dataType = getColumnType(attribute.getValueType(), isSpatialSupport()); String selectExpression = getColumnExpression(attribute.getValueType(), valueColumn); Skip skipIndex = skipIndex(attribute.getValueType(), attribute.hasOptionSet()); @@ -253,6 +253,16 @@ private List getColumnForOrgUnitAttribute( return columns; } + /** + * Returns the value column with alias. + * + * @param attribute the {@link TrackedEntityAttribute}. + * @return the vlaue column with alias. + */ + private String getValueColumn(TrackedEntityAttribute attribute) { + return String.format("%s.%s", quote(attribute.getUid()), "value"); + } + /** * The select subquery statement. * From c3597a6f8fda2b5c14efed6794789cac517ac0e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Fri, 6 Dec 2024 20:05:55 +0100 Subject: [PATCH 093/110] fix: Update strings --- .../org/hisp/dhis/system/grid/ListGrid.java | 60 ++++++++----------- 1 file changed, 25 insertions(+), 35 deletions(-) diff --git a/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java b/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java index d7266b8c5368..17d4eb7c8e89 100644 --- a/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java +++ b/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java @@ -31,11 +31,6 @@ import static org.hisp.dhis.common.ValueType.getValueTypeFromSqlType; import static org.hisp.dhis.common.collection.CollectionUtils.mapToList; import static org.hisp.dhis.feedback.ErrorCode.E7230; - -import com.fasterxml.jackson.annotation.JsonIgnore; -import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.databind.annotation.JsonSerialize; -import com.google.common.collect.Iterables; import java.io.Serializable; import java.sql.ResultSet; import java.sql.ResultSetMetaData; @@ -52,10 +47,6 @@ import java.util.Set; import java.util.TreeMap; import java.util.regex.Pattern; -import lombok.AccessLevel; -import lombok.Getter; -import net.sf.jasperreports.engine.JRException; -import net.sf.jasperreports.engine.JRField; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.Validate; import org.apache.commons.math3.stat.regression.SimpleRegression; @@ -72,6 +63,14 @@ import org.hisp.dhis.system.util.MathUtils; import org.springframework.jdbc.support.rowset.SqlRowSet; import org.springframework.jdbc.support.rowset.SqlRowSetMetaData; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.google.common.collect.Iterables; +import lombok.AccessLevel; +import lombok.Getter; +import net.sf.jasperreports.engine.JRException; +import net.sf.jasperreports.engine.JRField; /** * @author Lars Helge Overland @@ -502,12 +501,9 @@ public Grid addColumn(List columnValues) { int currentColumnIndex = 0; if (grid.size() != columnValues.size()) { - throw new IllegalStateException( - "Number of column values (" - + columnValues.size() - + ") is not equal to number of rows (" - + grid.size() - + ")"); + throw new IllegalStateException(String.format( + "Number of column values (%d) is not equal to number of rows (%d)", + columnValues.size(), grid.size())); } for (int i = 0; i < grid.size(); i++) { @@ -525,12 +521,9 @@ public Grid addColumn(int columnIndex, List columnValues) { int currentColumnIndex = 0; if (grid.size() != columnValues.size()) { - throw new IllegalStateException( - "Number of column values (" - + columnValues.size() - + ") is not equal to number of rows (" - + grid.size() - + ")"); + throw new IllegalStateException(String.format( + "Number of column values (%d) is not equal to number of rows (%d)", + columnValues.size(), grid.size())); } for (int i = 0; i < grid.size(); i++) { @@ -657,7 +650,7 @@ public Grid limitGrid(int limit) { public Grid limitGrid(int startPos, int endPos) { if (startPos < 0 || endPos < startPos || endPos > getHeight()) { throw new IllegalStateException( - "Illegal start / end pos: " + startPos + ", " + endPos + ", " + getHeight()); + "Illegal start or end pos: " + startPos + ", " + endPos + ", " + getHeight()); } grid = grid.subList(startPos, endPos); @@ -806,16 +799,15 @@ public Grid substituteMetaData(Map metaDataMap) { GridHeader header = headers.get(colIndex); // Header - + Object headerMetaName = metaDataMap.get(header.getName()); if (headerMetaName != null) { header.setName(String.valueOf(headerMetaName)); } + // Column cells if (header.isMeta()) { - // Column cells - substituteMetaData(colIndex, colIndex, metaDataMap); } } @@ -1101,7 +1093,8 @@ public void repositionColumns(List columnIndexes) { row.addAll(orderedValues); } - // reposition columns in the row context structure + // Reposition columns in the row context structure + Map> orderedRowContext = new HashMap<>(); for (Map.Entry> rowContextEntry : rowContext.entrySet()) { @@ -1113,7 +1106,8 @@ public void repositionColumns(List columnIndexes) { .forEach( key -> { if (numberRegex.matcher(key).matches()) { - // reindexing of columns + // Reindexing of columns + orderedRowContextItems.put( columnIndexes.get(Integer.parseInt(key)).toString(), ctxItem.get(key)); } @@ -1150,13 +1144,9 @@ private void verifyGridState() { for (List row : grid) { if (rowLength != null && rowLength != row.size()) { - throw new IllegalStateException( - "Grid rows do not have the same number of cells, previous: " - + rowLength - + ", this: " - + row.size() - + ", at row: " - + rowPos); + throw new IllegalStateException(String.format( + "Grid rows do not have the same number of cells, previous: %d, this: %d, at row: %d", + rowLength, row.size(), rowPos)); } rowPos++; @@ -1177,7 +1167,7 @@ private void updateColumnIndexMap() { } // ------------------------------------------------------------------------- - // toString + // ToString // ------------------------------------------------------------------------- @Override From bec56ad0d0c7e878435439ca1c864dbae616d59f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 10 Dec 2024 17:59:49 +0100 Subject: [PATCH 094/110] fix: Update code --- .../table/AbstractEventJdbcTableManager.java | 46 ++++++++----------- .../JdbcEnrollmentAnalyticsTableManager.java | 3 +- .../table/JdbcEventAnalyticsTableManager.java | 12 +++-- 3 files changed, 29 insertions(+), 32 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java index 658627459339..c6eb428842c1 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java @@ -28,11 +28,9 @@ package org.hisp.dhis.analytics.table; import static org.hisp.dhis.analytics.table.model.Skip.SKIP; -import static org.hisp.dhis.analytics.util.AnalyticsUtils.getClosingParentheses; import static org.hisp.dhis.analytics.util.AnalyticsUtils.getColumnType; import static org.hisp.dhis.db.model.DataType.GEOMETRY; import static org.hisp.dhis.db.model.DataType.TEXT; - import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -50,6 +48,7 @@ import org.hisp.dhis.common.IdentifiableObjectManager; import org.hisp.dhis.common.ValueType; import org.hisp.dhis.dataapproval.DataApprovalLevelService; +import org.hisp.dhis.dataelement.DataElement; import org.hisp.dhis.db.model.DataType; import org.hisp.dhis.db.model.IndexType; import org.hisp.dhis.db.sql.SqlBuilder; @@ -222,31 +221,23 @@ private List getColumnForOrgUnitAttribute( Validate.isTrue(attribute.getValueType().isOrganisationUnit()); List columns = new ArrayList<>(); - String fromClause = - qualifyVariables("from ${organisationunit} ou where ou.uid = (select value"); - if (isSpatialSupport()) { - String selectExpression = "ou.geometry " + fromClause; - String ouGeoSql = getSelectSubquery(attribute, selectExpression); columns.add( AnalyticsTableColumn.builder() .name((attribute.getUid() + OU_GEOMETRY_COL_SUFFIX)) .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(GEOMETRY) - .selectExpression(ouGeoSql) + .selectExpression(getOrgUnitSelectSubquery(attribute, "geometry")) .indexType(IndexType.GIST) .build()); } - String selectExpression = "ou.name " + fromClause; - String ouNameSql = getSelectSubquery(attribute, selectExpression); - columns.add( AnalyticsTableColumn.builder() .name((attribute.getUid() + OU_NAME_COL_SUFFIX)) .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(TEXT) - .selectExpression(ouNameSql) + .selectExpression(getOrgUnitSelectSubquery(attribute, "name")) .skipIndex(SKIP) .build()); @@ -264,24 +255,27 @@ private String getValueColumn(TrackedEntityAttribute attribute) { } /** - * The select subquery statement. + * Returns a org unit select query. * - * @param attribute the {@link TrackedEntityAttribute}. - * @param selectExpression the select expression. - * @return a select statement. + * @param column the column name. + * @param dataElement the {@link DataElement}. + * @return an org unit select query. */ - private String getSelectSubquery(TrackedEntityAttribute attribute, String selectExpression) { - return replaceQualify( + private String getOrgUnitSelectSubquery(TrackedEntityAttribute attribute, String column) { + String format = """ - (select ${selectExpression} from ${trackedentityattributevalue} \ - where trackedentityid=en.trackedentityid \ - and trackedentityattributeid=${attributeId})\ - ${closingParentheses} as ${attributeUid}""", + (select ou.${column} from ${organisationunit} ou \ + where ou.uid = ${columnExpression}) as ${alias}"""; + String valueColumn = getValueColumn(attribute); + String columnExpression = getColumnExpression(attribute.getValueType(), valueColumn); + String alias = quote(attribute.getUid()); + + return replaceQualify( + format, Map.of( - "selectExpression", selectExpression, - "attributeId", String.valueOf(attribute.getId()), - "closingParentheses", getClosingParentheses(selectExpression), - "attributeUid", quote(attribute.getUid()))); + "column", column, + "columnExpression", columnExpression, + "alias", alias)); } /** diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java index 172745e793d3..5ac39cc8f5b7 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java @@ -34,6 +34,7 @@ import java.util.Date; import java.util.List; import java.util.Map; + import org.hisp.dhis.analytics.AnalyticsTableHookService; import org.hisp.dhis.analytics.AnalyticsTableType; import org.hisp.dhis.analytics.AnalyticsTableUpdateParams; @@ -150,7 +151,7 @@ left join analytics_rs_dateperiodstructure dps on cast(en.enrollmentdate as date left join analytics_rs_orgunitstructure ous on en.organisationunitid=ous.organisationunitid \ left join analytics_rs_organisationunitgroupsetstructure ougs on en.organisationunitid=ougs.organisationunitid \ ${attributeJoinClause}\ - where pr.programid=${programId} \ + where pr.programid = ${programId} \ and en.organisationunitid is not null \ and (ougs.startdate is null or dps.monthstartdate=ougs.startdate) \ and en.lastupdated <= '${startTime}' \ diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 94c8a2805487..b545b7d893ed 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -50,7 +50,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; -import lombok.extern.slf4j.Slf4j; + import org.hisp.dhis.analytics.AnalyticsTableHookService; import org.hisp.dhis.analytics.AnalyticsTableType; import org.hisp.dhis.analytics.AnalyticsTableUpdateParams; @@ -89,6 +89,8 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; +import lombok.extern.slf4j.Slf4j; + /** * @author Lars Helge Overland */ @@ -532,7 +534,7 @@ private List getColumnForOrgUnitDataElement(DataElement da .name((dataElement.getUid() + OU_GEOMETRY_COL_SUFFIX)) .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(GEOMETRY) - .selectExpression(getOrgUnitSelectSubquery("geometry", dataElement)) + .selectExpression(getOrgUnitSelectSubquery(dataElement, "geometry")) .indexType(IndexType.GIST) .build()); } @@ -542,7 +544,7 @@ private List getColumnForOrgUnitDataElement(DataElement da .name((dataElement.getUid() + OU_NAME_COL_SUFFIX)) .dimensionType(AnalyticsDimensionType.DYNAMIC) .dataType(TEXT) - .selectExpression(getOrgUnitSelectSubquery("name", dataElement)) + .selectExpression(getOrgUnitSelectSubquery(dataElement, "name")) .skipIndex(SKIP) .build()); @@ -552,11 +554,11 @@ private List getColumnForOrgUnitDataElement(DataElement da /** * Returns a org unit select query. * - * @param column the column name. * @param dataElement the {@link DataElement}. + * @param column the column name. * @return an org unit select query. */ - private String getOrgUnitSelectSubquery(String column, DataElement dataElement) { + private String getOrgUnitSelectSubquery(DataElement dataElement, String column) { String format = """ (select ou.${column} from ${organisationunit} ou \ From c4b677519cdbdf1cc9e4bd64424e5697af6d76a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 10 Dec 2024 17:59:55 +0100 Subject: [PATCH 095/110] fix: Update code --- .../table/AbstractEventJdbcTableManager.java | 1 + .../JdbcEnrollmentAnalyticsTableManager.java | 1 - .../table/JdbcEventAnalyticsTableManager.java | 4 +- .../org/hisp/dhis/system/grid/ListGrid.java | 44 ++++++++++--------- 4 files changed, 26 insertions(+), 24 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java index c6eb428842c1..6dc4d8c43b02 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java @@ -31,6 +31,7 @@ import static org.hisp.dhis.analytics.util.AnalyticsUtils.getColumnType; import static org.hisp.dhis.db.model.DataType.GEOMETRY; import static org.hisp.dhis.db.model.DataType.TEXT; + import java.util.ArrayList; import java.util.List; import java.util.Map; diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java index 5ac39cc8f5b7..a57db9c8e68c 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java @@ -34,7 +34,6 @@ import java.util.Date; import java.util.List; import java.util.Map; - import org.hisp.dhis.analytics.AnalyticsTableHookService; import org.hisp.dhis.analytics.AnalyticsTableType; import org.hisp.dhis.analytics.AnalyticsTableUpdateParams; diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index b545b7d893ed..8b66f85e24ea 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -50,7 +50,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; - +import lombok.extern.slf4j.Slf4j; import org.hisp.dhis.analytics.AnalyticsTableHookService; import org.hisp.dhis.analytics.AnalyticsTableType; import org.hisp.dhis.analytics.AnalyticsTableUpdateParams; @@ -89,8 +89,6 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; -import lombok.extern.slf4j.Slf4j; - /** * @author Lars Helge Overland */ diff --git a/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java b/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java index 17d4eb7c8e89..f6858bf40692 100644 --- a/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java +++ b/dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java @@ -31,6 +31,11 @@ import static org.hisp.dhis.common.ValueType.getValueTypeFromSqlType; import static org.hisp.dhis.common.collection.CollectionUtils.mapToList; import static org.hisp.dhis.feedback.ErrorCode.E7230; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.google.common.collect.Iterables; import java.io.Serializable; import java.sql.ResultSet; import java.sql.ResultSetMetaData; @@ -47,6 +52,10 @@ import java.util.Set; import java.util.TreeMap; import java.util.regex.Pattern; +import lombok.AccessLevel; +import lombok.Getter; +import net.sf.jasperreports.engine.JRException; +import net.sf.jasperreports.engine.JRField; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.Validate; import org.apache.commons.math3.stat.regression.SimpleRegression; @@ -63,14 +72,6 @@ import org.hisp.dhis.system.util.MathUtils; import org.springframework.jdbc.support.rowset.SqlRowSet; import org.springframework.jdbc.support.rowset.SqlRowSetMetaData; -import com.fasterxml.jackson.annotation.JsonIgnore; -import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.databind.annotation.JsonSerialize; -import com.google.common.collect.Iterables; -import lombok.AccessLevel; -import lombok.Getter; -import net.sf.jasperreports.engine.JRException; -import net.sf.jasperreports.engine.JRField; /** * @author Lars Helge Overland @@ -501,9 +502,10 @@ public Grid addColumn(List columnValues) { int currentColumnIndex = 0; if (grid.size() != columnValues.size()) { - throw new IllegalStateException(String.format( - "Number of column values (%d) is not equal to number of rows (%d)", - columnValues.size(), grid.size())); + throw new IllegalStateException( + String.format( + "Number of column values (%d) is not equal to number of rows (%d)", + columnValues.size(), grid.size())); } for (int i = 0; i < grid.size(); i++) { @@ -521,9 +523,10 @@ public Grid addColumn(int columnIndex, List columnValues) { int currentColumnIndex = 0; if (grid.size() != columnValues.size()) { - throw new IllegalStateException(String.format( - "Number of column values (%d) is not equal to number of rows (%d)", - columnValues.size(), grid.size())); + throw new IllegalStateException( + String.format( + "Number of column values (%d) is not equal to number of rows (%d)", + columnValues.size(), grid.size())); } for (int i = 0; i < grid.size(); i++) { @@ -799,7 +802,7 @@ public Grid substituteMetaData(Map metaDataMap) { GridHeader header = headers.get(colIndex); // Header - + Object headerMetaName = metaDataMap.get(header.getName()); if (headerMetaName != null) { @@ -1094,7 +1097,7 @@ public void repositionColumns(List columnIndexes) { } // Reposition columns in the row context structure - + Map> orderedRowContext = new HashMap<>(); for (Map.Entry> rowContextEntry : rowContext.entrySet()) { @@ -1107,7 +1110,7 @@ public void repositionColumns(List columnIndexes) { key -> { if (numberRegex.matcher(key).matches()) { // Reindexing of columns - + orderedRowContextItems.put( columnIndexes.get(Integer.parseInt(key)).toString(), ctxItem.get(key)); } @@ -1144,9 +1147,10 @@ private void verifyGridState() { for (List row : grid) { if (rowLength != null && rowLength != row.size()) { - throw new IllegalStateException(String.format( - "Grid rows do not have the same number of cells, previous: %d, this: %d, at row: %d", - rowLength, row.size(), rowPos)); + throw new IllegalStateException( + String.format( + "Grid rows do not have the same number of cells, previous: %d, this: %d, at row: %d", + rowLength, row.size(), rowPos)); } rowPos++; From 2bec9de2477aee656c6cffcc7623b796ed74ec44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 10 Dec 2024 18:44:38 +0100 Subject: [PATCH 096/110] fix: Update code --- .../dhis/analytics/table/AbstractEventJdbcTableManager.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java index 6dc4d8c43b02..8a98bd7bb0b0 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java @@ -49,7 +49,6 @@ import org.hisp.dhis.common.IdentifiableObjectManager; import org.hisp.dhis.common.ValueType; import org.hisp.dhis.dataapproval.DataApprovalLevelService; -import org.hisp.dhis.dataelement.DataElement; import org.hisp.dhis.db.model.DataType; import org.hisp.dhis.db.model.IndexType; import org.hisp.dhis.db.sql.SqlBuilder; @@ -258,8 +257,8 @@ private String getValueColumn(TrackedEntityAttribute attribute) { /** * Returns a org unit select query. * + * @param attribute the {@link TrackedEntityAttribute}. * @param column the column name. - * @param dataElement the {@link DataElement}. * @return an org unit select query. */ private String getOrgUnitSelectSubquery(TrackedEntityAttribute attribute, String column) { From b76b93048395946e053197caed229bd6b5681aa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 10 Dec 2024 19:49:21 +0100 Subject: [PATCH 097/110] fix: Update code --- .../table/JdbcEventAnalyticsTableManagerTest.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java index 9a9db09b4f99..11198d1e70f3 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java @@ -646,13 +646,18 @@ void verifyTeiTypeOrgUnitFetchesOuNameWhenPopulatingEventAnalyticsTable() { String ouNameQuery = String.format( """ - (select ou.name from "organisationunit" ou where ou.uid = \ - (select value from "trackedentityattributevalue" where trackedentityid=en.trackedentityid and \ - trackedentityattributeid=9999)) as %s""", + (select ou.name from "organisationunit" ou where ou.uid = %1$s.value) as %1$s""", + quote(tea.getUid())); + + String ouGeometryQuery = + String.format( + """ + (select ou.geometry from "organisationunit" ou where ou.uid = %1$s.value) as %1$s""", quote(tea.getUid())); assertThat(sql.getValue(), containsString(ouUidQuery)); assertThat(sql.getValue(), containsString(ouNameQuery)); + assertThat(sql.getValue(), containsString(ouGeometryQuery)); } @Test From 70fbb1f3472ded6ec04c86cfbc545d6597a0003b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 10 Dec 2024 19:53:34 +0100 Subject: [PATCH 098/110] fix: Update code --- .../JdbcEventAnalyticsTableManagerTest.java | 49 ++++++++++--------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java index 11198d1e70f3..15e1eb3b9a6d 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManagerTest.java @@ -501,11 +501,17 @@ void verifyGetTableWithTrackedEntityAttribute() { eventdatavalues #>> '{deabcdefghZ, value}' as "deabcdefghZ\""""; String aliasTeaUid = "%s.value"; - String aliasTea1 = - """ - (select %s from "organisationunit" ou where ou.uid = \ - (select value from "trackedentityattributevalue" where trackedentityid=en.trackedentityid \ - and trackedentityattributeid=%d)) as "%s\""""; + String ouGeometryQuery = + String.format( + """ + (select ou.geometry from "organisationunit" ou where ou.uid = %1$s.value) as %1$s""", + quote(tea1.getUid())); + + String ouNameQuery = + String.format( + """ + (select ou.name from "organisationunit" ou where ou.uid = %1$s.value) as %1$s""", + quote(tea1.getUid())); AnalyticsTableUpdateParams params = AnalyticsTableUpdateParams.newBuilder() @@ -535,17 +541,9 @@ void verifyGetTableWithTrackedEntityAttribute() { .addColumn(d1.getUid(), TEXT, toSelectExpression(aliasD1, d1.getUid()), Skip.SKIP) .addColumn(tea1.getUid(), TEXT, String.format(aliasTeaUid, quote(tea1.getUid()))) // Org unit geometry column - .addColumn( - tea1.getUid() + "_geom", - GEOMETRY, - String.format(aliasTea1, "ou.geometry", tea1.getId(), tea1.getUid()), - IndexType.GIST) + .addColumn(tea1.getUid() + "_geom", GEOMETRY, ouGeometryQuery, IndexType.GIST) // Org unit name column - .addColumn( - tea1.getUid() + "_name", - TEXT, - String.format(aliasTea1, "ou.name", tea1.getId(), tea1.getUid()), - Skip.SKIP) + .addColumn(tea1.getUid() + "_name", TEXT, ouNameQuery, Skip.SKIP) .withDefaultColumns(EventAnalyticsColumn.getColumns(sqlBuilder)) .build() .verify(); @@ -643,21 +641,21 @@ void verifyTeiTypeOrgUnitFetchesOuNameWhenPopulatingEventAnalyticsTable() { String ouUidQuery = String.format("%s.value", quote(tea.getUid())); - String ouNameQuery = + String ouGeometryQuery = String.format( """ - (select ou.name from "organisationunit" ou where ou.uid = %1$s.value) as %1$s""", + (select ou.geometry from "organisationunit" ou where ou.uid = %1$s.value) as %1$s""", quote(tea.getUid())); - String ouGeometryQuery = + String ouNameQuery = String.format( """ - (select ou.geometry from "organisationunit" ou where ou.uid = %1$s.value) as %1$s""", + (select ou.name from "organisationunit" ou where ou.uid = %1$s.value) as %1$s""", quote(tea.getUid())); assertThat(sql.getValue(), containsString(ouUidQuery)); - assertThat(sql.getValue(), containsString(ouNameQuery)); assertThat(sql.getValue(), containsString(ouGeometryQuery)); + assertThat(sql.getValue(), containsString(ouNameQuery)); } @Test @@ -937,15 +935,20 @@ void verifyTeaTypeOrgUnitFetchesOuNameWhenPopulatingEventAnalyticsTable() { String ouUidQuery = String.format("%s.value", quote(tea.getUid())); + String ouGeometryQuery = + String.format( + """ + (select ou.geometry from "organisationunit" ou where ou.uid = %1$s.value) as %1$s""", + quote(tea.getUid())); + String ouNameQuery = String.format( """ - (select ou.name from "organisationunit" ou where ou.uid = \ - (select value from "trackedentityattributevalue" where trackedentityid=en.trackedentityid \ - and trackedentityattributeid=9999)) as %s""", + (select ou.name from "organisationunit" ou where ou.uid = %1$s.value) as %1$s""", quote(tea.getUid())); assertThat(sql.getValue(), containsString(ouUidQuery)); + assertThat(sql.getValue(), containsString(ouGeometryQuery)); assertThat(sql.getValue(), containsString(ouNameQuery)); } From 9c4143c8ad9730e20b609f3d8ddc47401938d030 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 10 Dec 2024 20:04:45 +0100 Subject: [PATCH 099/110] fix: Update code --- .../dhis/analytics/table/JdbcEventAnalyticsTableManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 8b66f85e24ea..bde55a83d977 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -346,7 +346,7 @@ left join analytics_rs_dateperiodstructure dps on cast(${eventDateExpression} as inner join analytics_rs_categorystructure acs on ev.attributeoptioncomboid=acs.categoryoptioncomboid \ ${attributeJoinClause}\ where ev.lastupdated < '${startTime}' ${partitionClause} \ - and pr.programid=${programId} \ + and pr.programid = ${programId} \ and ev.organisationunitid is not null \ and (${eventDateExpression}) is not null \ and (ougs.startdate is null or dps.monthstartdate=ougs.startdate) \ From 8747d39fc2b41ccb0a1ba77bc679e58136faafed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 10 Dec 2024 22:18:33 +0100 Subject: [PATCH 100/110] fix: Update code --- ...EntityEventsAnalyticsTableManagerTest.java | 76 +++++++++---------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManagerTest.java index bcc61a915cad..4cb4bdf9f195 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManagerTest.java @@ -114,7 +114,7 @@ void testTableName() { } @Test - void testPopulate() { + void testPopulateTable() { TrackedEntityType tet = mock(TrackedEntityType.class); when(tet.getUid()).thenReturn("tetUid"); when(trackedEntityTypeService.getAllTrackedEntityType()).thenReturn(List.of(tet)); @@ -135,46 +135,46 @@ void testPopulate() { String subQuery = """ - (select json_object_agg(l2.keys, l2.datavalue) as value - from ( - select l1.uid, - l1.keys, - json_strip_nulls(json_build_object( - 'value', l1.eventdatavalues -> l1.keys ->> 'value', - 'created', l1.eventdatavalues -> l1.keys ->> 'created', - 'storedBy', l1.eventdatavalues -> l1.keys ->> 'storedBy', - 'lastUpdated', l1.eventdatavalues -> l1.keys ->> 'lastUpdated', - 'providedElsewhere', l1.eventdatavalues -> l1.keys -> 'providedElsewhere', - 'value_name', (select ou.name - from organisationunit ou - where ou.uid = l1.eventdatavalues -> l1.keys ->> 'value'), - 'value_code', (select ou.code - from organisationunit ou - where ou.uid = l1.eventdatavalues -> l1.keys ->> 'value'))) as datavalue - from (select inner_evt.*, jsonb_object_keys(inner_evt.eventdatavalues) keys - from event inner_evt) as l1) as l2 - where l2.uid = ev.uid - group by l2.uid)::jsonb - """; + (select json_object_agg(l2.keys, l2.datavalue) as value + from ( + select l1.uid, + l1.keys, + json_strip_nulls(json_build_object( + 'value', l1.eventdatavalues -> l1.keys ->> 'value', + 'created', l1.eventdatavalues -> l1.keys ->> 'created', + 'storedBy', l1.eventdatavalues -> l1.keys ->> 'storedBy', + 'lastUpdated', l1.eventdatavalues -> l1.keys ->> 'lastUpdated', + 'providedElsewhere', l1.eventdatavalues -> l1.keys -> 'providedElsewhere', + 'value_name', (select ou.name + from organisationunit ou + where ou.uid = l1.eventdatavalues -> l1.keys ->> 'value'), + 'value_code', (select ou.code + from organisationunit ou + where ou.uid = l1.eventdatavalues -> l1.keys ->> 'value'))) as datavalue + from (select inner_evt.*, jsonb_object_keys(inner_evt.eventdatavalues) keys + from event inner_evt) as l1) as l2 + where l2.uid = ev.uid + group by l2.uid)::jsonb + """; String expectedSql = """ - insert into analytics_te_event_tetuid_temp ("trackedentity","program","enrollment","programstage","event","occurreddate","lastupdated","created", - "scheduleddate","status","uidlevel1","uidlevel2","uidlevel3","uidlevel4","ou","ouname","oucode","oulevel","eventdatavalues","eventgeometry", - "evlongitude","evlatitude","ounamehierarchy") select distinct te.uid,p.uid,en.uid,ps.uid,ev.uid,ev.occurreddate,ev.lastupdated, - ev.created,ev.scheduleddate,ev.status,ous.uidlevel1,ous.uidlevel2,ous.uidlevel3,ous.uidlevel4,ous.organisationunituid,ous.name,ous.code,ous.level, - %s, - ev.geometry,case when 'POINT' = GeometryType(ev.geometry) then ST_X(ev.geometry) end,case when 'POINT' = GeometryType(ev.geometry) then ST_Y(ev.geometry) end,concat_ws(' / ',) as ounamehierarchy - from "event" ev inner join "enrollment" en on en.enrollmentid=ev.enrollmentid - and en.deleted = false inner join "trackedentity" te on te.trackedentityid=en.trackedentityid - and te.deleted = false and te.trackedentitytypeid = 0 - and te.lastupdated < '2019-08-01T00:00:00' left join "programstage" ps on ev.programstageid=ps.programstageid - left join "program" p on ps.programid=p.programid - left join analytics_rs_orgunitstructure ous on ev.organisationunitid=ous.organisationunitid - where ev.status in ('COMPLETED','ACTIVE','SCHEDULE') - and (CASE WHEN 'SCHEDULE' = ev.status THEN ev.scheduleddate ELSE ev.occurreddate END) >= 'null' - and (CASE WHEN 'SCHEDULE' = ev.status THEN ev.scheduleddate ELSE ev.occurreddate END) < 'null' - and ev.deleted = false""" + insert into analytics_te_event_tetuid_temp ("trackedentity","program","enrollment","programstage","event","occurreddate","lastupdated","created", + "scheduleddate","status","uidlevel1","uidlevel2","uidlevel3","uidlevel4","ou","ouname","oucode","oulevel","eventdatavalues","eventgeometry", + "evlongitude","evlatitude","ounamehierarchy") select distinct te.uid,p.uid,en.uid,ps.uid,ev.uid,ev.occurreddate,ev.lastupdated, + ev.created,ev.scheduleddate,ev.status,ous.uidlevel1,ous.uidlevel2,ous.uidlevel3,ous.uidlevel4,ous.organisationunituid,ous.name,ous.code,ous.level, + %s, + ev.geometry,case when 'POINT' = GeometryType(ev.geometry) then ST_X(ev.geometry) end,case when 'POINT' = GeometryType(ev.geometry) then ST_Y(ev.geometry) end,concat_ws(' / ',) as ounamehierarchy + from "event" ev inner join "enrollment" en on en.enrollmentid=ev.enrollmentid + and en.deleted = false inner join "trackedentity" te on te.trackedentityid=en.trackedentityid + and te.deleted = false and te.trackedentitytypeid = 0 + and te.lastupdated < '2019-08-01T00:00:00' left join "programstage" ps on ev.programstageid=ps.programstageid + left join "program" p on ps.programid=p.programid + left join analytics_rs_orgunitstructure ous on ev.organisationunitid=ous.organisationunitid + where ev.status in ('COMPLETED','ACTIVE','SCHEDULE') + and (CASE WHEN 'SCHEDULE' = ev.status THEN ev.scheduleddate ELSE ev.occurreddate END) >= 'null' + and (CASE WHEN 'SCHEDULE' = ev.status THEN ev.scheduleddate ELSE ev.occurreddate END) < 'null' + and ev.deleted = false""" .formatted(subQuery); ArgumentCaptor sqlCaptor = ArgumentCaptor.forClass(String.class); From 311bb4f2dd826b0590dca046b9bf2b7142ba38a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 10 Dec 2024 22:19:43 +0100 Subject: [PATCH 101/110] fix: Update code --- .../dhis/analytics/table/AnalyticsTableServiceTest.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/AnalyticsTableServiceTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/AnalyticsTableServiceTest.java index 91389524f5de..3322599ee643 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/AnalyticsTableServiceTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/AnalyticsTableServiceTest.java @@ -30,7 +30,6 @@ import static org.hisp.dhis.db.model.DataType.DOUBLE; import static org.hisp.dhis.db.model.DataType.TEXT; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.Mockito.lenient; import static org.mockito.Mockito.when; import java.util.List; @@ -43,7 +42,6 @@ import org.hisp.dhis.setting.SystemSettings; import org.hisp.dhis.setting.SystemSettingsProvider; import org.joda.time.DateTime; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -63,11 +61,6 @@ class AnalyticsTableServiceTest { @InjectMocks private DefaultAnalyticsTableService tableService; - @BeforeEach - void setUp() { - lenient().when(settingsProvider.getCurrentSettings()).thenReturn(settings); - } - @Test void testGetTablePartitions() { when(sqlBuilder.supportsDeclarativePartitioning()).thenReturn(false); @@ -104,6 +97,7 @@ void testGetTablePartitions() { @Test void testGetParallelJobsA() { + when(settingsProvider.getCurrentSettings()).thenReturn(settings); when(settings.getParallelJobsInAnalyticsTableExport()).thenReturn(1); assertEquals(1, tableService.getParallelJobs()); @@ -111,6 +105,7 @@ void testGetParallelJobsA() { @Test void testGetParallelJobsB() { + when(settingsProvider.getCurrentSettings()).thenReturn(settings); when(settings.getParallelJobsInAnalyticsTableExport()).thenReturn(-1); when(settings.getDatabaseServerCpus()).thenReturn(8); From 62eb6645daf4a9af28790fbd369394450cbf8488 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 10 Dec 2024 22:30:06 +0100 Subject: [PATCH 102/110] fix: Update code --- .../table/JdbcAnalyticsTableManagerTest.java | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcAnalyticsTableManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcAnalyticsTableManagerTest.java index 76a6eb4bd460..7c968b08511a 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcAnalyticsTableManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcAnalyticsTableManagerTest.java @@ -35,7 +35,6 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -68,6 +67,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentMatchers; +import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.Spy; @@ -82,38 +82,38 @@ @MockitoSettings(strictness = Strictness.LENIENT) @ExtendWith(MockitoExtension.class) class JdbcAnalyticsTableManagerTest { + @Mock private IdentifiableObjectManager idObjectManager; + + @Mock private OrganisationUnitService organisationUnitService; + + @Mock private CategoryService categoryService; + @Mock private SystemSettingsProvider settingsProvider; @Mock private SystemSettings settings; + @Mock private DataApprovalLevelService dataApprovalLevelService; + + @Mock private ResourceTableService resourceTableService; + + @Mock private AnalyticsTableHookService analyticsTableHookService; + + @Mock private PartitionManager partitionManager; + @Mock private JdbcTemplate jdbcTemplate; @Mock private AnalyticsTableSettings analyticsTableSettings; @Mock private PeriodDataProvider periodDataProvider; - @Spy private final SqlBuilder sqlBuilder = new PostgreSqlBuilder(); + @Spy private SqlBuilder sqlBuilder = new PostgreSqlBuilder(); - private JdbcAnalyticsTableManager subject; + @InjectMocks private JdbcAnalyticsTableManager subject; @BeforeEach public void setUp() { when(settingsProvider.getCurrentSettings()).thenReturn(settings); when(settings.getLastSuccessfulResourceTablesUpdate()).thenReturn(new Date(0L)); - subject = - new JdbcAnalyticsTableManager( - mock(IdentifiableObjectManager.class), - mock(OrganisationUnitService.class), - mock(CategoryService.class), - settingsProvider, - mock(DataApprovalLevelService.class), - mock(ResourceTableService.class), - mock(AnalyticsTableHookService.class), - mock(PartitionManager.class), - jdbcTemplate, - analyticsTableSettings, - periodDataProvider, - sqlBuilder); } @Test From 6a4c3b196e9b5c065486cbf8cbe50cf566a8f0b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 10 Dec 2024 22:32:33 +0100 Subject: [PATCH 103/110] fix: Update code --- ...bcEnrollmentAnalyticsTableManagerTest.java | 36 ++++++++++--------- .../analytics/table/PartitionUtilsTest.java | 10 +++--- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManagerTest.java index f2f2d642cf42..81f150f5c395 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManagerTest.java @@ -68,7 +68,9 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.Spy; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.jdbc.core.JdbcTemplate; @@ -79,15 +81,31 @@ class JdbcEnrollmentAnalyticsTableManagerTest { @Mock private IdentifiableObjectManager idObjectManager; + @Mock private OrganisationUnitService organisationUnitService; + + @Mock private CategoryService categoryService; + + @Mock private SystemSettingsProvider settingsProvider; + + @Mock private SystemSettings settings; + + @Mock private DataApprovalLevelService dataApprovalLevelService; + + @Mock private ResourceTableService resourceTableService; + + @Mock private AnalyticsTableHookService analyticsTableHookService; + + @Mock private PartitionManager partitionManager; + @Mock private JdbcTemplate jdbcTemplate; @Mock private AnalyticsTableSettings analyticsTableSettings; @Mock private PeriodDataProvider periodDataProvider; - private final SqlBuilder sqlBuilder = new PostgreSqlBuilder(); + @Spy private SqlBuilder sqlBuilder = new PostgreSqlBuilder(); - private JdbcEnrollmentAnalyticsTableManager subject; + @InjectMocks private JdbcEnrollmentAnalyticsTableManager subject; private static final Date START_TIME = new DateTime(2019, 8, 1, 0, 0).toDate(); @@ -96,20 +114,6 @@ public void setUp() { when(analyticsTableSettings.isSpatialSupport()).thenReturn(true); SystemSettingsProvider settingsProvider = mock(SystemSettingsProvider.class); lenient().when(settingsProvider.getCurrentSettings()).thenReturn(SystemSettings.of(Map.of())); - subject = - new JdbcEnrollmentAnalyticsTableManager( - idObjectManager, - mock(OrganisationUnitService.class), - mock(CategoryService.class), - settingsProvider, - mock(DataApprovalLevelService.class), - mock(ResourceTableService.class), - mock(AnalyticsTableHookService.class), - mock(PartitionManager.class), - jdbcTemplate, - analyticsTableSettings, - periodDataProvider, - sqlBuilder); } @Test diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/PartitionUtilsTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/PartitionUtilsTest.java index cbd1947a91e8..20265191bce3 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/PartitionUtilsTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/PartitionUtilsTest.java @@ -47,13 +47,11 @@ * @author Lars Helge Overland */ class PartitionUtilsTest { - private PeriodType quarterly = new QuarterlyPeriodType(); + private final PeriodType quarterly = new QuarterlyPeriodType(); - private Period q1 = quarterly.createPeriod(new DateTime(2018, 7, 1, 0, 0).toDate()); - - private Period q2 = quarterly.createPeriod(new DateTime(2018, 10, 1, 0, 0).toDate()); - - private Period q3 = quarterly.createPeriod(new DateTime(2019, 1, 1, 0, 0).toDate()); + private final Period q1 = quarterly.createPeriod(new DateTime(2018, 7, 1, 0, 0).toDate()); + private final Period q2 = quarterly.createPeriod(new DateTime(2018, 10, 1, 0, 0).toDate()); + private final Period q3 = quarterly.createPeriod(new DateTime(2019, 1, 1, 0, 0).toDate()); @Test void testGetPartitions() { From f51f4c3280f62b35649178e7e3864e250a717671 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 10 Dec 2024 23:05:27 +0100 Subject: [PATCH 104/110] fix: Update code --- .../table/JdbcEnrollmentAnalyticsTableManagerTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManagerTest.java index 81f150f5c395..4d7463b8fb6e 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManagerTest.java @@ -36,7 +36,6 @@ import static org.hisp.dhis.test.TestBase.createTrackedEntityAttribute; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.mockito.Mockito.lenient; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -112,7 +111,6 @@ class JdbcEnrollmentAnalyticsTableManagerTest { @BeforeEach public void setUp() { when(analyticsTableSettings.isSpatialSupport()).thenReturn(true); - SystemSettingsProvider settingsProvider = mock(SystemSettingsProvider.class); lenient().when(settingsProvider.getCurrentSettings()).thenReturn(SystemSettings.of(Map.of())); } From 7f9e59d1a0b1d330d7940e0d92b9b17e5b78eb7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 10 Dec 2024 23:07:17 +0100 Subject: [PATCH 105/110] fix: Update code --- .../trackedentity/TrackedEntityQueryRequestValidatorTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/TrackedEntityQueryRequestValidatorTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/TrackedEntityQueryRequestValidatorTest.java index 1e1b37f8629f..4437c074a2cd 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/TrackedEntityQueryRequestValidatorTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/TrackedEntityQueryRequestValidatorTest.java @@ -54,7 +54,7 @@ void testValidateWhenTrackedEntityTypeIsInvalid() { () -> trackedEntityQueryRequestValidator.validate(trackedEntityRequestParams)); assertEquals( - "Invalid UID `" + teiUid + "` for property `trackedEntityType`", exception.getMessage()); + "Invalid UID '" + teiUid + "' for property 'trackedEntityType'", exception.getMessage()); } @Test @@ -69,6 +69,6 @@ void testValidateWhenNoTrackedEntityType() { IllegalQueryException.class, () -> trackedEntityQueryRequestValidator.validate(trackedEntityRequestParams)); - assertEquals("Invalid UID `null` for property `trackedEntityType`", exception.getMessage()); + assertEquals("Invalid UID 'null' for property 'trackedEntityType'", exception.getMessage()); } } From fd72ecc4d9f92cad8212128df1afced6c591ed83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Tue, 10 Dec 2024 23:07:34 +0100 Subject: [PATCH 106/110] fix: Update code --- .../trackedentity/TrackedEntityQueryRequestValidatorTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/TrackedEntityQueryRequestValidatorTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/TrackedEntityQueryRequestValidatorTest.java index 4437c074a2cd..1e1b37f8629f 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/TrackedEntityQueryRequestValidatorTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/TrackedEntityQueryRequestValidatorTest.java @@ -54,7 +54,7 @@ void testValidateWhenTrackedEntityTypeIsInvalid() { () -> trackedEntityQueryRequestValidator.validate(trackedEntityRequestParams)); assertEquals( - "Invalid UID '" + teiUid + "' for property 'trackedEntityType'", exception.getMessage()); + "Invalid UID `" + teiUid + "` for property `trackedEntityType`", exception.getMessage()); } @Test @@ -69,6 +69,6 @@ void testValidateWhenNoTrackedEntityType() { IllegalQueryException.class, () -> trackedEntityQueryRequestValidator.validate(trackedEntityRequestParams)); - assertEquals("Invalid UID 'null' for property 'trackedEntityType'", exception.getMessage()); + assertEquals("Invalid UID `null` for property `trackedEntityType`", exception.getMessage()); } } From a0257a32b3c7fdab8705635febecb15a08b8b186 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 11 Dec 2024 09:34:45 +0100 Subject: [PATCH 107/110] fix: Update code --- ...ntityEnrollmentsAnalyticsTableManager.java | 24 ------------------- ...ckedEntityEventsAnalyticsTableManager.java | 24 ------------------- .../dhis/db/sql/ClickHouseSqlBuilder.java | 5 ++++ .../org/hisp/dhis/db/sql/DorisSqlBuilder.java | 5 ++++ .../hisp/dhis/db/sql/PostgreSqlBuilder.java | 5 ++++ .../java/org/hisp/dhis/db/sql/SqlBuilder.java | 5 ++++ 6 files changed, 20 insertions(+), 48 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEnrollmentsAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEnrollmentsAnalyticsTableManager.java index 70428872148d..b99aa7af1502 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEnrollmentsAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEnrollmentsAnalyticsTableManager.java @@ -115,30 +115,6 @@ public class JdbcTrackedEntityEnrollmentsAnalyticsTableManager extends AbstractJ .dataType(VARCHAR_50) .selectExpression("en.status") .build(), - AnalyticsTableColumn.builder() - .name("uidlevel1") - .dataType(CHARACTER_11) - .nullable(NULL) - .selectExpression("ous.uidlevel1") - .build(), - AnalyticsTableColumn.builder() - .name("uidlevel2") - .dataType(CHARACTER_11) - .nullable(NULL) - .selectExpression("ous.uidlevel2") - .build(), - AnalyticsTableColumn.builder() - .name("uidlevel3") - .dataType(CHARACTER_11) - .nullable(NULL) - .selectExpression("ous.uidlevel3") - .build(), - AnalyticsTableColumn.builder() - .name("uidlevel4") - .dataType(CHARACTER_11) - .nullable(NULL) - .selectExpression("ous.uidlevel4") - .build(), AnalyticsTableColumn.builder() .name("ou") .dataType(CHARACTER_11) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManager.java index a89213becd23..9b96121fff2e 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManager.java @@ -143,30 +143,6 @@ public class JdbcTrackedEntityEventsAnalyticsTableManager extends AbstractJdbcTa .dataType(VARCHAR_50) .selectExpression("ev.status") .build(), - AnalyticsTableColumn.builder() - .name("uidlevel1") - .dataType(CHARACTER_11) - .nullable(NULL) - .selectExpression("ous.uidlevel1") - .build(), - AnalyticsTableColumn.builder() - .name("uidlevel2") - .dataType(CHARACTER_11) - .nullable(NULL) - .selectExpression("ous.uidlevel2") - .build(), - AnalyticsTableColumn.builder() - .name("uidlevel3") - .dataType(CHARACTER_11) - .nullable(NULL) - .selectExpression("ous.uidlevel3") - .build(), - AnalyticsTableColumn.builder() - .name("uidlevel4") - .dataType(CHARACTER_11) - .nullable(NULL) - .selectExpression("ous.uidlevel4") - .build(), AnalyticsTableColumn.builder() .name("ou") .dataType(CHARACTER_11) diff --git a/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/ClickHouseSqlBuilder.java b/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/ClickHouseSqlBuilder.java index f763eace89e7..6381be607075 100644 --- a/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/ClickHouseSqlBuilder.java +++ b/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/ClickHouseSqlBuilder.java @@ -164,6 +164,11 @@ public boolean supportsVacuum() { return false; } + @Override + public boolean supportsCorrelatedSubquery() { + return false; + } + @Override public boolean requiresIndexesForAnalytics() { return false; diff --git a/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/DorisSqlBuilder.java b/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/DorisSqlBuilder.java index e4414841175f..48bfe6bc31b8 100644 --- a/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/DorisSqlBuilder.java +++ b/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/DorisSqlBuilder.java @@ -166,6 +166,11 @@ public boolean supportsVacuum() { return false; } + @Override + public boolean supportsCorrelatedSubquery() { + return true; + } + @Override public boolean requiresIndexesForAnalytics() { return false; diff --git a/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/PostgreSqlBuilder.java b/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/PostgreSqlBuilder.java index f28259ce271b..92a14270f8cc 100644 --- a/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/PostgreSqlBuilder.java +++ b/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/PostgreSqlBuilder.java @@ -185,6 +185,11 @@ public boolean supportsVacuum() { return true; } + @Override + public boolean supportsCorrelatedSubquery() { + return true; + } + @Override public boolean requiresIndexesForAnalytics() { return true; diff --git a/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/SqlBuilder.java b/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/SqlBuilder.java index 78bbd2d866a4..07016f4c6e8d 100644 --- a/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/SqlBuilder.java +++ b/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/SqlBuilder.java @@ -174,6 +174,11 @@ public interface SqlBuilder { */ boolean supportsVacuum(); + /** + * @return true if the DBMS supports corrected subqueries. + */ + boolean supportsCorrelatedSubquery(); + /** * @return true if the DBMS requires indexes for analytics tables for performance. */ From 5ef9f34570cab8d142ec9e28016707735b67e030 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 11 Dec 2024 09:37:59 +0100 Subject: [PATCH 108/110] fix: Update code --- .../JdbcTrackedEntityEnrollmentsAnalyticsTableManager.java | 1 + .../table/JdbcTrackedEntityEventsAnalyticsTableManager.java | 1 + .../table/JdbcTrackedEntityEventsAnalyticsTableManagerTest.java | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEnrollmentsAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEnrollmentsAnalyticsTableManager.java index b99aa7af1502..a00e34eb392b 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEnrollmentsAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEnrollmentsAnalyticsTableManager.java @@ -201,6 +201,7 @@ private List getColumns() { List columns = new ArrayList<>(); columns.addAll(getFixedCols()); columns.add(getOrganisationUnitNameHierarchyColumn()); + columns.addAll(getOrganisationUnitLevelColumns()); if (sqlBuilder.supportsDeclarativePartitioning()) { columns.add(getPartitionColumn()); } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManager.java index 9b96121fff2e..b3968442378b 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManager.java @@ -343,6 +343,7 @@ private List getColumns() { } columns.add(getOrganisationUnitNameHierarchyColumn()); + columns.addAll(getOrganisationUnitLevelColumns()); return columns; } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManagerTest.java index 4cb4bdf9f195..6d1806fed5c2 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManagerTest.java @@ -162,7 +162,7 @@ void testPopulateTable() { insert into analytics_te_event_tetuid_temp ("trackedentity","program","enrollment","programstage","event","occurreddate","lastupdated","created", "scheduleddate","status","uidlevel1","uidlevel2","uidlevel3","uidlevel4","ou","ouname","oucode","oulevel","eventdatavalues","eventgeometry", "evlongitude","evlatitude","ounamehierarchy") select distinct te.uid,p.uid,en.uid,ps.uid,ev.uid,ev.occurreddate,ev.lastupdated, - ev.created,ev.scheduleddate,ev.status,ous.uidlevel1,ous.uidlevel2,ous.uidlevel3,ous.uidlevel4,ous.organisationunituid,ous.name,ous.code,ous.level, + ev.created,ev.scheduleddate,ev.status,ous.organisationunituid,ous.name,ous.code,ous.level, %s, ev.geometry,case when 'POINT' = GeometryType(ev.geometry) then ST_X(ev.geometry) end,case when 'POINT' = GeometryType(ev.geometry) then ST_Y(ev.geometry) end,concat_ws(' / ',) as ounamehierarchy from "event" ev inner join "enrollment" en on en.enrollmentid=ev.enrollmentid From 536a9504deecc24fe85120c3d9675ecc38ff8f1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 11 Dec 2024 09:45:32 +0100 Subject: [PATCH 109/110] fix: Update code --- ...dbcTrackedEntityAnalyticsTableManager.java | 2 +- .../dhis/analytics/util/AnalyticsUtils.java | 30 ------------------- ...EntityEventsAnalyticsTableManagerTest.java | 2 +- .../analytics/util/AnalyticsUtilsTest.java | 8 ----- 4 files changed, 2 insertions(+), 40 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java index 4a483d156d37..e99a46c56562 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java @@ -267,7 +267,7 @@ private String castBasedOnType(ValueType valueType, String columnName) { " cast(${columnName} as ${type})", Map.of("columnName", columnName, "type", sqlBuilder.dataTypeTimestamp())); } - if (valueType.isGeo() && isSpatialSupport() && sqlBuilder.supportsGeospatialData()) { + if (valueType.isGeo() && isSpatialSupport()) { return replace( """ \s ST_GeomFromGeoJSON('{"type":"Point", "coordinates":' || (${columnName}) || ', diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/util/AnalyticsUtils.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/util/AnalyticsUtils.java index cf73c25f349d..b61b7a82bbaa 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/util/AnalyticsUtils.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/util/AnalyticsUtils.java @@ -27,7 +27,6 @@ */ package org.hisp.dhis.analytics.util; -import static org.apache.commons.lang3.StringUtils.EMPTY; import static org.hisp.dhis.common.DataDimensionItem.DATA_DIM_TYPE_CLASS_MAP; import static org.hisp.dhis.common.DimensionalObject.ATTRIBUTEOPTIONCOMBO_DIM_ID; import static org.hisp.dhis.common.DimensionalObject.CATEGORYOPTIONCOMBO_DIM_ID; @@ -1202,33 +1201,4 @@ public static String replaceStringBetween( Matcher matcher = pattern.matcher(original); return matcher.replaceAll(startToken + replacement + endToken); } - - /** - * Returns a string containing closing parenthesis. The number of parenthesis is based on the - * number of missing closing parenthesis in the argument string. - * - *

Example: - * - *

{@code} input: "((( ))" -> output: ")" {@code} - * - * @param str a string. - * @return a String containing 0 or more "closing" parenthesis - */ - public static String getClosingParentheses(String str) { - if (StringUtils.isEmpty(str)) { - return EMPTY; - } - - int open = 0; - - for (int i = 0; i < str.length(); i++) { - if (str.charAt(i) == '(') { - open++; - } else if ((str.charAt(i) == ')') && open >= 1) { - open--; - } - } - - return StringUtils.repeat(")", open); - } } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManagerTest.java index 6d1806fed5c2..57fce6c172ac 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManagerTest.java @@ -160,7 +160,7 @@ void testPopulateTable() { String expectedSql = """ insert into analytics_te_event_tetuid_temp ("trackedentity","program","enrollment","programstage","event","occurreddate","lastupdated","created", - "scheduleddate","status","uidlevel1","uidlevel2","uidlevel3","uidlevel4","ou","ouname","oucode","oulevel","eventdatavalues","eventgeometry", + "scheduleddate","status","ou","ouname","oucode","oulevel","eventdatavalues","eventgeometry", "evlongitude","evlatitude","ounamehierarchy") select distinct te.uid,p.uid,en.uid,ps.uid,ev.uid,ev.occurreddate,ev.lastupdated, ev.created,ev.scheduleddate,ev.status,ous.organisationunituid,ous.name,ous.code,ous.level, %s, diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/util/AnalyticsUtilsTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/util/AnalyticsUtilsTest.java index dbbb563aa2ac..d2b77d2fecbf 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/util/AnalyticsUtilsTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/util/AnalyticsUtilsTest.java @@ -739,14 +739,6 @@ void testGetEnumCaseSensitivity() { assertNull(EnumUtils.getEnum(Database.class, "postgresql")); } - @Test - void testGetClosingParentheses() { - assertEquals("", AnalyticsUtils.getClosingParentheses(null)); - assertEquals("", AnalyticsUtils.getClosingParentheses("")); - assertEquals(")", AnalyticsUtils.getClosingParentheses("from(select(select (*))")); - assertEquals("))", AnalyticsUtils.getClosingParentheses("((")); - } - @Test void whenUncategorizedSQLException_withTableNotExisting_thenThrowException() { SQLException sqlException = new SQLException("relation does not exist", "42P01"); From dcc92066031bb879f388ae11fd530f6b4e0a73f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Wed, 11 Dec 2024 09:49:35 +0100 Subject: [PATCH 110/110] fix: Update code --- ...dbcTrackedEntityAnalyticsTableManager.java | 41 +------------------ 1 file changed, 2 insertions(+), 39 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java index e99a46c56562..5ef9f9a562d4 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java @@ -35,7 +35,6 @@ import static org.hisp.dhis.analytics.util.AnalyticsUtils.getColumnType; import static org.hisp.dhis.analytics.util.DisplayNameUtils.getDisplayName; import static org.hisp.dhis.commons.util.TextUtils.removeLastComma; -import static org.hisp.dhis.commons.util.TextUtils.replace; import static org.hisp.dhis.db.model.DataType.BOOLEAN; import static org.hisp.dhis.db.model.DataType.CHARACTER_11; import static org.hisp.dhis.db.model.DataType.DOUBLE; @@ -65,7 +64,6 @@ import org.hisp.dhis.analytics.table.setting.AnalyticsTableSettings; import org.hisp.dhis.category.CategoryService; import org.hisp.dhis.common.IdentifiableObjectManager; -import org.hisp.dhis.common.ValueType; import org.hisp.dhis.dataapproval.DataApprovalLevelService; import org.hisp.dhis.db.model.IndexType; import org.hisp.dhis.db.model.Logged; @@ -85,7 +83,7 @@ import org.springframework.transaction.annotation.Transactional; @Component("org.hisp.dhis.analytics.TrackedEntityAnalyticsTableManager") -public class JdbcTrackedEntityAnalyticsTableManager extends AbstractJdbcTableManager { +public class JdbcTrackedEntityAnalyticsTableManager extends AbstractEventJdbcTableManager { private static final String PROGRAMS_BY_TET_KEY = "programsByTetUid"; private static final String ALL_NON_CONFIDENTIAL_TET_ATTRIBUTES = @@ -215,7 +213,7 @@ private List getColumns( .name(tea.getUid()) .dataType(getColumnType(tea.getValueType(), isSpatialSupport())) .selectExpression( - castBasedOnType(tea.getValueType(), quote(tea.getUid()) + ".value")) + getColumnExpression(tea.getValueType(), quote(tea.getUid()) + ".value")) .build()) .toList()); @@ -242,41 +240,6 @@ private Stream getAllTrackedEntityAttributes( return getAllTrackedEntityAttributesByEntityType(trackedEntityType); } - /** - * Returns the select clause, potentially with a cast statement, based on the given value type. - * - * @param valueType the value type to represent as database column type. - */ - private String castBasedOnType(ValueType valueType, String columnName) { - if (valueType.isDecimal()) { - - return replace( - " cast(${columnName} as ${type})", - Map.of("columnName", columnName, "type", sqlBuilder.dataTypeDouble())); - } - if (valueType.isInteger()) { - return replace(" cast(${columnName} as bigint)", Map.of("columnName", columnName)); - } - if (valueType.isBoolean()) { - return replace( - " case when ${columnName} = 'true' then 1 when ${columnName} = 'false' then 0 end ", - Map.of("columnName", columnName)); - } - if (valueType.isDate()) { - return replace( - " cast(${columnName} as ${type})", - Map.of("columnName", columnName, "type", sqlBuilder.dataTypeTimestamp())); - } - if (valueType.isGeo() && isSpatialSupport()) { - return replace( - """ - \s ST_GeomFromGeoJSON('{"type":"Point", "coordinates":' || (${columnName}) || ', - "crs":{"type":"name", "properties":{"name":"EPSG:4326"}}}')""", - Map.of("columnName", columnName)); - } - return columnName; - } - /** * Returns all {@link TrackedEntityAttribute} for the given {@link TrackedEntityType} and * programs.