Skip to content

Commit

Permalink
refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisszmundy committed Dec 30, 2024
1 parent 65517c9 commit 265347c
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -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){}
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public ResponseEntity<Object> processJsonRawData(
@RequestParam("questionnaireId") String questionnaireId,
@RequestBody List<String> 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<GenesisError> errors = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mode> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, VariableType> variables;
}
public record VariableTypeModel (
String campaignId,
String questionnaireId,
Mode mode,
Map<String, VariableType> variables
){}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,6 +28,7 @@
import java.util.Set;

@Service
@Slf4j
public class LunaticJsonRawDataService implements LunaticJsonRawDataApiPort {
@Qualifier("lunaticJsonMongoAdapter")
private final LunaticJsonPersistancePort lunaticJsonPersistancePort;
Expand Down Expand Up @@ -97,9 +99,7 @@ public List<SurveyUnitModel> 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();

Expand All @@ -113,6 +113,14 @@ public List<SurveyUnitModel> 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<SurveyUnitModel> surveyUnitModels) {
Set<String> campaignIds = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand Down

0 comments on commit 265347c

Please sign in to comment.