RED-7026_RED-6743_RED-6734- Simplified redaction flow #39

Merged
corina.olariu.ext1 merged 1 commits from RED-7026_RED-6743_RED-6734 into master 2023-07-03 09:02:48 +02:00
4 changed files with 105 additions and 13 deletions

View File

@ -1,6 +1,7 @@
package com.iqser.red.service.persistence.management.v1.processor.service;
import static com.iqser.red.service.persistence.management.v1.processor.utils.TypeIdUtils.getDossierIdFromTypeId;
import static com.iqser.red.service.persistence.management.v1.processor.utils.TypeIdUtils.getDossierTemplateIdFromTypeId;
import static com.iqser.red.service.persistence.management.v1.processor.utils.TypeIdUtils.getDosssierTemplateTypeIdFromTypeId;
import static com.iqser.red.service.persistence.management.v1.processor.utils.TypeIdUtils.isDossierTypeId;
import static java.util.stream.Collectors.toSet;
@ -46,6 +47,7 @@ public class DictionaryManagementService {
private final DictionaryPersistenceService dictionaryPersistenceService;
private final ColorsService colorsService;
private final StopwordService stopwordService;
private final DossierService dossierService;
public List<DictionaryEntry> getAllEntriesInDossierTemplate(String dossierTemplateTypeId, String value){
@ -269,17 +271,29 @@ public class DictionaryManagementService {
try {
dictionaryPersistenceService.getType(typeId);
} catch (NotFoundException e) {
// type not found, it should be created based on the info from the dossier template type and if flag dossierDictionaryOnly is also true
// type not found check first dossier is matching the specified dossier template
var dossierId = getDossierIdFromTypeId(typeId);
var dossierTemplateId = getDossierTemplateIdFromTypeId(typeId);
log.info("--> " + dossierId + " - " + dossierTemplateId);
checkDossierMatchesDossierTemplate(dossierId, dossierTemplateId);
// type not found, it should be created based on the info from the dossier template type
var dossierTemplateType = dictionaryPersistenceService.getType(getDosssierTemplateTypeIdFromTypeId(typeId));
Type dossierDictionaryType = MagicConverter.convert(dossierTemplateType, Type.class);
dossierDictionaryType.setVersion(0);
dossierDictionaryType.setDossierId(getDossierIdFromTypeId(typeId));
dossierDictionaryType.setDossierId(dossierId);
var returnedType = this.addType(dossierDictionaryType);
log.info("Type added: " + returnedType.toString());
}
}
}
public void checkDossierMatchesDossierTemplate(String dossierId, String dossierTemplateId) {
var dossier = dossierService.getDossierById(dossierId);
if (!dossier.getDossierTemplateId().equals(dossierTemplateId)) {
throw new BadRequestException(String.format("Dossier %s and dossier template %s mismatched", dossierId, dossierTemplateId));
}
}
private String cleanDictionaryEntry(String entry) {

View File

@ -61,6 +61,7 @@ public class DictionaryService {
private final AccessControlService accessControlService;
private final EntryPersistenceService entryPersistenceService;
private final DictionaryMergeService dictionaryMergeService;
private final DossierService dossierService;
@PreAuthorize("hasAuthority('" + ADD_DICTIONARY_ENTRY + "')")
@ -100,9 +101,12 @@ public class DictionaryService {
@PreAuthorize("hasAuthority('" + DELETE_DOSSIER_DICTIONARY_ENTRY + "')")
public void deleteDossierEntries(String type, String dossierTemplateId, List<String> entries, String dossierId, DictionaryEntryType dictionaryEntryType) {
accessControlService.verifyUserHasAccessPermissions(dossierId);
accessControlService.verifyUserIsMemberOrApprover(dossierId);
//TODO: check if dossier type exists and dossier template type exists
try {
accessControlService.verifyUserHasAccessPermissions(dossierId);
accessControlService.verifyUserIsMemberOrApprover(dossierId);
} catch (AccessDeniedException e) {
throw new NotFoundException("Object not found");
}
deleteEntries(toTypeId(type, dossierTemplateId, dossierId), entries, dictionaryEntryType);
}
@ -208,6 +212,7 @@ public class DictionaryService {
if (dossierId != null) {
try {
accessControlService.verifyUserHasViewPermissions(dossierId);
dictionaryManagementService.checkDossierMatchesDossierTemplate(dossierId, dossierTemplateId);
// for every dossier template type check if a dossier type exists
types.forEach(t -> dictionaryManagementService.checkForDossierTypeExistenceAndCreate(toTypeId(t.getType(), t.getDossierTemplateId(), dossierId)));
types.addAll(MagicConverter.convert(dictionaryPersistenceService.getAllTypesForDossier(dossierId, includeDeleted), Type.class));
@ -327,15 +332,21 @@ public class DictionaryService {
Dictionary dict = Dictionary.builder()
.entries(cdm.getEntries()
.stream()
.filter(e -> !e.isDeleted())
.map(DictionaryEntry::getValue)
.sorted(Comparator.comparing(StringUtils::lowerCase))
.collect(Collectors.toList()))
.falsePositiveEntries(cdm.getFalsePositives()
.stream()
.filter(e -> !e.isDeleted())
.map(DictionaryEntry::getValue)
.sorted(Comparator.comparing(StringUtils::lowerCase))
.collect(Collectors.toList()))
.falseRecommendationEntries(cdm.getFalseRecommendations()
.stream()
.filter(e -> !e.isDeleted())
.map(DictionaryEntry::getValue)
.sorted(Comparator.comparing(StringUtils::lowerCase))
.collect(Collectors.toList()))
.hexColor(entity.getHexColor())
.recommendationHexColor(entity.getRecommendationHexColor())

View File

@ -21,13 +21,19 @@ public class TypeIdUtils {
}
public static String getDossierIdFromTypeId(String typeId) {
var index = typeId.lastIndexOf(":");
var index = typeId.lastIndexOf(':');
return typeId.substring(index + 1);
}
public static String getDosssierTemplateTypeIdFromTypeId(String typeId) {
var index = typeId.lastIndexOf(":");
var index = typeId.lastIndexOf(':');
return typeId.substring(0, index);
}
public static String getDossierTemplateIdFromTypeId(String typeId) {
var firstIndex = typeId.indexOf(':');
var secondIndex = typeId.lastIndexOf(':');
return secondIndex == -1 ? typeId.substring(firstIndex + 1) : typeId.substring(firstIndex + 1, secondIndex);
}
}

View File

@ -2,15 +2,19 @@ package com.iqser.red.service.peristence.v1.server.integration.tests;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.IntStream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import com.iqser.red.service.peristence.v1.server.integration.client.DictionaryClient;
import com.iqser.red.service.peristence.v1.server.integration.client.DossierTemplateClient;
@ -369,6 +373,19 @@ public class DictionaryTest extends AbstractPersistenceServerServiceTest {
assertThat(dictionary.getEntries().size()).isEqualTo(5);
}
@Test
public void testDossierTemplateAndDossierMissmatched() {
var dossierTemplate = dossierTemplateTesterAndProvider.provideTestTemplate();
var dossierTemplate1 = dossierTemplateTesterAndProvider.provideTestTemplate("dossierTemplate1");
var dossier = dossierTesterAndProvider.provideTestDossier(dossierTemplate, "Dossier");
var type = typeProvider.testAndProvideType(dossierTemplate);
Assertions.assertThrows(FeignException.BadRequest.class, () -> dictionaryClient.getAllTypes(dossierTemplate1.getDossierTemplateId(), dossier.getDossierId(), false));
Assertions.assertThrows(FeignException.BadRequest.class, () -> dictionaryClient.getDictionaryForType(type.getType(), dossierTemplate1.getDossierTemplateId(), dossier.getDossierId()));
}
@Test
public void testGetMergedDictionaries() {
@ -394,7 +411,48 @@ public class DictionaryTest extends AbstractPersistenceServerServiceTest {
assertThat(loadedType1.getFalsePositiveEntries()).hasSize(2);
assertThat(loadedType1.getFalseRecommendationEntries()).hasSize(2);
dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), List.of("word1", "word2", "word4"), false, dossier.getDossierId(), DictionaryEntryType.ENTRY);
dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), List.of("word1", "word4"), false, dossier.getDossierId(), DictionaryEntryType.ENTRY);
dictionaryClient.deleteEntries(type.getType(), type.getDossierTemplateId(), List.of("word2"), dossier.getDossierId(), DictionaryEntryType.ENTRY);
dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), List.of("false_positive1", "false_positive3"), false, dossier.getDossierId(), DictionaryEntryType.FALSE_POSITIVE);
dictionaryClient.addEntry(type.getType(),
type.getDossierTemplateId(),
List.of("false_recommendation3", "false_recommendation4"),
false,
dossier.getDossierId(),
DictionaryEntryType.FALSE_RECOMMENDATION);
Exception exception = Assertions.assertThrows(FeignException.Unauthorized.class, () -> dictionaryClient.getMergedDictionaries(type.getType() + ";", dossierTemplate.getDossierTemplateId(), dossier.getDossierId()));
Dictionary mergedDict = dictionaryClient.getMergedDictionaries(type.getType(), dossierTemplate.getDossierTemplateId(), dossier.getDossierId());
assertThat(mergedDict).isNotNull();
assertThat(mergedDict.getEntries().size()).isEqualTo(3);
assertThat(mergedDict.getFalsePositiveEntries().size()).isEqualTo(3);
assertThat(mergedDict.getFalseRecommendationEntries().size()).isEqualTo(4);
}
@Test
public void testGetDictionaryForType() {
var dossierTemplate = dossierTemplateTesterAndProvider.provideTestTemplate();
var dossier = dossierTesterAndProvider.provideTestDossier(dossierTemplate, "Dossier");
var type = typeProvider.testAndProvideType(dossierTemplate);
dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), List.of("word1", "word2", "word3"), false, null, DictionaryEntryType.ENTRY);
dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), List.of("false_positive1", "false_positive"), false, null, DictionaryEntryType.FALSE_POSITIVE);
dictionaryClient.addEntry(type.getType(),
type.getDossierTemplateId(),
List.of("false_recommendation1", "false_recommendation2"),
false,
null,
DictionaryEntryType.FALSE_RECOMMENDATION);
var loadedType1 = dictionaryClient.getDictionaryForType(type.getType(), type.getDossierTemplateId(), null);
assertThat(loadedType1.getEntries()).hasSize(3);
assertThat(loadedType1.getFalsePositiveEntries()).hasSize(2);
assertThat(loadedType1.getFalseRecommendationEntries()).hasSize(2);
dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), List.of("word1", "word4"), false, dossier.getDossierId(), DictionaryEntryType.ENTRY);
dictionaryClient.deleteEntries(type.getType(), type.getDossierTemplateId(), List.of("word2"), dossier.getDossierId(), DictionaryEntryType.ENTRY);
dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), List.of("false_positive1", "false_positive3"), false, dossier.getDossierId(), DictionaryEntryType.FALSE_POSITIVE);
dictionaryClient.addEntry(type.getType(),
type.getDossierTemplateId(),
@ -403,11 +461,14 @@ public class DictionaryTest extends AbstractPersistenceServerServiceTest {
dossier.getDossierId(),
DictionaryEntryType.FALSE_RECOMMENDATION);
Dictionary mergedDict = dictionaryClient.getMergedDictionaries(type.getType(), dossierTemplate.getDossierTemplateId(), dossier.getDossierId());
assertThat(mergedDict).isNotNull();
assertThat(mergedDict.getEntries().size()).isEqualTo(4);
assertThat(mergedDict.getFalsePositiveEntries().size()).isEqualTo(3);
assertThat(mergedDict.getEntries().size()).isEqualTo(4);
var loadedType2 = dictionaryClient.getDictionaryForType(type.getType(), type.getDossierTemplateId(), dossier.getDossierId());
assertThat(loadedType2.getEntries()).hasSize(2);
assertThat(loadedType2.getFalsePositiveEntries()).hasSize(2);
assertThat(loadedType2.getFalseRecommendationEntries()).hasSize(2);
// var result = dictionaryClient.downloadDictionary(type.getType(), dossierTemplate.getDossierTemplateId(), dossier.getDossierId(), DictionaryEntryType.ENTRY);
// assertThat(result.getStatusCode().value()).isEqualTo(HttpStatus.OK.value());
}
@Test