Merge branch 'RED-7384' into 'release/4.244.x'

RED-7384: migration fixes

See merge request redactmanager/redaction-service!339
This commit is contained in:
Kilian Schüttler 2024-03-26 10:00:07 +01:00
commit 455137131c
5 changed files with 43 additions and 20 deletions

View File

@ -1,11 +1,15 @@
package com.iqser.red.service.redaction.v1.model; package com.iqser.red.service.redaction.v1.model;
import java.util.Collections;
import java.util.Set;
import com.iqser.red.service.persistence.service.v1.api.shared.model.annotations.ManualRedactions; import com.iqser.red.service.persistence.service.v1.api.shared.model.annotations.ManualRedactions;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.NonNull;
@Data @Data
@Builder @Builder
@ -13,9 +17,16 @@ import lombok.NoArgsConstructor;
@AllArgsConstructor @AllArgsConstructor
public class MigrationRequest { public class MigrationRequest {
@NonNull
String dossierTemplateId; String dossierTemplateId;
@NonNull
String dossierId; String dossierId;
@NonNull
String fileId; String fileId;
@NonNull
ManualRedactions manualRedactions; ManualRedactions manualRedactions;
@NonNull
@Builder.Default
Set<String> entitiesWithComments = Collections.emptySet();
} }

View File

@ -67,7 +67,8 @@ public class MigrationMessageReceiver {
document, document,
migrationRequest.getDossierTemplateId(), migrationRequest.getDossierTemplateId(),
migrationRequest.getManualRedactions(), migrationRequest.getManualRedactions(),
migrationRequest.getFileId()); migrationRequest.getFileId(),
migrationRequest.getEntitiesWithComments());
log.info("Storing migrated entityLog and ids to migrate in DB for file {}", migrationRequest.getFileId()); log.info("Storing migrated entityLog and ids to migrate in DB for file {}", migrationRequest.getFileId());
redactionStorageService.storeObject(migrationRequest.getDossierId(), migrationRequest.getFileId(), FileType.ENTITY_LOG, migratedEntityLog.getEntityLog()); redactionStorageService.storeObject(migrationRequest.getDossierId(), migrationRequest.getFileId(), FileType.ENTITY_LOG, migratedEntityLog.getEntityLog());

View File

