|
30 | 30 | import static java.util.stream.Collectors.toList; |
31 | 31 | import static org.apache.commons.lang3.StringUtils.EMPTY; |
32 | 32 | import static org.hisp.dhis.analytics.table.model.Skip.SKIP; |
33 | | -import static org.hisp.dhis.analytics.util.AnalyticsUtils.getClosingParentheses; |
34 | 33 | import static org.hisp.dhis.analytics.util.AnalyticsUtils.getColumnType; |
35 | 34 | import static org.hisp.dhis.commons.util.TextUtils.emptyIfTrue; |
36 | 35 | import static org.hisp.dhis.commons.util.TextUtils.format; |
|
52 | 51 | import java.util.Map; |
53 | 52 | import java.util.Objects; |
54 | 53 | import lombok.extern.slf4j.Slf4j; |
55 | | -import org.apache.commons.lang3.Validate; |
56 | 54 | import org.hisp.dhis.analytics.AnalyticsTableHookService; |
57 | 55 | import org.hisp.dhis.analytics.AnalyticsTableType; |
58 | 56 | import org.hisp.dhis.analytics.AnalyticsTableUpdateParams; |
@@ -528,40 +526,53 @@ private List<AnalyticsTableColumn> getColumnForDataElement( |
528 | 526 | private List<AnalyticsTableColumn> getColumnForOrgUnitDataElement(DataElement dataElement) { |
529 | 527 | List<AnalyticsTableColumn> columns = new ArrayList<>(); |
530 | 528 |
|
531 | | - String columnExpression = |
532 | | - sqlBuilder.jsonExtractNested("eventdatavalues", dataElement.getUid(), "value"); |
533 | | - String fromClause = |
534 | | - qualifyVariables("from ${organisationunit} ou where ou.uid = " + columnExpression); |
535 | | - |
536 | 529 | if (isSpatialSupport()) { |
537 | | - String fromType = "ou.geometry " + fromClause; |
538 | | - String geoExpression = getOrgUnitSelectExpression(dataElement, fromType); |
539 | | - |
540 | 530 | columns.add( |
541 | 531 | AnalyticsTableColumn.builder() |
542 | 532 | .name((dataElement.getUid() + OU_GEOMETRY_COL_SUFFIX)) |
543 | 533 | .dimensionType(AnalyticsDimensionType.DYNAMIC) |
544 | 534 | .dataType(GEOMETRY) |
545 | | - .selectExpression(geoExpression) |
| 535 | + .selectExpression(getOrgUnitSelectSubquery("geometry", dataElement)) |
546 | 536 | .indexType(IndexType.GIST) |
547 | 537 | .build()); |
548 | 538 | } |
549 | 539 |
|
550 | | - String fromTypeSql = "ou.name " + fromClause; |
551 | | - String ouNameSql = getOrgUnitSelectExpression(dataElement, fromTypeSql); |
552 | | - |
553 | 540 | columns.add( |
554 | 541 | AnalyticsTableColumn.builder() |
555 | 542 | .name((dataElement.getUid() + OU_NAME_COL_SUFFIX)) |
556 | 543 | .dimensionType(AnalyticsDimensionType.DYNAMIC) |
557 | 544 | .dataType(TEXT) |
558 | | - .selectExpression(ouNameSql) |
| 545 | + .selectExpression(getOrgUnitSelectSubquery("name", dataElement)) |
559 | 546 | .skipIndex(SKIP) |
560 | 547 | .build()); |
561 | 548 |
|
562 | 549 | return columns; |
563 | 550 | } |
564 | 551 |
|
| 552 | + /** |
| 553 | + * Returns a org unit select query. |
| 554 | + * |
| 555 | + * @param column the column name. |
| 556 | + * @param dataElement the {@link DataElement}. |
| 557 | + * @return an org unit select query. |
| 558 | + */ |
| 559 | + private String getOrgUnitSelectSubquery(String column, DataElement dataElement) { |
| 560 | + String format = |
| 561 | + """ |
| 562 | + (select ou.${column} from ${organisationunit} ou \ |
| 563 | + where ou.uid = ${columnExpression}) as ${alias}"""; |
| 564 | + String columnExpression = |
| 565 | + sqlBuilder.jsonExtractNested("eventdatavalues", dataElement.getUid(), "value"); |
| 566 | + String alias = quote(dataElement.getUid()); |
| 567 | + |
| 568 | + return replaceQualify( |
| 569 | + format, |
| 570 | + Map.of( |
| 571 | + "column", column, |
| 572 | + "columnExpression", columnExpression, |
| 573 | + "alias", alias)); |
| 574 | + } |
| 575 | + |
565 | 576 | /** |
566 | 577 | * Returns columns for attributes of the given program. |
567 | 578 | * |
@@ -625,24 +636,6 @@ private List<AnalyticsTableColumn> getColumnForAttributeWithLegendSet( |
625 | 636 | .toList(); |
626 | 637 | } |
627 | 638 |
|
628 | | - /** |
629 | | - * Returns a select statement for the given select expression. |
630 | | - * |
631 | | - * @param dataElement the data element to create the select statement for. |
632 | | - * @param selectExpression the select expression. |
633 | | - * @return a select expression. |
634 | | - */ |
635 | | - private String getOrgUnitSelectExpression(DataElement dataElement, String selectExpression) { |
636 | | - Validate.isTrue(dataElement.getValueType().isOrganisationUnit()); |
637 | | - String prts = getClosingParentheses(selectExpression); |
638 | | - return replaceQualify( |
639 | | - "(select ${selectExpression})${closingParentheses} as ${uid}", |
640 | | - Map.of( |
641 | | - "selectExpression", selectExpression, |
642 | | - "closingParentheses", prts, |
643 | | - "uid", quote(dataElement.getUid()))); |
644 | | - } |
645 | | - |
646 | 639 | /** |
647 | 640 | * Returns a list of columns. |
648 | 641 | * |
|
0 commit comments