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

V3 next #1185

Merged
merged 13 commits into from
Jan 15, 2025
Merged

V3 next #1185

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ java {

allprojects {
group = "fr.insee.eno"
version = "3.31.5"
version = "3.32.1"
}

subprojects {
Expand Down
7 changes: 2 additions & 5 deletions eno-core/src/main/java/fr/insee/eno/core/DDIToEno.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@

import java.io.InputStream;

public class DDIToEno {

private DDIToEno() {}
public class DDIToEno implements InToEno {

/**
* Transform given DDI input stream into a Eno questionnaire object using parameters given.
Expand All @@ -22,7 +20,7 @@ private DDIToEno() {}
* @return Lunatic questionnaire object.
* @throws DDIParsingException if the input stream given cannot be parsed to a DDI object.
*/
public static EnoQuestionnaire transform(InputStream ddiInputStream, EnoParameters enoParameters)
public EnoQuestionnaire transform(InputStream ddiInputStream, EnoParameters enoParameters)
throws DDIParsingException {
//
DDIInstanceDocument ddiInstanceDocument = DDIDeserializer.deserialize(ddiInputStream);
Expand All @@ -39,5 +37,4 @@ public static EnoQuestionnaire transform(InputStream ddiInputStream, EnoParamete
//
return enoQuestionnaire;
}

}
13 changes: 4 additions & 9 deletions eno-core/src/main/java/fr/insee/eno/core/DDIToLunatic.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@

import java.io.InputStream;

public class DDIToLunatic {

private DDIToLunatic() {
throw new UnsupportedOperationException("Utility class");
}
public class DDIToLunatic implements InToOut<Questionnaire> {

/**
* Transform given DDI input stream into a Lunatic questionnaire object using parameters given.
Expand All @@ -20,12 +16,11 @@ private DDIToLunatic() {
* @return Lunatic questionnaire object.
* @throws DDIParsingException if the input stream given cannot be parsed to a DDI object.
*/
public static Questionnaire transform(InputStream ddiInputStream, EnoParameters enoParameters)
public Questionnaire transform(InputStream ddiInputStream, EnoParameters enoParameters)
throws DDIParsingException {
//
EnoQuestionnaire enoQuestionnaire = DDIToEno.transform(ddiInputStream, enoParameters);
EnoQuestionnaire enoQuestionnaire = new DDIToEno().transform(ddiInputStream, enoParameters);
//
return EnoToLunatic.transform(enoQuestionnaire, enoParameters);
return new EnoToLunatic().transform(enoQuestionnaire, enoParameters);
}

}
7 changes: 2 additions & 5 deletions eno-core/src/main/java/fr/insee/eno/core/EnoToLunatic.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@
import fr.insee.eno.core.processing.out.LunaticProcessing;
import fr.insee.lunatic.model.flat.Questionnaire;

public class EnoToLunatic {

private EnoToLunatic() {}
public class EnoToLunatic implements EnoToOut<Questionnaire> {

/**
* Transform given Eno questionnaire into a Lunatic questionnaire object using parameters given.
* @param enoQuestionnaire An Eno Questionnaire object.
* @param enoParameters Eno parameters object.
* @return Lunatic questionnaire object.
*/
public static Questionnaire transform(EnoQuestionnaire enoQuestionnaire, EnoParameters enoParameters) {
public Questionnaire transform(EnoQuestionnaire enoQuestionnaire, EnoParameters enoParameters) {
//
LunaticMapper lunaticMapper = new LunaticMapper();
Questionnaire lunaticQuestionnaire = new Questionnaire();
Expand All @@ -27,5 +25,4 @@ public static Questionnaire transform(EnoQuestionnaire enoQuestionnaire, EnoPara
//
return lunaticQuestionnaire;
}

}
10 changes: 10 additions & 0 deletions eno-core/src/main/java/fr/insee/eno/core/EnoToOut.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package fr.insee.eno.core;

import fr.insee.eno.core.model.EnoQuestionnaire;
import fr.insee.eno.core.parameter.EnoParameters;

public interface EnoToOut<T> {

T transform(EnoQuestionnaire enoQuestionnaire, EnoParameters enoParameters);

}
13 changes: 13 additions & 0 deletions eno-core/src/main/java/fr/insee/eno/core/InToEno.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package fr.insee.eno.core;

import fr.insee.eno.core.exceptions.business.ParsingException;
import fr.insee.eno.core.model.EnoQuestionnaire;
import fr.insee.eno.core.parameter.EnoParameters;

import java.io.InputStream;

public interface InToEno {

EnoQuestionnaire transform(InputStream inputStream, EnoParameters enoParameters) throws ParsingException;

}
12 changes: 12 additions & 0 deletions eno-core/src/main/java/fr/insee/eno/core/InToOut.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package fr.insee.eno.core;

import fr.insee.eno.core.exceptions.business.ParsingException;
import fr.insee.eno.core.parameter.EnoParameters;

import java.io.InputStream;

public interface InToOut<T> {

T transform(InputStream inputStream, EnoParameters enoParameters) throws ParsingException;

}
10 changes: 6 additions & 4 deletions eno-core/src/main/java/fr/insee/eno/core/PoguesToEno.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
import fr.insee.eno.core.model.EnoQuestionnaire;
import fr.insee.eno.core.parameter.EnoParameters;
import fr.insee.eno.core.processing.common.EnoProcessing;
import fr.insee.eno.core.processing.in.PoguesInProcessing;
import fr.insee.eno.core.serialize.PoguesDeserializer;
import fr.insee.pogues.model.Questionnaire;

import java.io.InputStream;

public class PoguesToEno {

private PoguesToEno() {}
public class PoguesToEno implements InToEno {

/**
* Transform given Pogues input stream into a Eno questionnaire object using parameters given.
Expand All @@ -21,7 +20,7 @@ private PoguesToEno() {}
* @return Lunatic questionnaire object.
* @throws PoguesDeserializationException if the input stream given cannot be parsed to a Pogues questionnaire.
*/
public static EnoQuestionnaire transform(InputStream poguesInputStream, EnoParameters enoParameters)
public EnoQuestionnaire transform(InputStream poguesInputStream, EnoParameters enoParameters)
throws PoguesDeserializationException {
//
Questionnaire poguesQuestionnaire = PoguesDeserializer.deserialize(poguesInputStream);
Expand All @@ -30,6 +29,9 @@ public static EnoQuestionnaire transform(InputStream poguesInputStream, EnoParam
EnoQuestionnaire enoQuestionnaire = new EnoQuestionnaire();
poguesMapper.mapPoguesQuestionnaire(poguesQuestionnaire, enoQuestionnaire);
//
PoguesInProcessing poguesInProcessing = new PoguesInProcessing();
poguesInProcessing.applyProcessing(enoQuestionnaire);
//
EnoProcessing enoProcessing = new EnoProcessing(enoParameters);
enoProcessing.applyProcessing(enoQuestionnaire);
//
Expand Down
9 changes: 3 additions & 6 deletions eno-core/src/main/java/fr/insee/eno/core/PoguesToLunatic.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,18 @@

public class PoguesToLunatic {

private PoguesToLunatic() {}

/**
* Transform given Pogues input stream into a Lunatic questionnaire object using parameters given.
* @param poguesInputStream Input stream of a Pogues json questionnaire.
* @param enoParameters Eno parameters object.
* @return Lunatic questionnaire object.
* @throws PoguesDeserializationException if the input stream given cannot be parsed to a Pogues questionnaire.
*/
public static Questionnaire transform(InputStream poguesInputStream, EnoParameters enoParameters)
public Questionnaire transform(InputStream poguesInputStream, EnoParameters enoParameters)
throws PoguesDeserializationException {
//
EnoQuestionnaire enoQuestionnaire = PoguesToEno.transform(poguesInputStream, enoParameters);
EnoQuestionnaire enoQuestionnaire = new PoguesToEno().transform(poguesInputStream, enoParameters);
//
return EnoToLunatic.transform(enoQuestionnaire, enoParameters);
return new EnoToLunatic().transform(enoQuestionnaire, enoParameters);
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
package fr.insee.eno.core.converter;

import fr.insee.eno.core.exceptions.technical.ConversionException;
import fr.insee.eno.core.model.EnoObject;
import fr.insee.eno.core.model.EnoQuestionnaire;
import fr.insee.pogues.model.QuestionType;
import fr.insee.pogues.model.ResponseType;
import fr.insee.pogues.model.VariableType;

public class PoguesConverter implements InConverter {

@Override
public EnoObject convertToEno(Object inputObject, Class<?> enoType) {
return null;
public EnoObject convertToEno(Object poguesObject, Class<?> enoType) {
if (poguesObject instanceof VariableType poguesVariable)
return PoguesVariableConversion.instantiateFrom(poguesVariable);
if (poguesObject instanceof QuestionType poguesQuestion
&& EnoQuestionnaire.isSingleResponseQuestion(poguesQuestion))
return PoguesSingleResponseQuestionConversion.instantiateFrom(poguesQuestion);
if (poguesObject instanceof QuestionType poguesQuestion
&& EnoQuestionnaire.isMultipleResponseQuestion(poguesQuestion))
return PoguesMultipleChoiceQuestionConverter.instantiateFrom(poguesQuestion);
if (poguesObject instanceof ResponseType poguesResponse)
return PoguesTableCellConversion.instantiateFrom(poguesResponse);
throw new ConversionException("Eno conversion for Pogues type " + poguesObject.getClass() + " not implemented.");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package fr.insee.eno.core.converter;

import fr.insee.eno.core.exceptions.technical.MappingException;
import fr.insee.eno.core.model.EnoObject;
import fr.insee.eno.core.model.question.ComplexMultipleChoiceQuestion;
import fr.insee.eno.core.model.question.DynamicTableQuestion;
import fr.insee.eno.core.model.question.SimpleMultipleChoiceQuestion;
import fr.insee.eno.core.model.question.TableQuestion;
import fr.insee.pogues.model.*;

class PoguesMultipleChoiceQuestionConverter {

private PoguesMultipleChoiceQuestionConverter() {}

static EnoObject instantiateFrom(QuestionType poguesQuestion) {
QuestionTypeEnum questionType = poguesQuestion.getQuestionType();
if (QuestionTypeEnum.MULTIPLE_CHOICE.equals(questionType))
return convertMultipleChoice(poguesQuestion);
if (QuestionTypeEnum.TABLE.equals(questionType))
return convertTable(poguesQuestion);
throw new MappingException("Unexpected multiple response question of type " + questionType + ".");
}

private static EnoObject convertMultipleChoice(QuestionType poguesQuestion) {
if (areAllResponsesBoolean(poguesQuestion))
return new SimpleMultipleChoiceQuestion();
return new ComplexMultipleChoiceQuestion();
}
private static boolean areAllResponsesBoolean(QuestionType poguesQuestion) {
return poguesQuestion.getResponse().stream()
.map(ResponseType::getDatatype)
.map(DatatypeType::getTypeName)
.allMatch(DatatypeTypeEnum.BOOLEAN::equals);
}

private static EnoObject convertTable(QuestionType poguesQuestion) {
if (isStaticTable(poguesQuestion))
return new TableQuestion();
return new DynamicTableQuestion();
}
private static boolean isStaticTable(QuestionType poguesQuestion) {
// A Pogues table is a static table if all of its "dimensions" are non-dynamic.
return poguesQuestion.getResponseStructure().getDimension().stream()
.map(DimensionType::getDynamic)
.allMatch("0"::equals);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package fr.insee.eno.core.converter;

import fr.insee.eno.core.exceptions.technical.MappingException;
import fr.insee.eno.core.model.EnoObject;
import fr.insee.eno.core.model.question.*;
import fr.insee.pogues.model.DatatypeTypeEnum;
import fr.insee.pogues.model.QuestionType;
import fr.insee.pogues.model.QuestionTypeEnum;
import fr.insee.pogues.model.ResponseType;

class PoguesSingleResponseQuestionConversion {

private PoguesSingleResponseQuestionConversion() {}

static EnoObject instantiateFrom(QuestionType poguesQuestion) {
assert poguesQuestion.getResponse().size() == 1;
QuestionTypeEnum questionType = poguesQuestion.getQuestionType();
if (QuestionTypeEnum.SIMPLE.equals(questionType))
return convertSimpleQuestion(poguesQuestion);
if (QuestionTypeEnum.SINGLE_CHOICE.equals(questionType))
return new UniqueChoiceQuestion();
throw new MappingException("Unexpected single response question of type " + questionType + ".");
}

private static EnoObject convertSimpleQuestion(QuestionType poguesQuestion) {
ResponseType response = poguesQuestion.getResponse().getFirst();
DatatypeTypeEnum datatype = response.getDatatype().getTypeName();
return switch (datatype) {
case TEXT -> new TextQuestion();
case NUMERIC -> new NumericQuestion();
case BOOLEAN -> new BooleanQuestion();
case DATE -> new DateQuestion();
case DURATION -> new DurationQuestion();
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package fr.insee.eno.core.converter;

import fr.insee.eno.core.exceptions.technical.ConversionException;
import fr.insee.eno.core.model.EnoObject;
import fr.insee.eno.core.model.question.table.TextCell;
import fr.insee.eno.core.model.question.table.UniqueChoiceCell;
import fr.insee.pogues.model.DatatypeTypeEnum;
import fr.insee.pogues.model.ResponseType;

class PoguesTableCellConversion {

private PoguesTableCellConversion() {}

static EnoObject instantiateFrom(ResponseType poguesResponse) {
DatatypeTypeEnum typeName = poguesResponse.getDatatype().getTypeName();
if (DatatypeTypeEnum.TEXT.equals(typeName)) {
if (poguesResponse.getCodeListReference() != null)
return new UniqueChoiceCell();
return new TextCell();
}
throw new ConversionException("Conversion of Pogues table cell of type " + typeName + " not implemented.");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package fr.insee.eno.core.converter;

import fr.insee.eno.core.exceptions.technical.ConversionException;
import fr.insee.eno.core.model.EnoObject;
import fr.insee.eno.core.model.variable.CalculatedVariable;
import fr.insee.eno.core.model.variable.CollectedVariable;
import fr.insee.eno.core.model.variable.ExternalVariable;
import fr.insee.pogues.model.CalculatedVariableType;
import fr.insee.pogues.model.CollectedVariableType;
import fr.insee.pogues.model.ExternalVariableType;
import fr.insee.pogues.model.VariableType;

class PoguesVariableConversion {

private PoguesVariableConversion() {}

static EnoObject instantiateFrom(VariableType poguesVariable) {
if (poguesVariable instanceof CollectedVariableType)
return new CollectedVariable();
if (poguesVariable instanceof ExternalVariableType)
return new ExternalVariable();
if (poguesVariable instanceof CalculatedVariableType)
return new CalculatedVariable();
throw new ConversionException("Unknown variable type: " + poguesVariable.getClass().getSimpleName());
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fr.insee.eno.core.exceptions.business;

public class DDIParsingException extends Exception {
public class DDIParsingException extends ParsingException {

public DDIParsingException(String message) {
super(message);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package fr.insee.eno.core.exceptions.business;

public class IllegalPoguesElementException extends RuntimeException {

public IllegalPoguesElementException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package fr.insee.eno.core.exceptions.business;

public abstract class ParsingException extends Exception {

ParsingException(String message) {
super(message);
}

ParsingException(String message, Exception e) {
super(message, e);
}

ParsingException(Exception e) {
super(e);
}
}
Loading
Loading