From eb9f2d7b8b69f96636bdb4ced72eaeee6591a532 Mon Sep 17 00:00:00 2001 From: Luciano Fiandesio Date: Mon, 6 Jan 2025 13:46:00 +0100 Subject: [PATCH] WIP - spotless --- .../data/JdbcEnrollmentAnalyticsManager.java | 30 +++----- .../util/sql/SqlConditionJoiner.java | 75 +++++++++++++------ 2 files changed, 62 insertions(+), 43 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEnrollmentAnalyticsManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEnrollmentAnalyticsManager.java index b2cfb44f249..52846c56eb2 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEnrollmentAnalyticsManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEnrollmentAnalyticsManager.java @@ -525,11 +525,7 @@ private String addCteFiltersToWhereClause(EventQueryParams params, CTEContext ct for (QueryFilter filter : item.getFilters()) { if (IN.equals(filter.getOperator())) { InQueryCteFilter inQueryCteFilter = - new InQueryCteFilter( - "value", - filter.getFilter(), - item.isText(), - cteDef); + new InQueryCteFilter("value", filter.getFilter(), item.isText(), cteDef); whereClause .append(" and ") .append(inQueryCteFilter.getSqlFilter(createOffset2(item.getProgramStageOffset()))); @@ -755,8 +751,7 @@ protected String getColumnWithCte(QueryItem item, String suffix, CTEContext cteC CteDefinition cteDef = cteContext.getDefinitionByItemUid(computeKey(item)); int programStageOffset = createOffset2(item.getProgramStageOffset()); String alias = getAlias(item).orElse(null); - columns.add( - "%s.value as %s".formatted(cteDef.getAlias(programStageOffset), quote(alias))); + columns.add("%s.value as %s".formatted(cteDef.getAlias(programStageOffset), quote(alias))); if (cteDef.isRowContext()) { // Add additional status and exists columns for row context columns.add( @@ -1025,7 +1020,7 @@ private CTEContext getCteDefinitions(EventQueryParams params) { String colName = quote(item.getItemName()); String cteSql = - """ + """ select enrollment, %s as value, @@ -1037,10 +1032,7 @@ private CTEContext getCteDefinitions(EventQueryParams params) { WHERE eventstatus != 'SCHEDULE' AND ps = '%s' """ - .formatted( - colName, - eventTableName, - item.getProgramStage().getUid()); + .formatted(colName, eventTableName, item.getProgramStage().getUid()); cteContext.addCTE( item.getProgramStage(), item, @@ -1136,13 +1128,13 @@ private String buildEnrollmentQueryWithCte(EventQueryParams params) { CteDefinition cteDef = cteContext.getDefinitionByItemUid(itemUid); if (cteDef.isProgramStage()) { List offsets = cteDef.getOffsets(); - for (Integer offset : offsets) { - String alias = cteDef.getAlias(offset); - String join = "%s %s ON %s.enrollment = ax.enrollment and %s.rn = %s" - .formatted(cteDef.asCteName(itemUid), alias, alias, alias, offset + 1); - sql.append(LEFT_JOIN).append(join); - } - + for (Integer offset : offsets) { + String alias = cteDef.getAlias(offset); + String join = + "%s %s ON %s.enrollment = ax.enrollment and %s.rn = %s" + .formatted(cteDef.asCteName(itemUid), alias, alias, alias, offset + 1); + sql.append(LEFT_JOIN).append(join); + } } if (cteDef.isExists()) { sql.append(LEFT_JOIN) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/util/sql/SqlConditionJoiner.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/util/sql/SqlConditionJoiner.java index 97cfe92503d..af51d0cb117 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/util/sql/SqlConditionJoiner.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/util/sql/SqlConditionJoiner.java @@ -1,35 +1,62 @@ +/* + * Copyright (c) 2004-2025, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ package org.hisp.dhis.analytics.util.sql; public class SqlConditionJoiner { - public static String joinSqlConditions(String... conditions) { - if (conditions == null || conditions.length == 0) { - return ""; - } + public static String joinSqlConditions(String... conditions) { + if (conditions == null || conditions.length == 0) { + return ""; + } - StringBuilder result = new StringBuilder("where "); - boolean firstCondition = true; + StringBuilder result = new StringBuilder("where "); + boolean firstCondition = true; - for (String condition : conditions) { - if (condition == null || condition.trim().isEmpty()) { - continue; - } + for (String condition : conditions) { + if (condition == null || condition.trim().isEmpty()) { + continue; + } - // Remove leading "where" or " where" and trim - String cleanedCondition = condition.trim(); - if (cleanedCondition.toLowerCase().startsWith("where")) { - cleanedCondition = cleanedCondition.substring(5).trim(); - } + // Remove leading "where" or " where" and trim + String cleanedCondition = condition.trim(); + if (cleanedCondition.toLowerCase().startsWith("where")) { + cleanedCondition = cleanedCondition.substring(5).trim(); + } - if (!cleanedCondition.isEmpty()) { - if (!firstCondition) { - result.append(" and "); - } - result.append(cleanedCondition); - firstCondition = false; - } + if (!cleanedCondition.isEmpty()) { + if (!firstCondition) { + result.append(" and "); } - - return result.toString(); + result.append(cleanedCondition); + firstCondition = false; + } } + + return result.toString(); + } }