Skip to content

Commit e2782e3

Browse files
authored
4.25.0 (#398)
1 parent 4db7a4e commit e2782e3

File tree

10 files changed

+310
-99
lines changed

10 files changed

+310
-99
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55
<parent>
66
<groupId>org.springframework.boot</groupId>
77
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>3.5.7</version>
8+
<version>3.5.8</version>
99
<relativePath/>
1010
</parent>
1111

1212
<groupId>fr.insee</groupId>
1313
<artifactId>Pogues-BO</artifactId>
1414
<packaging>jar</packaging>
15-
<version>4.24.0</version>
15+
<version>4.25.0</version>
1616
<name>Pogues-Back-Office</name>
1717

1818
<properties>
1919
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2020
<java.version>21</java.version>
2121
<final.asset.name>pogues-bo</final.asset.name>
22-
<pogues-model.version>1.12.2</pogues-model.version>
22+
<pogues-model.version>1.13.0</pogues-model.version>
2323
<fop.version>2.11</fop.version>
2424
<springdoc-openapi-ui.version>2.8.14</springdoc-openapi-ui.version>
2525
<jacoco.version>0.8.14</jacoco.version>

src/main/java/fr/insee/pogues/mapper/VariablesMapper.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public static VariableType toModel(VariableDTO variableDTO) throws VariableInval
4141

4242
setDatatype(variable, variableDTO.getDatatype());
4343

44+
if (variable instanceof ExternalVariableType externalVariable)
45+
externalVariable.setDeletedOnReset(variableDTO.getIsDeletedOnReset());
46+
4447
return variable;
4548
}
4649

@@ -149,13 +152,13 @@ public static VariableDTO toDTO(VariableType variable) throws VariableInvalidMod
149152

150153
switch (variable) {
151154
case CollectedVariableType v -> {
152-
return new VariableDTO(v.getId(), v.getName(), v.getLabel(), VariableDTOTypeEnum.COLLECTED, v.getScope(), null, datatypeDTO);
155+
return new VariableDTO(v.getId(), v.getName(), v.getLabel(), VariableDTOTypeEnum.COLLECTED, v.getScope(), null, datatypeDTO, null);
153156
}
154157
case ExternalVariableType v -> {
155-
return new VariableDTO(v.getId(), v.getName(), v.getLabel(), VariableDTOTypeEnum.EXTERNAL, v.getScope(), null, datatypeDTO);
158+
return new VariableDTO(v.getId(), v.getName(), v.getLabel(), VariableDTOTypeEnum.EXTERNAL, v.getScope(), null, datatypeDTO, v.isDeletedOnReset());
156159
}
157160
case CalculatedVariableType v -> {
158-
return new VariableDTO(v.getId(), v.getName(), v.getLabel(), VariableDTOTypeEnum.CALCULATED, v.getScope(), v.getFormula().getValue(), datatypeDTO);
161+
return new VariableDTO(v.getId(), v.getName(), v.getLabel(), VariableDTOTypeEnum.CALCULATED, v.getScope(), v.getFormula().getValue(), datatypeDTO, null);
159162
}
160163
default -> throw new VariableInvalidModelException(String.format("Invalid variable type %s", variable.getClass()), variable.toString());
161164
}

src/main/java/fr/insee/pogues/model/dto/variables/VariableDTO.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ public class VariableDTO {
2424
private String scope;
2525
private String formula;
2626
private VariableDTODatatype datatype;
27+
private Boolean isDeletedOnReset;
2728

2829
}

src/main/java/fr/insee/pogues/service/VariableService.java

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,17 @@
99
import fr.insee.pogues.utils.DateUtils;
1010
import fr.insee.pogues.utils.PoguesDeserializer;
1111
import fr.insee.pogues.utils.PoguesSerializer;
12-
import fr.insee.pogues.utils.model.PoguesModelUtils;
1312
import lombok.extern.slf4j.Slf4j;
1413
import org.springframework.stereotype.Service;
15-
import org.springframework.web.client.RestTemplate;
1614

17-
import java.net.http.HttpClient;
1815
import java.time.Instant;
1916
import java.util.List;
2017
import java.util.Objects;
21-
import java.util.Optional;
2218
import java.util.UUID;
2319

2420
import static fr.insee.pogues.utils.ListUtils.replaceElementInListAccordingToCondition;
2521
import static fr.insee.pogues.utils.json.JSONFunctions.jsonStringtoJsonNode;
22+
import static fr.insee.pogues.utils.model.PoguesModelUtils.getScopeNameFromID;
2623

2724
/**
2825
* Variable Service used to fetch or update variables in questionnaires.
@@ -73,34 +70,30 @@ public List<VariableType> getVersionVariables(UUID versionId) throws Exception {
7370
* Get the questionnaire's variables and, if they have a scope, compute the scope name instead of the id.
7471
* @param questionnaire Questionnaire from which we want the variables
7572
* @return Questionnaire's variables with a readable scope.
73+
* @throws IllegalIterationException
7674
*/
7775
private List<VariableType> getQuestionnaireVariables(Questionnaire questionnaire) {
7876
List<VariableType> variables = questionnaire.getVariables().getVariable().stream().toList();
79-
variables.forEach(v -> computeScopeNameFromScopeId(v, questionnaire.getIterations()));
77+
for (VariableType variable : variables) {
78+
computeScopeNameFromScopeId(variable, questionnaire);
79+
}
8080
return variables;
8181
}
8282

