Skip to content

Commit

Permalink
Add support for NOT, MATCHES and LIKE conditions to AqlUtil.removePar…
Browse files Browse the repository at this point in the history
…ameter (#657)
  • Loading branch information
askask authored Nov 27, 2024
1 parent 888b0f7 commit fe7a214
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 9 deletions.
43 changes: 34 additions & 9 deletions aql/src/main/java/org/ehrbase/openehr/sdk/aql/util/AqlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.ehrbase.openehr.sdk.aql.dto.AqlQuery;
import org.ehrbase.openehr.sdk.aql.dto.condition.ComparisonOperatorCondition;
import org.ehrbase.openehr.sdk.aql.dto.condition.LogicalOperatorCondition;
import org.ehrbase.openehr.sdk.aql.dto.condition.WhereCondition;
import org.ehrbase.openehr.sdk.aql.dto.condition.*;
import org.ehrbase.openehr.sdk.aql.dto.containment.AbstractContainmentExpression;
import org.ehrbase.openehr.sdk.aql.dto.containment.Containment;
import org.ehrbase.openehr.sdk.aql.dto.containment.ContainmentNotOperator;
import org.ehrbase.openehr.sdk.aql.dto.containment.ContainmentSetOperator;
import org.ehrbase.openehr.sdk.aql.dto.operand.LikeOperand;
import org.ehrbase.openehr.sdk.aql.dto.operand.MatchesOperand;
import org.ehrbase.openehr.sdk.aql.dto.operand.Operand;
import org.ehrbase.openehr.sdk.aql.dto.operand.QueryParameter;
import org.ehrbase.openehr.sdk.aql.dto.path.AndOperatorPredicate;
Expand Down Expand Up @@ -58,13 +58,14 @@ public static String removeParameter(String aql, String parameterName) {
}

private static WhereCondition removeParameter(WhereCondition condition, String parameterName) {
if (condition instanceof ComparisonOperatorCondition) {
Operand value = ((ComparisonOperatorCondition) condition).getValue();
if (value instanceof QueryParameter && Objects.equals(((QueryParameter) value).getName(), parameterName)) {
if (condition instanceof ComparisonOperatorCondition comparisonOperatorCondition) {
Operand value = comparisonOperatorCondition.getValue();
if (value instanceof QueryParameter queryParameter
&& Objects.equals(queryParameter.getName(), parameterName)) {
return null;
}
} else if (condition instanceof LogicalOperatorCondition) {
List<WhereCondition> values = ((LogicalOperatorCondition) condition).getValues();
} else if (condition instanceof LogicalOperatorCondition logicalOperatorCondition) {
List<WhereCondition> values = logicalOperatorCondition.getValues();

for (WhereCondition value : new ArrayList<>(values)) {
values.remove(value);
Expand All @@ -80,8 +81,32 @@ private static WhereCondition removeParameter(WhereCondition condition, String p
return null;
} else if (values.size() == 1) {
return values.get(0);
}
} else if (condition instanceof NotCondition notCondition) {
var value = removeParameter(notCondition.getConditionDto(), parameterName);
if (value != null) {
notCondition.setConditionDto(value);
} else {
return condition;
return null;
}
} else if (condition instanceof MatchesCondition matchesCondition) {
List<MatchesOperand> values = matchesCondition.getValues();
values.removeIf(value -> value instanceof QueryParameter queryParameter
&& Objects.equals(queryParameter.getName(), parameterName));
if (values.isEmpty()) {
return null;
}
} else if (condition instanceof LikeCondition likeCondition) {
LikeOperand value = likeCondition.getValue();
if (value instanceof QueryParameter queryParameter
&& Objects.equals(queryParameter.getName(), parameterName)) {
return null;
}
} else if (condition instanceof ExistsCondition) {
// doesn't have parameter
} else {
if (condition != null) {
throw new IllegalStateException("Unexpected condition: " + condition);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,93 @@ public void removeParameterNoWhere() {
assertThat(actual).isEqualToIgnoringCase(aql);
}

@Test
public void removeParameterInNotConditionInsideAndCondition() {
String aql =
"""
SELECT c0 AS F1, e/ehr_id/value
FROM EHR e
CONTAINS COMPOSITION c0[openEHR-EHR-COMPOSITION.report.v1]
WHERE (
e/ehr_id/value = $ehrid1
AND NOT e/ehr_id/value = $ehrid2
)""";
String actual = AqlUtil.removeParameter(aql, "ehrid2");

assertThat(actual)
.isEqualToIgnoringCase(
"SELECT c0 AS F1, e/ehr_id/value FROM EHR e CONTAINS COMPOSITION c0[openEHR-EHR-COMPOSITION.report.v1] WHERE e/ehr_id/value = $ehrid1");
}

@Test
public void removeParameterInNotCondition() {
String aql =
"""
SELECT c0 AS F1, e/ehr_id/value
FROM EHR e
CONTAINS COMPOSITION c0[openEHR-EHR-COMPOSITION.report.v1]
WHERE NOT (
e/ehr_id/value = $ehrid1
AND e/ehr_id/value = $ehrid2
)""";
String actual = AqlUtil.removeParameter(aql, "ehrid2");

assertThat(actual)
.isEqualToIgnoringCase(
"SELECT c0 AS F1, e/ehr_id/value FROM EHR e CONTAINS COMPOSITION c0[openEHR-EHR-COMPOSITION.report.v1] WHERE NOT e/ehr_id/value = $ehrid1");
}

@Test
public void removeParameterInLikeCondition() {
String aql =
"""
SELECT c0 AS F1, e/ehr_id/value
FROM EHR e
CONTAINS COMPOSITION c0[openEHR-EHR-COMPOSITION.report.v1]
WHERE
e/ehr_id/value LIKE $ehrid1
""";
String actual = AqlUtil.removeParameter(aql, "ehrid1");

assertThat(actual)
.isEqualToIgnoringCase(
"SELECT c0 AS F1, e/ehr_id/value FROM EHR e CONTAINS COMPOSITION c0[openEHR-EHR-COMPOSITION.report.v1]");
}

@Test
public void removeParameterInMatchesConditionMatchesIsKept() {
String aql =
"""
SELECT c0 AS F1, e/ehr_id/value
FROM EHR e
CONTAINS COMPOSITION c0[openEHR-EHR-COMPOSITION.report.v1]
WHERE
e/ehr_id/value MATCHES { $ehrid1, $ehrid2 }
""";
String actual = AqlUtil.removeParameter(aql, "ehrid1");

assertThat(actual)
.isEqualToIgnoringCase(
"SELECT c0 AS F1, e/ehr_id/value FROM EHR e CONTAINS COMPOSITION c0[openEHR-EHR-COMPOSITION.report.v1] WHERE e/ehr_id/value MATCHES {$ehrid2}");
}

@Test
public void removeParameterInMatchesConditionMatchesIsRemoved() {
String aql =
"""
SELECT c0 AS F1, e/ehr_id/value
FROM EHR e
CONTAINS COMPOSITION c0[openEHR-EHR-COMPOSITION.report.v1]
WHERE
e/ehr_id/value MATCHES { $ehrid1 }
""";
String actual = AqlUtil.removeParameter(aql, "ehrid1");

assertThat(actual)
.isEqualToIgnoringCase(
"SELECT c0 AS F1, e/ehr_id/value FROM EHR e CONTAINS COMPOSITION c0[openEHR-EHR-COMPOSITION.report.v1]");
}

@Test
public void containmentExpressionsByIdentifier() {
AqlQuery aqlQuery = AqlQueryParser.parse(
Expand Down

0 comments on commit fe7a214

Please sign in to comment.