@ -8,6 +8,7 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -57,7 +58,12 @@ public class RedactionLogToEntityLogMigrationService {
ManualChangesApplicationService manualChangesApplicationService; ManualChangesApplicationService manualChangesApplicationService;
public MigratedEntityLog migrate(RedactionLog redactionLog, Document document, String dossierTemplateId, ManualRedactions manualRedactions, String fileId) { public MigratedEntityLog migrate(RedactionLog redactionLog,
Document document,
String dossierTemplateId,
ManualRedactions manualRedactions,
String fileId,
Set<String> entitiesWithComments) {
log.info("Migrating entities for file {}", fileId); log.info("Migrating entities for file {}", fileId);
List<MigrationEntity> entitiesToMigrate = calculateMigrationEntitiesFromRedactionLog(redactionLog, document, dossierTemplateId, fileId); List<MigrationEntity> entitiesToMigrate = calculateMigrationEntitiesFromRedactionLog(redactionLog, document, dossierTemplateId, fileId);
@ -89,15 +95,14 @@ public class RedactionLogToEntityLogMigrationService {
if (getNumberOfApprovedEntries(redactionLog, document.getNumberOfPages()) != entityLog.getEntityLogEntry().size()) { if (getNumberOfApprovedEntries(redactionLog, document.getNumberOfPages()) != entityLog.getEntityLogEntry().size()) {
String message = String.format("Not all entities have been found during the migration redactionLog has %d entries and new entityLog %d", String message = String.format("Not all entities have been found during the migration redactionLog has %d entries and new entityLog %d",
redactionLog.getRedactionLogEntry() redactionLog.getRedactionLogEntry().size(),
.size(),
entityLog.getEntityLogEntry().size()); entityLog.getEntityLogEntry().size());
log.error(message); log.error(message);
throw new AssertionError(message); throw new AssertionError(message);
} }
MigratedIds idsToMigrateInDb = entitiesToMigrate.stream() MigratedIds idsToMigrateInDb = entitiesToMigrate.stream()
.filter(MigrationEntity::hasManualChangesOrComments) .filter(migrationEntity -> migrationEntity.hasManualChangesOrComments(entitiesWithComments))
.filter(m -> !m.getOldId().equals(m.getNewId())) .filter(m -> !m.getOldId().equals(m.getNewId()))
.collect(new MigratedIdsCollector()); .collect(new MigratedIdsCollector());
@ -136,7 +141,10 @@ public class RedactionLogToEntityLogMigrationService {
private long getNumberOfApprovedEntries(RedactionLog redactionLog, int numberOfPages) { private long getNumberOfApprovedEntries(RedactionLog redactionLog, int numberOfPages) {
return redactionLog.getRedactionLogEntry().stream().filter(redactionLogEntry -> isOnExistingPage(redactionLogEntry, numberOfPages)).collect(Collectors.toList()).size(); return redactionLog.getRedactionLogEntry()
.stream()
.filter(redactionLogEntry -> isOnExistingPage(redactionLogEntry, numberOfPages))
.count();
} }
@ -288,17 +296,19 @@ public class RedactionLogToEntityLogMigrationService {
} }
private boolean isOnExistingPage(RedactionLogEntry redactionLogEntry, int numberOfPages) {
private boolean isOnExistingPage(RedactionLogEntry redactionLogEntry, int numberOfPages){ var pages = redactionLogEntry.getPositions()
var pages = redactionLogEntry.getPositions().stream().map(Rectangle::getPage).collect(Collectors.toSet()); .stream()
.map(Rectangle::getPage)
.collect(Collectors.toSet());
for (int page: pages){ for (int page : pages) {
if(page > numberOfPages){ if (page > numberOfPages) {
return false; return false;
} }
} }
return true; return true;
} }
} }

View File

@ -194,9 +194,6 @@ public final class MigrationEntity {
entityLogEntry.setReference(migrateSetOfIds(redactionLogEntry.getReference(), oldToNewIdMapping)); entityLogEntry.setReference(migrateSetOfIds(redactionLogEntry.getReference(), oldToNewIdMapping));
entityLogEntry.setImportedRedactionIntersections(migrateSetOfIds(redactionLogEntry.getImportedRedactionIntersections(), oldToNewIdMapping)); entityLogEntry.setImportedRedactionIntersections(migrateSetOfIds(redactionLogEntry.getImportedRedactionIntersections(), oldToNewIdMapping));
entityLogEntry.setEngines(MigrationMapper.getMigratedEngines(redactionLogEntry)); entityLogEntry.setEngines(MigrationMapper.getMigratedEngines(redactionLogEntry));
if (redactionLogEntry.getLegalBasis() != null) {
entityLogEntry.setLegalBasis(redactionLogEntry.getLegalBasis());
}
if (entityLogEntry.getEntryType().equals(EntryType.HINT) && lastManualChangeIsRemoveLocally(entityLogEntry)) { if (entityLogEntry.getEntryType().equals(EntryType.HINT) && lastManualChangeIsRemoveLocally(entityLogEntry)) {
entityLogEntry.setState(EntryState.IGNORED); entityLogEntry.setState(EntryState.IGNORED);
@ -236,7 +233,8 @@ public final class MigrationEntity {
.value(image.getValue()) .value(image.getValue())
.type(imageType) .type(imageType)
.reason(image.buildReasonWithManualChangeDescriptions()) .reason(image.buildReasonWithManualChangeDescriptions())
.legalBasis(image.legalBasis()) .legalBasis(image.getManualOverwrite().getLegalBasis()
.orElse(redactionLogEntry.getLegalBasis()))
.matchedRule(image.getMatchedRule().getRuleIdentifier().toString()) .matchedRule(image.getMatchedRule().getRuleIdentifier().toString())
.dictionaryEntry(false) .dictionaryEntry(false)
.positions(positions) .positions(positions)
@ -259,7 +257,8 @@ public final class MigrationEntity {
return EntityLogEntry.builder() return EntityLogEntry.builder()
.id(precursorEntity.getId()) .id(precursorEntity.getId())
.reason(precursorEntity.buildReasonWithManualChangeDescriptions()) .reason(precursorEntity.buildReasonWithManualChangeDescriptions())
.legalBasis(precursorEntity.legalBasis()) .legalBasis(precursorEntity.getManualOverwrite().getLegalBasis()
.orElse(redactionLogEntry.getLegalBasis()))
.value(precursorEntity.value()) .value(precursorEntity.value())
.type(precursorEntity.type()) .type(precursorEntity.type())
.state(buildEntryState(precursorEntity)) .state(buildEntryState(precursorEntity))
@ -293,7 +292,8 @@ public final class MigrationEntity {
.id(entity.getId()) .id(entity.getId())
.positions(rectanglesPerLine) .positions(rectanglesPerLine)
.reason(entity.buildReasonWithManualChangeDescriptions()) .reason(entity.buildReasonWithManualChangeDescriptions())
.legalBasis(entity.legalBasis()) .legalBasis(entity.getManualOverwrite().getLegalBasis()
.orElse(redactionLogEntry.getLegalBasis()))
.value(entity.getManualOverwrite().getValue() .value(entity.getManualOverwrite().getValue()
.orElse(entity.getMatchedRule().isWriteValueWithLineBreaks() ? entity.getValueWithLineBreaks() : entity.getValue())) .orElse(entity.getMatchedRule().isWriteValueWithLineBreaks() ? entity.getValueWithLineBreaks() : entity.getValue()))
.type(entity.type()) .type(entity.type())
@ -336,11 +336,11 @@ public final class MigrationEntity {
} }
public boolean hasManualChangesOrComments() { public boolean hasManualChangesOrComments(Set<String> entitiesWithComments) {
return !(redactionLogEntry.getManualChanges() == null || redactionLogEntry.getManualChanges().isEmpty()) || // return !(redactionLogEntry.getManualChanges() == null || redactionLogEntry.getManualChanges().isEmpty()) || //
!(redactionLogEntry.getComments() == null || redactionLogEntry.getComments().isEmpty()) // !(redactionLogEntry.getComments() == null || redactionLogEntry.getComments().isEmpty()) //
|| hasManualChanges(); || hasManualChanges() || entitiesWithComments.contains(oldId);
} }

View File

@ -177,7 +177,8 @@ public class MigrationIntegrationTest extends BuildDocumentIntegrationTest {
document, document,
TEST_DOSSIER_TEMPLATE_ID, TEST_DOSSIER_TEMPLATE_ID,
manualRedactions, manualRedactions,
TEST_FILE_ID); TEST_FILE_ID,
Collections.emptySet());
redactionStorageService.storeObject(TEST_DOSSIER_ID, TEST_FILE_ID, FileType.ENTITY_LOG, migratedEntityLog.getEntityLog()); redactionStorageService.storeObject(TEST_DOSSIER_ID, TEST_FILE_ID, FileType.ENTITY_LOG, migratedEntityLog.getEntityLog());
assertEquals(mergedRedactionLog.getRedactionLogEntry().size(), migratedEntityLog.getEntityLog().getEntityLogEntry().size()); assertEquals(mergedRedactionLog.getRedactionLogEntry().size(), migratedEntityLog.getEntityLog().getEntityLogEntry().size());