Skip to content

Commit

Permalink
Merge pull request #96 from InseeFr/devRefactors
Browse files Browse the repository at this point in the history
Refactors
  • Loading branch information
alexisszmundy authored Sep 25, 2024
2 parents 8396f40 + 85d338a commit 9621194
Show file tree
Hide file tree
Showing 80 changed files with 1,984 additions and 1,777 deletions.
23 changes: 0 additions & 23 deletions src/main/java/fr/insee/genesis/configuration/PortConfig.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package fr.insee.genesis.controller.adapter;

import fr.insee.genesis.controller.sources.json.LunaticJsonSurveyUnit;
import fr.insee.genesis.domain.dtos.DataState;
import fr.insee.genesis.domain.dtos.Mode;
import fr.insee.genesis.domain.dtos.SurveyUnitUpdateDto;
import fr.insee.genesis.domain.model.surveyunit.DataState;
import fr.insee.genesis.domain.model.surveyunit.Mode;
import fr.insee.genesis.domain.model.surveyunit.SurveyUnitModel;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class LunaticJsonAdapter {

public SurveyUnitUpdateDto convert(LunaticJsonSurveyUnit su){
return SurveyUnitUpdateDto.builder()
public SurveyUnitModel convert(LunaticJsonSurveyUnit su){
return SurveyUnitModel.builder()
.idQuest(su.getIdQuest())
.idCampaign("")
.idUE(su.getIdUE())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package fr.insee.genesis.controller.adapter;

import fr.insee.bpm.metadata.model.VariablesMap;
import fr.insee.genesis.controller.sources.xml.LunaticXmlCollectedData;
import fr.insee.genesis.controller.sources.xml.LunaticXmlSurveyUnit;
import fr.insee.genesis.controller.sources.xml.ValueType;
import fr.insee.genesis.controller.utils.LoopIdentifier;

import fr.insee.genesis.domain.dtos.CollectedVariableDto;
import fr.insee.genesis.domain.dtos.DataState;
import fr.insee.genesis.domain.dtos.Mode;
import fr.insee.genesis.domain.dtos.SurveyUnitUpdateDto;
import fr.insee.genesis.domain.dtos.VariableDto;
import fr.insee.bpm.metadata.model.VariablesMap;
import fr.insee.genesis.domain.model.surveyunit.SurveyUnitModel;
import fr.insee.genesis.domain.utils.LoopIdentifier;
import fr.insee.genesis.domain.model.surveyunit.CollectedVariable;
import fr.insee.genesis.domain.model.surveyunit.DataState;
import fr.insee.genesis.domain.model.surveyunit.Mode;
import fr.insee.genesis.domain.model.surveyunit.Variable;
import lombok.experimental.UtilityClass;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@UtilityClass
public class LunaticXmlAdapter {
Expand All @@ -27,29 +27,37 @@ public class LunaticXmlAdapter {
* @param idCampaign survey id
* @return a genesis survey unit DTO
*/
public static List<SurveyUnitUpdateDto> convert(LunaticXmlSurveyUnit su, VariablesMap variablesMap, String idCampaign, Mode mode){
public static List<SurveyUnitModel> convert(LunaticXmlSurveyUnit su, VariablesMap variablesMap, String idCampaign, Mode mode){
//Get COLLECTED Data and external variables
List<SurveyUnitUpdateDto> surveyUnitUpdateDtoList = new ArrayList<>();
SurveyUnitUpdateDto surveyUnitUpdateDto = getStateDataFromSurveyUnit(su, variablesMap, idCampaign, DataState.COLLECTED, mode);
getExternalDataFromSurveyUnit(su,surveyUnitUpdateDto);
List<SurveyUnitModel> surveyUnitModelList = new ArrayList<>();
SurveyUnitModel surveyUnitModel = getStateDataFromSurveyUnit(su, variablesMap, idCampaign, DataState.COLLECTED, mode);
getExternalDataFromSurveyUnit(su, surveyUnitModel);

surveyUnitUpdateDtoList.add(surveyUnitUpdateDto);
surveyUnitModelList.add(surveyUnitModel);

//Get data from other states
SurveyUnitUpdateDto editedSurveyUnitUpdateDto = getStateDataFromSurveyUnit(su, variablesMap, idCampaign, DataState.EDITED,mode);
if(editedSurveyUnitUpdateDto != null) surveyUnitUpdateDtoList.add(editedSurveyUnitUpdateDto);
SurveyUnitModel editedSurveyUnitModel = getStateDataFromSurveyUnit(su, variablesMap, idCampaign, DataState.EDITED,mode);
if(editedSurveyUnitModel != null){
surveyUnitModelList.add(editedSurveyUnitModel);
}

SurveyUnitUpdateDto inputedSurveyUnitUpdateDto = getStateDataFromSurveyUnit(su, variablesMap, idCampaign, DataState.INPUTED,mode);
if(inputedSurveyUnitUpdateDto != null) surveyUnitUpdateDtoList.add(inputedSurveyUnitUpdateDto);
SurveyUnitModel inputedSurveyUnitModel = getStateDataFromSurveyUnit(su, variablesMap, idCampaign, DataState.INPUTED,mode);
if(inputedSurveyUnitModel != null){
surveyUnitModelList.add(inputedSurveyUnitModel);
}

SurveyUnitUpdateDto forcedSurveyUnitUpdateDto = getStateDataFromSurveyUnit(su, variablesMap, idCampaign, DataState.FORCED,mode);
if(forcedSurveyUnitUpdateDto != null) surveyUnitUpdateDtoList.add(forcedSurveyUnitUpdateDto);
SurveyUnitModel forcedSurveyUnitModel = getStateDataFromSurveyUnit(su, variablesMap, idCampaign, DataState.FORCED,mode);
if(forcedSurveyUnitModel != null){
surveyUnitModelList.add(forcedSurveyUnitModel);
}

SurveyUnitUpdateDto previousSurveyUnitUpdateDto = getStateDataFromSurveyUnit(su, variablesMap, idCampaign, DataState.PREVIOUS,mode);
if(previousSurveyUnitUpdateDto != null) surveyUnitUpdateDtoList.add(previousSurveyUnitUpdateDto);
SurveyUnitModel previousSurveyUnitModel = getStateDataFromSurveyUnit(su, variablesMap, idCampaign, DataState.PREVIOUS,mode);
if(previousSurveyUnitModel != null){
surveyUnitModelList.add(previousSurveyUnitModel);
}


return surveyUnitUpdateDtoList;
return surveyUnitModelList;
}

/**
Expand All @@ -60,8 +68,8 @@ public static List<SurveyUnitUpdateDto> convert(LunaticXmlSurveyUnit su, Variabl
* @param dataState state of the DTO to generate
* @return Survey Unit DTO with a specific state
*/
private static SurveyUnitUpdateDto getStateDataFromSurveyUnit(LunaticXmlSurveyUnit su, VariablesMap variablesMap, String idCampaign, DataState dataState,Mode mode) {
SurveyUnitUpdateDto surveyUnitUpdateDto = SurveyUnitUpdateDto.builder()
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())
Expand All @@ -71,35 +79,40 @@ private static SurveyUnitUpdateDto getStateDataFromSurveyUnit(LunaticXmlSurveyUn
.fileDate(su.getFileDate())
.build();

return getCollectedDataFromSurveyUnit(su, surveyUnitUpdateDto, variablesMap, dataState);
return getCollectedDataFromSurveyUnit(su, surveyUnitModel, variablesMap, dataState);
}


/**
* Gets data from a specific state and put it into DTO's data
* @param su XML survey unit to extract from
* @param surveyUnitUpdateDto DTO to aliment
* @param surveyUnitModel DTO to aliment
* @param variablesMap variables definitions (used for loops)
* @param dataState data state from XML
* @return the DTO containing data, null if no data and not COLLECTED
*/
private static SurveyUnitUpdateDto getCollectedDataFromSurveyUnit(LunaticXmlSurveyUnit su, SurveyUnitUpdateDto surveyUnitUpdateDto, VariablesMap variablesMap, DataState dataState) {
List<CollectedVariableDto> variablesUpdate = new ArrayList<>();
private static SurveyUnitModel getCollectedDataFromSurveyUnit(LunaticXmlSurveyUnit su, SurveyUnitModel surveyUnitModel, VariablesMap variablesMap, DataState dataState) {
List<CollectedVariable> variablesUpdate = new ArrayList<>();

int dataCount = 0;
for (LunaticXmlCollectedData lunaticXmlCollectedData : su.getData().getCollected()){
List<ValueType> valueTypeList;
switch (dataState){
case COLLECTED: valueTypeList = lunaticXmlCollectedData.getCollected();
break;
case EDITED : valueTypeList = lunaticXmlCollectedData.getEdited();
break;
case FORCED : valueTypeList = lunaticXmlCollectedData.getForced();
break;
case INPUTED: valueTypeList = lunaticXmlCollectedData.getInputed();
break;
case PREVIOUS: valueTypeList = lunaticXmlCollectedData.getPrevious();
break;
case COLLECTED:
valueTypeList = lunaticXmlCollectedData.getCollected();
break;
case EDITED :
valueTypeList = lunaticXmlCollectedData.getEdited();
break;
case FORCED :
valueTypeList = lunaticXmlCollectedData.getForced();
break;
case INPUTED:
valueTypeList = lunaticXmlCollectedData.getInputed();
break;
case PREVIOUS:
valueTypeList = lunaticXmlCollectedData.getPrevious();
break;
default:
return null;
}
Expand All @@ -108,7 +121,7 @@ private static SurveyUnitUpdateDto getCollectedDataFromSurveyUnit(LunaticXmlSurv
List<String> variableValues = new ArrayList<>();
if (valueTypeList.get(i-1).getValue()!=null) {
variableValues.add(valueTypeList.get(i-1).getValue());
variablesUpdate.add(CollectedVariableDto.collectedVariableBuilder()
variablesUpdate.add(CollectedVariable.collectedVariableBuilder()
.idVar(lunaticXmlCollectedData.getVariableName())
.values(variableValues)
.idLoop(LoopIdentifier.getLoopIdentifier(lunaticXmlCollectedData.getVariableName(), variablesMap, i))
Expand All @@ -119,10 +132,12 @@ private static SurveyUnitUpdateDto getCollectedDataFromSurveyUnit(LunaticXmlSurv
}
}
}
surveyUnitUpdateDto.setCollectedVariables(variablesUpdate);
surveyUnitModel.setCollectedVariables(variablesUpdate);

//Return null if no data and not COLLECTED
if(dataCount > 0 || dataState.equals(DataState.COLLECTED)) return surveyUnitUpdateDto;
if(dataCount > 0 || dataState.equals(DataState.COLLECTED)){
return surveyUnitModel;
}
return null;
}

Expand All @@ -131,27 +146,25 @@ private static SurveyUnitUpdateDto getCollectedDataFromSurveyUnit(LunaticXmlSurv
/**
* Extract external data from XML survey unit and put it into DTO
* @param su XML survey unit
* @param surveyUnitUpdateDto DTO to aliment
* @param surveyUnitModel DTO to aliment
*/
private static void getExternalDataFromSurveyUnit(LunaticXmlSurveyUnit su, SurveyUnitUpdateDto surveyUnitUpdateDto) {
List<VariableDto> externalVariables = new ArrayList<>();
private static void getExternalDataFromSurveyUnit(LunaticXmlSurveyUnit su, SurveyUnitModel surveyUnitModel) {
List<Variable> externalVariables = new ArrayList<>();
su.getData().getExternal().forEach(lunaticXmlExternalData ->
externalVariables.add(VariableDto.builder()
externalVariables.add(Variable.builder()
.idVar(lunaticXmlExternalData.getVariableName())
.values(getValuesFromValueTypeList(lunaticXmlExternalData.getValues()))
.build())
);
surveyUnitUpdateDto.setExternalVariables(externalVariables);
surveyUnitModel.setExternalVariables(externalVariables);
}

private static List<String> getValuesFromValueTypeList(List<ValueType> valueTypeList) {
if (!valueTypeList.isEmpty()){
List<String> values = new ArrayList<>();
for(ValueType valueType : valueTypeList)
if (valueType.getValue() != null) {
values.add(valueType.getValue());
}
return values;
return valueTypeList.stream()
.map(ValueType::getValue)
.filter(Objects::nonNull)
.toList();
}
return List.of();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package fr.insee.genesis.domain.dtos;
package fr.insee.genesis.controller.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Set;

@AllArgsConstructor
@NoArgsConstructor
@Getter
public class CampaignWithQuestionnaire {
private String idCampaign;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package fr.insee.genesis.domain.dtos;
package fr.insee.genesis.controller.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Set;

@AllArgsConstructor
@NoArgsConstructor
@Getter
public class QuestionnaireWithCampaign {
private String idQuestionnaire;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.insee.genesis.domain.dtos;
package fr.insee.genesis.controller.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package fr.insee.genesis.controller.dto;

import fr.insee.genesis.domain.model.surveyunit.CollectedVariable;
import fr.insee.genesis.domain.model.surveyunit.Variable;
import fr.insee.genesis.domain.model.surveyunit.Mode;
import lombok.Builder;
import lombok.Data;

import java.util.List;

@Builder
@Data
public class SurveyUnitSimplified {

private String idQuest;
private String idCampaign;
private String idUE;
private Mode mode;
private List<CollectedVariable> variablesUpdate;
private List<Variable> externalVariables;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fr.insee.genesis.controller.rest;

import fr.insee.genesis.domain.ports.api.ScheduleApiPort;
import fr.insee.genesis.domain.ports.api.SurveyUnitUpdateApiPort;
import fr.insee.genesis.domain.ports.api.SurveyUnitApiPort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
Expand All @@ -13,14 +13,14 @@
@RequestMapping("/health-check")
@RestController
public class HealthCheckController {
private final SurveyUnitUpdateApiPort surveyUnitUpdateApiPort;
private final SurveyUnitApiPort surveyUnitApiPort;
private final ScheduleApiPort scheduleApiPort;
@Value("${fr.insee.genesis.version}")
private String projectVersion;

@Autowired
public HealthCheckController(SurveyUnitUpdateApiPort surveyUnitUpdateApiPort, ScheduleApiPort scheduleApiPort) {
this.surveyUnitUpdateApiPort = surveyUnitUpdateApiPort;
public HealthCheckController(SurveyUnitApiPort surveyUnitApiPort, ScheduleApiPort scheduleApiPort) {
this.surveyUnitApiPort = surveyUnitApiPort;
this.scheduleApiPort = scheduleApiPort;
}

Expand All @@ -29,10 +29,10 @@ public ResponseEntity<String> healthcheck() {
return ResponseEntity.ok(
"""
OK
\s
Version %s
User %s
"""
\s"""
.formatted(
projectVersion,
SecurityContextHolder.getContext().getAuthentication().getName()
Expand All @@ -44,12 +44,12 @@ public ResponseEntity<String> healthcheckMongo() {
return ResponseEntity.ok(
"""
MongoDB OK
\s
%s Responses
%s Schedules
"""
\s"""
.formatted(
surveyUnitUpdateApiPort.countResponses(),
surveyUnitApiPort.countResponses(),
scheduleApiPort.countSchedules()
));
}
Expand Down
Loading

0 comments on commit 9621194

Please sign in to comment.