Skip to content

Commit

Permalink
Merge branch 'master' into DHIS2-16233
Browse files Browse the repository at this point in the history
  • Loading branch information
jason-p-pickering authored Dec 1, 2023
2 parents 3529116 + d0e12c6 commit d4b5201
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2004-2023, 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.common.params.dimension;

import lombok.AccessLevel;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.hisp.dhis.common.QueryOperator;

@Data
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class AnalyticsQueryOperator {

private final QueryOperator queryOperator;
private final boolean negated;

public AnalyticsQueryOperator negate() {
return new AnalyticsQueryOperator(queryOperator, !negated);
}

public static AnalyticsQueryOperator of(QueryOperator queryOperator) {
return new AnalyticsQueryOperator(queryOperator, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
@Getter
@RequiredArgsConstructor(access = PRIVATE)
public class DimensionParamItem {
private final QueryOperator operator;
private final AnalyticsQueryOperator operator;

private final List<String> values;

Expand All @@ -59,7 +59,7 @@ public static List<DimensionParamItem> ofStrings(List<String> items) {

if (firstElement.contains(DIMENSION_NAME_SEP)) { // Has operator.
String[] parts = firstElement.split(DIMENSION_NAME_SEP);
QueryOperator queryOperator = getOperator(parts[0]);
AnalyticsQueryOperator queryOperator = getOperator(parts[0].trim());
return singletonList(
new DimensionParamItem(
queryOperator,
Expand All @@ -70,10 +70,14 @@ public static List<DimensionParamItem> ofStrings(List<String> items) {
}
}

private static QueryOperator getOperator(String operator) {
private static AnalyticsQueryOperator 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)
.orElseThrow(() -> new IllegalQueryException(E2035, operator));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +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.tei.query.context.sql.QueryContext;
import org.hisp.dhis.common.IllegalQueryException;
import org.hisp.dhis.common.QueryOperator;
Expand All @@ -57,14 +58,16 @@
public class BinaryConditionRenderer extends BaseRenderable {
private final Renderable left;

private final QueryOperator queryOperator;
private final AnalyticsQueryOperator analyticsQueryOperator;

private final Renderable right;

public static BinaryConditionRenderer fieldsEqual(
String leftAlias, String left, String rightAlias, String right) {
return BinaryConditionRenderer.of(
Field.of(leftAlias, () -> left, EMPTY), EQ, Field.of(rightAlias, () -> right, EMPTY));
Field.of(leftAlias, () -> left, EMPTY),
AnalyticsQueryOperator.of(EQ),
Field.of(rightAlias, () -> right, EMPTY));
}

public static BinaryConditionRenderer of(
Expand All @@ -74,59 +77,81 @@ public static BinaryConditionRenderer of(
ValueTypeMapping valueTypeMapping,
QueryContext queryContext) {
return BinaryConditionRenderer.of(
field, queryOperator, ConstantValuesRenderer.of(values, valueTypeMapping, queryContext));
field,
AnalyticsQueryOperator.of(queryOperator),
ConstantValuesRenderer.of(values, valueTypeMapping, queryContext));
}

public static BinaryConditionRenderer of(
Renderable field,
AnalyticsQueryOperator analyticsQueryOperator,
List<String> values,
ValueTypeMapping valueTypeMapping,
QueryContext queryContext) {
return BinaryConditionRenderer.of(
field,
analyticsQueryOperator,
ConstantValuesRenderer.of(values, valueTypeMapping, queryContext));
}

public static Renderable of(Renderable left, QueryOperator queryOperator, Renderable right) {
return BinaryConditionRenderer.of(left, AnalyticsQueryOperator.of(queryOperator), right);
}

private static final Collection<QueryOperator> comparisonOperators =
Arrays.asList(GT, GE, LT, LE);

@Nonnull
@Override
public String render() {
private Renderable getCondition(QueryOperator queryOperator) {
// EQ / IN
if (QueryOperator.EQ == queryOperator || QueryOperator.IN == queryOperator) {
return InOrEqConditionRenderer.of(left, right).render();
return InOrEqConditionRenderer.of(left, right);
}

// NE / NEQ
if (NEQ == queryOperator) {
if (hasNullValue(right)) {
return IsNullConditionRenderer.of(left, false).render();
return IsNullConditionRenderer.of(left, false);
}
return OrCondition.of(
List.of(
IsNullConditionRenderer.of(left, true), NotEqConditionRenderer.of(left, right)))
.render();
List.of(IsNullConditionRenderer.of(left, true), NotEqConditionRenderer.of(left, right)));
}

// LIKE / ILIKE
if (LikeOperatorMapper.likeOperators().contains(queryOperator)) {
return NullValueAwareConditionRenderer.of(LikeOperatorMapper.of(queryOperator), left, right)
.render();
return NullValueAwareConditionRenderer.of(LikeOperatorMapper.of(queryOperator), left, right);
}

// NLIKE / NILIKE
if (NLIKE == queryOperator || NILIKE == queryOperator) {
if (hasNullValue(right)) {
return IsNullConditionRenderer.of(left, false).render();
return IsNullConditionRenderer.of(left, false);
}

return OrCondition.of(
List.of(
IsNullConditionRenderer.of(left, true),
NLIKE == queryOperator
? NotLikeConditionRenderer.of(left, right)
: NotILikeConditionRenderer.of(left, right)))
.render();
List.of(
IsNullConditionRenderer.of(left, true),
NLIKE == queryOperator
? NotLikeConditionRenderer.of(left, right)
: NotILikeConditionRenderer.of(left, right)));
}

if (comparisonOperators.contains(queryOperator)) {
return left.render() + SPACE + queryOperator.getValue() + SPACE + right.render();
return () -> left.render() + SPACE + queryOperator.getValue() + SPACE + right.render();
}

throw new IllegalQueryException(E2035, queryOperator);
}

@Nonnull
@Override
public String render() {
if (analyticsQueryOperator.isNegated()) {
return NotConditionRenderer.of(getCondition(analyticsQueryOperator.getQueryOperator()))
.render();
}
return getCondition(analyticsQueryOperator.getQueryOperator()).render();
}

/** This class is responsible for mapping a "like" {@link QueryOperator} */
@RequiredArgsConstructor
private enum LikeOperatorMapper {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2004-2023, 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.common.query;

import static org.hisp.dhis.commons.util.TextUtils.EMPTY;

import javax.annotation.Nonnull;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;

@RequiredArgsConstructor(staticName = "of")
public class NotConditionRenderer extends BaseRenderable {
private final Renderable renderable;

@Nonnull
@Override
public String render() {
String rendered = renderable.render();
if (StringUtils.isEmpty(rendered)) {
return EMPTY;
}
return "not (" + rendered + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import static org.hisp.dhis.analytics.common.params.dimension.DimensionParamObjectType.DATA_ELEMENT;
import static org.hisp.dhis.analytics.common.params.dimension.DimensionParamObjectType.ORGANISATION_UNIT;
import static org.hisp.dhis.analytics.common.params.dimension.DimensionParamObjectType.ORGANISATION_UNIT_GROUP;
import static org.hisp.dhis.analytics.common.params.dimension.DimensionParamObjectType.ORGANISATION_UNIT_GROUP_SET;
import static org.hisp.dhis.analytics.common.params.dimension.DimensionParamObjectType.ORGANISATION_UNIT_LEVEL;
import static org.hisp.dhis.analytics.common.params.dimension.DimensionParamObjectType.PERIOD;
import static org.hisp.dhis.analytics.common.params.dimension.DimensionParamObjectType.PROGRAM_ATTRIBUTE;
import static org.hisp.dhis.analytics.common.params.dimension.DimensionParamObjectType.PROGRAM_INDICATOR;
Expand Down Expand Up @@ -76,7 +80,15 @@
@Slf4j
public class CommonParamsSecurityManager {
private static final Collection<DimensionParamObjectType> SECURITY_CHECK_SKIP_TYPES =
List.of(PROGRAM_ATTRIBUTE, DATA_ELEMENT, PROGRAM_INDICATOR, PERIOD);
List.of(
PROGRAM_ATTRIBUTE,
DATA_ELEMENT,
PROGRAM_INDICATOR,
PERIOD,
ORGANISATION_UNIT,
ORGANISATION_UNIT_GROUP,
ORGANISATION_UNIT_GROUP_SET,
ORGANISATION_UNIT_LEVEL);

private final AnalyticsSecurityManager securityManager;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +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.tei.query.context.sql.QueryContext;
import org.hisp.dhis.analytics.tei.query.context.sql.SqlParameterManager;
import org.hisp.dhis.common.IllegalQueryException;
Expand All @@ -66,6 +67,16 @@ void testInWithSingleValueProduceCorrectSql() {
List.of(getQueryContextAssertEqualsConsumer("v1")));
}

@Test
void testNegatedInWithSingleValueProduceCorrectSql() {
genericTestExecutor(
AnalyticsQueryOperator.of(IN).negate(),
List.of("v1"),
ValueTypeMapping.STRING,
"not (\"field\" = :1)",
List.of(getQueryContextAssertEqualsConsumer("v1")));
}

@Test
void testInNVWithSingleValueProduceCorrectSql() {
genericTestExecutor(
Expand Down Expand Up @@ -290,10 +301,25 @@ private void genericTestExecutor(
ValueTypeMapping valueTypeMapping,
String expectedSql,
List<Consumer<QueryContext>> queryContextConsumers) {
genericTestExecutor(
AnalyticsQueryOperator.of(operator),
values,
valueTypeMapping,
expectedSql,
queryContextConsumers);
}

private void genericTestExecutor(
AnalyticsQueryOperator analyticsQueryOperator,
List<String> values,
ValueTypeMapping valueTypeMapping,
String expectedSql,
List<Consumer<QueryContext>> queryContextConsumers) {
SqlParameterManager sqlParameterManager = new SqlParameterManager();
QueryContext queryContext = QueryContext.of(null, sqlParameterManager);
String render =
BinaryConditionRenderer.of(of("field"), operator, values, valueTypeMapping, queryContext)
BinaryConditionRenderer.of(
of("field"), analyticsQueryOperator, values, valueTypeMapping, queryContext)
.render();
assertEquals(expectedSql, render);
queryContextConsumers.forEach(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2004-2023, 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.common.query;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

class NotConditionRendererTest {

@Test
void testInWithSingleValueProduceCorrectSql() {
Renderable renderable = () -> "test";
assertEquals("not (test)", NotConditionRenderer.of(renderable).render());
}
}

0 comments on commit d4b5201

Please sign in to comment.