From 6855607fecb8d28f8e691787ef9db125b8cce523 Mon Sep 17 00:00:00 2001 From: chvostek Date: Tue, 29 Sep 2026 15:18:31 +0200 Subject: [PATCH] [NAE-2486] QueryLangEvaluator project implementations - make QueryLangEvaluator of prototype bean --- .../PrototypesConfiguration.java | 15 ++++- .../pfql/service/QueryLangEvaluator.java | 56 +++++++++---------- .../pfql/service/utils/SearchUtils.java | 4 +- 3 files changed, 43 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/netgrif/application/engine/configuration/PrototypesConfiguration.java b/src/main/java/com/netgrif/application/engine/configuration/PrototypesConfiguration.java index f5ec750ded1..a4d0ef74c68 100644 --- a/src/main/java/com/netgrif/application/engine/configuration/PrototypesConfiguration.java +++ b/src/main/java/com/netgrif/application/engine/configuration/PrototypesConfiguration.java @@ -1,5 +1,6 @@ package com.netgrif.application.engine.configuration; +import com.netgrif.application.engine.auth.service.interfaces.IUserService; import com.netgrif.application.engine.auth.web.responsebodies.UserResourceAssembler; import com.netgrif.application.engine.importer.service.Importer; import com.netgrif.application.engine.pdf.generator.service.PdfDataHelper; @@ -9,16 +10,21 @@ import com.netgrif.application.engine.pdf.generator.service.interfaces.IPdfDrawer; import com.netgrif.application.engine.pdf.generator.service.interfaces.IPdfGenerator; import com.netgrif.application.engine.petrinet.domain.dataset.logic.action.ActionDelegate; +import com.netgrif.application.engine.pfql.service.QueryLangEvaluator; import com.netgrif.application.engine.workflow.domain.FileStorageConfiguration; +import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Scope; @Configuration +@RequiredArgsConstructor public class PrototypesConfiguration { + private final IUserService userService; + @Bean("importer") @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) public Importer importer() { @@ -60,4 +66,11 @@ public IPdfDrawer pdfDrawer() { public UserResourceAssembler userResourceAssembler() { return new UserResourceAssembler(); } + + @Bean("queryLangEvaluator") + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) + @ConditionalOnMissingBean(QueryLangEvaluator.class) + public QueryLangEvaluator queryLangEvaluator() { + return new QueryLangEvaluator(userService); + } } \ No newline at end of file diff --git a/src/main/java/com/netgrif/application/engine/pfql/service/QueryLangEvaluator.java b/src/main/java/com/netgrif/application/engine/pfql/service/QueryLangEvaluator.java index 9b8a3971421..7230c30854f 100644 --- a/src/main/java/com/netgrif/application/engine/pfql/service/QueryLangEvaluator.java +++ b/src/main/java/com/netgrif/application/engine/pfql/service/QueryLangEvaluator.java @@ -38,30 +38,30 @@ public class QueryLangEvaluator extends QueryLangBaseListener { - private final ParseTreeProperty elasticQuery = new ParseTreeProperty<>(); - private final ParseTreeProperty mongoQuery = new ParseTreeProperty<>(); - private final String elasticFuzzyMaxDistance = "AUTO"; + protected final ParseTreeProperty elasticQuery = new ParseTreeProperty<>(); + protected final ParseTreeProperty mongoQuery = new ParseTreeProperty<>(); + protected final String elasticFuzzyMaxDistance = "AUTO"; - private final IUserService userService; + protected final IUserService userService; @Getter - private QueryType resourceType; + protected QueryType resourceType; @Getter - private Boolean multiple; + protected Boolean multiple; @Getter @Setter - private Boolean searchWithElastic = false; + protected Boolean searchWithElastic = false; @Getter - private Predicate fullMongoQuery; + protected Predicate fullMongoQuery; @Getter - private String fullElasticQuery; + protected String fullElasticQuery; @Getter @Setter - private Pageable pageable; + protected Pageable pageable; - private int pageNumber = 0; - private int pageSize = 20; - private final List sortOrders = new ArrayList<>(); + protected int pageNumber = 0; + protected int pageSize = 20; + protected final List sortOrders = new ArrayList<>(); public QueryLangEvaluator(IUserService userService) { this.userService = userService; @@ -83,12 +83,12 @@ public Predicate getMongoQuery(ParseTree node) { return mongoQuery.get(node); } - private void processBasicExpression(ParseTree child, ParseTree current) { + protected void processBasicExpression(ParseTree child, ParseTree current) { setMongoQuery(current, getMongoQuery(child)); setElasticQuery(current, getElasticQuery(child)); } - private void processOrExpression(List children, ParseTree current) { + protected void processOrExpression(List children, ParseTree current) { List predicates = children.stream() .map(this::getMongoQuery) .filter(Objects::nonNull) @@ -106,7 +106,7 @@ private void processOrExpression(List children, ParseTree current) { setElasticQuery(current, elasticQuery.isBlank() ? null : elasticQuery); } - private void processAndExpression(List children, ParseTree current) { + protected void processAndExpression(List children, ParseTree current) { List predicates = children.stream() .map(this::getMongoQuery) .filter(Objects::nonNull) @@ -124,7 +124,7 @@ private void processAndExpression(List children, ParseTree current) { setElasticQuery(current, elasticQuery.isBlank() ? null : elasticQuery); } - private void processConditionGroup(ParseTree child, ParseTree current, Boolean not, Boolean parenthesis) { + protected void processConditionGroup(ParseTree child, ParseTree current, Boolean not, Boolean parenthesis) { Predicate predicate = getMongoQuery(child); String elasticQuery = getElasticQuery(child); @@ -146,7 +146,7 @@ private void processConditionGroup(ParseTree child, ParseTree current, Boolean n setElasticQuery(current, elasticQuery); } - private String handleStringComparisonWithPlaceholders(QueryLangParser.StringComparisonContext ctx) { + protected String handleStringComparisonWithPlaceholders(QueryLangParser.StringComparisonContext ctx) { if (ctx.STRING() != null) { return getStringValue(ctx.STRING().getText()); } @@ -158,7 +158,7 @@ private String handleStringComparisonWithPlaceholders(QueryLangParser.StringComp throw new IllegalArgumentException("Wrong or missing query value on string comparison"); } - private ObjectId handleObjectIdComparisonWithPlaceholders(QueryLangParser.ObjectIdComparisonContext ctx) { + protected ObjectId handleObjectIdComparisonWithPlaceholders(QueryLangParser.ObjectIdComparisonContext ctx) { if (ctx.STRING() != null) { return getObjectIdValue(ctx.STRING().getText()); } @@ -170,7 +170,7 @@ private ObjectId handleObjectIdComparisonWithPlaceholders(QueryLangParser.Object throw new IllegalArgumentException("Wrong or missing query value on object id comparison"); } - private List handleStringListComparison(QueryLangParser.StringListContext ctx) { + protected List handleStringListComparison(QueryLangParser.StringListContext ctx) { List result = new ArrayList<>(); if (ctx.STRING() != null && !ctx.STRING().isEmpty()) { @@ -187,7 +187,7 @@ private List handleStringListComparison(QueryLangParser.StringListContex return result; } - private List handleObjectIdListComparison(QueryLangParser.StringListContext ctx) { + protected List handleObjectIdListComparison(QueryLangParser.StringListContext ctx) { List result = new ArrayList<>(); if (ctx.STRING() != null && !ctx.STRING().isEmpty()) { @@ -207,7 +207,7 @@ private List handleObjectIdListComparison(QueryLangParser.StringListCo return result; } - private String handleLoggedUserStringAttribute(QueryLangParser.LoggedUserStringAttributeContext ctx) { + protected String handleLoggedUserStringAttribute(QueryLangParser.LoggedUserStringAttributeContext ctx) { LoggedUser loggedUser = this.userService.getLoggedOrSystem().transformToLoggedUser(); if (ctx.LOGGED_USER_ID() != null) { return loggedUser.getId(); @@ -222,7 +222,7 @@ private String handleLoggedUserStringAttribute(QueryLangParser.LoggedUserStringA } /// returns pair, where the first element is left value and the second element is right value - private Pair handleInRangeStringComparison(QueryLangParser.StringRangeContext ctx) { + protected Pair handleInRangeStringComparison(QueryLangParser.StringRangeContext ctx) { List filteredChildren = ctx.children.stream() .filter(node -> node instanceof TerminalNode && ((TerminalNode) node).getSymbol().getType() == QueryLangParser.STRING || node instanceof QueryLangParser.LoggedUserStringAttributeContext) @@ -251,7 +251,7 @@ private Pair handleInRangeStringComparison(QueryLangParser.Strin return Pair.of(left, right); } - private String handleBooleanComparison(QueryLangParser.BooleanComparisonContext ctx) { + protected String handleBooleanComparison(QueryLangParser.BooleanComparisonContext ctx) { if (ctx.BOOLEAN() != null) { return ctx.BOOLEAN().getText(); } @@ -263,15 +263,15 @@ private String handleBooleanComparison(QueryLangParser.BooleanComparisonContext throw new IllegalArgumentException("Wrong or missing query value on boolean comparison"); } - private Predicate getEmptyMongoQuery() { + protected Predicate getEmptyMongoQuery() { return new BooleanBuilder(); } - private String getEmptyElasticQuery() { + protected String getEmptyElasticQuery() { return "*"; } - private void handleNoneConditions() { + protected void handleNoneConditions() { fullMongoQuery = getEmptyMongoQuery(); fullElasticQuery = getEmptyElasticQuery(); } @@ -1882,7 +1882,7 @@ public void exitDataStringLike(QueryLangParser.DataStringLikeContext ctx) { this.searchWithElastic = true; } - private boolean shouldBeNotNull(QueryLangParser.NullComparisonContext ctx) { + protected boolean shouldBeNotNull(QueryLangParser.NullComparisonContext ctx) { if (ctx == null) { throw new IllegalArgumentException("Null comparison context must be provided"); } diff --git a/src/main/java/com/netgrif/application/engine/pfql/service/utils/SearchUtils.java b/src/main/java/com/netgrif/application/engine/pfql/service/utils/SearchUtils.java index ba474721bce..3d6dc8f19d4 100644 --- a/src/main/java/com/netgrif/application/engine/pfql/service/utils/SearchUtils.java +++ b/src/main/java/com/netgrif/application/engine/pfql/service/utils/SearchUtils.java @@ -1,6 +1,5 @@ package com.netgrif.application.engine.pfql.service.utils; -import com.netgrif.application.engine.auth.service.interfaces.IUserService; import com.netgrif.application.engine.configuration.ApplicationContextProvider; import com.netgrif.application.engine.elastic.service.ElasticsearchQuerySanitizer; import com.netgrif.application.engine.petrinet.domain.QPetriNet; @@ -174,8 +173,7 @@ private static QueryLangBaseListener evaluateQueryInternal(String input, boolean return explainQueryInternal(walker, query, errorListener); } - IUserService userService = (IUserService) ApplicationContextProvider.getBean(IUserService.class); - QueryLangEvaluator evaluator = new QueryLangEvaluator(userService); + QueryLangEvaluator evaluator = (QueryLangEvaluator) ApplicationContextProvider.getBean(QueryLangEvaluator.class); walker.walk(evaluator, query); return evaluator;