Skip to content

Commit

Permalink
Link the value of a collected variable to the tuple idVar/idLoop
Browse files Browse the repository at this point in the history
  • Loading branch information
loichenninger committed Dec 9, 2024
1 parent 66206cf commit e348c4e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
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) {
}
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 @@ -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,14 +229,15 @@ 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) {
Expand All @@ -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)) {
Expand Down

0 comments on commit e348c4e

Please sign in to comment.