From 265347cad1c818b0a409a9ff727ac886e1884c6b Mon Sep 17 00:00:00 2001 From: QDIBYS Date: Mon, 30 Dec 2024 10:44:24 +0100 Subject: [PATCH] refactors --- .../rawdata/LunaticJsonRawDataUnprocessedDto.java | 7 +------ .../rest/responses/ResponseController.java | 2 +- .../genesis/controller/utils/ControllerUtils.java | 13 ++++++++++--- .../model/variabletype/VariableTypeModel.java | 14 ++++++-------- .../service/rawdata/LunaticJsonRawDataService.java | 14 +++++++++++--- .../service/variabletype/VariableTypeService.java | 2 +- .../adapter/VariableTypeMongoAdapter.java | 2 +- .../document/rawdata/LunaticJsonDataDocument.java | 6 +----- .../rest/responses/ResponseControllerTest.java | 7 +++---- 9 files changed, 35 insertions(+), 32 deletions(-) diff --git a/src/main/java/fr/insee/genesis/controller/dto/rawdata/LunaticJsonRawDataUnprocessedDto.java b/src/main/java/fr/insee/genesis/controller/dto/rawdata/LunaticJsonRawDataUnprocessedDto.java index f99a1ae..b949288 100644 --- a/src/main/java/fr/insee/genesis/controller/dto/rawdata/LunaticJsonRawDataUnprocessedDto.java +++ b/src/main/java/fr/insee/genesis/controller/dto/rawdata/LunaticJsonRawDataUnprocessedDto.java @@ -1,11 +1,6 @@ package fr.insee.genesis.controller.dto.rawdata; import lombok.Builder; -import lombok.Data; @Builder -@Data -public class LunaticJsonRawDataUnprocessedDto { - private String campaignId; - private String idUE; -} +public record LunaticJsonRawDataUnprocessedDto(String campaignId, String idUE){} \ No newline at end of file diff --git a/src/main/java/fr/insee/genesis/controller/rest/responses/ResponseController.java b/src/main/java/fr/insee/genesis/controller/rest/responses/ResponseController.java index 5155178..20ce843 100644 --- a/src/main/java/fr/insee/genesis/controller/rest/responses/ResponseController.java +++ b/src/main/java/fr/insee/genesis/controller/rest/responses/ResponseController.java @@ -235,7 +235,7 @@ public ResponseEntity processJsonRawData( @RequestParam("questionnaireId") String questionnaireId, @RequestBody List idUEList ){ - log.info("Try to get process raw JSON datas for campaign {} and {} idUEs", campaignName, idUEList.size()); + log.info("Try to process raw JSON datas for campaign {} and {} idUEs", campaignName, idUEList.size()); int dataCount = 0; List errors = new ArrayList<>(); diff --git a/src/main/java/fr/insee/genesis/controller/utils/ControllerUtils.java b/src/main/java/fr/insee/genesis/controller/utils/ControllerUtils.java index 06a6a89..39f25f0 100644 --- a/src/main/java/fr/insee/genesis/controller/utils/ControllerUtils.java +++ b/src/main/java/fr/insee/genesis/controller/utils/ControllerUtils.java @@ -21,10 +21,17 @@ public ControllerUtils(FileUtils fileUtils) { this.fileUtils = fileUtils; } + + /** + * If a mode is specified, we treat only this mode. + * If no mode is specified, we treat all modes in the campaign. + * If no mode is specified and no specs are found, we return an error + * @param campaign campaign id to get modes + * @param modeSpecified a Mode to use, null if we want all modes available + * @return a list with the mode in modeSpecified or all modes if null + * @throws GenesisException if error in specs structure + */ public List getModesList(String campaign, Mode modeSpecified) throws GenesisException { - // If a mode is specified, we treat only this mode. - // If no mode is specified, we treat all modes in the campaign. - // If no mode is specified and no specs are found, we return an error if (modeSpecified != null){ return Collections.singletonList(modeSpecified); } diff --git a/src/main/java/fr/insee/genesis/domain/model/variabletype/VariableTypeModel.java b/src/main/java/fr/insee/genesis/domain/model/variabletype/VariableTypeModel.java index 6ac4dac..043f7b6 100644 --- a/src/main/java/fr/insee/genesis/domain/model/variabletype/VariableTypeModel.java +++ b/src/main/java/fr/insee/genesis/domain/model/variabletype/VariableTypeModel.java @@ -3,15 +3,13 @@ import fr.insee.bpm.metadata.model.VariableType; import fr.insee.genesis.domain.model.surveyunit.Mode; import lombok.Builder; -import lombok.Data; import java.util.Map; -@Data @Builder -public class VariableTypeModel { - private String campaignId; - private String questionnaireId; - private Mode mode; - private Map variables; -} +public record VariableTypeModel ( + String campaignId, + String questionnaireId, + Mode mode, + Map variables +){} diff --git a/src/main/java/fr/insee/genesis/domain/service/rawdata/LunaticJsonRawDataService.java b/src/main/java/fr/insee/genesis/domain/service/rawdata/LunaticJsonRawDataService.java index 7410769..db57a5d 100644 --- a/src/main/java/fr/insee/genesis/domain/service/rawdata/LunaticJsonRawDataService.java +++ b/src/main/java/fr/insee/genesis/domain/service/rawdata/LunaticJsonRawDataService.java @@ -16,6 +16,7 @@ import fr.insee.genesis.domain.ports.spi.LunaticJsonPersistancePort; import fr.insee.genesis.domain.utils.LoopIdentifier; import fr.insee.genesis.infrastructure.document.rawdata.LunaticJsonDataDocument; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; @@ -27,6 +28,7 @@ import java.util.Set; @Service +@Slf4j public class LunaticJsonRawDataService implements LunaticJsonRawDataApiPort { @Qualifier("lunaticJsonMongoAdapter") private final LunaticJsonPersistancePort lunaticJsonPersistancePort; @@ -97,9 +99,7 @@ public List parseRawData( CollectedVariable collectedVariable = CollectedVariable.collectedVariableBuilder() .idVar(variableName) .values(new ArrayList<>()) - .idLoop(variablesMap.getVariable(variableName) == null ? - Constants.ROOT_GROUP_NAME : //TODO What do we do if null ? Exception ? - variablesMap.getVariable(variableName).getGroupName()) + .idLoop(getIdLoop(variablesMap, variableName)) .idParent(LoopIdentifier.getRelatedVariableName(variableName, variablesMap)) .build(); @@ -113,6 +113,14 @@ public List parseRawData( return surveyUnitModels; } + private static String getIdLoop(VariablesMap variablesMap, String variableName) { + if (variablesMap.getVariable(variableName) == null){ + log.warn("Variable {} not present in metadatas, assigning to {}", variableName, Constants.ROOT_GROUP_NAME); + return Constants.ROOT_GROUP_NAME; + } + return variablesMap.getVariable(variableName).getGroupName(); + } + @Override public void updateProcessDates(List surveyUnitModels) { Set campaignIds = new HashSet<>(); diff --git a/src/main/java/fr/insee/genesis/domain/service/variabletype/VariableTypeService.java b/src/main/java/fr/insee/genesis/domain/service/variabletype/VariableTypeService.java index 13c9a8b..a9449b2 100644 --- a/src/main/java/fr/insee/genesis/domain/service/variabletype/VariableTypeService.java +++ b/src/main/java/fr/insee/genesis/domain/service/variabletype/VariableTypeService.java @@ -33,7 +33,7 @@ public void saveMetadatas(String campaignId, String questionnaireId, Mode mode, for(String variableName : variablesMap.getVariables().keySet()){ Variable bpmVariable = variablesMap.getVariable(variableName); - variableTypeModel.getVariables().put(variableName,bpmVariable.getType()); + variableTypeModel.variables().put(variableName,bpmVariable.getType()); } variableTypePersistancePort.save(variableTypeModel); diff --git a/src/main/java/fr/insee/genesis/infrastructure/adapter/VariableTypeMongoAdapter.java b/src/main/java/fr/insee/genesis/infrastructure/adapter/VariableTypeMongoAdapter.java index ff329e1..55ac040 100644 --- a/src/main/java/fr/insee/genesis/infrastructure/adapter/VariableTypeMongoAdapter.java +++ b/src/main/java/fr/insee/genesis/infrastructure/adapter/VariableTypeMongoAdapter.java @@ -29,7 +29,7 @@ public VariableTypeMongoAdapter(VariableTypeMongoDBRepository variableTypeMongoD @Override public void save(VariableTypeModel variableTypeModel) { mongoTemplate.update(VariableTypeDocument.class) - .matching(Query.query(Criteria.where("campaignId").is(variableTypeModel.getCampaignId()))) + .matching(Query.query(Criteria.where("campaignId").is(variableTypeModel.campaignId()))) .replaceWith(VariableTypeDocumentMapper.INSTANCE.modelToDocument(variableTypeModel)) .withOptions(FindAndReplaceOptions.options().upsert()) .findAndReplace(); diff --git a/src/main/java/fr/insee/genesis/infrastructure/document/rawdata/LunaticJsonDataDocument.java b/src/main/java/fr/insee/genesis/infrastructure/document/rawdata/LunaticJsonDataDocument.java index fd5aa2b..d71a763 100644 --- a/src/main/java/fr/insee/genesis/infrastructure/document/rawdata/LunaticJsonDataDocument.java +++ b/src/main/java/fr/insee/genesis/infrastructure/document/rawdata/LunaticJsonDataDocument.java @@ -2,9 +2,7 @@ import fr.insee.genesis.Constants; import fr.insee.genesis.domain.model.surveyunit.Mode; -import lombok.AllArgsConstructor; import lombok.Data; -import lombok.NoArgsConstructor; import org.bson.types.ObjectId; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; @@ -13,10 +11,8 @@ import java.util.Map; @Data -@NoArgsConstructor -@AllArgsConstructor @Document(collection = Constants.MONGODB_LUNATIC_RAWDATA_COLLECTION_NAME) -public class LunaticJsonDataDocument { +public class LunaticJsonDataDocument { //TODO try to use record @Id private ObjectId id; private String campaignId; diff --git a/src/test/java/fr/insee/genesis/controller/rest/responses/ResponseControllerTest.java b/src/test/java/fr/insee/genesis/controller/rest/responses/ResponseControllerTest.java index 8a4fff6..9917023 100644 --- a/src/test/java/fr/insee/genesis/controller/rest/responses/ResponseControllerTest.java +++ b/src/test/java/fr/insee/genesis/controller/rest/responses/ResponseControllerTest.java @@ -20,7 +20,6 @@ import fr.insee.genesis.domain.service.surveyunit.SurveyUnitQualityService; import fr.insee.genesis.domain.service.surveyunit.SurveyUnitService; import fr.insee.genesis.domain.service.variabletype.VariableTypeService; -import fr.insee.genesis.exceptions.GenesisException; import fr.insee.genesis.infrastructure.document.rawdata.LunaticJsonDataDocument; import fr.insee.genesis.infrastructure.utils.FileUtils; import fr.insee.genesis.stubs.ConfigStub; @@ -219,8 +218,8 @@ void getUnprocessedDataTest(){ //THEN Assertions.assertThat(dtos).isNotNull().isNotEmpty().hasSize(1); - Assertions.assertThat(dtos.getFirst().getCampaignId()).isEqualTo(campaignId); - Assertions.assertThat(dtos.getFirst().getIdUE()).isEqualTo(idUE); + Assertions.assertThat(dtos.getFirst().campaignId()).isEqualTo(campaignId); + Assertions.assertThat(dtos.getFirst().idUE()).isEqualTo(idUE); } @Test @@ -241,7 +240,7 @@ void getUnprocessedDataTest_processDate_present(){ //raw data process //json @Test - void processJsonRawDataTest() throws GenesisException { + void processJsonRawDataTest() { //GIVEN lunaticJsonPersistanceStub.getMongoStub().clear(); surveyUnitPersistencePortStub.getMongoStub().clear();