RED-8727 - Add rank de-duplication to migration #425

Merged
corina.olariu.ext1 merged 2 commits from RED-8727-updates into release/2.349.x 2024-04-05 10:48:51 +02:00
4 changed files with 52 additions and 57 deletions

View File

@ -39,6 +39,7 @@ public class RankDeDuplicationService {
private void deduplicate(String dossierTemplateId) {
log.info("deduplicate for dossierTemplateID: {}", dossierTemplateId);
List<TypeEntity> allDossierTemplateTypes = dictionaryPersistenceService.getAllTypesForDossierTemplate(dossierTemplateId, false);
TreeMap<Integer, List<TypeEntity>> typesSortedByRank = new TreeMap<>(allDossierTemplateTypes.stream()
.collect(Collectors.groupingBy(TypeEntity::getRank, Collectors.toList())));
@ -47,9 +48,9 @@ public class RankDeDuplicationService {
// we create a map with every rank that needs to be updated and the value is the starting point for that rank
Map<Integer, Integer> rankToNewRankUpdateMap = new TreeMap<>(Collections.reverseOrder());
for (var typesByRankEntry : typesSortedByRank.entrySet()) {
log.debug(" {} - {}", typesByRankEntry.getKey(), typesByRankEntry.getValue().size());
log.debug("Rank: {} - number of types {}", typesByRankEntry.getKey(), typesByRankEntry.getValue().size());
typesByRankEntry.getValue()
.forEach(t -> log.debug("type: {} - dtId: {} - dId: {}", t.getType(), t.getDossierTemplateId(), t.getDossierId()));
.forEach(t -> log.debug("type: {} - dtId: {} - dId: {} typeId: {}", t.getType(), t.getDossierTemplateId(), t.getDossierId(), t.getId()));
if (typesByRankEntry.getValue().size() > 1) {
if (typesByRankEntry.getKey() > lastRankOk) {
@ -74,14 +75,13 @@ public class RankDeDuplicationService {
for (TypeEntity t : typesSortedByRank.get(rankToUpdateEntry.getKey())) {
if (newRank != t.getRank()) {
log.info("Type {} with rank: {} will be updated to rank: {}", t.getType(), t.getRank(), newRank);
t.setRank(newRank);
dictionaryPersistenceService.updateType(t.getId(), t);
log.info("Type {} with rank: {} will be updated to rank: {} - typeId {}", t.getType(), t.getRank(), newRank, t.getId());
dictionaryPersistenceService.updateRankForType(t.getId(), newRank);
var dossierTypes = dictionaryPersistenceService.getAllDossierTypesForDossierTemplateAndType(dossierTemplateId, t.getType(), false);
for (TypeEntity td : dossierTypes) {
td.setRank(newRank);
dictionaryPersistenceService.updateType(td.getId(), td);
log.info("Type {} with rank: {} will be updated to rank: {} - typeId {}", td.getType(), td.getRank(), newRank, td.getId());
dictionaryPersistenceService.updateRankForType(td.getId(), newRank);
}
}

View File

@ -90,7 +90,7 @@ public class DictionaryPersistenceService {
if (!existingTypesForRank.isEmpty()) {
for (var existingTypeValueForRank : existingTypesForRank) {
if (!existingTypeValueForRank.isDeleted() && !existingTypeValueForRank.getType().equalsIgnoreCase(type)) {
throw new ConflictException("Rank already exists: " + rank + " on type: " + existingTypeValueForRank.getType());
throw new ConflictException("Rank already exists: " + rank + " on type: " + existingTypeValueForRank.getType() + " type id: " + existingTypeValueForRank.getId());
}
}
}
@ -140,7 +140,7 @@ public class DictionaryPersistenceService {
"version",
"dossierDictionaryOnly");
}
typeRepository.save(type);
typeRepository.saveAndFlush(type);
});
}
@ -287,4 +287,10 @@ public class DictionaryPersistenceService {
}
@Transactional
public void updateRankForType(String typeId, int newRank) {
typeRepository.updateRankForType(typeId, newRank);
}
}

View File

@ -67,9 +67,9 @@ public interface TypeRepository extends JpaRepository<TypeEntity, String> {
@Query("""
select new com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.type.TypeRankSummary(t.dossierTemplateId, t.dossierId, t.rank, count(t))
from TypeEntity t where t.dossierTemplateId = :dossierTemplateId and t.softDeletedTime is null
group by t.dossierTemplateId, t.dossierId, t.rank
select new com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.type.TypeRankSummary(t.dossierTemplateId, t.dossierId, t.rank, count(t))
from TypeEntity t where t.dossierTemplateId = :dossierTemplateId and t.softDeletedTime is null
group by t.dossierTemplateId, t.dossierId, t.rank
order by t.rank""")
List<TypeRankSummary> findTypeRankSummaryList(@Param("dossierTemplateId") String dossierTemplateId);
@ -83,4 +83,9 @@ public interface TypeRepository extends JpaRepository<TypeEntity, String> {
@Query("Update TypeEntity t set t.softDeletedTime = null where t.id = :typeId and t.softDeletedTime is not null")
int unSoftDeleteTypeById(@Param("typeId") String typeId);
@Modifying
@Query("Update TypeEntity t set t.rank = :newRank, t.version = t.version + 1 where t.id = :typeId")
void updateRankForType(@Param("typeId") String typeId, @Param("newRank") int newRank);
}

View File

@ -104,7 +104,7 @@ public class DossierTemplateCloneAndExportWithDuplicateRanksTest {
private String dossierTemplateName = "Clone of " + dossierTemplateId;
@Captor
private ArgumentCaptor<TypeEntity> captor;
private ArgumentCaptor<Integer> captor;
@BeforeEach
@ -221,63 +221,47 @@ public class DossierTemplateCloneAndExportWithDuplicateRanksTest {
rankDeDuplicationService.deduplicate();
TypeEntity caughtType;
verify(dictionaryPersistenceService, times(1)).updateRankForType(eq(toTypeId("type01", dossierTemplateId)), captor.capture());
Assertions.assertEquals(captor.getValue(), 1);
verify(dictionaryPersistenceService, times(1)).updateType(eq(toTypeId("type01", dossierTemplateId)), captor.capture());
caughtType = captor.getValue();
Assertions.assertEquals(caughtType.getRank(), 1);
verify(dictionaryPersistenceService, times(1)).updateRankForType(eq(toTypeId("type02", dossierTemplateId)), captor.capture());
Assertions.assertEquals(captor.getValue(), 2);
verify(dictionaryPersistenceService, times(1)).updateType(eq(toTypeId("type02", dossierTemplateId)), captor.capture());
caughtType = captor.getValue();
Assertions.assertEquals(caughtType.getRank(), 2);
verify(dictionaryPersistenceService, times(1)).updateRankForType(eq(toTypeId("type02", dossierTemplateId, dossierId)), captor.capture());
Assertions.assertEquals(captor.getValue(), 2);
verify(dictionaryPersistenceService, times(1)).updateType(eq(toTypeId("type02", dossierTemplateId, dossierId)), captor.capture());
caughtType = captor.getValue();
Assertions.assertEquals(caughtType.getRank(), 2);
verify(dictionaryPersistenceService, times(1)).updateRankForType(eq(toTypeId("type22", dossierTemplateId)), captor.capture());
Assertions.assertEquals(captor.getValue(), 3);
verify(dictionaryPersistenceService, times(1)).updateType(eq(toTypeId("type22", dossierTemplateId)), captor.capture());
caughtType = captor.getValue();
Assertions.assertEquals(caughtType.getRank(), 3);
verify(dictionaryPersistenceService, times(1)).updateRankForType(eq(toTypeId("type2", dossierTemplateId)), captor.capture());
Assertions.assertEquals(captor.getValue(), 51);
verify(dictionaryPersistenceService, times(1)).updateType(eq(toTypeId("type2", dossierTemplateId)), captor.capture());
caughtType = captor.getValue();
Assertions.assertEquals(caughtType.getRank(), 51);
verify(dictionaryPersistenceService, times(1)).updateRankForType(eq(toTypeId("type3", dossierTemplateId)), captor.capture());
Assertions.assertEquals(captor.getValue(), 52);
verify(dictionaryPersistenceService, times(1)).updateType(eq(toTypeId("type3", dossierTemplateId)), captor.capture());
caughtType = captor.getValue();
Assertions.assertEquals(caughtType.getRank(), 52);
verify(dictionaryPersistenceService, times(1)).updateRankForType(eq(toTypeId("type4", dossierTemplateId)), captor.capture());
Assertions.assertEquals(captor.getValue(), 53);
verify(dictionaryPersistenceService, times(1)).updateType(eq(toTypeId("type4", dossierTemplateId)), captor.capture());
caughtType = captor.getValue();
Assertions.assertEquals(caughtType.getRank(), 53);
verify(dictionaryPersistenceService, times(1)).updateRankForType(eq(toTypeId("type5", dossierTemplateId)), captor.capture());
Assertions.assertEquals(captor.getValue(), 54);
verify(dictionaryPersistenceService, times(1)).updateType(eq(toTypeId("type5", dossierTemplateId)), captor.capture());
caughtType = captor.getValue();
Assertions.assertEquals(caughtType.getRank(), 54);
verify(dictionaryPersistenceService, times(1)).updateRankForType(eq(toTypeId("type6", dossierTemplateId)), captor.capture());
Assertions.assertEquals(captor.getValue(), 55);
verify(dictionaryPersistenceService, times(1)).updateType(eq(toTypeId("type6", dossierTemplateId)), captor.capture());
caughtType = captor.getValue();
Assertions.assertEquals(caughtType.getRank(), 55);
verify(dictionaryPersistenceService, times(1)).updateRankForType(eq(toTypeId("type7", dossierTemplateId)), captor.capture());
Assertions.assertEquals(captor.getValue(), 56);
verify(dictionaryPersistenceService, times(1)).updateType(eq(toTypeId("type7", dossierTemplateId)), captor.capture());
caughtType = captor.getValue();
Assertions.assertEquals(caughtType.getRank(), 56);
verify(dictionaryPersistenceService, times(1)).updateRankForType(eq(toTypeId("type8", dossierTemplateId)), captor.capture());
Assertions.assertEquals(captor.getValue(), 57);
verify(dictionaryPersistenceService, times(1)).updateType(eq(toTypeId("type8", dossierTemplateId)), captor.capture());
caughtType = captor.getValue();
Assertions.assertEquals(caughtType.getRank(), 57);
verify(dictionaryPersistenceService, times(1)).updateRankForType(eq(toTypeId("type9", dossierTemplateId)), captor.capture());
Assertions.assertEquals(captor.getValue(), 58);
verify(dictionaryPersistenceService, times(1)).updateType(eq(toTypeId("type9", dossierTemplateId)), captor.capture());
caughtType = captor.getValue();
Assertions.assertEquals(caughtType.getRank(), 58);
verify(dictionaryPersistenceService, times(1)).updateRankForType(eq(toTypeId("type10", dossierTemplateId)), captor.capture());
Assertions.assertEquals(captor.getValue(), 59);
verify(dictionaryPersistenceService, times(1)).updateType(eq(toTypeId("type10", dossierTemplateId)), captor.capture());
caughtType = captor.getValue();
Assertions.assertEquals(caughtType.getRank(), 59);
verify(dictionaryPersistenceService, times(1)).updateType(eq(toTypeId("type11", dossierTemplateId)), captor.capture());
caughtType = captor.getValue();
Assertions.assertEquals(caughtType.getRank(), 60);
verify(dictionaryPersistenceService, times(1)).updateRankForType(eq(toTypeId("type11", dossierTemplateId)), captor.capture());
Assertions.assertEquals(captor.getValue(), 60);
}