From 98ecc4854d520f02c48dd8bdba4ea90f44a2c184 Mon Sep 17 00:00:00 2001 From: Ali Oezyetimoglu Date: Tue, 30 Aug 2022 16:30:34 +0200 Subject: [PATCH] RED-4895: Mechanism to remove longer redactions when they are shortened does not work for Suggestions/Approve --- .../service/ManualRedactionService.java | 106 +++++++++--------- 1 file changed, 50 insertions(+), 56 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 709eb7142..0ea2b9a0e 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 @@ -121,7 +121,8 @@ public class ManualRedactionService { commentId = addComment(fileId, annotationId, addRedactionRequest.getComment(), addRedactionRequest.getUser()).getId(); } - var addedToDictionary = handleAddToDictionary(fileId, annotationId, addRedactionRequest.getTypeId(), addRedactionRequest.getValue(), addRedactionRequest.getStatus(), addRedactionRequest.isAddToDictionary(), addRedactionRequest.isAddToDossierDictionary(), false, dossierId, addRedactionRequest.getDictionaryEntryType()); + var addedToDictionary = handleAddToDictionary(fileId, annotationId, addRedactionRequest.getTypeId(), addRedactionRequest.getValue(), addRedactionRequest.getStatus(), + addRedactionRequest.isAddToDictionary(), addRedactionRequest.isAddToDossierDictionary(), false, dossierId, addRedactionRequest.getDictionaryEntryType()); actionPerformed = actionPerformed || addedToDictionary; @@ -211,7 +212,8 @@ public class ManualRedactionService { actionPerformed = actionPerformed || redactionLogEntryOptional.isPresent() && redactionLogEntryOptional.get().isHint(); } - var removedFromDictionary = handleRemoveFromDictionary(redactionLog, dossier, fileId, removeRedactionRequest.getAnnotationId(), removeRedactionRequest.getStatus(), removeRedactionRequest.isRemoveFromDictionary(), false); + var removedFromDictionary = handleRemoveFromDictionary(redactionLog, dossier, fileId, removeRedactionRequest.getAnnotationId(), removeRedactionRequest.getStatus(), + removeRedactionRequest.isRemoveFromDictionary(), false); actionPerformed = actionPerformed || removedFromDictionary; @@ -291,7 +293,8 @@ public class ManualRedactionService { Long commentId = null; if (imageRecategorizationRequest.getComment() != null) { - commentId = addComment(fileId, imageRecategorizationRequest.getAnnotationId(), imageRecategorizationRequest.getComment(), imageRecategorizationRequest.getUser()).getId(); + commentId = addComment(fileId, imageRecategorizationRequest.getAnnotationId(), imageRecategorizationRequest.getComment(), + imageRecategorizationRequest.getUser()).getId(); } actionPerformed = actionPerformed || !AnnotationStatus.REQUESTED.equals(imageRecategorizationRequest.getStatus()); @@ -367,7 +370,8 @@ public class ManualRedactionService { for (var annotationId : annotationIds) { var addRedaction = getAddRedaction(fileId, annotationId); - actionPerformed = actionPerformed || handleAddToDictionary(fileId, annotationId, addRedaction.getTypeId(), addRedaction.getValue(), addRedaction.getStatus(), addRedaction.isAddToDictionary(), addRedaction.isAddToDossierDictionary(), true, dossier.getId(), addRedaction.getDictionaryEntryType()); + actionPerformed = actionPerformed || handleAddToDictionary(fileId, annotationId, addRedaction.getTypeId(), addRedaction.getValue(), addRedaction.getStatus(), + addRedaction.isAddToDictionary(), addRedaction.isAddToDossierDictionary(), true, dossier.getId(), addRedaction.getDictionaryEntryType()); addRedactionPersistenceService.softDelete(fileId, annotationId, OffsetDateTime.now()); } @@ -389,7 +393,8 @@ public class ManualRedactionService { var removeRedaction = getRemoveRedaction(fileId, annotationId); - var removedFromDictionary = handleRemoveFromDictionary(redactionLog, dossier, fileId, annotationId, removeRedaction.getStatus(), removeRedaction.isRemoveFromDictionary(), true); + var removedFromDictionary = handleRemoveFromDictionary(redactionLog, dossier, fileId, annotationId, removeRedaction.getStatus(), + removeRedaction.isRemoveFromDictionary(), true); actionPerformed = actionPerformed || removedFromDictionary; removeRedactionPersistenceService.softDelete(fileId, annotationId, OffsetDateTime.now()); @@ -470,30 +475,18 @@ public class ManualRedactionService { 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)) { - 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)); - } + var resizeRedaction = resizeRedactionPersistenceService.insert(fileId, resizeRedactionRequest); - Long commentId = null; if (resizeRedactionRequest.getComment() != null) { - - commentId = addComment(fileId, resizeRedactionRequest.getAnnotationId(), resizeRedactionRequest.getComment(), resizeRedactionRequest.getUser()).getId(); + var 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()); + updateDictionaryForResizeRedactions(dossierId, fileId, resizeRedaction, redactionLog); } - - if(resizeRedactionRequests.stream().anyMatch(resize -> AnnotationStatus.APPROVED.equals(resize.getStatus()))) { + + if (resizeRedactionRequests.stream().anyMatch(resize -> AnnotationStatus.APPROVED.equals(resize.getStatus()))) { reprocess(dossierId, fileId); } @@ -503,23 +496,35 @@ public class ManualRedactionService { } - private void updateDictionaryForResizeRedactions(String dossierId, String fileId, String annotationId, String newValue, String oldValue) { + private void updateDictionaryForResizeRedactions(String dossierId, String fileId, ManualResizeRedactionEntity resizeRedaction, RedactionLog redactionLog) { - RedactionLog redactionLog = redactionLogService.getRedactionLog(dossierId, fileId, true, true); + Optional redactionLogEntryOptional = redactionLog.getRedactionLogEntry() + .stream() + .filter(r -> r.getId().equals(resizeRedaction.getId().getAnnotationId())) + .findFirst(); - var dossier = dossierPersistenceService.getAndValidateDossier(dossierId); + if (redactionLogEntryOptional.isEmpty()) { + return; + } - Optional redactionLogEntry = redactionLog.getRedactionLogEntry().stream().filter(r -> r.getId().equals(annotationId)).findFirst(); - if (redactionLogEntry.isPresent() && (redactionLogEntry.get().isDictionaryEntry() || redactionLogEntry.get().isDossierDictionaryEntry())) { + var redactionLogEntry = redactionLogEntryOptional.get(); + + if (resizeRedaction.getUpdateDictionary() != null && resizeRedaction.getUpdateDictionary() && resizeRedaction.getStatus() + .equals(AnnotationStatus.APPROVED) && (redactionLogEntry.isDictionaryEntry() || redactionLogEntry.isDossierDictionaryEntry())) { + var dossier = dossierPersistenceService.findByDossierId(dossierId); + + var typeId = buildTypeId(redactionLogEntry, dossier); + var newValue = resizeRedaction.getValue(); + var oldValue = redactionLogEntry.getValue(); + var dictionaryEntryType = getDictionaryEntryType(redactionLogEntry); - 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())); + removeFromDictionary(buildTypeId(redactionLogEntry, dossier), oldValue, dossierId, fileId, dictionaryEntryType); } log.info("Add new value '{}' to dictionary", newValue); - addToDictionary(buildTypeId(redactionLogEntry.get(), dossier), newValue, dossierId, fileId, getDictionaryEntryType(redactionLogEntry.get())); + addToDictionary(typeId, newValue, dossierId, fileId, dictionaryEntryType); } } @@ -589,8 +594,7 @@ public class ManualRedactionService { for (var annotationId : annotationIds) { var forceRedaction = forceRedactionPersistenceService.findForceRedaction(fileId, annotationId); forceRedactionPersistenceService.updateStatus(fileId, annotationId, annotationStatus); - actionPerformed = actionPerformed || !(forceRedaction.getStatus() == AnnotationStatus.REQUESTED - && annotationStatus == AnnotationStatus.DECLINED); + actionPerformed = actionPerformed || !(forceRedaction.getStatus() == AnnotationStatus.REQUESTED && annotationStatus == AnnotationStatus.DECLINED); } if (actionPerformed) { @@ -626,8 +630,7 @@ public class ManualRedactionService { for (var annotationId : annotationIds) { var imageRecategorization = recategorizationPersistenceService.findRecategorization(fileId, annotationId); recategorizationPersistenceService.updateStatus(fileId, annotationId, annotationStatus); - actionPerformed = actionPerformed || !(imageRecategorization.getStatus() == AnnotationStatus.REQUESTED - && annotationStatus == AnnotationStatus.DECLINED); + actionPerformed = actionPerformed || !(imageRecategorization.getStatus() == AnnotationStatus.REQUESTED && annotationStatus == AnnotationStatus.DECLINED); } if (actionPerformed) { @@ -652,25 +655,19 @@ public class ManualRedactionService { dossierPersistenceService.getAndValidateDossier(dossierId); var actionPerformed = false; + + RedactionLog redactionLog = redactionLogService.getRedactionLog(dossierId, fileId, true, true); + for (var annotationId : annotationIds) { var resizeRedaction = resizeRedactionPersistenceService.findResizeRedaction(fileId, annotationId); - RedactionLog redactionLog = redactionLogService.getRedactionLog(dossierId, fileId, true, true); - Optional redactionLogEntry = redactionLog.getRedactionLogEntry() - .stream() - .filter(r -> r.getId().equals(annotationId)) - .findFirst(); - - if (resizeRedaction.getUpdateDictionary() != null && resizeRedaction.getUpdateDictionary() && annotationStatus.equals(AnnotationStatus.APPROVED)) { - updateDictionaryForResizeRedactions(dossierId, fileId, annotationId, resizeRedaction.getValue(), redactionLogEntry.map(RedactionLogEntry::getValue) - .orElse(null)); - } - - actionPerformed = actionPerformed || !(resizeRedaction.getStatus() == AnnotationStatus.REQUESTED - && annotationStatus == AnnotationStatus.DECLINED); + actionPerformed = actionPerformed || !(resizeRedaction.getStatus() == AnnotationStatus.REQUESTED && annotationStatus == AnnotationStatus.DECLINED); resizeRedactionPersistenceService.updateStatus(fileId, annotationId, annotationStatus); + resizeRedaction.setStatus(annotationStatus); + + updateDictionaryForResizeRedactions(dossierId, fileId, resizeRedaction, redactionLog); } analysisFlagsCalculationService.calculateFlags(dossierId, fileId); @@ -690,7 +687,8 @@ public class ManualRedactionService { if (annotationStatus == AnnotationStatus.APPROVED) { addToDictionary(manualRedactionEntry.getTypeId(), manualRedactionEntry.getValue(), dossierId, fileId, manualRedactionEntry.getDictionaryEntryType()); reprocess(dossierId, fileId); - approveStatusForRedactionsWithSameValue(dossier, manualRedactionEntry.isAddToDictionary(), manualRedactionEntry.isAddToDossierDictionary(), manualRedactionEntry.getValue()); + approveStatusForRedactionsWithSameValue(dossier, manualRedactionEntry.isAddToDictionary(), manualRedactionEntry.isAddToDossierDictionary(), + manualRedactionEntry.getValue()); } else if (annotationStatus == AnnotationStatus.DECLINED) { // if it was previously approved, revert the add @@ -790,6 +788,7 @@ public class ManualRedactionService { fileStatusService.setStatusReprocessForManual(dossierId, fileId, true); } + private boolean handleRemoveFromDictionary(RedactionLog redactionLog, DossierEntity dossier, String fileId, String annotationId, AnnotationStatus status, boolean removeFromDictionary, boolean revert) { @@ -860,13 +859,8 @@ public class ManualRedactionService { private CommentEntity addComment(String fileId, String annotationId, String comment, String user) { - return commentPersistenceService.insert(CommentEntity.builder() - .text(comment) - .fileId(fileId) - .annotationId(annotationId) - .user(user) - .date(OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS)) - .build()); + return commentPersistenceService.insert( + CommentEntity.builder().text(comment).fileId(fileId).annotationId(annotationId).user(user).date(OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS)).build()); }