From e52a54b0a3eb8491ed3e79b3f514364a5cf22f76 Mon Sep 17 00:00:00 2001 From: deiflaender Date: Fri, 11 Sep 2020 11:00:46 +0200 Subject: [PATCH] RED-305: Added ids to manual redaction entries, RED-306: set redacet to false in redactionLog for manual deletions --- .../service/AnnotationHighlightService.java | 47 +++++++++++++++---- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/visualization/service/AnnotationHighlightService.java b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/visualization/service/AnnotationHighlightService.java index 7355cf84..9836bd4d 100644 --- a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/visualization/service/AnnotationHighlightService.java +++ b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/visualization/service/AnnotationHighlightService.java @@ -2,7 +2,9 @@ package com.iqser.red.service.redaction.v1.server.visualization.service; import java.awt.Color; import java.io.IOException; +import java.util.HashSet; import java.util.List; +import java.util.Set; import org.apache.commons.collections4.CollectionUtils; import org.apache.pdfbox.pdmodel.PDDocument; @@ -45,22 +47,42 @@ public class AnnotationHighlightService { public void highlight(PDDocument document, Document classifiedDoc, boolean flatRedaction, ManualRedactions manualRedactions) throws IOException { + Set manualRedactionPages = getManualRedactionPages(manualRedactions); + for (int page = 1; page <= document.getNumberOfPages(); page++) { PDPage pdPage = document.getPage(page - 1); drawSectionFrames(document, classifiedDoc, flatRedaction, pdPage, page); - if (classifiedDoc.getEntities().get(page) == null) { - continue; + if (classifiedDoc.getEntities().get(page) != null) { + addAnnotations(pdPage, classifiedDoc, flatRedaction, manualRedactions, page); } - addAnnotations(pdPage, classifiedDoc, flatRedaction, manualRedactions, page); - addManualAnnotations(pdPage, classifiedDoc, manualRedactions, page); + if (manualRedactionPages.contains(page)) { + addManualAnnotations(pdPage, classifiedDoc, manualRedactions, page); + } } } + private Set getManualRedactionPages(ManualRedactions manualRedactions) { + + Set manualRedactionPages = new HashSet<>(); + + if (manualRedactions == null) { + return manualRedactionPages; + } + + manualRedactions.getEntriesToAdd().forEach(entry -> { + entry.getPositions().forEach(pos -> { + manualRedactionPages.add(pos.getPage()); + }); + }); + return manualRedactionPages; + } + + private void addAnnotations(PDPage pdPage, Document classifiedDoc, boolean flatRedaction, ManualRedactions manualRedactions, int page) throws IOException { List annotations = pdPage.getAnnotations(); @@ -77,8 +99,11 @@ public class AnnotationHighlightService { if (manualRedactions != null && manualRedactions.getIdsToRemove() .contains(entityPositionSequence.getId())) { + String manualOverrideReason = entity.getRedactionReason() + ", removed by manual override"; entity.setRedaction(false); - entity.setRedactionReason(entity.getRedactionReason() + ", removed by manual override"); + entity.setRedactionReason(manualOverrideReason); + redactionLogEntry.setRedacted(false); + redactionLogEntry.setReason(manualOverrideReason); redactionLogEntry.setManual(true); } @@ -107,28 +132,34 @@ public class AnnotationHighlightService { String id = IdBuilder.buildId(manualRedactionEntry); - RedactionLogEntry redactionLogEntry = createRedactionLogEntry(manualRedactionEntry); + RedactionLogEntry redactionLogEntry = createRedactionLogEntry(manualRedactionEntry, id); + boolean foundOnPage = false; for (Rectangle rectangle : manualRedactionEntry.getPositions()) { if (page != rectangle.getPage()) { continue; } + foundOnPage = true; + PDAnnotationTextMarkup highlight = createAnnotation(rectangle, id, createAnnotationContent(manualRedactionEntry), getColor(manualRedactionEntry .getType()), true); annotations.add(highlight); redactionLogEntry.getPositions().add(rectangle); } - classifiedDoc.getRedactionLogEntities().add(redactionLogEntry); + if (foundOnPage) { + classifiedDoc.getRedactionLogEntities().add(redactionLogEntry); + } } } - private RedactionLogEntry createRedactionLogEntry(ManualRedactionEntry manualRedactionEntry) { + private RedactionLogEntry createRedactionLogEntry(ManualRedactionEntry manualRedactionEntry, String id) { return RedactionLogEntry.builder() + .id(id) .color(getColor(manualRedactionEntry.getType())) .reason(manualRedactionEntry.getReason()) .value(manualRedactionEntry.getValue())