Merge branch 'RED-10442' into 'release/2.589.x'

RED-10442: Migration Issue: False Warnings appears when re-approve migrated...

See merge request redactmanager/persistence-service!852
This commit is contained in:
Maverick Studer 2024-11-12 12:47:31 +01:00
commit ded782850a

View File

@ -1,18 +1,13 @@
package com.iqser.red.service.persistence.management.v1.processor.service.persistence; package com.iqser.red.service.persistence.management.v1.processor.service.persistence;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.google.common.hash.HashFunction; import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing; import com.google.common.hash.Hashing;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.FileRepository;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.LegalBasisMappingRepository; import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.LegalBasisMappingRepository;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.annotationentity.LegalBasisChangeRepository;
import com.iqser.red.service.persistence.service.v1.api.shared.mongo.repository.EntityLogDocumentRepository;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -26,87 +21,66 @@ public class LegalBasisMigrationService {
private static final HashFunction hashFunction = Hashing.murmur3_128(); private static final HashFunction hashFunction = Hashing.murmur3_128();
//FIXME mapping table needs to be based on reason, not name. private static final Map<String, String> technicalNameMapping = Map.ofEntries(Map.entry("Article 39(e)(3) of Regulation (EC) No 178/2002",
private static final Map<String, String> technicalNameMapping = Map.ofEntries(Map.entry("1. names and addresses of persons", "names_addresses_persons"),
Map.entry("1.1 personal data (incl. geolocation); Article 39(e)(3)",
"personal_data_geolocation_article_39e3"), "personal_data_geolocation_article_39e3"),
Map.entry("1.2 vertebrate study related personal data (incl. geolocation); Article 39(e)(2)", Map.entry("Article 39(e)(2) of Regulation (EC) No 178/2002",
"vertebrate_study_personal_data_geolocation_article_39e2"), "vertebrate_study_personal_data_geolocation_article_39e2"),
Map.entry("2. proprietary information", "proprietary_information"), Map.entry(
Map.entry("2. manufacturing or production process", "manufacturing_production_process"), "Article 63(2)(a) of Regulation (EC) No 1107/2009 (reference to Article 39 of Regulation EC No 178/2002)",
Map.entry("2. methods of analysis for impurities", "methods_analysis_impurities"), "manufacturing_production_process"),
Map.entry("3. method of manufacture", "method_manufacture"), Map.entry(
Map.entry("3. n/a", "n_a"), "Article 63(2)(a) of Regulation (EC) No 1107/2009 (making reference to Article 39 of Regulation EC No 178/2002)",
Map.entry("3. links between a producer and applicant", "links_producer_applicant"), "links_producer_applicant"),
Map.entry("4. commercial information", "commercial_information"), Map.entry(
Map.entry("4. composition of a plant protection product", "composition_plant_protection_product"), "Article 63(2)(a) of Regulation (EC) No1107/2009 (making reference to Article 39 of Regulation EC No 178/2002)",
Map.entry("5. quantitative composition", "quantitative_composition"), "links_producer_applicant"),
Map.entry("5. results of production batches", "results_production_batches"), Map.entry(
Map.entry("6. specification of impurity of the active substance", "Article 63(2)(a) of Regulation (EC) No 1107/2009 (making reference to Article 39 of Regulation EC No 178/2002)",
"specification_impurity_active_substance"), "commercial_information"),
Map.entry("6. specification of impurity", "specification_impurity"), Map.entry(
Map.entry("7. links between a producer and applicant", "links_producer_applicant"), "Article 63(2)(a) of Regulation (EC) No 1107/2009 (making reference to Article 39 of Regulation EC No 178/2002)",
Map.entry("7. results of production batches", "results_production_batches"), "commercial_information"),
Map.entry("8. composition of a plant protection product", "composition_plant_protection_product") Map.entry(
// Add any additional mappings here "Article 63(2)(a) of Regulation (EC) No 1107/2009 (making reference to Article 39 of Regulation EC No 178/2002)",
); "quantitative_composition"),
Map.entry(
"Article 63(2)(a) of Regulation (EC) No 1107/2009 (making reference to Article 39 of Regulation EC No 178/2002)",
"quantitative_composition"),
Map.entry("Article 63(2)(b) of Regulation (EC) No 1107/2009", "specification_impurity"),
Map.entry("Article 63(2)(c) of Regulation (EC) No 1107/2009", "results_production_batches"),
Map.entry("Article 63(2)(d) of Regulation (EC) No 1107/2009",
"composition_plant_protection_product"),
Map.entry("Reg (EC) No 1107/2009 Art. 63 (2g)", "names_addresses_persons"),
Map.entry("Reg (EC) No 1107/2009 Art. 63 (2d)", "methods_analysis_impurities"),
Map.entry("Reg (EC) No 1107/2009 Art. 63 (2a)", "method_manufacture"),
Map.entry("Reg (EC) No 1107/2009 Art. 63 (2b)", "specification_impurity_active_substance"),
Map.entry("Article 4(1)(b), Regulation (EC) No 1049/2001 (Personal data)",
"personal_data_geolocation"),
Map.entry("Article 4(2), first indent, Regulation (EC) No 1049/2001 (commercial interest)",
"proprietary_information"),
Map.entry("3. n/a", "n_a"));
private final LegalBasisMappingRepository legalBasisMappingRepository; private final LegalBasisMappingRepository legalBasisMappingRepository;
private final EntityLogDocumentRepository entityLogDocumentRepository;
private final FileRepository fileRepository;
private final LegalBasisChangeRepository legalBasisChangeRepository;
public void migrate() { public void migrate() {
// log.info("Starting migration: Adding technical names to legal basis"); log.info("Starting migration: Adding technical names to legal basis");
// Removed all the code, because the logic is completely wrong. this.legalBasisMappingRepository.findAll()
// The correct lookup needs to be on the reason field. With the hash value based on the name, we can not lookup anything. .stream()
// The technical name in the entityLog makes only sense if we can look it up in the entitylog entries legalbasis field, that is equal to the reason, not the name. .peek(entry -> entry.getLegalBasis()
// Since rules are not updated to technical names and feature is too incomplete for production, we decided to put it in a future release. .forEach(lb -> {
lb.setTechnicalName(getTechnicalNameOrHash(lb.getReason()));
// List<String> approvedFileIds = fileRepository.getApprovedFiles() }))
// .stream() .forEach(legalBasisMappingRepository::save);
// .map(file -> file.getId()) log.info("Finishing migration: Adding technical names to legal basis");
// .collect(Collectors.toList());
// List<String> hardDeletedFileIds = fileRepository.getHardDeletedFiles()
// .stream()
// .map(file -> file.getId())
// .collect(Collectors.toList());
// this.legalBasisMappingRepository.findAll()
// .stream()
// .peek(entry -> entry.getLegalBasis()
// .forEach(lb -> {
// lb.setTechnicalName(getOrDefault(lb.getName()));
// }))
// .forEach(legalBasisMappingRepository::save);
// this.entityLogDocumentRepository.findAll()
// .stream()
// .filter(ent -> {
// if (approvedFileIds.contains(ent.getFileId()) || hardDeletedFileIds.contains(ent.getFileId())) {
// return false;
// }
// return true;
// })
// .peek(entry -> entry.getLegalBasis()
// .forEach(lb -> {
// lb.setTechnicalName(getOrDefault(lb.getName()));
// }))
// .forEach(entityLogDocumentRepository::save);
// this.legalBasisChangeRepository.findAll()
// .stream()
// .peek(entry -> {
// entry.setLegalBasis(technicalNameMapping.getOrDefault(entry.getLegalBasis(), entry.getLegalBasis()));
// })
// .forEach(legalBasisChangeRepository::save);
// log.info("Finishing migration: Adding technical names to legal basis");
} }
private static String getOrDefault(String lbName) { private static String getTechnicalNameOrHash(String reason) {
return technicalNameMapping.getOrDefault(lbName, String.valueOf(hashFunction.hashBytes(lbName.getBytes(StandardCharsets.UTF_8)))); return technicalNameMapping.getOrDefault(reason, String.valueOf(hashFunction.hashString(reason, StandardCharsets.UTF_8)));
} }
} }