Skip to content

Commit

Permalink
refactor: Remove unnecessary parenthesis check [DHIS2-18417] (#19385)
Browse files Browse the repository at this point in the history
  • Loading branch information
larshelge authored Dec 4, 2024
1 parent 43d4175 commit db805c1
Showing 1 changed file with 26 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -52,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;
Expand Down Expand Up @@ -528,40 +526,53 @@ private List<AnalyticsTableColumn> getColumnForDataElement(
private List<AnalyticsTableColumn> getColumnForOrgUnitDataElement(DataElement dataElement) {
List<AnalyticsTableColumn> columns = new ArrayList<>();

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);

columns.add(
AnalyticsTableColumn.builder()
.name((dataElement.getUid() + OU_GEOMETRY_COL_SUFFIX))
.dimensionType(AnalyticsDimensionType.DYNAMIC)
.dataType(GEOMETRY)
.selectExpression(geoExpression)
.selectExpression(getOrgUnitSelectSubquery("geometry", dataElement))
.indexType(IndexType.GIST)
.build());
}

String fromTypeSql = "ou.name " + fromClause;
String ouNameSql = getOrgUnitSelectExpression(dataElement, fromTypeSql);

columns.add(
AnalyticsTableColumn.builder()
.name((dataElement.getUid() + OU_NAME_COL_SUFFIX))
.dimensionType(AnalyticsDimensionType.DYNAMIC)
.dataType(TEXT)
.selectExpression(ouNameSql)
.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 ${alias}""";
String columnExpression =
sqlBuilder.jsonExtractNested("eventdatavalues", dataElement.getUid(), "value");
String alias = quote(dataElement.getUid());

return replaceQualify(
format,
Map.of(
"column", column,
"columnExpression", columnExpression,
"alias", alias));
}

/**
* Returns columns for attributes of the given program.
*
Expand Down Expand Up @@ -625,24 +636,6 @@ private List<AnalyticsTableColumn> 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());
String prts = getClosingParentheses(selectExpression);
return replaceQualify(
"(select ${selectExpression})${closingParentheses} as ${uid}",
Map.of(
"selectExpression", selectExpression,
"closingParentheses", prts,
"uid", quote(dataElement.getUid())));
}

/**
* Returns a list of columns.
*
Expand Down

0 comments on commit db805c1

Please sign in to comment.