From 34bee36e11022dfe0701a3bcc0c2afa455dab775 Mon Sep 17 00:00:00 2001 From: aoezyetimoglu Date: Tue, 12 Jul 2022 12:03:30 +0200 Subject: [PATCH] RED-4586: When a redaction is shortened via resize the new value should replace the old one --- .../service/ManualRedactionService.java | 143 +++++++----------- 1 file changed, 52 insertions(+), 91 deletions(-) diff --git a/persistence-service-v1/persistence-service-server-v1/src/main/java/com/iqser/red/service/peristence/v1/server/service/ManualRedactionService.java b/persistence-service-v1/persistence-service-server-v1/src/main/java/com/iqser/red/service/peristence/v1/server/service/ManualRedactionService.java index 2ec13d456..1f88073cc 100644 --- a/persistence-service-v1/persistence-service-server-v1/src/main/java/com/iqser/red/service/peristence/v1/server/service/ManualRedactionService.java +++ b/persistence-service-v1/persistence-service-server-v1/src/main/java/com/iqser/red/service/peristence/v1/server/service/ManualRedactionService.java @@ -100,8 +100,7 @@ public class ManualRedactionService { private final HashFunction hashFunction = Hashing.murmur3_128(); - public List addAddRedaction(String dossierId, String fileId, - List addRedactionRequests) { + public List addAddRedaction(String dossierId, String fileId, List addRedactionRequests) { var response = new ArrayList(); @@ -114,8 +113,7 @@ public class ManualRedactionService { } validatePositions(fileId, addRedactionRequest); - String annotationId = hashFunction.hashString(fileId + addRedactionRequest, StandardCharsets.UTF_8) - .toString(); + String annotationId = hashFunction.hashString(fileId + addRedactionRequest, StandardCharsets.UTF_8).toString(); addRedactionPersistenceService.insert(fileId, annotationId, addRedactionRequest); Long commentId = null; @@ -143,9 +141,7 @@ public class ManualRedactionService { .collect(Collectors.toList()), ManualRedactionEntry.class, new ManualRedactionMapper()); if (!manualTextRedactions.isEmpty()) { - ManualRedactions manualRedactions = ManualRedactions.builder() - .entriesToAdd(new HashSet<>(manualTextRedactions)) - .build(); + ManualRedactions manualRedactions = ManualRedactions.builder().entriesToAdd(new HashSet<>(manualTextRedactions)).build(); addManualRedactionToAnalysisQueue(dossierId, fileId, manualRedactions); } @@ -179,8 +175,7 @@ public class ManualRedactionService { } - public List addRemoveRedaction(String dossierId, String fileId, - List removeRedactionRequests) { + public List addRemoveRedaction(String dossierId, String fileId, List removeRedactionRequests) { var response = new ArrayList(); var dossier = dossierPersistenceService.getAndValidateDossier(dossierId); @@ -209,18 +204,14 @@ public class ManualRedactionService { .stream() .filter(entry -> entry.getId().equals(removeRedactionRequest.getAnnotationId())) .findFirst(); - actionPerformed = actionPerformed || redactionLogEntryOptional.isPresent() && redactionLogEntryOptional.get() - .isHint(); + actionPerformed = actionPerformed || redactionLogEntryOptional.isPresent() && redactionLogEntryOptional.get().isHint(); } var removedFromDictionary = handleRemoveFromDictionary(redactionLog, dossier, fileId, removeRedactionRequest.getAnnotationId(), removeRedactionRequest.getStatus(), removeRedactionRequest.isRemoveFromDictionary(), false); actionPerformed = actionPerformed || removedFromDictionary; - response.add(ManualAddResponse.builder() - .annotationId(removeRedactionRequest.getAnnotationId()) - .commentId(commentId) - .build()); + response.add(ManualAddResponse.builder().annotationId(removeRedactionRequest.getAnnotationId()).commentId(commentId).build()); } } @@ -234,8 +225,7 @@ public class ManualRedactionService { } - public List addForceRedaction(String dossierId, String fileId, - List forceRedactionRequests) { + public List addForceRedaction(String dossierId, String fileId, List forceRedactionRequests) { var response = new ArrayList(); dossierPersistenceService.getAndValidateDossier(dossierId); @@ -250,10 +240,7 @@ public class ManualRedactionService { commentId = addComment(fileId, forceRedactionRequest.getAnnotationId(), forceRedactionRequest.getComment(), forceRedactionRequest.getUser()).getId(); } actionPerformed = actionPerformed || AnnotationStatus.APPROVED.equals(forceRedactionRequest.getStatus()); - response.add(ManualAddResponse.builder() - .annotationId(forceRedactionRequest.getAnnotationId()) - .commentId(commentId) - .build()); + response.add(ManualAddResponse.builder().annotationId(forceRedactionRequest.getAnnotationId()).commentId(commentId).build()); } if (actionPerformed) { @@ -266,8 +253,7 @@ public class ManualRedactionService { } - public List addLegalBasisChange(String dossierId, String fileId, - List legalBasisChangeRequests) { + public List addLegalBasisChange(String dossierId, String fileId, List legalBasisChangeRequests) { var response = new ArrayList(); dossierPersistenceService.getAndValidateDossier(dossierId); @@ -281,10 +267,7 @@ public class ManualRedactionService { commentId = addComment(fileId, legalBasisChangeRequest.getAnnotationId(), legalBasisChangeRequest.getComment(), legalBasisChangeRequest.getUser()).getId(); } - response.add(ManualAddResponse.builder() - .annotationId(legalBasisChangeRequest.getAnnotationId()) - .commentId(commentId) - .build()); + response.add(ManualAddResponse.builder().annotationId(legalBasisChangeRequest.getAnnotationId()).commentId(commentId).build()); } analysisFlagsCalculationService.calculateFlags(dossierId, fileId); @@ -292,8 +275,7 @@ public class ManualRedactionService { } - public List addImageRecategorization(String dossierId, String fileId, - List imageRecategorizationRequests) { + public List addImageRecategorization(String dossierId, String fileId, List imageRecategorizationRequests) { var response = new ArrayList(); var actionPerformed = false; @@ -310,10 +292,7 @@ public class ManualRedactionService { actionPerformed = actionPerformed || !AnnotationStatus.REQUESTED.equals(imageRecategorizationRequest.getStatus()); - response.add(ManualAddResponse.builder() - .annotationId(imageRecategorizationRequest.getAnnotationId()) - .commentId(commentId) - .build()); + response.add(ManualAddResponse.builder().annotationId(imageRecategorizationRequest.getAnnotationId()).commentId(commentId).build()); } if (actionPerformed) { reprocess(dossierId, fileId); @@ -480,18 +459,25 @@ public class ManualRedactionService { } - public List addResizeRedaction(String dossierId, String fileId, - List resizeRedactionRequests) { + public List addResizeRedaction(String dossierId, String fileId, List resizeRedactionRequests) { var response = new ArrayList(); + RedactionLog redactionLog = redactionLogService.getRedactionLog(dossierId, fileId, true, true); + for (ResizeRedactionRequest resizeRedactionRequest : resizeRedactionRequests) { + Optional redactionLogEntry = redactionLog.getRedactionLogEntry() + .stream() + .filter(r -> r.getId().equals(resizeRedactionRequest.getAnnotationId())) + .findFirst(); resizeRedactionPersistenceService.insert(fileId, resizeRedactionRequest); if (resizeRedactionRequest.getUpdateDictionary() != null && resizeRedactionRequest.getUpdateDictionary() && resizeRedactionRequest.getStatus() .equals(AnnotationStatus.APPROVED)) { - - updateDictionaryForResizeRedactions(dossierId, fileId, resizeRedactionRequest.getAnnotationId(), resizeRedactionRequest.getValue()); + log.debug("Value of ResizeRedactionRequest {}, Value of old entry {}", resizeRedactionRequest.getValue(), redactionLogEntry.map(RedactionLogEntry::getValue) + .orElse(null)); + updateDictionaryForResizeRedactions(dossierId, fileId, resizeRedactionRequest.getAnnotationId(), resizeRedactionRequest.getValue(), redactionLogEntry.map(RedactionLogEntry::getValue) + .orElse(null)); } Long commentId = null; @@ -500,10 +486,7 @@ public class ManualRedactionService { commentId = addComment(fileId, resizeRedactionRequest.getAnnotationId(), resizeRedactionRequest.getComment(), resizeRedactionRequest.getUser()).getId(); } - response.add(ManualAddResponse.builder() - .annotationId(resizeRedactionRequest.getAnnotationId()) - .commentId(commentId) - .build()); + response.add(ManualAddResponse.builder().annotationId(resizeRedactionRequest.getAnnotationId()).commentId(commentId).build()); } reprocess(dossierId, fileId); @@ -514,24 +497,19 @@ public class ManualRedactionService { } - private void updateDictionaryForResizeRedactions(String dossierId, String fileId, String annotationId, - String newValue) { + private void updateDictionaryForResizeRedactions(String dossierId, String fileId, String annotationId, String newValue, String oldValue) { RedactionLog redactionLog = redactionLogService.getRedactionLog(dossierId, fileId, true, true); var dossier = dossierPersistenceService.getAndValidateDossier(dossierId); - Optional redactionLogEntry = redactionLog.getRedactionLogEntry() - .stream() - .filter(r -> r.getId().equals(annotationId)) - .findFirst(); - if (redactionLogEntry.isPresent() && (redactionLogEntry.get().isDictionaryEntry() || redactionLogEntry.get() - .isDossierDictionaryEntry())) { + Optional redactionLogEntry = redactionLog.getRedactionLogEntry().stream().filter(r -> r.getId().equals(annotationId)).findFirst(); + if (redactionLogEntry.isPresent() && (redactionLogEntry.get().isDictionaryEntry() || redactionLogEntry.get().isDossierDictionaryEntry())) { - if (redactionLogEntry.get().getValue().length() > newValue.length()) { - log.info("Remove old value '{}' from dictionary", redactionLogEntry.get().getValue()); - removeFromDictionary(buildTypeId(redactionLogEntry.get(), dossier), redactionLogEntry.get() - .getValue(), dossierId, fileId, getDictionaryEntryType(redactionLogEntry.get())); + log.debug("old: {}, new: {}", oldValue, newValue); + if (oldValue != null && oldValue.length() > newValue.length()) { + log.info("Remove old value '{}' from dictionary", oldValue); + removeFromDictionary(buildTypeId(redactionLogEntry.get(), dossier), oldValue, dossierId, fileId, getDictionaryEntryType(redactionLogEntry.get())); } log.info("Add new value '{}' to dictionary", newValue); @@ -553,8 +531,7 @@ public class ManualRedactionService { @SuppressWarnings("PMD") - public void updateRemoveRedactionStatus(String dossierId, String fileId, List annotationIds, - AnnotationStatus annotationStatus) { + public void updateRemoveRedactionStatus(String dossierId, String fileId, List annotationIds, AnnotationStatus annotationStatus) { var dossier = dossierPersistenceService.getAndValidateDossier(dossierId); RedactionLog redactionLog = redactionLogService.getRedactionLog(dossierId, fileId, true, true); @@ -598,8 +575,7 @@ public class ManualRedactionService { } - public void updateForceRedactionStatus(String dossierId, String fileId, List annotationIds, - AnnotationStatus annotationStatus) { + public void updateForceRedactionStatus(String dossierId, String fileId, List annotationIds, AnnotationStatus annotationStatus) { dossierPersistenceService.getAndValidateDossier(dossierId); for (var annotationId : annotationIds) { @@ -618,8 +594,7 @@ public class ManualRedactionService { @Transactional - public void updateLegalBasisChangeStatus(String dossierId, String fileId, List annotationIds, - AnnotationStatus annotationStatus) { + public void updateLegalBasisChangeStatus(String dossierId, String fileId, List annotationIds, AnnotationStatus annotationStatus) { dossierPersistenceService.getAndValidateDossier(dossierId); for (var annotationId : annotationIds) { @@ -629,8 +604,7 @@ public class ManualRedactionService { } - public void updateImageRecategorizationStatus(String dossierId, String fileId, List annotationIds, - AnnotationStatus annotationStatus) { + public void updateImageRecategorizationStatus(String dossierId, String fileId, List annotationIds, AnnotationStatus annotationStatus) { dossierPersistenceService.getAndValidateDossier(dossierId); @@ -653,15 +627,14 @@ public class ManualRedactionService { } - public void updateResizeRedactionStatus(String dossierId, String fileId, List annotationIds, - AnnotationStatus annotationStatus) { + public void updateResizeRedactionStatus(String dossierId, String fileId, List annotationIds, AnnotationStatus annotationStatus) { dossierPersistenceService.getAndValidateDossier(dossierId); for (var annotationId : annotationIds) { var resizeRedaction = resizeRedactionPersistenceService.findResizeRedaction(fileId, annotationId); if (resizeRedaction.getUpdateDictionary() != null && resizeRedaction.getUpdateDictionary() && annotationStatus.equals(AnnotationStatus.APPROVED)) { - updateDictionaryForResizeRedactions(dossierId, fileId, annotationId, resizeRedaction.getValue()); + updateDictionaryForResizeRedactions(dossierId, fileId, annotationId, resizeRedaction.getValue(), null); } resizeRedactionPersistenceService.updateStatus(fileId, annotationId, annotationStatus); @@ -672,8 +645,7 @@ public class ManualRedactionService { @SuppressWarnings("PMD") - public void updateAddRedactionStatus(String dossierId, String fileId, List annotationIds, - AnnotationStatus annotationStatus) { + public void updateAddRedactionStatus(String dossierId, String fileId, List annotationIds, AnnotationStatus annotationStatus) { var dossier = dossierPersistenceService.getAndValidateDossier(dossierId); for (var annotationId : annotationIds) { @@ -692,8 +664,7 @@ public class ManualRedactionService { } } - addRedactionPersistenceService.updateStatus(fileId, annotationId, annotationStatus, OffsetDateTime.now() - .truncatedTo(ChronoUnit.MILLIS)); + addRedactionPersistenceService.updateStatus(fileId, annotationId, annotationStatus, OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS)); } analysisFlagsCalculationService.calculateFlags(dossierId, fileId); } @@ -740,12 +711,7 @@ public class ManualRedactionService { fileStatusPersistenceService.updateProcessingStatus(fileId, ProcessingStatus.SURROUNDING_TEXT_PROCESSING); - var analyseRequest = AnalyzeRequest.builder() - .messageType(MessageType.SURROUNDING_TEXT) - .dossierId(dossierId) - .fileId(fileId) - .manualRedactions(manualRedactions) - .build(); + var analyseRequest = AnalyzeRequest.builder().messageType(MessageType.SURROUNDING_TEXT).dossierId(dossierId).fileId(fileId).manualRedactions(manualRedactions).build(); try { rabbitTemplate.convertAndSend(MessagingConfiguration.REDACTION_QUEUE, objectMapper.writeValueAsString(analyseRequest), message -> { @@ -759,13 +725,11 @@ public class ManualRedactionService { } - private void approveStatusForRedactionsWithSameValue(DossierEntity dossier, boolean addToDictionary, - boolean addToDossierDictionary, String value) { + private void approveStatusForRedactionsWithSameValue(DossierEntity dossier, boolean addToDictionary, boolean addToDossierDictionary, String value) { List dossiers = new ArrayList<>(); if (addToDictionary) { - dossiers = dossierTemplatePersistenceService.getDossierTemplate(dossier.getDossierTemplateId()) - .getDossiers(); + dossiers = dossierTemplatePersistenceService.getDossierTemplate(dossier.getDossierTemplateId()).getDossiers(); } if (addToDossierDictionary) { @@ -791,8 +755,7 @@ public class ManualRedactionService { } - private boolean handleRemoveFromDictionary(RedactionLog redactionLog, DossierEntity dossier, String fileId, - String annotationId, AnnotationStatus status, + private boolean handleRemoveFromDictionary(RedactionLog redactionLog, DossierEntity dossier, String fileId, String annotationId, AnnotationStatus status, boolean removeFromDictionary, boolean revert) { if (status == AnnotationStatus.APPROVED) { @@ -824,20 +787,21 @@ public class ManualRedactionService { return false; } + private void removeResizeRedactionsWithAddToDictionary(String dossierTemplateId, String redactionLogEntryValue) { + var resizeRedactionsWithSameValue = resizeRedactionPersistenceService.findByAnnotationStatusAndValue(AnnotationStatus.APPROVED, redactionLogEntryValue); resizeRedactionsWithSameValue.forEach(resizeRedaction -> { var file = fileStatusPersistenceService.getStatus(resizeRedaction.getId().getFileId()); var dossierForResizeRedaction = dossierPersistenceService.findByDossierId(file.getDossierId()); - if(!file.getWorkflowStatus().equals(WorkflowStatus.APPROVED) && dossierTemplateId.equals(dossierForResizeRedaction.getDossierTemplateId())) { - resizeRedactionPersistenceService.hardDelete(resizeRedaction.getId() - .getFileId(), resizeRedaction.getId().getAnnotationId()); + if (!file.getWorkflowStatus().equals(WorkflowStatus.APPROVED) && dossierTemplateId.equals(dossierForResizeRedaction.getDossierTemplateId())) { + resizeRedactionPersistenceService.hardDelete(resizeRedaction.getId().getFileId(), resizeRedaction.getId().getAnnotationId()); } }); } - private void addToDictionary(String typeId, String value, String dossierId, String fileId, - DictionaryEntryType dictionaryEntryType) { + + private void addToDictionary(String typeId, String value, String dossierId, String fileId, DictionaryEntryType dictionaryEntryType) { try { log.debug("Adding entry: {} to {} for {} / {}", value, typeId, dossierId, fileId); @@ -848,8 +812,7 @@ public class ManualRedactionService { } - private void removeFromDictionary(String typeId, String value, String dossierId, String fileId, - DictionaryEntryType dictionaryEntryType) { + private void removeFromDictionary(String typeId, String value, String dossierId, String fileId, DictionaryEntryType dictionaryEntryType) { try { log.debug("Deleting entries to {} for {} / {}", typeId, dossierId, fileId); @@ -872,10 +835,8 @@ public class ManualRedactionService { } - private boolean handleAddToDictionary(String fileId, String annotationId, String typeId, String value, - AnnotationStatus status, boolean addToDictionary, - boolean addToDossierDictionary, boolean revert, String dossierId, - DictionaryEntryType dictionaryEntryType) { + private boolean handleAddToDictionary(String fileId, String annotationId, String typeId, String value, AnnotationStatus status, boolean addToDictionary, + boolean addToDossierDictionary, boolean revert, String dossierId, DictionaryEntryType dictionaryEntryType) { if (status == AnnotationStatus.APPROVED) { addRedactionPersistenceService.updateStatus(fileId, annotationId, status, addToDictionary, addToDossierDictionary);