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 ac747204d146..a6f0841dc2c0 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 @@ -165,6 +165,12 @@ left join analytics_rs_dateperiodstructure dps on cast(en.enrollmentdate as date populateTableInternal(partition, fromClause); } + /** + * Returns a list of columns for the given program. + * + * @param program the {@link Program}. + * @return a list of {@link AnalyticsTableColumn}. + */ private List getColumns(Program program) { List columns = new ArrayList<>(); columns.addAll(fixedColumns); 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 0c3e85fe9e0a..3c6f5c905ec3 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 @@ -225,7 +225,7 @@ private List getLatestAnalyticsTables(AnalyticsTableUpdateParams Assert.isTrue( lastFullTableUpdate.getTime() > 0L, - "A full analytics table update process must be run prior to a latest partition update process"); + "A full analytics table update process must be run prior to a latest partition update"); Date startDate = lastFullTableUpdate; Date endDate = params.getStartTime(); @@ -429,31 +429,25 @@ private List getColumns(Program program) { columns.addAll( program.getAnalyticsDataElements().stream() - .map(de -> getColumnFromDataElement(de, false)) + .map(de -> getColumnForDataElement(de, false)) .flatMap(Collection::stream) .toList()); columns.addAll( program.getAnalyticsDataElementsWithLegendSet().stream() - .map(de -> getColumnFromDataElement(de, true)) + .map(de -> getColumnForDataElement(de, true)) .flatMap(Collection::stream) .toList()); columns.addAll( program.getNonConfidentialTrackedEntityAttributes().stream() - .map( - tea -> - getColumnFromTrackedEntityAttribute( - tea, getNumericClause(), getDateClause(), false)) + .map(tea -> getColumnForTrackedEntityAttribute(tea, false)) .flatMap(Collection::stream) .toList()); columns.addAll( program.getNonConfidentialTrackedEntityAttributesWithLegendSet().stream() - .map( - tea -> - getColumnFromTrackedEntityAttribute( - tea, getNumericClause(), getDateClause(), true)) + .map(tea -> getColumnForTrackedEntityAttribute(tea, true)) .flatMap(Collection::stream) .toList()); @@ -488,7 +482,7 @@ protected AnalyticsTableColumn getPartitionColumn() { * @param withLegendSet indicates * @return */ - private List getColumnFromDataElement( + private List getColumnForDataElement( DataElement dataElement, boolean withLegendSet) { List columns = new ArrayList<>(); @@ -496,12 +490,12 @@ private List getColumnFromDataElement( String columnExpression = sqlBuilder.jsonExtractNested("eventdatavalues", dataElement.getUid(), "value"); String selectExpression = getSelectExpression(dataElement.getValueType(), columnExpression); - String dataFilterClause = getDataFilterClause(dataElement.getUid(), dataElement.getValueType()); + String dataFilterClause = getDataFilterClause(dataElement); String sql = getSelectForInsert(dataElement, selectExpression, dataFilterClause); Skip skipIndex = skipIndex(dataElement.getValueType(), dataElement.hasOptionSet()); if (dataElement.getValueType().isOrganisationUnit()) { - columns.addAll(getColumnFromOrgUnitDataElement(dataElement, dataFilterClause)); + columns.addAll(getColumnForOrgUnitDataElement(dataElement, dataFilterClause)); } columns.add( @@ -518,7 +512,7 @@ private List getColumnFromDataElement( : columns; } - private List getColumnFromOrgUnitDataElement( + private List getColumnForOrgUnitDataElement( DataElement dataElement, String dataFilterClause) { List columns = new ArrayList<>(); @@ -556,22 +550,18 @@ private List getColumnFromOrgUnitDataElement( return columns; } - private List getColumnFromTrackedEntityAttribute( - TrackedEntityAttribute attribute, - String numericClause, - String dateClause, - boolean withLegendSet) { + private List getColumnForTrackedEntityAttribute( + TrackedEntityAttribute attribute, boolean withLegendSet) { List columns = new ArrayList<>(); DataType dataType = getColumnType(attribute.getValueType(), isSpatialSupport()); String selectExpression = getSelectExpressionForAttribute(attribute.getValueType(), "value"); - String dataExpression = - attribute.isNumericType() ? numericClause : attribute.isDateType() ? dateClause : ""; + String dataExpression = getDataFilterClause(attribute); String sql = selectForInsert(attribute, selectExpression, dataExpression); Skip skipIndex = skipIndex(attribute.getValueType(), attribute.hasOptionSet()); if (attribute.getValueType().isOrganisationUnit()) { - columns.addAll(getColumnsFromOrgUnitTrackedEntityAttribute(attribute, dataExpression)); + columns.addAll(getColumnsForOrgUnitTrackedEntityAttribute(attribute, dataExpression)); } columns.add( @@ -583,14 +573,19 @@ private List getColumnFromTrackedEntityAttribute( .skipIndex(skipIndex) .build()); - return withLegendSet - ? getColumnFromTrackedEntityAttributeWithLegendSet(attribute, numericClause) - : columns; + return withLegendSet ? getColumnForAttributeWithLegendSet(attribute) : columns; } - private List getColumnFromTrackedEntityAttributeWithLegendSet( - TrackedEntityAttribute attribute, String numericClause) { + /** + * Returns a list of columns based on the given attribute with legend set. + * + * @param attribute the {@link TrackedEntityAttribute}. + * @return a list of {@link AnalyticsTableColumn}. + */ + private List getColumnForAttributeWithLegendSet( + TrackedEntityAttribute attribute) { String selectClause = getSelectExpression(attribute.getValueType(), "value"); + String numericClause = getNumericClause(); String query = """ \s(select l.uid from ${maplegend} l \ @@ -623,7 +618,14 @@ private List getColumnFromTrackedEntityAttributeWithLegend .collect(toList()); } - private List getColumnsFromOrgUnitTrackedEntityAttribute( + /** + * 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 getColumnsForOrgUnitTrackedEntityAttribute( TrackedEntityAttribute attribute, String dataFilterClause) { List columns = new ArrayList<>(); @@ -670,21 +672,20 @@ private String getSelectForInsert( DataElement dataElement, String selectExpression, String dataFilterClause) { String sqlTemplate = dataElement.getValueType().isOrganisationUnit() - ? "(select ${fromType} ${dataClause})${closingParentheses} as ${uid}" - : "(select ${fromType} from ${event} where eventid=ev.eventid ${dataClause})${closingParentheses} as ${uid}"; + ? "(select ${selectExpression} ${dataClause})${closingParentheses} as ${uid}" + : "(select ${selectExpression} from ${event} where eventid=ev.eventid ${dataClause})${closingParentheses} as ${uid}"; - Map variables = + return replaceQualify( + sqlTemplate, Map.of( - "fromType", + "selectExpression", selectExpression, "dataClause", dataFilterClause, "closingParentheses", getClosingParentheses(selectExpression), "uid", - quote(dataElement.getUid())); - - return replaceQualify(sqlTemplate, variables); + quote(dataElement.getUid()))); } /** @@ -731,11 +732,13 @@ private List getColumnFromDataElementWithLegendSet( * 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 uid the identifier. - * @param valueType the {@link ValueType}. - * @return an expression for extracting a data value. + * @param dataElement the {@link DataElement}. + * @return an filter expression. */ - private String getDataFilterClause(String uid, ValueType valueType) { + private String getDataFilterClause(DataElement dataElement) { + String uid = dataElement.getUid(); + ValueType valueType = dataElement.getValueType(); + if (valueType.isNumeric() || valueType.isDate()) { String regex = valueType.isNumeric() ? NUMERIC_LENIENT_REGEXP : DATE_REGEXP; @@ -747,6 +750,20 @@ private String getDataFilterClause(String uid, ValueType valueType) { return EMPTY; } + /** + * 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 an filter expression. + */ + private String getDataFilterClause(TrackedEntityAttribute attribute) { + if (attribute.isNumericType()) { + return getNumericClause(); + } + return attribute.isDateType() ? getDateClause() : EMPTY; + } + /** * Returns a list of years for which data exist. *