Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 5 additions & 1 deletion src/main/java/fr/insee/genesis/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ public class Constants {
public static final String VOLUMETRY_RAW_FILE_SUFFIX = "_RAW_VOLUMETRY";
public static final String VOLUMETRY_FILE_DATE_FORMAT = "yyyy_MM_dd";
public static final int VOLUMETRY_FILE_EXPIRATION_DAYS = 30;

public static final int MAX_LINKS_ALLOWED = 21;
public static final String LIEN = "LIEN_";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename PAIRWISE_PREFIX

public static final String LIENS = "LIENS";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename PAIRWISES

public static final String NO_PAIRWISE_VALUE = "99";
public static final String SCHEDULE_ARCHIVE_FOLDER_NAME = "genesis_deleted_schedules";
public static final String SAME_AXIS_VALUE = "0";


// XML sequential reading parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import static fr.insee.genesis.domain.service.rawdata.RawResponseService.handleLiensCollectedVariable;

@Service
@Slf4j
Expand Down Expand Up @@ -487,26 +488,28 @@ private static void convertToCollectedVar(
final var dest = dstSurveyUnitModel.getCollectedVariables();

for (Map.Entry<String, Object> collectedVariable : collectedMap.entrySet()) {
// Map for this variable (COLLECTED/EDITED -> value)
Map<String, Object> states = JsonUtils.asMap(collectedVariable.getValue());

// nothing if no state
if (states == null || states.isEmpty()) {
String variableName = collectedVariable.getKey();

if (Constants.LIENS.equals(variableName)) {
handleLiensCollectedVariable(
collectedVariable,
dataState,
variablesMap,
dstSurveyUnitModel
);
continue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: Sonar doesn't like more than one continue in a method

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose to a extract a method like this, that excludes all "continue" and is easier to read

private static void processCollectedVariable(
Map.Entry<String, Object> entry,
String stateKey,
VariablesMap variablesMap,
SurveyUnitModel dstSurveyUnitModel,
CollectedVariables dest
) {
if (Constants.LIENS.equals(entry.getKey())) {
handleLiensCollectedVariable(entry, DataState.valueOf(stateKey), variablesMap, dstSurveyUnitModel);
return;
}

Map<String, Object> states = JsonUtils.asMap(entry.getValue());
if (states == null) return;

Object value = states.get(stateKey);
if (value == null) return;

if (value instanceof List<?> list) {
    convertListVar(list, entry, variablesMap, dest);
} else {
    convertOneVar(entry, getValueString(value), variablesMap, 1, dest);
}

}

}

if (states.containsKey(stateKey)) {
Map<String, Object> states = JsonUtils.asMap(collectedVariable.getValue());
if (states != null && states.containsKey(stateKey)) {
Object value = states.get(stateKey);

// liste ?
if (value instanceof List<?>) {
// on garde exactement ta signature existante
convertListVar(value, collectedVariable, variablesMap, dest);
}

// scalaire non null ?
if (value != null && !(value instanceof List<?>)) {
// idem: on garde convertOneVar(entry, String, ...)
convertOneVar(collectedVariable, getValueString(value), variablesMap, 1, dest);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ private void convertRawDataCollectedVariables(
convertToCollectedVar(dstSurveyUnitModel, dataState, variablesMap, collectedMap);
}


private static void convertToCollectedVar(
SurveyUnitModel dstSurveyUnitModel,
DataState dataState,
Expand All @@ -433,6 +434,17 @@ private static void convertToCollectedVar(

for (Map.Entry<String, Object> collectedVariable : collectedMap.entrySet()) {
// Map for this variable (COLLECTED/EDITED -> value)
String variableName = collectedVariable.getKey();

if (Constants.LIENS.equals(variableName)) {
handleLiensCollectedVariable(
collectedVariable,
dataState,
variablesMap,
dstSurveyUnitModel
);
continue;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this method can be extract as in LunaticJsonRawDataService

Map<String, Object> states = JsonUtils.asMap(collectedVariable.getValue());

if (states != null && states.containsKey(stateKey)) {
Expand Down Expand Up @@ -519,4 +531,67 @@ private static void convertToExternalVar(SurveyUnitModel dstSurveyUnitModel, Var
public Page<RawResponseModel> findRawResponseDataByCampaignIdAndDate(String campaignId, Instant startDate, Instant endDate, Pageable pageable) {
return rawResponsePersistencePort.findByCampaignIdAndDate(campaignId,startDate, endDate,pageable);
}

@SuppressWarnings("unchecked")
static void handleLiensCollectedVariable(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change Liens into pairwise

Map.Entry<String, Object> collectedVariable,
DataState dataState,
VariablesMap variablesMap,
SurveyUnitModel dstSurveyUnitModel
) {
Object value = getValueForState(collectedVariable, dataState.toString());

if (!(value instanceof List<?> individus)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a boolean method to explain why you do these test

|| !variablesMap.hasVariable(Constants.LIEN + 1)) {
return;
}

String groupName = variablesMap
.getVariable(Constants.LIEN + 1)
.getGroupName();

for (int individualIndex = 0; individualIndex < individus.size(); individualIndex++) {
List<String> individualLinks = (List<String>) individus.get(individualIndex);

for (int linkIndex = 1; linkIndex <= Constants.MAX_LINKS_ALLOWED; linkIndex++) {
dstSurveyUnitModel.getCollectedVariables().add(
buildLienVariable(individualLinks, linkIndex, individualIndex, groupName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here is "lien" and "link".

);
}
}
}

private static VariableModel buildLienVariable(
List<String> liensIndividu,
int linkIndex,
int iteration,
String groupName
) {
String value = Constants.NO_PAIRWISE_VALUE;

if (linkIndex <= liensIndividu.size()) {
String v = liensIndividu.get(linkIndex - 1);
value = (v == null || v.isBlank())
? Constants.SAME_AXIS_VALUE
: v;
}

return VariableModel.builder()
.varId(Constants.LIEN + linkIndex)
.value(value)
.scope(groupName)
.iteration(iteration)
.parentId(groupName)
.build();
}


private static Object getValueForState(
Map.Entry<String, Object> collectedVariable,
String stateKey
) {
Map<String, Object> states = JsonUtils.asMap(collectedVariable.getValue());
return states != null ? states.get(stateKey) : null;
}

}
Loading