Skip to content

Commit

Permalink
refactor: Rename columnType to dimensionType [DHIS2-16705] (#19296)
Browse files Browse the repository at this point in the history
  • Loading branch information
larshelge authored Nov 26, 2024
1 parent 8c0c69c commit b3b35f6
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -195,6 +196,11 @@ public boolean hasOptionCombos() {
return optionCombos != null && !optionCombos.isEmpty();
}

@JsonIgnore
public List<Category> getDataDimensionCategories() {
return categories.stream().filter(Category::isDataDimension).toList();
}

// -------------------------------------------------------------------------
// Logic
// -------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ public static DimensionalObject linkAssociations(
EventAnalyticalObject eventAnalyticalObject,
DimensionalObject dimensionalObject,
Attribute parent) {
// Associating event repetitions.
List<EventRepetition> repetitions = eventAnalyticalObject.getEventRepetitions();

if (isNotEmpty(repetitions)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -259,7 +259,7 @@ protected List<AnalyticsTableColumn> getTrackedEntityAttributeColumns(Program pr
columns.add(
AnalyticsTableColumn.builder()
.name(attribute.getUid())
.columnType(AnalyticsColumnType.DYNAMIC)
.dimensionType(AnalyticsDimensionType.DYNAMIC)
.dataType(dataType)
.selectExpression(sql)
.skipIndex(skipIndex)
Expand All @@ -272,7 +272,7 @@ protected List<AnalyticsTableColumn> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,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;
Expand Down Expand Up @@ -535,7 +535,7 @@ protected List<AnalyticsTableColumn> 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))
Expand All @@ -552,7 +552,7 @@ protected List<AnalyticsTableColumn> 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))
Expand All @@ -569,7 +569,7 @@ protected List<AnalyticsTableColumn> 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))
Expand All @@ -586,7 +586,7 @@ protected List<AnalyticsTableColumn> 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))
Expand All @@ -603,7 +603,7 @@ protected List<AnalyticsTableColumn> 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))
Expand All @@ -620,7 +620,7 @@ protected List<AnalyticsTableColumn> 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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -398,53 +398,14 @@ private String getPartitionClause(AnalyticsTablePartition partition) {
*/
private List<AnalyticsTableColumn> getColumns(Program program) {
List<AnalyticsTableColumn> columns = new ArrayList<>(fixedColumns);

if (program.hasNonDefaultCategoryCombo()) {
List<Category> categories = program.getCategoryCombo().getCategories();

for (Category category : categories) {
if (category.isDataDimension()) {
columns.add(
AnalyticsTableColumn.builder()
.name(category.getUid())
.columnType(AnalyticsColumnType.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());
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);
Expand All @@ -468,6 +429,52 @@ 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<AnalyticsTableColumn> getAttributeCategoryColumns(Program program) {
if (program.hasNonDefaultCategoryCombo()) {
List<Category> 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.
*
* @param program the {@link Program}.
* @return a list of {@link AnalyticsTableColumn}.
*/
private List<AnalyticsTableColumn> getDataElementColumns(Program program) {
List<AnalyticsTableColumn> 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.
Expand Down Expand Up @@ -495,7 +502,7 @@ private List<AnalyticsTableColumn> getColumnForDataElement(
columns.add(
AnalyticsTableColumn.builder()
.name(dataElement.getUid())
.columnType(AnalyticsColumnType.DYNAMIC)
.dimensionType(AnalyticsDimensionType.DYNAMIC)
.dataType(dataType)
.selectExpression(sql)
.skipIndex(skipIndex)
Expand All @@ -522,7 +529,7 @@ private List<AnalyticsTableColumn> 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)
Expand All @@ -535,7 +542,7 @@ private List<AnalyticsTableColumn> getColumnForOrgUnitDataElement(
columns.add(
AnalyticsTableColumn.builder()
.name((dataElement.getUid() + OU_NAME_COL_SUFFIX))
.columnType(AnalyticsColumnType.DYNAMIC)
.dimensionType(AnalyticsDimensionType.DYNAMIC)
.dataType(TEXT)
.selectExpression(ouNameSql)
.skipIndex(SKIP)
Expand All @@ -544,6 +551,34 @@ private List<AnalyticsTableColumn> getColumnForOrgUnitDataElement(
return columns;
}

/**
* Returns columns for attributes of the given program.
*
* @param program the {@link Program}.
* @return a list of {@link AnalyticsTableColumn}.
*/
private List<AnalyticsTableColumn> getAttributeColumns(Program program) {
List<AnalyticsTableColumn> 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<AnalyticsTableColumn> getColumnForTrackedEntityAttribute(
TrackedEntityAttribute attribute, boolean withLegendSet) {
List<AnalyticsTableColumn> columns = new ArrayList<>();
Expand All @@ -561,7 +596,7 @@ private List<AnalyticsTableColumn> getColumnForTrackedEntityAttribute(
columns.add(
AnalyticsTableColumn.builder()
.name(attribute.getUid())
.columnType(AnalyticsColumnType.DYNAMIC)
.dimensionType(AnalyticsDimensionType.DYNAMIC)
.dataType(dataType)
.selectExpression(sql)
.skipIndex(skipIndex)
Expand Down Expand Up @@ -632,7 +667,7 @@ private List<AnalyticsTableColumn> 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)
Expand All @@ -645,7 +680,7 @@ private List<AnalyticsTableColumn> getColumnsForOrgUnitTrackedEntityAttribute(
columns.add(
AnalyticsTableColumn.builder()
.name((attribute.getUid() + OU_NAME_COL_SUFFIX))
.columnType(AnalyticsColumnType.DYNAMIC)
.dimensionType(AnalyticsDimensionType.DYNAMIC)
.dataType(TEXT)
.selectExpression(ouNameSql)
.skipIndex(SKIP)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public class AnalyticsTableColumn {
@Builder.Default private final List<String> 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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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")
Expand Down

0 comments on commit b3b35f6

Please sign in to comment.