8383
/**
84-
* Compute the scope name instead of the id. If no related scope is found, do nothing.
84+
* <p>Compute the scope name instead of the id. If no related scope is found, do nothing.</p>
85+
* <p>The scope name is from either an iteration or a question.</p>
8586
* @param variable Variable to update
86-
* @param iterations Iterations in which we will find the scope name
87+
* @param questionnaire Questionnaire in which we will find the scope name
8788
*/
88-
private void computeScopeNameFromScopeId(VariableType variable, Questionnaire.Iterations iterations) {
89-
if (iterations == null) return;
90-
89+
private void computeScopeNameFromScopeId(VariableType variable, Questionnaire questionnaire) {
9190
String scopeId = variable.getScope();
9291
if (scopeId == null) return;
9392

94-
Optional<IterationType> iteration = iterations.getIteration().stream().filter(v -> {
95-
try {
96-
return PoguesModelUtils.isIterationRelatedToScopeId(v, scopeId);
97-
} catch (IllegalIterationException e) {
98-
return false;
99-
}
100-
}).findFirst();
101-
if (iteration.isPresent()) {
102-
variable.setScope(iteration.get().getName());
103-
}
93+
String scopeName = getScopeNameFromID(questionnaire, scopeId);
94+
if (scopeName == null) return;
95+
96+
variable.setScope(scopeName);
10497
}
10598

10699
/**

src/main/java/fr/insee/pogues/utils/model/PoguesModelUtils.java

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import fr.insee.pogues.exception.IllegalFlowControlException;
44
import fr.insee.pogues.exception.IllegalIterationException;
55
import fr.insee.pogues.model.*;
6+
import fr.insee.pogues.model.Questionnaire.Iterations;
67
import lombok.extern.slf4j.Slf4j;
78

89
import java.util.List;
@@ -121,17 +122,55 @@ public static String getLinkedLoopReference(IterationType iterationType) throws
121122
}
122123

123124
/**
124-
* Check if the provided iteration is associated to the scope id
125-
* (which can be found in variable for example).
126-
* If the iteration is a "main" loop, we directly check the id.
127-
* If it is a linked loop, we check the identifier of the corresponding "main" loop.
128-
* @param iterationType A Pogues iteration (loop) object.
129-
* @param scopeId The id of the scope.
130-
* @return Whether the iteration is the one referenced by the scope id.
131-
* @throws IllegalIterationException If the iteration object given is not a DynamicIterationType.
125+
* The scope name of the given id.
126+
* The scope is either the name of a "main" loop, or the name of a question.
127+
* @param id The scope id.
128+
* @return The scope name of the given id.
132129
*/
133-
public static boolean isIterationRelatedToScopeId(IterationType iterationType, String scopeId) throws IllegalIterationException {
134-
return scopeId.equals(iterationType.getId()) || scopeId.equals(getLinkedLoopReference(iterationType));
130+
public static String getScopeNameFromID(Questionnaire questionnaire, String id) {
131+
// Case 1: get the associated scope from loops
132+
Iterations iterations = questionnaire.getIterations();
133+
if (iterations != null) {
134+
Optional<IterationType> loopIterable = questionnaire.getIterations().getIteration().stream().filter(v ->
135+
id.equals(v.getId())
136+
).findFirst();
137+
if (loopIterable.isPresent()) {
138+
return loopIterable.get().getName();
139+
}
140+
}
141+
142+
// Case 2: get the associated scope from questions
143+
Optional<QuestionType> questionIterable = getQuestionByID(questionnaire.getChild(), id);
144+
if (questionIterable.isPresent()) {
145+
return questionIterable.get().getName();
146+
}
147+
148+
return null;
149+
}
150+
151+
/**
152+
* Find the question associated to the id from a list of components.
153+
* @param components A list of components (questions and sequences).
154+
* @param id ID of the question we want to find.
155+
* @return The question associated to the id, or null.
156+
*/
157+
public static Optional<QuestionType> getQuestionByID(List<ComponentType> components, String id) {
158+
Optional<QuestionType> question = components.stream()
159+
.filter(QuestionType.class::isInstance)
160+
.map(QuestionType.class::cast)
161+
.filter(q -> id.equals(q.getId())).findFirst();
162+
if (question.isPresent()) return question;
163+
164+
// Look into (sub)sequences if question was not found
165+
List<SequenceType> sequences = components.stream().filter(SequenceType.class::isInstance).map(SequenceType.class::cast).toList();
166+
for (SequenceType sequence : sequences) {
167+
Optional<QuestionType> questionFromSequence = getQuestionByID(sequence.getChild(), id);
168+
if (questionFromSequence.isPresent()) {
169+
return questionFromSequence;
170+
}
171+
}
172+
173+
return Optional.empty();
135174
}
136175

137176
/**
@@ -143,7 +182,7 @@ public static boolean isIterationRelatedToScopeId(IterationType iterationType, S
143182
* @throws IllegalIterationException If the iteration object given is not a DynamicIterationType.
144183
*/
145184
private static void checkIterationInstance(IterationType iterationType) throws IllegalIterationException {
146-
if (! (iterationType instanceof DynamicIterationType)) // (
185+
if (! (iterationType instanceof DynamicIterationType))
147186
throw new IllegalIterationException(String.format(
148187
"Pogues iteration with id=%s and name=%s is not is of type %s. " +
149188
"Only DynamicIterationType is supported.",

src/test/java/fr/insee/pogues/controller/VariableControllerTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
import fr.insee.pogues.exception.PoguesException;
55
import fr.insee.pogues.exception.VariableNotFoundException;
66
import fr.insee.pogues.model.*;
7-
import fr.insee.pogues.service.VariableService;
87
import fr.insee.pogues.model.dto.variables.VariableDTO;
98
import fr.insee.pogues.model.dto.variables.VariableDTODatatype;
109
import fr.insee.pogues.model.dto.variables.VariableDTODatatypeTypeEnum;
1110
import fr.insee.pogues.model.dto.variables.VariableDTOTypeEnum;
11+
import fr.insee.pogues.service.VariableService;
1212
import org.junit.jupiter.api.DisplayName;
1313
import org.junit.jupiter.api.Test;
1414
import org.mockito.Mockito;
@@ -23,10 +23,12 @@
2323
import java.util.Objects;
2424
import java.util.UUID;
2525

26-
import static org.mockito.ArgumentMatchers.*;
26+
import static org.mockito.ArgumentMatchers.argThat;
27+
import static org.mockito.ArgumentMatchers.eq;
2728
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt;
2829
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
29-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
31+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3032

3133
@WebMvcTest(VariableController.class)
3234
class VariableControllerTest {
@@ -139,7 +141,7 @@ void getQuestionnaireVersionVariables_error_notFound() throws Exception {
139141
void upsertQuestionnaireVariable_success_created() throws Exception {
140142
// Given a variable
141143
VariableDTODatatype datatypeDTO = new VariableDTODatatype(VariableDTODatatypeTypeEnum.BOOLEAN, null, null, null, null, null, null, null);
142-
VariableDTO variable = new VariableDTO("id", "name", "description", VariableDTOTypeEnum.COLLECTED, null, null, datatypeDTO);
144+
VariableDTO variable = new VariableDTO("id", "name", "description", VariableDTOTypeEnum.COLLECTED, null, null, datatypeDTO, null);
143145
ObjectMapper objectMapper = new ObjectMapper();
144146
String expectedJSON = objectMapper.writeValueAsString(variable);
145147
Mockito.when(variableService.upsertQuestionnaireVariable(eq("my-q-id"), argThat(arg -> Objects.equals(arg.getId(), variable.getId()))))
@@ -160,7 +162,7 @@ void upsertQuestionnaireVariable_success_created() throws Exception {
160162
void upsertQuestionnaireVariable_success_updated() throws Exception {
161163
// Given a questionnaire with a variable
162164
VariableDTODatatype datatypeDTO = new VariableDTODatatype(VariableDTODatatypeTypeEnum.BOOLEAN, null, null, null, null, null, null, null);
163-
VariableDTO variable = new VariableDTO("id", "name", "description", VariableDTOTypeEnum.COLLECTED, null, null, datatypeDTO);
165+
VariableDTO variable = new VariableDTO("id", "name", "description", VariableDTOTypeEnum.COLLECTED, null, null, datatypeDTO, null);
164166
ObjectMapper objectMapper = new ObjectMapper();
165167
String expectedJSON = objectMapper.writeValueAsString(variable);
166168
Mockito.when(variableService.upsertQuestionnaireVariable(eq("my-q-id"), argThat(arg -> Objects.equals(arg.getId(), variable.getId()))))
@@ -181,7 +183,7 @@ void upsertQuestionnaireVariable_success_updated() throws Exception {
181183
void upsertQuestionnaireVariable_error_notFound() throws Exception {
182184
// Given a variable
183185
VariableDTODatatype datatypeDTO = new VariableDTODatatype(VariableDTODatatypeTypeEnum.BOOLEAN, null, null, null, null, null, null, null);
184-
VariableDTO variable = new VariableDTO("id", "name", "description", VariableDTOTypeEnum.COLLECTED, null, null, datatypeDTO);
186+
VariableDTO variable = new VariableDTO("id", "name", "description", VariableDTOTypeEnum.COLLECTED, null, null, datatypeDTO, null);
185187
ObjectMapper objectMapper = new ObjectMapper();
186188
String expectedJSON = objectMapper.writeValueAsString(variable);
187189
Mockito.when(variableService.upsertQuestionnaireVariable(eq("my-q-id"), argThat(arg -> Objects.equals(arg.getId(), variable.getId()))))

0 commit comments

Comments
 (0)