Skip to content

Commit

Permalink
refactor: Code readability [DHIS2-16705] (#19451)
Browse files Browse the repository at this point in the history
  • Loading branch information
larshelge authored Dec 11, 2024
1 parent 1140322 commit 95d8a45
Showing 1 changed file with 39 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import static java.lang.String.join;
import static java.util.stream.Collectors.groupingBy;
import static org.apache.commons.collections4.CollectionUtils.emptyIfNull;
import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;
import static org.hisp.dhis.analytics.AnalyticsTableType.TRACKED_ENTITY_INSTANCE;
import static org.hisp.dhis.analytics.table.JdbcEventAnalyticsTableManager.EXPORTABLE_EVENT_STATUSES;
import static org.hisp.dhis.analytics.util.AnalyticsUtils.getColumnType;
Expand Down Expand Up @@ -176,25 +177,27 @@ private List<AnalyticsTableColumn> getColumns(
(Map<String, List<Program>>) params.getExtraParam("", PROGRAMS_BY_TET_KEY);

List<AnalyticsTableColumn> columns = new ArrayList<>(getFixedColumns());

String enrolledInProgramExpression =
"""
\s exists(select 1 from ${enrollment} en_0 \
where en_0.trackedentityid = te.trackedentityid \
and en_0.programid = ${programId})""";

emptyIfNull(programsByTetUid.get(trackedEntityType.getUid()))
.forEach(
program ->
columns.add(
AnalyticsTableColumn.builder()
.name(program.getUid())
.dataType(BOOLEAN)
.selectExpression(
replaceQualify(
enrolledInProgramExpression,
Map.of("programId", String.valueOf(program.getId()))))
.build()));
List<Program> programs = programsByTetUid.get(trackedEntityType.getUid());

if (isNotEmpty(programs)) {
String enrolledInProgramExpression =
"""
\s exists(select 1 from ${enrollment} en_0 \
where en_0.trackedentityid = te.trackedentityid \
and en_0.programid = ${programId})""";

programs.forEach(
program ->
columns.add(
AnalyticsTableColumn.builder()
.name(program.getUid())
.dataType(BOOLEAN)
.selectExpression(
replaceQualify(
enrolledInProgramExpression,
Map.of("programId", String.valueOf(program.getId()))))
.build()));
}

List<TrackedEntityAttribute> trackedEntityAttributes =
getAllTrackedEntityAttributes(trackedEntityType, programsByTetUid)
Expand Down Expand Up @@ -271,7 +274,6 @@ private List<AnalyticsTableColumn> getFixedColumns() {
columns.addAll(getOrganisationUnitLevelColumns());
columns.add(getOrganisationUnitNameHierarchyColumn());
columns.addAll(getFixedNonGroupByColumns());

return columns;
}

Expand Down Expand Up @@ -308,18 +310,23 @@ public void populateTable(AnalyticsTableUpdateParams params, AnalyticsTableParti
left join analytics_rs_organisationunitgroupsetstructure ougs on te.organisationunitid=ougs.organisationunitid""",
Map.of()));

((List<TrackedEntityAttribute>)
params.getExtraParam(trackedEntityType.getUid(), ALL_NON_CONFIDENTIAL_TET_ATTRIBUTES))
.forEach(
tea ->
sql.append(
replaceQualify(
"""
\s left join ${trackedentityattributevalue} ${teaUid} on ${teaUid}.trackedentityid=te.trackedentityid \
and ${teaUid}.trackedentityattributeid = ${teaId}""",
Map.of(
"teaUid", quote(tea.getUid()),
"teaId", String.valueOf(tea.getId())))));
List<TrackedEntityAttribute> attributes =
((List<TrackedEntityAttribute>)
params.getExtraParam(trackedEntityType.getUid(), ALL_NON_CONFIDENTIAL_TET_ATTRIBUTES));

if (isNotEmpty(attributes)) {
attributes.forEach(
tea ->
sql.append(
replaceQualify(
"""
\s left join ${trackedentityattributevalue} ${teaUid} on ${teaUid}.trackedentityid=te.trackedentityid \
and ${teaUid}.trackedentityattributeid = ${teaId}""",
Map.of(
"teaUid", quote(tea.getUid()),
"teaId", String.valueOf(tea.getId())))));
}

sql.append(
replaceQualify(
"""
Expand Down

0 comments on commit 95d8a45

Please sign in to comment.