Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tableaux dynamiques fix perret #137

Merged
merged 5 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@Data
public class VariableDto {
private String variableName;
private String idLoop;

@JsonProperty("variableStates")
private List<VariableStateDto> variableStateDtoList;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package fr.insee.genesis.domain.model.surveyunit;

public record IdTuple(String idVar, String idLoop) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Peut être renommer en IdLoopTuple ?

}
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
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;
import org.springframework.beans.factory.annotation.Qualifier;
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
Expand Down Expand Up @@ -77,7 +65,7 @@ public List<SurveyUnitModel> 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<String> addedVariables = new ArrayList<>();
List<IdTuple> addedVariables = new ArrayList<>();
SurveyUnitModel latestUpdate = suByMode.getFirst();

if(latestUpdate.getCollectedVariables() == null){
Expand All @@ -86,33 +74,33 @@ public List<SurveyUnitModel> 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<CollectedVariable> variablesToKeep = new ArrayList<>();
List<Variable> 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()
.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);
}
});
});
Expand All @@ -128,7 +116,7 @@ public SurveyUnitDto findLatestValuesByStateByIdAndByIdQuestionnaire(String idUE
.build();

//Extract variables
Map<String, VariableDto> collectedVariableMap = new HashMap<>();
Map<IdTuple, VariableDto> collectedVariableMap = new HashMap<>();
Map<String, VariableDto> externalVariableMap = new HashMap<>();
List<SurveyUnitModel> surveyUnitModels = surveyUnitPersistencePort.findByIds(idUE, idQuest);
List<Mode> modes = getDistinctsModes(surveyUnitModels);
Expand All @@ -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;
}
Expand Down Expand Up @@ -241,22 +229,24 @@ private static List<Mode> getDistinctsModes(List<SurveyUnitModel> surveyUnitMode
* @param externalVariableMap External variable DTO map to populate
*/
private void extractVariables(SurveyUnitModel surveyUnitModel,
Map<String, VariableDto> collectedVariableMap,
Map<IdTuple, VariableDto> collectedVariableMap,
Map<String, VariableDto> 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) {
variableDto = VariableDto.builder()
.variableName(collectedVariable.getIdVar())
.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)) {
Expand Down
Loading