Skip to content
Merged
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
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 PAIRWISE_PREFIX = "LIEN_";
public static final String PAIRWISES = "LIENS";
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.processCollectedVariable;

@Service
@Slf4j
Expand Down Expand Up @@ -487,32 +488,11 @@ 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()) {
continue;
}

if (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);
}
}
processCollectedVariable(collectedVariable, stateKey, variablesMap, dstSurveyUnitModel, dest);
}
}


private static void convertListVar(Object valuesForState, Map.Entry<String, Object> collectedVariable, VariablesMap variablesMap, List<VariableModel> dstSurveyUnitModel) {
List<String> values = JsonUtils.asStringList(valuesForState);
if (!values.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
import java.util.Set;
import java.util.stream.Collectors;

import static fr.insee.genesis.domain.service.rawdata.LunaticJsonRawDataService.getValueString;

@Service
@Slf4j
public class RawResponseService implements RawResponseApiPort {
Expand Down Expand Up @@ -429,30 +431,40 @@ private static void convertToCollectedVar(
Map<String, Object> collectedMap
) {
final String stateKey = dataState.toString();
final var dest = dstSurveyUnitModel.getCollectedVariables();
final var collectedVariables = 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());
processCollectedVariable(collectedVariable, stateKey, variablesMap, dstSurveyUnitModel, collectedVariables);
}
}

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);
}
static void processCollectedVariable(
Map.Entry<String, Object> entry,
String stateKey,
VariablesMap variablesMap,
SurveyUnitModel dstSurveyUnitModel,
List<VariableModel> variableModelList
) {
if (Constants.PAIRWISES.equals(entry.getKey())) {
handlePairwiseCollectedVariable(entry, DataState.valueOf(stateKey), variablesMap, dstSurveyUnitModel);
return;
}

// scalaire non null ?
if (value != null && !(value instanceof List<?>)) {
// idem: on garde convertOneVar(entry, String, ...)
convertOneVar(collectedVariable, String.valueOf(value), variablesMap, 1, dest);
}
}
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, variableModelList);
} else {
convertOneVar(entry, getValueString(value), variablesMap, 1, variableModelList);
}
}


private static void convertListVar(Object valuesForState, Map.Entry<String, Object> collectedVariable, VariablesMap variablesMap, List<VariableModel> dstSurveyUnitModel) {
List<String> values = JsonUtils.asStringList(valuesForState);
if (!values.isEmpty()) {
Expand Down Expand Up @@ -519,4 +531,72 @@ 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 handlePairwiseCollectedVariable(
Map.Entry<String, Object> collectedVariable,
DataState dataState,
VariablesMap variablesMap,
SurveyUnitModel dstSurveyUnitModel
) {
Object value = getValueForState(collectedVariable, dataState.toString());

if (isInvalidPairwiseVariable(value, variablesMap)) {
return;
}

List<?> individuals = (List<?>) value;

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

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

for (int linkIndex = 1; linkIndex <= Constants.MAX_LINKS_ALLOWED; linkIndex++) {
dstSurveyUnitModel.getCollectedVariables().add(
buildPairwiseVariable(individualLinks, linkIndex, individualIndex, groupName)
);
}
}
}

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

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

return VariableModel.builder()
.varId(Constants.PAIRWISE_PREFIX + linkIndex)
.value(value)
.scope(groupName)
.iteration(iteration)
.parentId(Constants.ROOT_GROUP_NAME)
.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;
}

private static boolean isInvalidPairwiseVariable(Object value, VariablesMap variablesMap) {
return !(value instanceof List<?>) || !variablesMap.hasVariable(Constants.PAIRWISE_PREFIX + 1);
}

}
Loading