From 3387fabfb441f13183901918f37f3b2500ac2430 Mon Sep 17 00:00:00 2001 From: Giuseppe Nespolino Date: Thu, 30 Nov 2023 17:23:06 +0100 Subject: [PATCH] refactor: renamed class AnalyticsQueryOperator to NegatableQueryOperator [DHIS2-16187] --- .../params/dimension/DimensionParamItem.java | 8 ++++---- ...rator.java => NegatableQueryOperator.java} | 14 ++++++++----- .../common/query/BinaryConditionRenderer.java | 20 +++++++++---------- .../query/BinaryConditionRendererTest.java | 10 +++++----- 4 files changed, 28 insertions(+), 24 deletions(-) rename dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/common/params/dimension/{AnalyticsQueryOperator.java => NegatableQueryOperator.java} (81%) diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/common/params/dimension/DimensionParamItem.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/common/params/dimension/DimensionParamItem.java index aa0c1580aa9e..1b2adeb919be 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/common/params/dimension/DimensionParamItem.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/common/params/dimension/DimensionParamItem.java @@ -46,7 +46,7 @@ @Getter @RequiredArgsConstructor(access = PRIVATE) public class DimensionParamItem { - private final AnalyticsQueryOperator operator; + private final NegatableQueryOperator operator; private final List values; @@ -59,7 +59,7 @@ public static List ofStrings(List items) { if (firstElement.contains(DIMENSION_NAME_SEP)) { // Has operator. String[] parts = firstElement.split(DIMENSION_NAME_SEP); - AnalyticsQueryOperator queryOperator = getOperator(parts[0].trim()); + NegatableQueryOperator queryOperator = getOperator(parts[0].trim()); return singletonList( new DimensionParamItem( queryOperator, @@ -70,14 +70,14 @@ public static List ofStrings(List items) { } } - private static AnalyticsQueryOperator getOperator(String operator) { + private static NegatableQueryOperator getOperator(String operator) { if (operator.startsWith("!")) { return getOperator(operator.substring(1)).negate(); } return Arrays.stream(QueryOperator.values()) .filter(queryOperator -> equalsIgnoreCase(queryOperator.name(), operator)) .findFirst() - .map(AnalyticsQueryOperator::of) + .map(NegatableQueryOperator::of) .orElseThrow(() -> new IllegalQueryException(E2035, operator)); } } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/common/params/dimension/AnalyticsQueryOperator.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/common/params/dimension/NegatableQueryOperator.java similarity index 81% rename from dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/common/params/dimension/AnalyticsQueryOperator.java rename to dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/common/params/dimension/NegatableQueryOperator.java index ab7bdead0baf..87c21c6eaac6 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/common/params/dimension/AnalyticsQueryOperator.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/common/params/dimension/NegatableQueryOperator.java @@ -32,18 +32,22 @@ import lombok.RequiredArgsConstructor; import org.hisp.dhis.common.QueryOperator; +/** + * Represents a query operator that can be negated. It holds Internally a {@link QueryOperator} and + * a boolean flag that indicates if the operator is negated. + */ @Data @RequiredArgsConstructor(access = AccessLevel.PRIVATE) -public class AnalyticsQueryOperator { +public class NegatableQueryOperator { private final QueryOperator queryOperator; private final boolean negated; - public AnalyticsQueryOperator negate() { - return new AnalyticsQueryOperator(queryOperator, !negated); + public NegatableQueryOperator negate() { + return new NegatableQueryOperator(queryOperator, !negated); } - public static AnalyticsQueryOperator of(QueryOperator queryOperator) { - return new AnalyticsQueryOperator(queryOperator, false); + public static NegatableQueryOperator of(QueryOperator queryOperator) { + return new NegatableQueryOperator(queryOperator, false); } } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/common/query/BinaryConditionRenderer.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/common/query/BinaryConditionRenderer.java index d6d0a1ec7dd6..7d18bd1c4f9e 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/common/query/BinaryConditionRenderer.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/common/query/BinaryConditionRenderer.java @@ -48,7 +48,7 @@ import javax.annotation.Nonnull; import lombok.RequiredArgsConstructor; import org.hisp.dhis.analytics.common.ValueTypeMapping; -import org.hisp.dhis.analytics.common.params.dimension.AnalyticsQueryOperator; +import org.hisp.dhis.analytics.common.params.dimension.NegatableQueryOperator; import org.hisp.dhis.analytics.tei.query.context.sql.QueryContext; import org.hisp.dhis.common.IllegalQueryException; import org.hisp.dhis.common.QueryOperator; @@ -58,7 +58,7 @@ public class BinaryConditionRenderer extends BaseRenderable { private final Renderable left; - private final AnalyticsQueryOperator analyticsQueryOperator; + private final NegatableQueryOperator negatableQueryOperator; private final Renderable right; @@ -66,7 +66,7 @@ public static BinaryConditionRenderer fieldsEqual( String leftAlias, String left, String rightAlias, String right) { return BinaryConditionRenderer.of( Field.of(leftAlias, () -> left, EMPTY), - AnalyticsQueryOperator.of(EQ), + NegatableQueryOperator.of(EQ), Field.of(rightAlias, () -> right, EMPTY)); } @@ -78,24 +78,24 @@ public static BinaryConditionRenderer of( QueryContext queryContext) { return BinaryConditionRenderer.of( field, - AnalyticsQueryOperator.of(queryOperator), + NegatableQueryOperator.of(queryOperator), ConstantValuesRenderer.of(values, valueTypeMapping, queryContext)); } public static BinaryConditionRenderer of( Renderable field, - AnalyticsQueryOperator analyticsQueryOperator, + NegatableQueryOperator negatableQueryOperator, List values, ValueTypeMapping valueTypeMapping, QueryContext queryContext) { return BinaryConditionRenderer.of( field, - analyticsQueryOperator, + negatableQueryOperator, ConstantValuesRenderer.of(values, valueTypeMapping, queryContext)); } public static Renderable of(Renderable left, QueryOperator queryOperator, Renderable right) { - return BinaryConditionRenderer.of(left, AnalyticsQueryOperator.of(queryOperator), right); + return BinaryConditionRenderer.of(left, NegatableQueryOperator.of(queryOperator), right); } private static final Collection comparisonOperators = @@ -145,11 +145,11 @@ private Renderable getCondition(QueryOperator queryOperator) { @Nonnull @Override public String render() { - if (analyticsQueryOperator.isNegated()) { - return NotConditionRenderer.of(getCondition(analyticsQueryOperator.getQueryOperator())) + if (negatableQueryOperator.isNegated()) { + return NotConditionRenderer.of(getCondition(negatableQueryOperator.getQueryOperator())) .render(); } - return getCondition(analyticsQueryOperator.getQueryOperator()).render(); + return getCondition(negatableQueryOperator.getQueryOperator()).render(); } /** This class is responsible for mapping a "like" {@link QueryOperator} */ diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/common/query/BinaryConditionRendererTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/common/query/BinaryConditionRendererTest.java index bf13aeda9e1d..bee2e2a211f0 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/common/query/BinaryConditionRendererTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/common/query/BinaryConditionRendererTest.java @@ -49,7 +49,7 @@ import java.util.function.Consumer; import java.util.stream.Stream; import org.hisp.dhis.analytics.common.ValueTypeMapping; -import org.hisp.dhis.analytics.common.params.dimension.AnalyticsQueryOperator; +import org.hisp.dhis.analytics.common.params.dimension.NegatableQueryOperator; import org.hisp.dhis.analytics.tei.query.context.sql.QueryContext; import org.hisp.dhis.analytics.tei.query.context.sql.SqlParameterManager; import org.hisp.dhis.common.IllegalQueryException; @@ -70,7 +70,7 @@ void testInWithSingleValueProduceCorrectSql() { @Test void testNegatedInWithSingleValueProduceCorrectSql() { genericTestExecutor( - AnalyticsQueryOperator.of(IN).negate(), + NegatableQueryOperator.of(IN).negate(), List.of("v1"), ValueTypeMapping.STRING, "not (\"field\" = :1)", @@ -302,7 +302,7 @@ private void genericTestExecutor( String expectedSql, List> queryContextConsumers) { genericTestExecutor( - AnalyticsQueryOperator.of(operator), + NegatableQueryOperator.of(operator), values, valueTypeMapping, expectedSql, @@ -310,7 +310,7 @@ private void genericTestExecutor( } private void genericTestExecutor( - AnalyticsQueryOperator analyticsQueryOperator, + NegatableQueryOperator negatableQueryOperator, List values, ValueTypeMapping valueTypeMapping, String expectedSql, @@ -319,7 +319,7 @@ private void genericTestExecutor( QueryContext queryContext = QueryContext.of(null, sqlParameterManager); String render = BinaryConditionRenderer.of( - of("field"), analyticsQueryOperator, values, valueTypeMapping, queryContext) + of("field"), negatableQueryOperator, values, valueTypeMapping, queryContext) .render(); assertEquals(expectedSql, render); queryContextConsumers.forEach(