diff --git a/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/RSSPoc2Service.java b/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/RSSPoc2Service.java index 0fdb559..bb36584 100644 --- a/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/RSSPoc2Service.java +++ b/redaction-report-service-v1/redaction-report-service-server-v1/src/main/java/com/iqser/red/service/redaction/report/v1/server/service/RSSPoc2Service.java @@ -198,7 +198,7 @@ public class RSSPoc2Service { } if (oecdIn(oecd, Set.of("402"))) { - resultMap.put(NECROPSY_FINDINGS, getLongestBlockOrElse(redactionLog, "necropsy_findings", "")); + resultMap.put(NECROPSY_FINDINGS, getLongestCombinedSectionBlockOrElse(redactionLog, "necropsy_findings", "")); resultMap.put(DOSES_MG_PER_KG_BW, getAsOneBlock(redactionLog, "doses_(mg_kg_bw)")); } @@ -561,28 +561,22 @@ public class RSSPoc2Service { String transformation = String.format("Values of type '%s' as sentences", type); List sentences = new ArrayList<>(); + var typeStringsEntries = redactionLog.getRedactionLogEntry().stream().filter(r -> r.getType().equals(type)).collect(Collectors.toList()); - Map> entriesPerSection = new HashMap<>(); - redactionLog.getRedactionLogEntry().stream().filter(r -> r.getType().equals(type)).forEach(e -> { - entriesPerSection.computeIfAbsent(e.getSectionNumber(), (x) -> new ArrayList<>()).add(e); - }); - - if (entriesPerSection.isEmpty()) { + if (typeStringsEntries.isEmpty()) { return sentences; } - for (Map.Entry> entriesInSection : entriesPerSection.entrySet()) { - - String combinedString = entriesInSection.getValue().stream().map(RedactionLogEntry::getValue).collect(Collectors.joining(" ")).trim(); + for (RedactionLogEntry typeStringEntry : typeStringsEntries) { BreakIterator iterator = BreakIterator.getSentenceInstance(Locale.US); - iterator.setText(combinedString); + iterator.setText(typeStringEntry.getValue()); int start = iterator.first(); for (int end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator.next()) { sentences.add(SCMComponent.builder() - .originalValue(combinedString.substring(start, end).replaceAll("\\n", "").trim()) - .scmAnnotations(entriesInSection.getValue().stream().map(this::toScmAnnotations).toList()) + .originalValue(typeStringEntry.getValue().substring(start, end).replaceAll("\\n", "").trim()) + .scmAnnotations(List.of(toScmAnnotations(typeStringEntry))) .transformation(transformation) .build()); @@ -593,24 +587,35 @@ public class RSSPoc2Service { } - private SCMComponent getLongestBlockOrElse(RedactionLog redactionLog, String type, String elseValue) { + private SCMComponent getLongestCombinedSectionBlockOrElse(RedactionLog redactionLog, String type, String elseValue) { - String transformation = String.format("Longest value of type '%s' if present or else '%s'", type, elseValue); + String transformation = String.format("Longest combined section value of type '%s' if present or else '%s'", type, elseValue); - var entries = redactionLog.getRedactionLogEntry() - .stream() - .filter(r -> r.getType().equals(type)) - .sorted(Comparator.comparing(s -> s.getValue().length())) - .collect(Collectors.toList()); + Map> entriesPerSection = new HashMap<>(); + redactionLog.getRedactionLogEntry().stream().filter(r -> r.getType().equals(type)).forEach(e -> { + entriesPerSection.computeIfAbsent(e.getSectionNumber(), (x) -> new ArrayList<>()).add(e); + }); - if (!entries.isEmpty()) { - - var firstEntry = entries.get(entries.size() - 1); - return SCMComponent.builder().originalValue(firstEntry.getValue()).scmAnnotations(List.of(toScmAnnotations(firstEntry))).transformation(transformation).build(); + if (entriesPerSection.isEmpty()) { + return SCMComponent.builder().originalValue(elseValue).transformation(transformation).build(); } - return SCMComponent.builder().originalValue(elseValue).transformation(transformation).build(); + int sectionNrOfLongestValue = 0; + String longestValue = ""; + for (Map.Entry> sectionEntry : entriesPerSection.entrySet()) { + String value = sectionEntry.getValue().stream().map(RedactionLogEntry::getValue).collect(Collectors.joining(" ")).trim(); + if (value.length() > longestValue.length()) { + longestValue = value; + sectionNrOfLongestValue = sectionEntry.getKey(); + } + } + + return SCMComponent.builder() + .originalValue(longestValue) + .scmAnnotations(entriesPerSection.get(sectionNrOfLongestValue).stream().map(this::toScmAnnotations).toList()) + .transformation(transformation) + .build(); }