Skip to content

Commit

Permalink
fix: subsequence pagination in regrouping treatment (#1095)
Browse files Browse the repository at this point in the history
* fix: subsequence pagination in regrouping treatment

* test: add unit test for regrouping pagination
  • Loading branch information
nsenave authored Aug 13, 2024
1 parent d112d7b commit 4e7f570
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ tasks.withType(JavaCompile).configureEach {

allprojects {
group = 'fr.insee.eno'
version = '3.24.1'
version = '3.24.2'
}

subprojects {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public boolean canIncrementPageCount(ComponentType component, boolean isParentPa
// if component is a subsequence and has no declarations set, it will regroup with next component, so no
// increment in this specific case
return !component.getComponentType().equals(ComponentTypeEnum.SUBSEQUENCE) ||
(component.getDeclarations() != null && !component.getDeclarations().isEmpty());
hasDeclarationOrDescription(component);
}

@Override
Expand All @@ -40,25 +40,41 @@ public void applyLoopPaginationProperty(Loop loop) {

@Override
public void applyNumPageOnSubsequence(Subsequence subsequence, String numPagePrefix, int pageCount, boolean isParentPaginated) {
// Clear page attributes in case of previous pagination
subsequence.setPage(null);
subsequence.setGoToPage(null);

String numPage = numPagePrefix + pageCount;
// special case where a subsequence has no declarations (so no page attribute set) and must link to next component
if(isParentPaginated && (subsequence.getDeclarations() == null || subsequence.getDeclarations().isEmpty())) {
if (isParentPaginated && !hasDeclarationOrDescription(subsequence)) {
int pageSequence = pageCount + 1;
numPage = numPagePrefix + pageSequence;
}

// if parent paginated or empty declarations
if (subsequence.getDeclarations() == null || subsequence.getDeclarations().isEmpty() || isParentPaginated) {
if (isParentPaginated || !hasDeclarationOrDescription(subsequence)) {
subsequence.setGoToPage(numPage);
}

// if parent not paginated and declarations set
if(!isParentPaginated || (subsequence.getDeclarations() != null && !subsequence.getDeclarations().isEmpty())) {
if (!isParentPaginated || hasDeclarationOrDescription(subsequence)) {
subsequence.setPage(numPage);
}
}

/**
* Checks if the given component has a declaration (one or more) or a description.
* @param component Lunatic component.
* @return True if the component has a declaration or a description.
*/
private static boolean hasDeclarationOrDescription(ComponentType component) {
if (component.getDescription() != null)
return true;
if (component.getDeclarations() != null)
return !component.getDeclarations().isEmpty();
return false;
}

/**
* is Loop linked
* @param loop to check
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public LunaticPaginationRegrouping(Regroupements regroupements) {
*/
@Override
public boolean canIncrementPageCount(ComponentType component, boolean isParentPaginated) {
if(component instanceof Question question)
return canIncrementPageCount(question.getComponents().getFirst(), isParentPaginated);

if(!super.canIncrementPageCount(component, isParentPaginated)) {
return false;
}
Expand All @@ -42,9 +45,6 @@ public boolean canIncrementPageCount(ComponentType component, boolean isParentPa
responseName = simpleResponse.getResponse().getName();
}

if(component instanceof Question question)
return canIncrementPageCount(question.getComponents().getFirst(), isParentPaginated);

// no response name, so no regroupement, we can increment
if(responseName == null) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import static org.junit.jupiter.api.Assertions.*;

class LunaticPaginationRegroupingTest {
class LunaticPaginationRegroupingIntegrationTest {

private Questionnaire questionnaire;
private Sequence s1, s4, s8, s9;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package fr.insee.eno.treatments;

import fr.insee.eno.treatments.dto.Regroupements;
import fr.insee.lunatic.model.flat.*;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;

import static org.junit.jupiter.api.Assertions.*;

class LunaticPaginationRegroupingUnitTest {

@Test
void subsequence_noDescription() {
//
Questionnaire questionnaire = new Questionnaire();
Sequence sequence = new Sequence();
sequence.setComponentType(ComponentTypeEnum.SEQUENCE);
Subsequence subsequence = new Subsequence();
subsequence.setComponentType(ComponentTypeEnum.SUBSEQUENCE);
Question question = new Question();
Input input = new Input();
input.setResponse(new ResponseType());
input.getResponse().setName("INPUT_VAR");
question.getComponents().add(input);
questionnaire.getComponents().add(sequence);
questionnaire.getComponents().add(subsequence);
questionnaire.getComponents().add(question);
//
new LunaticPaginationRegrouping(new Regroupements(new ArrayList<>())).apply(questionnaire);
//
assertEquals("1", questionnaire.getComponents().get(0).getPage());
assertNull(questionnaire.getComponents().get(1).getPage());
assertEquals("2", ((Subsequence) questionnaire.getComponents().get(1)).getGoToPage());
assertEquals("2", questionnaire.getComponents().get(2).getPage());
}

@Test
void subsequence_withDescription() {
//
Questionnaire questionnaire = new Questionnaire();
Sequence sequence = new Sequence();
sequence.setComponentType(ComponentTypeEnum.SEQUENCE);
Subsequence subsequence = new Subsequence();
subsequence.setComponentType(ComponentTypeEnum.SUBSEQUENCE);
subsequence.setDescription(new LabelType());
Question question = new Question();
Input input = new Input();
input.setResponse(new ResponseType());
input.getResponse().setName("INPUT_VAR");
question.getComponents().add(input);
questionnaire.getComponents().add(sequence);
questionnaire.getComponents().add(subsequence);
questionnaire.getComponents().add(question);
//
new LunaticPaginationRegrouping(new Regroupements(new ArrayList<>())).apply(questionnaire);
//
assertEquals("1", questionnaire.getComponents().get(0).getPage());
assertEquals("2", questionnaire.getComponents().get(1).getPage());
assertEquals("2", ((Subsequence) questionnaire.getComponents().get(1)).getGoToPage());
assertEquals("3", questionnaire.getComponents().get(2).getPage());
}

}

0 comments on commit 4e7f570

Please sign in to comment.