From df7033934864208ee13170091e5f49b8c876a675 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20S=C3=A9nave?= <59770457+nsenave@users.noreply.github.com> Date: Wed, 18 Dec 2024 16:00:21 +0100 Subject: [PATCH] refactor(dynamic unit): code simplifications (#1178) --- .../core/model/question/NumericQuestion.java | 17 +++++++++++++ .../steps/EnoAddResponseTimeSection.java | 2 +- .../in/steps/ddi/DDIMoveUnitInQuestions.java | 12 ++------- .../steps/lunatic/LunaticEditLabelTypes.java | 25 ------------------- 4 files changed, 20 insertions(+), 36 deletions(-) diff --git a/eno-core/src/main/java/fr/insee/eno/core/model/question/NumericQuestion.java b/eno-core/src/main/java/fr/insee/eno/core/model/question/NumericQuestion.java index 85950628f..f9c369449 100644 --- a/eno-core/src/main/java/fr/insee/eno/core/model/question/NumericQuestion.java +++ b/eno-core/src/main/java/fr/insee/eno/core/model/question/NumericQuestion.java @@ -7,6 +7,7 @@ import fr.insee.eno.core.model.label.DynamicLabel; import fr.insee.eno.core.parameter.Format; import fr.insee.lunatic.model.flat.InputNumber; +import fr.insee.lunatic.model.flat.LabelTypeEnum; import lombok.Getter; import lombok.Setter; @@ -60,4 +61,20 @@ public class NumericQuestion extends SingleResponseQuestion { @Lunatic("setComponentType(T(fr.insee.lunatic.model.flat.ComponentTypeEnum).valueOf(#param))") String lunaticComponentType = "INPUT_NUMBER"; + /** + * Creates a dynamic label object that fits the 'unit' property of numeric questions with the label value given. + * A number question doesn't necessarily have a unit, so the result can be null if the value given is null. + * In Lunatic, unit labels must be of type 'VTL', so the type of the returned dynamic label is set to this value. + * @param value Label value. Can be null. + * @return Dynamic label object with given value. null if the given value is null. + */ + public static DynamicLabel createUnit(String value) { + if (value == null) + return null; + DynamicLabel label = new DynamicLabel(); + label.setValue(value); + label.setType(LabelTypeEnum.VTL.value()); + return label; + } + } diff --git a/eno-core/src/main/java/fr/insee/eno/core/processing/common/steps/EnoAddResponseTimeSection.java b/eno-core/src/main/java/fr/insee/eno/core/processing/common/steps/EnoAddResponseTimeSection.java index a5cc309e2..55cfa433a 100644 --- a/eno-core/src/main/java/fr/insee/eno/core/processing/common/steps/EnoAddResponseTimeSection.java +++ b/eno-core/src/main/java/fr/insee/eno/core/processing/common/steps/EnoAddResponseTimeSection.java @@ -14,7 +14,7 @@ import java.math.BigInteger; -import static fr.insee.eno.core.processing.in.steps.ddi.DDIMoveUnitInQuestions.createUnit; +import static fr.insee.eno.core.model.question.NumericQuestion.createUnit; public class EnoAddResponseTimeSection implements ProcessingStep { diff --git a/eno-core/src/main/java/fr/insee/eno/core/processing/in/steps/ddi/DDIMoveUnitInQuestions.java b/eno-core/src/main/java/fr/insee/eno/core/processing/in/steps/ddi/DDIMoveUnitInQuestions.java index d70d0e80d..c45487b93 100644 --- a/eno-core/src/main/java/fr/insee/eno/core/processing/in/steps/ddi/DDIMoveUnitInQuestions.java +++ b/eno-core/src/main/java/fr/insee/eno/core/processing/in/steps/ddi/DDIMoveUnitInQuestions.java @@ -1,7 +1,6 @@ package fr.insee.eno.core.processing.in.steps.ddi; import fr.insee.eno.core.model.EnoQuestionnaire; -import fr.insee.eno.core.model.label.DynamicLabel; import fr.insee.eno.core.model.navigation.Binding; import fr.insee.eno.core.model.question.*; import fr.insee.eno.core.model.question.table.NumericCell; @@ -15,6 +14,8 @@ import java.util.List; import java.util.Optional; +import static fr.insee.eno.core.model.question.NumericQuestion.createUnit; + /** Processing step to move units which are mapped in variable objects to numeric questions. */ @Slf4j public class DDIMoveUnitInQuestions implements ProcessingStep { @@ -92,13 +93,4 @@ private void applyUnitOnNumericCell(Variable variable, List bindings, L numericCell.get().setUnit(createUnit(variable.getUnit())); } - public static DynamicLabel createUnit(String value) { - if (value == null) - return null; - DynamicLabel label = new DynamicLabel(); - label.setValue(value); - // (type is left at its default value) - return label; - } - } diff --git a/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticEditLabelTypes.java b/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticEditLabelTypes.java index e7d4949c8..e7654a3b2 100644 --- a/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticEditLabelTypes.java +++ b/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticEditLabelTypes.java @@ -35,31 +35,6 @@ private void editLabels(List lunaticComponents) { lunaticComponents.stream() .filter(Sequence.class::isInstance).map(Sequence.class::cast) .forEach(this::editSequenceLabel); - lunaticComponents.stream() - .filter(InputNumber.class::isInstance).map(InputNumber.class::cast) - .forEach(this::editInputNumberUnit); - lunaticComponents.stream() - .filter(Table.class::isInstance).map(Table.class::cast) - .map(Table::getBodyLines) - .forEach(bodyLines -> bodyLines.stream() - .map(BodyLine::getBodyCells) - .forEach(this::editCellUnits)); - lunaticComponents.stream() - .filter(RosterForLoop.class::isInstance).map(RosterForLoop.class::cast) - .map(RosterForLoop::getComponents) - .forEach(this::editCellUnits); - } - - private void editCellUnits(List bodyCells) { - bodyCells.stream() - .filter(bodyCell -> bodyCell.getUnitLabel() != null) - .forEach(bodyCell -> bodyCell.getUnitLabel().setType(LabelTypeEnum.VTL)); - } - - private void editInputNumberUnit(InputNumber inputNumber) { - if (inputNumber.getUnitLabel() == null) - return; - inputNumber.getUnitLabel().setType(LabelTypeEnum.VTL); } private void editSequenceLabel(Sequence sequence) {