Skip to content

Commit

Permalink
refactor: Improved method naming clarity [DHIS2-16705] (#19300)
Browse files Browse the repository at this point in the history
  • Loading branch information
larshelge authored Nov 26, 2024
1 parent a7d2298 commit 9b6d958
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,14 @@ protected void populateTableInternal(AnalyticsTablePartition partition, String f
}

/**
* The select statement used by the table population.
* The select subquery statement.
*
* @param attribute the {@link TrackedEntityAttribute}.
* @param columnExpression the column expression.
* @param dataClause the data type related clause like "NUMERIC".
* @return a select statement.
*/
protected String selectForInsert(
protected String getSelectSubquery(
TrackedEntityAttribute attribute, String columnExpression, String dataClause) {
return replaceQualify(
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ private List<AnalyticsTableColumn> getTrackedEntityAttributeColumns(Program prog

if (attribute.getValueType().isOrganisationUnit()) {
String fromTypeSql = "ou.name from organisationunit ou where ou.uid = (select value";
String ouNameSql = selectForInsert(attribute, fromTypeSql, dataClause);
String ouNameSql = getSelectSubquery(attribute, fromTypeSql, dataClause);

columns.add(
AnalyticsTableColumn.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,12 @@ private List<AnalyticsTableColumn> getColumns(Program program) {
return filterDimensionColumns(columns);
}

protected AnalyticsTableColumn getPartitionColumn() {
/**
* Returns a partition column.
*
* @return an {@link AnalyticsTableColumn}.
*/
private AnalyticsTableColumn getPartitionColumn() {
return AnalyticsTableColumn.builder()
.name("year")
.dataType(INTEGER)
Expand Down Expand Up @@ -513,6 +518,13 @@ private List<AnalyticsTableColumn> getColumnForDataElement(
: columns;
}

/**
* Returns a list of columns.
*
* @param dataElement the {@link DataElement}.
* @param dataFilterClause the data filter SQL clause.
* @return a list of {@link AnalyticsTableColumn}.
*/
private List<AnalyticsTableColumn> getColumnForOrgUnitDataElement(
DataElement dataElement, String dataFilterClause) {
List<AnalyticsTableColumn> columns = new ArrayList<>();
Expand Down Expand Up @@ -561,12 +573,12 @@ private List<AnalyticsTableColumn> getAttributeColumns(Program program) {
List<AnalyticsTableColumn> columns = new ArrayList<>();
columns.addAll(
program.getNonConfidentialTrackedEntityAttributes().stream()
.map(tea -> getColumnForTrackedEntityAttribute(tea, false))
.map(this::getColumnForAttribute)
.flatMap(Collection::stream)
.toList());
columns.addAll(
program.getNonConfidentialTrackedEntityAttributesWithLegendSet().stream()
.map(tea -> getColumnForTrackedEntityAttribute(tea, true))
.map(this::getColumnForAttributeWithLegendSet)
.flatMap(Collection::stream)
.toList());
return columns;
Expand All @@ -579,14 +591,13 @@ private List<AnalyticsTableColumn> getAttributeColumns(Program program) {
* @param withLegendSet indicates whether the attribute has a legend set.
* @return a list of {@link AnaylyticsTableColumn}.
*/
private List<AnalyticsTableColumn> getColumnForTrackedEntityAttribute(
TrackedEntityAttribute attribute, boolean withLegendSet) {
private List<AnalyticsTableColumn> getColumnForAttribute(TrackedEntityAttribute attribute) {
List<AnalyticsTableColumn> columns = new ArrayList<>();

DataType dataType = getColumnType(attribute.getValueType(), isSpatialSupport());
String selectExpression = getSelectExpressionForAttribute(attribute.getValueType(), "value");
String dataExpression = getDataFilterClause(attribute);
String sql = selectForInsert(attribute, selectExpression, dataExpression);
String sql = getSelectSubquery(attribute, selectExpression, dataExpression);
Skip skipIndex = skipIndex(attribute.getValueType(), attribute.hasOptionSet());

if (attribute.getValueType().isOrganisationUnit()) {
Expand All @@ -602,7 +613,7 @@ private List<AnalyticsTableColumn> getColumnForTrackedEntityAttribute(
.skipIndex(skipIndex)
.build());

return withLegendSet ? getColumnForAttributeWithLegendSet(attribute) : columns;
return columns;
}

/**
Expand Down Expand Up @@ -663,7 +674,7 @@ private List<AnalyticsTableColumn> getColumnsForOrgUnitTrackedEntityAttribute(

if (isSpatialSupport()) {
String fromType = "ou.geometry " + fromClause;
String geoSql = selectForInsert(attribute, fromType, dataFilterClause);
String geoSql = getSelectSubquery(attribute, fromType, dataFilterClause);
columns.add(
AnalyticsTableColumn.builder()
.name((attribute.getUid() + OU_GEOMETRY_COL_SUFFIX))
Expand All @@ -675,7 +686,7 @@ private List<AnalyticsTableColumn> getColumnsForOrgUnitTrackedEntityAttribute(
}

String fromTypeSql = "ou.name " + fromClause;
String ouNameSql = selectForInsert(attribute, fromTypeSql, dataFilterClause);
String ouNameSql = getSelectSubquery(attribute, fromTypeSql, dataFilterClause);

columns.add(
AnalyticsTableColumn.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,12 @@ private List<AnalyticsTableColumn> getColumns(
private Stream<TrackedEntityAttribute> getAllTrackedEntityAttributes(
TrackedEntityType trackedEntityType, Map<String, List<Program>> programsByTetUid) {

// Given TET has program(s) defined.
if (programsByTetUid.containsKey(trackedEntityType.getUid())) {

// Programs defined for TET -> get attr from program and TET.
return getAllTrackedEntityAttributesByPrograms(
trackedEntityType, programsByTetUid.get(trackedEntityType.getUid()));
}

// No programs defined for TET -> get only attributes from TET.
return getAllTrackedEntityAttributesByEntityType(trackedEntityType);
}

Expand Down Expand Up @@ -297,9 +294,7 @@ private String castBasedOnType(ValueType valueType, String columnName) {
private Stream<TrackedEntityAttribute> getAllTrackedEntityAttributesByPrograms(
TrackedEntityType trackedEntityType, List<Program> programs) {
return Stream.concat(
/* all attributes of programs */
trackedEntityAttributeService.getProgramTrackedEntityAttributes(programs).stream(),
/* all attributes of the trackedEntityType */
getAllTrackedEntityAttributesByEntityType(trackedEntityType))
.distinct();
}
Expand Down Expand Up @@ -374,8 +369,8 @@ public void populateTable(AnalyticsTableUpdateParams params, AnalyticsTableParti
sql.append(
replaceQualify(
"""
\s left join ${trackedentityattributevalue} ${teaUid} on ${teaUid}.trackedentityid=te.trackedentityid \
and ${teaUid}.trackedentityattributeid = ${teaId}""",
\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())))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,11 @@ private List<AnalyticsTableColumn> getFixedCols() {
return columns;
}

/**
* Returns a list of geospatial columns.
*
* @return a list of {@link AnalyticsTableColumn}.
*/
private List<AnalyticsTableColumn> getGeospatialCols() {

return List.of(
Expand All @@ -319,6 +324,11 @@ private List<AnalyticsTableColumn> getGeospatialCols() {
.build());
}

/**
* Returns a partition column.
*
* @return an {@link AnalyticsTableColumn}.
*/
private AnalyticsTableColumn getPartitionColumn() {
return AnalyticsTableColumn.builder()
.name("year")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,8 @@ public class JdbcTrackedEntityEventsAnalyticsTableManager extends AbstractJdbcTa
.selectExpression("ous.level")
.build());

private static final String AND = " and (";

private final TrackedEntityTypeService trackedEntityTypeService;

private final AnalyticsSqlBuilder analyticsSqlBuilder;

public JdbcTrackedEntityEventsAnalyticsTableManager(
Expand Down Expand Up @@ -340,7 +339,8 @@ private List<Integer> getDataYears(AnalyticsTableUpdateParams params, TrackedEnt
"tetId", String.valueOf(tet.getId()))));

if (params.getFromDate() != null) {
sql.append(AND + eventDateExpression + ") >= '" + toMediumDate(params.getFromDate()) + "'");
sql.append(
" and (" + eventDateExpression + ") >= '" + toMediumDate(params.getFromDate()) + "'");
}

List<Integer> availableDataYears =
Expand Down Expand Up @@ -451,6 +451,11 @@ private String getPartitionClause(AnalyticsTablePartition partition) {
: emptyIfTrue(partitionFilter, sqlBuilder.supportsDeclarativePartitioning());
}

/**
* Returns a partition column.
*
* @return an {@link AnalyticsTableColumn}.
*/
private AnalyticsTableColumn getPartitionColumn() {
return AnalyticsTableColumn.builder()
.name("year")
Expand Down

0 comments on commit 9b6d958

Please sign in to comment.