From 191b73986b1868cb30bf7936880617c727258948 Mon Sep 17 00:00:00 2001 From: Ali Oezyetimoglu Date: Mon, 6 Nov 2023 15:16:53 +0100 Subject: [PATCH] RED-7679: fixed method rowValueCount --- .../document/ComponentCreationService.java | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/service/document/ComponentCreationService.java b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/service/document/ComponentCreationService.java index 9c8b22e9..8cf6e017 100644 --- a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/service/document/ComponentCreationService.java +++ b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/service/document/ComponentCreationService.java @@ -19,6 +19,7 @@ import org.kie.api.runtime.KieSession; import com.iqser.red.service.redaction.v1.server.model.component.Component; import com.iqser.red.service.redaction.v1.server.model.component.Entity; +import com.iqser.red.service.redaction.v1.server.model.document.nodes.Paragraph; import com.iqser.red.service.redaction.v1.server.model.document.nodes.SemanticNode; import com.iqser.red.service.redaction.v1.server.model.document.nodes.Table; import com.iqser.red.service.redaction.v1.server.model.document.nodes.TableCell; @@ -298,7 +299,7 @@ public class ComponentCreationService { /** - * Computes the number of rows in table with values in the collection of entities and creates a component with the result. + * Counts the distinct rows where any of the provided entities occur and creates a component for each distinct table with its respective row count. * * @param ruleIdentifier the identifier of the rule * @param name the name of the record @@ -306,16 +307,17 @@ public class ComponentCreationService { */ public void rowValueCount(String ruleIdentifier, String name, Collection entities) { - entities.stream().collect(Collectors.groupingBy(this::getFirstTable)).forEach((optionalTable, groupedEntities) -> { + entities.stream().collect(Collectors.groupingBy(this::getFirstTableCell)).forEach((optionalTable, groupedEntities) -> { + + if (optionalTable.isEmpty()) { + return; + } long count = groupedEntities.stream() - .filter(entity -> entity.getContainingNode() instanceof TableCell) + .filter(entity -> !(entity.getContainingNode() instanceof Paragraph) && entity.getContainingNode() instanceof TableCell) .collect(Collectors.groupingBy(entity -> ((TableCell) entity.getContainingNode()).getRow())) - .entrySet() - .stream() - .sorted(Comparator.comparingInt(Map.Entry::getKey)) - .map(Map.Entry::getValue) - .count(); + .size(); + create(ruleIdentifier, name, String.valueOf(count), "Count rows with values in the entity references in same table", entities); }); } @@ -481,6 +483,20 @@ public class ComponentCreationService { } + private Optional getFirstTableCell(Entity entity) { + + SemanticNode node = entity.getContainingNode(); + while (!(node instanceof TableCell)) { + if (!node.hasParent()) { + return Optional.empty(); + } + node = node.getParent(); + } + + return Optional.of((TableCell) node); + } + + /** * Creates a new component with the given rule identifier, name, value, and value description. * If the component is part of a table, it also takes a list of entities that belong to the same table row.