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

Refactor : rename ids #149

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -13,9 +13,9 @@ public class LunaticJsonAdapter {

public SurveyUnitModel convert(LunaticJsonSurveyUnit su){
return SurveyUnitModel.builder()
.idQuest(su.getIdQuest())
.idCampaign("")
.idUE(su.getIdUE())
.questionnaireId(su.getIdQuest())
.campaignId("")
.interrogationId(su.getIdUE())
.state(DataState.COLLECTED)
.mode(Mode.WEB)
.recordDate(LocalDateTime.now())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ public static List<SurveyUnitModel> convert(LunaticXmlSurveyUnit su, VariablesMa
*/
private static SurveyUnitModel getStateDataFromSurveyUnit(LunaticXmlSurveyUnit su, VariablesMap variablesMap, String idCampaign, DataState dataState, Mode mode) {
SurveyUnitModel surveyUnitModel = SurveyUnitModel.builder()
.idQuest(su.getQuestionnaireModelId())
.idCampaign(idCampaign)
.idUE(su.getId())
.questionnaireId(su.getQuestionnaireModelId())
.campaignId(idCampaign)
.interrogationId(su.getId())
.state(dataState)
.mode(mode)
.recordDate(LocalDateTime.now())
Expand Down Expand Up @@ -122,10 +122,10 @@ private static SurveyUnitModel getCollectedDataFromSurveyUnit(LunaticXmlSurveyUn
if (valueTypeList.get(i-1).getValue()!=null) {
variableValues.add(valueTypeList.get(i-1).getValue());
variablesUpdate.add(CollectedVariable.collectedVariableBuilder()
.idVar(lunaticXmlCollectedData.getVariableName())
.varId(lunaticXmlCollectedData.getVariableName())
.values(variableValues)
.idLoop(LoopIdentifier.getLoopIdentifier(lunaticXmlCollectedData.getVariableName(), variablesMap, i))
.idParent(LoopIdentifier.getRelatedVariableName(lunaticXmlCollectedData.getVariableName(), variablesMap))
.loopId(LoopIdentifier.getLoopIdentifier(lunaticXmlCollectedData.getVariableName(), variablesMap, i))
.parentId(LoopIdentifier.getRelatedVariableName(lunaticXmlCollectedData.getVariableName(), variablesMap))
.build());
dataCount++;
}
Expand All @@ -152,7 +152,7 @@ private static void getExternalDataFromSurveyUnit(LunaticXmlSurveyUnit su, Surve
List<Variable> externalVariables = new ArrayList<>();
su.getData().getExternal().forEach(lunaticXmlExternalData ->
externalVariables.add(Variable.builder()
.idVar(lunaticXmlExternalData.getVariableName())
.varId(lunaticXmlExternalData.getVariableName())
.values(getValuesFromValueTypeList(lunaticXmlExternalData.getValues()))
.build())
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,9 @@ public ResponseEntity<SurveyUnitSimplified> getLatestByUEOneObject(@RequestParam
outputExternalVariables.addAll(response.getExternalVariables());
});
return ResponseEntity.ok(SurveyUnitSimplified.builder()
.idQuest(responses.getFirst().getIdQuest())
.idCampaign(responses.getFirst().getIdCampaign())
.idUE(responses.getFirst().getIdUE())
.idQuest(responses.getFirst().getQuestionnaireId())
.idCampaign(responses.getFirst().getCampaignId())
.idUE(responses.getFirst().getInterrogationId())
.variablesUpdate(outputVariables)
.externalVariables(outputExternalVariables)
.build());
Expand All @@ -347,9 +347,9 @@ public ResponseEntity<List<SurveyUnitSimplified>> getLatestForUEList(@RequestPar
});
if (!outputVariables.isEmpty() || !outputExternalVariables.isEmpty()) {
results.add(SurveyUnitSimplified.builder()
.idQuest(responses.getFirst().getIdQuest())
.idCampaign(responses.getFirst().getIdCampaign())
.idUE(responses.getFirst().getIdUE())
.idQuest(responses.getFirst().getQuestionnaireId())
.idCampaign(responses.getFirst().getCampaignId())
.idUE(responses.getFirst().getInterrogationId())
.mode(mode)
.variablesUpdate(outputVariables)
.externalVariables(outputExternalVariables)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
@Setter
public class CollectedVariable extends Variable {

private String idLoop;
private String idParent;
private String loopId;
private String parentId;

@Builder(builderMethodName = "collectedVariableBuilder")
public CollectedVariable(String idVar, List<String> values, String idLoop, String idParent) {
super(idVar, values);
this.idLoop = idLoop;
this.idParent = idParent;
public CollectedVariable(String varId, List<String> values, String loopId, String parentId) {
super(varId, values);
this.loopId = loopId;
this.parentId = parentId;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.insee.genesis.domain.model.surveyunit;

public record IdLoopTuple(String idVar, String idLoop) {
public record IdLoopTuple(String varId, String idLoop) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
@AllArgsConstructor
public class SurveyUnitModel {

private String idQuest;
private String idCampaign;
private String idUE;
private String questionnaireId;
private String campaignId;
private String interrogationId;
private DataState state;
private Mode mode;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'hh:mm")
Expand All @@ -29,8 +29,8 @@ public class SurveyUnitModel {
private List<CollectedVariable> collectedVariables;
private List<Variable> externalVariables;

public SurveyUnitModel(String idUE, Mode mode) {
this.idUE = idUE;
public SurveyUnitModel(String interrogationId, Mode mode) {
this.interrogationId = interrogationId;
this.mode = mode;
}

Expand All @@ -43,11 +43,11 @@ public boolean equals(Object o) {
return false;
}
SurveyUnitModel that = (SurveyUnitModel) o;
return Objects.equals(idUE, that.idUE) && Objects.equals(mode, that.mode);
return Objects.equals(interrogationId, that.interrogationId) && Objects.equals(mode, that.mode);
}

@Override
public int hashCode() {
return Objects.hash(idUE) + Objects.hash(mode);
return Objects.hash(interrogationId) + Objects.hash(mode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@Setter
public class Variable {

private String idVar;
private String varId;
private List<String> values;

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,25 @@ public List<SurveyUnitModel> findLatestByIdAndByIdQuestionnaire(String idUE, Str
if(latestUpdate.getExternalVariables() == null){
latestUpdate.setExternalVariables(new ArrayList<>());
}
latestUpdate.getCollectedVariables().forEach(colVar -> addedVariables.add(new IdLoopTuple(colVar.getIdVar(), colVar.getIdLoop())));
latestUpdate.getExternalVariables().forEach(extVar -> addedVariables.add(new IdLoopTuple(extVar.getIdVar(), "")));
latestUpdate.getCollectedVariables().forEach(colVar -> addedVariables.add(new IdLoopTuple(colVar.getVarId(), colVar.getLoopId())));
latestUpdate.getExternalVariables().forEach(extVar -> addedVariables.add(new IdLoopTuple(extVar.getVarId(), "")));

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
surveyUnitModel.getCollectedVariables().stream()
.filter(colVar -> !addedVariables.contains(new IdLoopTuple(colVar.getIdVar(), colVar.getIdLoop())))
.filter(colVar -> !addedVariables.contains(new IdLoopTuple(colVar.getVarId(), colVar.getLoopId())))
.forEach(colVar -> {
variablesToKeep.add(colVar);
addedVariables.add(new IdLoopTuple(colVar.getIdVar(), colVar.getIdLoop()));
addedVariables.add(new IdLoopTuple(colVar.getVarId(), colVar.getLoopId()));
});
if (surveyUnitModel.getExternalVariables() != null){
surveyUnitModel.getExternalVariables().stream()
.filter(extVar -> !addedVariables.contains(new IdLoopTuple(extVar.getIdVar(), "")))
.filter(extVar -> !addedVariables.contains(new IdLoopTuple(extVar.getVarId(), "")))
.forEach(extVar -> {
externalToKeep.add(extVar);
addedVariables.add(new IdLoopTuple(extVar.getIdVar(), ""));
addedVariables.add(new IdLoopTuple(extVar.getVarId(), ""));
});
}

Expand Down Expand Up @@ -136,7 +136,7 @@ public SurveyUnitDto findLatestValuesByStateByIdAndByIdQuestionnaire(String idUE
public List<SurveyUnitId> findDistinctIdUEsByIdQuestionnaire(String idQuestionnaire) {
List<SurveyUnitModel> surveyUnitModels = surveyUnitPersistencePort.findIdUEsByIdQuestionnaire(idQuestionnaire);
List<SurveyUnitId> suIds = new ArrayList<>();
surveyUnitModels.forEach(surveyUnitDto -> suIds.add(new SurveyUnitId(surveyUnitDto.getIdUE())));
surveyUnitModels.forEach(surveyUnitDto -> suIds.add(new SurveyUnitId(surveyUnitDto.getInterrogationId())));
return suIds.stream().distinct().toList();
}

Expand Down Expand Up @@ -236,14 +236,14 @@ private void extractVariables(SurveyUnitModel surveyUnitModel,
surveyUnitModel.setCollectedVariables(new ArrayList<>());
}
for (CollectedVariable collectedVariable : surveyUnitModel.getCollectedVariables()) {
IdLoopTuple idLoopTuple = new IdLoopTuple(collectedVariable.getIdVar(), collectedVariable.getIdLoop());
IdLoopTuple idLoopTuple = new IdLoopTuple(collectedVariable.getVarId(), collectedVariable.getLoopId());
VariableDto variableDto = collectedVariableMap.get(idLoopTuple);

//Create variable into map if not exists
if (variableDto == null) {
variableDto = VariableDto.builder()
.variableName(collectedVariable.getIdVar())
.idLoop(collectedVariable.getIdLoop())
.variableName(collectedVariable.getVarId())
.idLoop(collectedVariable.getLoopId())
.variableStateDtoList(new ArrayList<>())
.build();
collectedVariableMap.put(idLoopTuple, variableDto);
Expand All @@ -265,15 +265,15 @@ private void extractVariables(SurveyUnitModel surveyUnitModel,
surveyUnitModel.setExternalVariables(new ArrayList<>());
}
for(Variable externalVariable : surveyUnitModel.getExternalVariables()){
VariableDto variableDto = externalVariableMap.get(externalVariable.getIdVar());
VariableDto variableDto = externalVariableMap.get(externalVariable.getVarId());

//Create variable into map if not exists
if(variableDto == null){
variableDto = VariableDto.builder()
.variableName(externalVariable.getIdVar())
.variableName(externalVariable.getVarId())
.variableStateDtoList(new ArrayList<>())
.build();
externalVariableMap.put(externalVariable.getIdVar(), variableDto);
externalVariableMap.put(externalVariable.getVarId(), variableDto);
}
//Extract variable state
if(!externalVariable.getValues().isEmpty() && isMostRecentForSameState(surveyUnitModel, variableDto)){
Expand Down
40 changes: 20 additions & 20 deletions src/main/java/fr/insee/genesis/domain/utils/DataVerifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static void verifySurveyUnits(List<SurveyUnitModel> suDtosList, Variables
List<SurveyUnitModel> suDtosListForced = new ArrayList<>(); // Created FORCED SU DTOs

for(String idUE : getIdUEs(suDtosList)) { // For each id of the list
List<SurveyUnitModel> srcSuDtosOfIdUE = suDtosList.stream().filter(element -> element.getIdUE().equals(idUE)).toList();
List<SurveyUnitModel> srcSuDtosOfIdUE = suDtosList.stream().filter(element -> element.getInterrogationId().equals(idUE)).toList();
List<CollectedVariable> correctedCollectedVariables = new ArrayList<>();
List<Variable> correctedExternalVariables = new ArrayList<>();

Expand All @@ -71,11 +71,11 @@ private static SurveyUnitModel createForcedDto(
List<CollectedVariable> correctedCollectedVariables,
List<Variable> correctedExternalVariables
) {
SurveyUnitModel sampleSuDto = suDtosList.stream().filter(element -> element.getIdUE().equals(idUE)).toList().getFirst();
SurveyUnitModel sampleSuDto = suDtosList.stream().filter(element -> element.getInterrogationId().equals(idUE)).toList().getFirst();
SurveyUnitModel newForcedSuDto = SurveyUnitModel.builder()
.idQuest(sampleSuDto.getIdQuest())
.idCampaign(sampleSuDto.getIdCampaign())
.idUE(idUE)
.questionnaireId(sampleSuDto.getQuestionnaireId())
.campaignId(sampleSuDto.getCampaignId())
.interrogationId(idUE)
.state(DataState.FORCED)
.mode(sampleSuDto.getMode())
.recordDate(LocalDateTime.now())
Expand All @@ -86,18 +86,18 @@ private static SurveyUnitModel createForcedDto(

for(CollectedVariable correctedCollectedVariable : correctedCollectedVariables){
newForcedSuDto.getCollectedVariables().add(
new CollectedVariable(correctedCollectedVariable.getIdVar(),
new CollectedVariable(correctedCollectedVariable.getVarId(),
correctedCollectedVariable.getValues()
,correctedCollectedVariable.getIdLoop()
,correctedCollectedVariable.getIdParent()
,correctedCollectedVariable.getLoopId()
,correctedCollectedVariable.getParentId()
)
);
}

for(Variable correctedExternalVariable : correctedExternalVariables){
newForcedSuDto.getExternalVariables().add(
Variable.builder()
.idVar(correctedExternalVariable.getIdVar())
.varId(correctedExternalVariable.getVarId())
.values(correctedExternalVariable.getValues())
.build()
);
Expand All @@ -114,7 +114,7 @@ private static SurveyUnitModel createForcedDto(
private static Set<String> getIdUEs(List<SurveyUnitModel> suDtosList) {
Set<String> idUEs = new HashSet<>();
for(SurveyUnitModel surveyUnitModel : suDtosList){
idUEs.add(surveyUnitModel.getIdUE());
idUEs.add(surveyUnitModel.getInterrogationId());
}

return idUEs;
Expand All @@ -136,20 +136,20 @@ private static void collectedVariablesManagement(List<SurveyUnitModel> srcSuDtos
//Get more priority variables to verify
for(SurveyUnitModel srcSuDto : sortedSuDtos){
for(CollectedVariable collectedVariable : srcSuDto.getCollectedVariables()){
if(!variableNames.contains(collectedVariable.getIdVar())){
variableNames.add(collectedVariable.getIdVar());
if(!variableNames.contains(collectedVariable.getVarId())){
variableNames.add(collectedVariable.getVarId());
variablesToVerify.add(collectedVariable);
}
}
}

//Verify variables
for(CollectedVariable collectedVariableToVerify : variablesToVerify){
if(variablesMap.hasVariable(collectedVariableToVerify.getIdVar()))
if(variablesMap.hasVariable(collectedVariableToVerify.getVarId()))
{
CollectedVariable correctedCollectedVariable = verifyCollectedVariable(
collectedVariableToVerify,
variablesMap.getVariable(collectedVariableToVerify.getIdVar())
variablesMap.getVariable(collectedVariableToVerify.getVarId())
);

if(correctedCollectedVariable != null){
Expand All @@ -173,10 +173,10 @@ private static CollectedVariable verifyCollectedVariable(CollectedVariable colle
}

return isInvalid ? new CollectedVariable(
collectedVariable.getIdVar(),
collectedVariable.getVarId(),
newValues,
collectedVariable.getIdLoop(),
collectedVariable.getIdParent()
collectedVariable.getLoopId(),
collectedVariable.getParentId()
) : null;
}

Expand All @@ -189,10 +189,10 @@ private static void externalVariablesManagement(List<SurveyUnitModel> srcSuDtosO
//Verify variables
if(collectedSuDtoOpt.isPresent()){
for(Variable variable: collectedSuDtoOpt.get().getExternalVariables()){
if(variablesMap.hasVariable(variable.getIdVar())) {
if(variablesMap.hasVariable(variable.getVarId())) {
Variable correctedExternalVariable = verifyExternalVariable(
variable,
variablesMap.getVariable(variable.getIdVar())
variablesMap.getVariable(variable.getVarId())
);
if (correctedExternalVariable != null) {
correctedExternalVariables.add(correctedExternalVariable);
Expand Down Expand Up @@ -222,7 +222,7 @@ private static Variable verifyExternalVariable(Variable externalVariable, fr.ins
}

return isInvalid ? Variable.builder()
.idVar(externalVariable.getIdVar())
.varId(externalVariable.getVarId())
.values(newValues)
.build() : null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public List<SurveyUnitModel> findByIdUEsAndIdQuestionnaire(List<SurveyUnitModel>
List<SurveyUnitDocument> surveyUnits= new ArrayList<>();
// TODO: 18-10-2023 : find a way to do this in one query
idUEs.forEach(su -> {
List<SurveyUnitDocument> docs = mongoRepository.findByIdUEAndIdQuestionnaire(su.getIdUE(), idQuestionnaire);
List<SurveyUnitDocument> docs = mongoRepository.findByIdUEAndIdQuestionnaire(su.getInterrogationId(), idQuestionnaire);
surveyUnits.addAll(docs);
});
return surveyUnits.isEmpty() ? Collections.emptyList() : SurveyUnitDocumentMapper.INSTANCE.listDocumentToListModel(surveyUnits);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public class ExternalVariable implements Serializable {

@Serial
private static final long serialVersionUID = 6267528628435012000L;
private String idVar;
private String varId;
private List<String> values;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
@CompoundIndex(name = "idQuestionnaire_1_idCampaign_1", def = "{'idQuestionnaire': 1, 'idCampaign': 1}")
@CompoundIndex(name = "idQuestionnaire_1_idUE_1", def = "{'idQuestionnaire': 1, 'idUE': 1}")
public class SurveyUnitDocument {
private String idCampaign;
private String idUE;
private String idQuestionnaire;
private String campaignId;
private String interrogationId;
private String questionnaireId;
private String state;
private String mode;
private LocalDateTime recordDate;
Expand Down
Loading
Loading