Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/manage same code lists #161

Merged
merged 12 commits into from
Apr 27, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package fr.insee.pogues.exception;

/**
* Exception thrown if an error occurs during questionnaire de-referencing (composition feature).
*/
public class DeReferencingException extends Exception {

public DeReferencingException(String message, Exception e) {
super(message, e);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public class VariablesServiceImpl implements VariablesService {
@Autowired
private QuestionnairesServiceQuery questionnaireServiceQuery;

public VariablesServiceImpl() {}

public VariablesServiceImpl(QuestionnairesServiceQuery questionnairesServiceQuery) {
this.questionnaireServiceQuery = questionnairesServiceQuery;
}

public JSONArray getVariablesByQuestionnaireForPublicEnemy(String id){
try {
JSONObject questionnaire = questionnaireServiceQuery.getQuestionnaireByID(id);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package fr.insee.pogues.transforms.visualize.composition;

import fr.insee.pogues.exception.DeReferencingException;
import fr.insee.pogues.model.Questionnaire;

/**
* Interface for processing step when de-referencing a questionnaire.
*/
public interface CompositionStep {

/**
* Update questionnaire content with referenced questionnaire given.
* @param questionnaire Referencing questionnaire.
* @param referencedQuestionnaire Referenced questionnaire.
* @throws DeReferencingException if an error occurs during the de-referencing step.
*/
void apply(Questionnaire questionnaire, Questionnaire referencedQuestionnaire) throws DeReferencingException;

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package fr.insee.pogues.transforms.visualize.composition;

import fr.insee.pogues.model.CodeLists;
import fr.insee.pogues.model.Questionnaire;
import lombok.extern.slf4j.Slf4j;

import java.util.HashSet;
import java.util.Set;

/**
* Implementation of CompositionStep to insert code lists of a referenced questionnaire.
*/
@Slf4j
class InsertCodeLists implements CompositionStep {

/** Host questionnaire. */
private Questionnaire questionnaire;
/** Host questionnaire code list names. */
private final Set<String> codeListNames = new HashSet<>();

/**
* Insert code lists of the referenced questionnaire in the referencing questionnaire.
* If a code list of the referenced questionnaire has the same name asa list in the referencing questionnaire,
* the code list is not added.
* @param questionnaire Referencing questionnaire.
* @param referencedQuestionnaire Referenced questionnaire.
*/
@Override
public void apply(Questionnaire questionnaire, Questionnaire referencedQuestionnaire) {
//
this.questionnaire = questionnaire;
//
CodeLists refCodeLists = referencedQuestionnaire.getCodeLists();
if (refCodeLists != null) {
//
hostCodeLists();
//
if (questionnaire.getCodeLists() == null)
questionnaire.setCodeLists(new CodeLists());
//
refCodeLists.getCodeList().forEach(codeList -> {
if (! codeListNames.contains(codeList.getName()))
questionnaire.getCodeLists().getCodeList().add(codeList);
else
log.info("Code list with name '{}' is already in host questionnaire '{}', " +
"so it has not been inserted from reference '{}'",
codeList.getName(), questionnaire.getId(), referencedQuestionnaire.getId());
});
log.info("Code lists from '{}' inserted in '{}'", referencedQuestionnaire.getId(), questionnaire.getId());
} else {
log.info("No code lists in referenced questionnaire '{}'", referencedQuestionnaire.getId());
}
}

private void hostCodeLists() {
if (questionnaire.getCodeLists() != null)
questionnaire.getCodeLists().getCodeList().forEach(codeList -> codeListNames.add(codeList.getName()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package fr.insee.pogues.transforms.visualize.composition;

import fr.insee.pogues.model.Questionnaire;
import lombok.extern.slf4j.Slf4j;

/**
* Implementation of CompositionStep to insert flow controls of a referenced questionnaire.
*/
@Slf4j
class InsertFlowControls implements CompositionStep {

/**
* Insert flow controls of the referenced questionnaire in the referencing questionnaire.
* @param questionnaire Referencing questionnaire.
* @param referencedQuestionnaire Referenced questionnaire.
*/
@Override
public void apply(Questionnaire questionnaire, Questionnaire referencedQuestionnaire) {
questionnaire.getFlowControl().addAll(referencedQuestionnaire.getFlowControl());
log.info("FlowControl from '{}' inserted in '{}'", referencedQuestionnaire.getId(), questionnaire.getId());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package fr.insee.pogues.transforms.visualize.composition;

import fr.insee.pogues.model.Questionnaire;
import lombok.extern.slf4j.Slf4j;

/**
* Implementation of CompositionStep to insert iterations of a referenced questionnaire.
*/
@Slf4j
class InsertIterations implements CompositionStep {

/**
* Insert iterations of the referenced questionnaire in the referencing questionnaire.
* @param questionnaire Referencing questionnaire.
* @param referencedQuestionnaire Referenced questionnaire.
*/
@Override
public void apply(Questionnaire questionnaire, Questionnaire referencedQuestionnaire) {
Questionnaire.Iterations refIterations = referencedQuestionnaire.getIterations();
if (refIterations != null) {
if (questionnaire.getIterations() == null)
questionnaire.setIterations(new Questionnaire.Iterations());
questionnaire.getIterations().getIteration().addAll(refIterations.getIteration());
log.info("Iterations from '{}' inserted in '{}'", referencedQuestionnaire.getId(), questionnaire.getId());
} else {
log.info("No iterations in referenced questionnaire '{}'", referencedQuestionnaire.getId());
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.insee.pogues.transforms.visualize.composition;

import fr.insee.pogues.exception.DeReferencingException;
import fr.insee.pogues.model.ComponentType;
import fr.insee.pogues.model.Questionnaire;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -9,19 +10,18 @@
import static fr.insee.pogues.utils.PoguesModelUtils.getSequences;

/**
* Methods to insert and update sequence objects when de-referencing a questionnaire.
* Implementation of CompositionStep to replace questionnaire reference by its sequences.
*/
@Slf4j
class SequenceComposition {

private SequenceComposition() {}
class InsertSequences implements CompositionStep {

/**
* Replace questionnaire reference by its sequences.
* @param questionnaire Referencing questionnaire.
* @param referencedQuestionnaire Referenced questionnaire.
*/
static void insertSequences(Questionnaire questionnaire, Questionnaire referencedQuestionnaire) {
@Override
public void apply(Questionnaire questionnaire, Questionnaire referencedQuestionnaire) {
//
List<ComponentType> refSequences = getSequences(referencedQuestionnaire);
int indexOfModification = 0;
Expand All @@ -38,6 +38,8 @@ static void insertSequences(Questionnaire questionnaire, Questionnaire reference
for (int i=0; i<refSequences.size();i++) {
questionnaire.getChild().add(indexOfModification, referencedQuestionnaire.getChild().get(refSequences.size()-1-i));
}
//
log.info("Sequences from '{}' inserted in '{}'", referencedQuestionnaire.getId(), questionnaire.getId());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package fr.insee.pogues.transforms.visualize.composition;

import fr.insee.pogues.exception.DeReferencingException;
import fr.insee.pogues.model.Questionnaire;
import lombok.extern.slf4j.Slf4j;

/**
* Implementation of CompositionStep to insert variables of a referenced questionnaire.
*/
@Slf4j
class InsertVariables implements CompositionStep {

/**
* Insert variables of the referenced questionnaire in the referencing questionnaire.
* @param questionnaire Referencing questionnaire.
* @param referencedQuestionnaire Referenced questionnaire.
*/
@Override
public void apply(Questionnaire questionnaire, Questionnaire referencedQuestionnaire) {
questionnaire.getVariables().getVariable().addAll(referencedQuestionnaire.getVariables().getVariable());
log.info("Variables from '{}' inserted in '{}'", referencedQuestionnaire.getId(), questionnaire.getId());
}

}
Loading