RED-5624: migration #655
@ -0,0 +1,36 @@
|
||||
package com.iqser.red.service.persistence.management.v1.processor.migration.migrations;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.iqser.red.service.persistence.management.v1.processor.migration.Migration;
|
||||
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.LegalBasisMigrationService;
|
||||
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@Setter
|
||||
@Service
|
||||
public class AddTechnicalNameToJustifications20 extends Migration {
|
||||
|
||||
private static final String NAME = "Migration to add a technical name to justifications";
|
||||
private static final long VERSION = 20;
|
||||
@Autowired
|
||||
private LegalBasisMigrationService legalBasisMigrationService;
|
||||
|
||||
|
||||
public AddTechnicalNameToJustifications20() {
|
||||
|
||||
super(NAME, VERSION);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void migrate() {
|
||||
|
||||
this.legalBasisMigrationService.migrate();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -7,8 +7,6 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import jakarta.transaction.Transactional;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.iqser.red.service.persistence.management.v1.processor.entity.configuration.LegalBasisEntity;
|
||||
@ -17,6 +15,7 @@ import com.iqser.red.service.persistence.management.v1.processor.exception.BadRe
|
||||
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.LegalBasisMappingRepository;
|
||||
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.legalbasis.LegalBasis;
|
||||
|
||||
import jakarta.transaction.Transactional;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@Service
|
||||
@ -112,7 +111,7 @@ public class LegalBasisMappingPersistenceService {
|
||||
throw new BadRequestException(String.format("The legal basis is too long (%s), max length %s", legalBasis.getReason().length(), MAX_LEGAL_BASIS_LENGTH));
|
||||
}
|
||||
|
||||
if(legalBasis.getTechnicalName().length() > MAX_LEGAL_BASIS_LENGTH) {
|
||||
if (legalBasis.getTechnicalName().length() > MAX_LEGAL_BASIS_LENGTH) {
|
||||
throw new BadRequestException(String.format("The legal basis is too long (%s), max length %s", legalBasis.getTechnicalName().length(), MAX_LEGAL_BASIS_LENGTH));
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,104 @@
|
||||
package com.iqser.red.service.persistence.management.v1.processor.service.persistence;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.google.common.hash.HashFunction;
|
||||
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.annotationentity.LegalBasisChangeRepository;
|
||||
import com.iqser.red.service.persistence.service.v1.api.shared.mongo.repository.EntityLogDocumentRepository;
|
||||
|
||||
import jakarta.transaction.Transactional;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class LegalBasisMigrationService {
|
||||
|
||||
private static final HashFunction hashFunction = Hashing.murmur3_128();
|
||||
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"),
|
||||
Map.entry("1.2 vertebrate study related personal data (incl. geolocation); Article 39(e)(2)",
|
||||
"vertebrate_study_personal_data_geolocation_article_39e2"),
|
||||
Map.entry("2. proprietary information", "proprietary_information"),
|
||||
Map.entry("2. manufacturing or production process", "manufacturing_production_process"),
|
||||
Map.entry("2. methods of analysis for impurities", "methods_analysis_impurities"),
|
||||
Map.entry("3. method of manufacture", "method_manufacture"),
|
||||
Map.entry("3. n/a", "n_a"),
|
||||
Map.entry("3. links between a producer and applicant", "links_producer_applicant"),
|
||||
Map.entry("4. commercial information", "commercial_information"),
|
||||
Map.entry("4. composition of a plant protection product", "composition_plant_protection_product"),
|
||||
Map.entry("5. quantitative composition", "quantitative_composition"),
|
||||
Map.entry("5. results of production batches", "results_production_batches"),
|
||||
Map.entry("6. specification of impurity of the active substance",
|
||||
"specification_impurity_active_substance"),
|
||||
Map.entry("6. specification of impurity", "specification_impurity"),
|
||||
Map.entry("7. links between a producer and applicant", "links_producer_applicant"),
|
||||
Map.entry("7. results of production batches", "results_production_batches"),
|
||||
Map.entry("8. composition of a plant protection product", "composition_plant_protection_product")
|
||||
// Add any additional mappings here
|
||||
);
|
||||
private final LegalBasisMappingRepository legalBasisMappingRepository;
|
||||
private final EntityLogDocumentRepository entityLogDocumentRepository;
|
||||
private final FileRepository fileRepository;
|
||||
private final LegalBasisChangeRepository legalBasisChangeRepository;
|
||||
|
||||
|
||||
public void migrate() {
|
||||
|
||||
log.info("Starting migration: Adding technical names to legal basis");
|
||||
List<String> approvedFileIds = fileRepository.getApprovedFiles()
|
||||
.stream()
|
||||
.map(file -> file.getId())
|
||||
.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) {
|
||||
|
||||
return technicalNameMapping.getOrDefault(lbName, String.valueOf(hashFunction.hashBytes(lbName.getBytes(StandardCharsets.UTF_8))));
|
||||
}
|
||||
|
||||
}
|
||||
@ -304,6 +304,13 @@ public interface FileRepository extends JpaRepository<FileEntity, String> {
|
||||
List<FileEntity> getSoftDeletedFiles(@Param("dossierIds") List<String> dossierIds);
|
||||
|
||||
|
||||
@Query("select f from FileEntity f where f.workflowStatus = 'APPROVED'")
|
||||
List<FileEntity> getApprovedFiles();
|
||||
|
||||
|
||||
@Query("select f from FileEntity f where f.hardDeletedTime is not null")
|
||||
List<FileEntity> getHardDeletedFiles();
|
||||
|
||||
@Query("select f from FileEntity f where f.processingStatus = 'ERROR' and f.deleted is null and f.hardDeletedTime is null ")
|
||||
List<FileEntity> getAllErrorFilesExcludeDeleted();
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user