From 66206cf1bdd550c96de71917ea3114ed9bbdb3f1 Mon Sep 17 00:00:00 2001 From: QDIBYS Date: Thu, 28 Nov 2024 14:53:33 +0100 Subject: [PATCH 1/5] add IdLoop to dto --- src/main/java/fr/insee/genesis/controller/dto/VariableDto.java | 1 + .../genesis/domain/service/surveyunit/SurveyUnitService.java | 1 + 2 files changed, 2 insertions(+) diff --git a/src/main/java/fr/insee/genesis/controller/dto/VariableDto.java b/src/main/java/fr/insee/genesis/controller/dto/VariableDto.java index 6f12a4d..0709d3e 100644 --- a/src/main/java/fr/insee/genesis/controller/dto/VariableDto.java +++ b/src/main/java/fr/insee/genesis/controller/dto/VariableDto.java @@ -10,6 +10,7 @@ @Data public class VariableDto { private String variableName; + private String idLoop; @JsonProperty("variableStates") private List variableStateDtoList; diff --git a/src/main/java/fr/insee/genesis/domain/service/surveyunit/SurveyUnitService.java b/src/main/java/fr/insee/genesis/domain/service/surveyunit/SurveyUnitService.java index eede16e..f10319b 100644 --- a/src/main/java/fr/insee/genesis/domain/service/surveyunit/SurveyUnitService.java +++ b/src/main/java/fr/insee/genesis/domain/service/surveyunit/SurveyUnitService.java @@ -254,6 +254,7 @@ private void extractVariables(SurveyUnitModel surveyUnitModel, if (variableDto == null) { variableDto = VariableDto.builder() .variableName(collectedVariable.getIdVar()) + .idLoop(collectedVariable.getIdLoop()) .variableStateDtoList(new ArrayList<>()) .build(); collectedVariableMap.put(collectedVariable.getIdVar(), variableDto); From e348c4e25e7cff59796fd27fcb2ff91c37599f77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Henninger?= Date: Mon, 9 Dec 2024 14:55:42 +0100 Subject: [PATCH 2/5] Link the value of a collected variable to the tuple idVar/idLoop --- .../domain/model/surveyunit/IdTuple.java | 4 +++ .../service/surveyunit/SurveyUnitService.java | 29 ++++++------------- 2 files changed, 13 insertions(+), 20 deletions(-) create mode 100644 src/main/java/fr/insee/genesis/domain/model/surveyunit/IdTuple.java diff --git a/src/main/java/fr/insee/genesis/domain/model/surveyunit/IdTuple.java b/src/main/java/fr/insee/genesis/domain/model/surveyunit/IdTuple.java new file mode 100644 index 0000000..55726d0 --- /dev/null +++ b/src/main/java/fr/insee/genesis/domain/model/surveyunit/IdTuple.java @@ -0,0 +1,4 @@ +package fr.insee.genesis.domain.model.surveyunit; + +public record IdTuple(String idVar, String idLoop) { +} diff --git a/src/main/java/fr/insee/genesis/domain/service/surveyunit/SurveyUnitService.java b/src/main/java/fr/insee/genesis/domain/service/surveyunit/SurveyUnitService.java index f10319b..1ee34ad 100644 --- a/src/main/java/fr/insee/genesis/domain/service/surveyunit/SurveyUnitService.java +++ b/src/main/java/fr/insee/genesis/domain/service/surveyunit/SurveyUnitService.java @@ -1,15 +1,7 @@ package fr.insee.genesis.domain.service.surveyunit; -import fr.insee.genesis.controller.dto.CampaignWithQuestionnaire; -import fr.insee.genesis.controller.dto.QuestionnaireWithCampaign; -import fr.insee.genesis.controller.dto.SurveyUnitDto; -import fr.insee.genesis.controller.dto.SurveyUnitId; -import fr.insee.genesis.controller.dto.VariableDto; -import fr.insee.genesis.controller.dto.VariableStateDto; -import fr.insee.genesis.domain.model.surveyunit.CollectedVariable; -import fr.insee.genesis.domain.model.surveyunit.Mode; -import fr.insee.genesis.domain.model.surveyunit.SurveyUnitModel; -import fr.insee.genesis.domain.model.surveyunit.Variable; +import fr.insee.genesis.controller.dto.*; +import fr.insee.genesis.domain.model.surveyunit.*; import fr.insee.genesis.domain.ports.api.SurveyUnitApiPort; import fr.insee.genesis.domain.ports.spi.SurveyUnitPersistencePort; import org.springframework.beans.factory.annotation.Autowired; @@ -17,11 +9,7 @@ import org.springframework.stereotype.Service; import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.stream.Stream; @Service @@ -128,7 +116,7 @@ public SurveyUnitDto findLatestValuesByStateByIdAndByIdQuestionnaire(String idUE .build(); //Extract variables - Map collectedVariableMap = new HashMap<>(); + Map collectedVariableMap = new HashMap<>(); Map externalVariableMap = new HashMap<>(); List surveyUnitModels = surveyUnitPersistencePort.findByIds(idUE, idQuest); List modes = getDistinctsModes(surveyUnitModels); @@ -139,7 +127,7 @@ public SurveyUnitDto findLatestValuesByStateByIdAndByIdQuestionnaire(String idUE .toList(); suByMode.forEach(surveyUnitModel -> extractVariables(surveyUnitModel, collectedVariableMap,externalVariableMap)); }); - collectedVariableMap.keySet().forEach(variableName -> surveyUnitDto.getCollectedVariables().add(collectedVariableMap.get(variableName))); + collectedVariableMap.keySet().forEach(variableTuple -> surveyUnitDto.getCollectedVariables().add(collectedVariableMap.get(variableTuple))); externalVariableMap.keySet().forEach(variableName -> surveyUnitDto.getExternalVariables().add(externalVariableMap.get(variableName))); return surveyUnitDto; } @@ -241,14 +229,15 @@ private static List getDistinctsModes(List surveyUnitMode * @param externalVariableMap External variable DTO map to populate */ private void extractVariables(SurveyUnitModel surveyUnitModel, - Map collectedVariableMap, + Map collectedVariableMap, Map externalVariableMap) { if(surveyUnitModel.getCollectedVariables() == null){ surveyUnitModel.setCollectedVariables(new ArrayList<>()); } for (CollectedVariable collectedVariable : surveyUnitModel.getCollectedVariables()) { - VariableDto variableDto = collectedVariableMap.get(collectedVariable.getIdVar()); + IdTuple idTuple = new IdTuple(collectedVariable.getIdVar(), collectedVariable.getIdLoop()); + VariableDto variableDto = collectedVariableMap.get(idTuple); //Create variable into map if not exists if (variableDto == null) { @@ -257,7 +246,7 @@ private void extractVariables(SurveyUnitModel surveyUnitModel, .idLoop(collectedVariable.getIdLoop()) .variableStateDtoList(new ArrayList<>()) .build(); - collectedVariableMap.put(collectedVariable.getIdVar(), variableDto); + collectedVariableMap.put(idTuple, variableDto); } //Extract variable state if (!collectedVariable.getValues().isEmpty() && isMostRecentForSameState(surveyUnitModel, variableDto)) { From 5ebe602d7332c5e9f673871f32b226ae14531e9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Henninger?= Date: Wed, 11 Dec 2024 11:01:30 +0100 Subject: [PATCH 3/5] Find latest states for a tuple (idVar, idLoop) and not just idVar --- .../service/surveyunit/SurveyUnitService.java | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/src/main/java/fr/insee/genesis/domain/service/surveyunit/SurveyUnitService.java b/src/main/java/fr/insee/genesis/domain/service/surveyunit/SurveyUnitService.java index 1ee34ad..94fd00b 100644 --- a/src/main/java/fr/insee/genesis/domain/service/surveyunit/SurveyUnitService.java +++ b/src/main/java/fr/insee/genesis/domain/service/surveyunit/SurveyUnitService.java @@ -65,7 +65,7 @@ public List findLatestByIdAndByIdQuestionnaire(String idUE, Str //We had all the variables of the oldest update latestUpdatesbyVariables.add(suByMode.getFirst()); //We keep the name of already added variables to skip them in older updates - List addedVariables = new ArrayList<>(); + List addedVariables = new ArrayList<>(); SurveyUnitModel latestUpdate = suByMode.getFirst(); if(latestUpdate.getCollectedVariables() == null){ @@ -74,33 +74,38 @@ public List findLatestByIdAndByIdQuestionnaire(String idUE, Str if(latestUpdate.getExternalVariables() == null){ latestUpdate.setExternalVariables(new ArrayList<>()); } - latestUpdate.getCollectedVariables().forEach(variableStateDto -> addedVariables.add(variableStateDto.getIdVar())); - latestUpdate.getExternalVariables().forEach(externalVariableDto -> addedVariables.add(externalVariableDto.getIdVar())); + latestUpdate.getCollectedVariables().forEach(colVar -> addedVariables.add(new IdTuple(colVar.getIdVar(), colVar.getIdLoop()))); + latestUpdate.getExternalVariables().forEach(extVar -> addedVariables.add(new IdTuple(extVar.getIdVar(), ""))); - suByMode.forEach(surveyUnitDto -> { + suByMode.forEach(surveyUnitModel -> { List variablesToKeep = new ArrayList<>(); List externalToKeep = new ArrayList<>(); // We iterate over the variables of the update and add them to the list if they are not already added - surveyUnitDto.getCollectedVariables().stream() - .filter(variableStateDto -> !addedVariables.contains(variableStateDto.getIdVar())) - .forEach(variableStateDto -> { - variablesToKeep.add(variableStateDto); - addedVariables.add(variableStateDto.getIdVar()); + surveyUnitModel.getCollectedVariables().stream() + .peek(colVar -> { + if (addedVariables.contains(new IdTuple(colVar.getIdVar(), colVar.getIdLoop()))) { + System.out.println("Filtré : " + colVar.getIdVar() + " " + colVar.getIdLoop()); + } + }) + .filter(colVar -> !addedVariables.contains(new IdTuple(colVar.getIdVar(), colVar.getIdLoop()))) + .forEach(colVar -> { + variablesToKeep.add(colVar); + addedVariables.add(new IdTuple(colVar.getIdVar(), colVar.getIdLoop())); }); - if (surveyUnitDto.getExternalVariables() != null){ - surveyUnitDto.getExternalVariables().stream() - .filter(externalVariableDto -> !addedVariables.contains(externalVariableDto.getIdVar())) - .forEach(externalVariableDto -> { - externalToKeep.add(externalVariableDto); - addedVariables.add(externalVariableDto.getIdVar()); + if (surveyUnitModel.getExternalVariables() != null){ + surveyUnitModel.getExternalVariables().stream() + .filter(extVar -> !addedVariables.contains(new IdTuple(extVar.getIdVar(), ""))) + .forEach(extVar -> { + externalToKeep.add(extVar); + addedVariables.add(new IdTuple(extVar.getIdVar(), "")); }); } // If there are new variables, we add the update to the list of latest updates if (!variablesToKeep.isEmpty() || !externalToKeep.isEmpty()){ - surveyUnitDto.setCollectedVariables(variablesToKeep); - surveyUnitDto.setExternalVariables(externalToKeep); - latestUpdatesbyVariables.add(surveyUnitDto); + surveyUnitModel.setCollectedVariables(variablesToKeep); + surveyUnitModel.setExternalVariables(externalToKeep); + latestUpdatesbyVariables.add(surveyUnitModel); } }); }); From 3519d7095fc5412fbfbd4db6a74d3bbb1e226276 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Henninger?= Date: Thu, 12 Dec 2024 10:18:18 +0100 Subject: [PATCH 4/5] Remove debug code --- .../genesis/domain/service/surveyunit/SurveyUnitService.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/main/java/fr/insee/genesis/domain/service/surveyunit/SurveyUnitService.java b/src/main/java/fr/insee/genesis/domain/service/surveyunit/SurveyUnitService.java index 94fd00b..d70bb8c 100644 --- a/src/main/java/fr/insee/genesis/domain/service/surveyunit/SurveyUnitService.java +++ b/src/main/java/fr/insee/genesis/domain/service/surveyunit/SurveyUnitService.java @@ -82,11 +82,6 @@ public List findLatestByIdAndByIdQuestionnaire(String idUE, Str List externalToKeep = new ArrayList<>(); // We iterate over the variables of the update and add them to the list if they are not already added surveyUnitModel.getCollectedVariables().stream() - .peek(colVar -> { - if (addedVariables.contains(new IdTuple(colVar.getIdVar(), colVar.getIdLoop()))) { - System.out.println("Filtré : " + colVar.getIdVar() + " " + colVar.getIdLoop()); - } - }) .filter(colVar -> !addedVariables.contains(new IdTuple(colVar.getIdVar(), colVar.getIdLoop()))) .forEach(colVar -> { variablesToKeep.add(colVar); From 5d665d07ce83c5d4551d180889f751dc2f2c588e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Henninger?= Date: Mon, 16 Dec 2024 10:37:12 +0100 Subject: [PATCH 5/5] Rename idTuple --- .../domain/model/surveyunit/IdLoopTuple.java | 4 ++++ .../domain/model/surveyunit/IdTuple.java | 4 ---- .../service/surveyunit/SurveyUnitService.java | 24 +++++++++---------- 3 files changed, 16 insertions(+), 16 deletions(-) create mode 100644 src/main/java/fr/insee/genesis/domain/model/surveyunit/IdLoopTuple.java delete mode 100644 src/main/java/fr/insee/genesis/domain/model/surveyunit/IdTuple.java diff --git a/src/main/java/fr/insee/genesis/domain/model/surveyunit/IdLoopTuple.java b/src/main/java/fr/insee/genesis/domain/model/surveyunit/IdLoopTuple.java new file mode 100644 index 0000000..6d1ac55 --- /dev/null +++ b/src/main/java/fr/insee/genesis/domain/model/surveyunit/IdLoopTuple.java @@ -0,0 +1,4 @@ +package fr.insee.genesis.domain.model.surveyunit; + +public record IdLoopTuple(String idVar, String idLoop) { +} diff --git a/src/main/java/fr/insee/genesis/domain/model/surveyunit/IdTuple.java b/src/main/java/fr/insee/genesis/domain/model/surveyunit/IdTuple.java deleted file mode 100644 index 55726d0..0000000 --- a/src/main/java/fr/insee/genesis/domain/model/surveyunit/IdTuple.java +++ /dev/null @@ -1,4 +0,0 @@ -package fr.insee.genesis.domain.model.surveyunit; - -public record IdTuple(String idVar, String idLoop) { -} diff --git a/src/main/java/fr/insee/genesis/domain/service/surveyunit/SurveyUnitService.java b/src/main/java/fr/insee/genesis/domain/service/surveyunit/SurveyUnitService.java index d70bb8c..e44ae9e 100644 --- a/src/main/java/fr/insee/genesis/domain/service/surveyunit/SurveyUnitService.java +++ b/src/main/java/fr/insee/genesis/domain/service/surveyunit/SurveyUnitService.java @@ -65,7 +65,7 @@ public List findLatestByIdAndByIdQuestionnaire(String idUE, Str //We had all the variables of the oldest update latestUpdatesbyVariables.add(suByMode.getFirst()); //We keep the name of already added variables to skip them in older updates - List addedVariables = new ArrayList<>(); + List addedVariables = new ArrayList<>(); SurveyUnitModel latestUpdate = suByMode.getFirst(); if(latestUpdate.getCollectedVariables() == null){ @@ -74,25 +74,25 @@ public List findLatestByIdAndByIdQuestionnaire(String idUE, Str if(latestUpdate.getExternalVariables() == null){ latestUpdate.setExternalVariables(new ArrayList<>()); } - latestUpdate.getCollectedVariables().forEach(colVar -> addedVariables.add(new IdTuple(colVar.getIdVar(), colVar.getIdLoop()))); - latestUpdate.getExternalVariables().forEach(extVar -> addedVariables.add(new IdTuple(extVar.getIdVar(), ""))); + latestUpdate.getCollectedVariables().forEach(colVar -> addedVariables.add(new IdLoopTuple(colVar.getIdVar(), colVar.getIdLoop()))); + latestUpdate.getExternalVariables().forEach(extVar -> addedVariables.add(new IdLoopTuple(extVar.getIdVar(), ""))); suByMode.forEach(surveyUnitModel -> { List variablesToKeep = new ArrayList<>(); List externalToKeep = new ArrayList<>(); // We iterate over the variables of the update and add them to the list if they are not already added surveyUnitModel.getCollectedVariables().stream() - .filter(colVar -> !addedVariables.contains(new IdTuple(colVar.getIdVar(), colVar.getIdLoop()))) + .filter(colVar -> !addedVariables.contains(new IdLoopTuple(colVar.getIdVar(), colVar.getIdLoop()))) .forEach(colVar -> { variablesToKeep.add(colVar); - addedVariables.add(new IdTuple(colVar.getIdVar(), colVar.getIdLoop())); + addedVariables.add(new IdLoopTuple(colVar.getIdVar(), colVar.getIdLoop())); }); if (surveyUnitModel.getExternalVariables() != null){ surveyUnitModel.getExternalVariables().stream() - .filter(extVar -> !addedVariables.contains(new IdTuple(extVar.getIdVar(), ""))) + .filter(extVar -> !addedVariables.contains(new IdLoopTuple(extVar.getIdVar(), ""))) .forEach(extVar -> { externalToKeep.add(extVar); - addedVariables.add(new IdTuple(extVar.getIdVar(), "")); + addedVariables.add(new IdLoopTuple(extVar.getIdVar(), "")); }); } @@ -116,7 +116,7 @@ public SurveyUnitDto findLatestValuesByStateByIdAndByIdQuestionnaire(String idUE .build(); //Extract variables - Map collectedVariableMap = new HashMap<>(); + Map collectedVariableMap = new HashMap<>(); Map externalVariableMap = new HashMap<>(); List surveyUnitModels = surveyUnitPersistencePort.findByIds(idUE, idQuest); List modes = getDistinctsModes(surveyUnitModels); @@ -229,15 +229,15 @@ private static List getDistinctsModes(List surveyUnitMode * @param externalVariableMap External variable DTO map to populate */ private void extractVariables(SurveyUnitModel surveyUnitModel, - Map collectedVariableMap, + Map collectedVariableMap, Map externalVariableMap) { if(surveyUnitModel.getCollectedVariables() == null){ surveyUnitModel.setCollectedVariables(new ArrayList<>()); } for (CollectedVariable collectedVariable : surveyUnitModel.getCollectedVariables()) { - IdTuple idTuple = new IdTuple(collectedVariable.getIdVar(), collectedVariable.getIdLoop()); - VariableDto variableDto = collectedVariableMap.get(idTuple); + IdLoopTuple idLoopTuple = new IdLoopTuple(collectedVariable.getIdVar(), collectedVariable.getIdLoop()); + VariableDto variableDto = collectedVariableMap.get(idLoopTuple); //Create variable into map if not exists if (variableDto == null) { @@ -246,7 +246,7 @@ private void extractVariables(SurveyUnitModel surveyUnitModel, .idLoop(collectedVariable.getIdLoop()) .variableStateDtoList(new ArrayList<>()) .build(); - collectedVariableMap.put(idTuple, variableDto); + collectedVariableMap.put(idLoopTuple, variableDto); } //Extract variable state if (!collectedVariable.getValues().isEmpty() && isMostRecentForSameState(surveyUnitModel, variableDto)) {