Skip to content

Commit

Permalink
refactor(dynamic unit): code simplifications (#1178)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsenave authored Dec 18, 2024
1 parent 3748591 commit df70339
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<EnoQuestionnaire> {

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<EnoQuestionnaire> {
Expand Down Expand Up @@ -92,13 +93,4 @@ private void applyUnitOnNumericCell(Variable variable, List<Binding> 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,6 @@ private void editLabels(List<ComponentType> 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<BodyCell> 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) {
Expand Down

0 comments on commit df70339

Please sign in to comment.