Skip to content
Merged
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 pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>fr.insee.pogues</groupId>
<artifactId>pogues-model</artifactId>
<version>1.16.0</version>
<version>1.17.0</version>
<packaging>jar</packaging>

<name>Pogues Model</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class JSONSynonymsPreProcessor {

private static final Logger logger = LoggerFactory.getLogger(JSONSynonymsPreProcessor.class);

private static final String FIELDS_KEY = "fields";

public String transform(String jsonQuestionnaireString) {
// Should throw an exception, yet it may have impacts in Pogues-Back-Office
if (jsonQuestionnaireString == null) {
Expand Down Expand Up @@ -57,6 +59,52 @@ public String transform(InputStream jsonQuestionnaireInputStream) {
return result;
}

public String transformSuggesterParameters(String jsonSuggesterParams){
// Should throw an exception, yet it may have impacts in Pogues-Back-Office
if (jsonSuggesterParams == null) {
logger.warn("null string given in JSON synonyms pre-processing method.");
return null;
}

return transformSuggesterParameters(new ByteArrayInputStream(jsonSuggesterParams.getBytes()));
}

public String transformSuggesterParameters(InputStream jsonSuggesterParamsIS){
// Should throw an exception, yet it may have impacts in Pogues-Back-Office
if (jsonSuggesterParamsIS == null) {
logger.warn("null input stream given in JSON synonyms pre-processing method.");
return null;
}

logger.debug("Pre-processing json suggesterParams stream source...");

OutputStream outputStream = new ByteArrayOutputStream();

try (JsonReader jsonReader = Json.createReader(jsonSuggesterParamsIS);
JsonWriter jsonWriter = Json.createWriter(outputStream)) {

JsonObject jsonSuggesterParams = jsonReader.readObject();

JsonObjectBuilder jsonSuggesterParamsBuilder = Json.createObjectBuilder();
jsonSuggesterParams.forEach((key, jsonValue)-> {
if (! FIELDS_KEY.equals(key)) {
jsonSuggesterParamsBuilder.add(key, jsonValue);
} else {
editFieldsArray(jsonSuggesterParamsBuilder, (JsonArray) jsonValue);
}
});
jsonWriter.writeObject(jsonSuggesterParamsBuilder.build());
}

String result = outputStream.toString();
try {
outputStream.close();
} catch (IOException e) {
throw new PreProcessingException("IO exception occurred when trying to close pre processing output.", e);
}

return result;
}

private static void editQuestionnaire(JsonObject jsonQuestionnaire, JsonObjectBuilder jsonQuestionnaireBuilder) {
jsonQuestionnaire.forEach((key, jsonValue) -> {
Expand Down Expand Up @@ -99,7 +147,7 @@ private static void editCodeList(JsonArrayBuilder jsonCodeListArrayBuilder, Json
private static void editSuggesterParameters(JsonObjectBuilder jsonCodeListBuilder, JsonObject jsonSuggesterParameters) {
JsonObjectBuilder jsonSuggesterParametersBuilder = Json.createObjectBuilder();
jsonSuggesterParameters.forEach((key, jsonValue) -> {
if (! "fields".equals(key)) {
if (! FIELDS_KEY.equals(key)) {
jsonSuggesterParametersBuilder.add(key, jsonValue);
} else {
editFieldsArray(jsonSuggesterParametersBuilder, (JsonArray) jsonValue);
Expand All @@ -114,7 +162,7 @@ private static void editFieldsArray(JsonObjectBuilder jsonSuggesterParametersBui
JsonObject jsonFields = (JsonObject) jsonValue;
editFields(jsonFieldsArrayBuilder, jsonFields);
}
jsonSuggesterParametersBuilder.add("fields", jsonFieldsArrayBuilder.build());
jsonSuggesterParametersBuilder.add(FIELDS_KEY, jsonFieldsArrayBuilder.build());
}

private static void editFields(JsonArrayBuilder jsonFieldsArrayBuilder, JsonObject jsonFields) {
Expand Down
54 changes: 54 additions & 0 deletions src/test/java/fr/insee/pogues/test/JSONSynonymsProcessorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,53 @@ class JSONSynonymsProcessorTest {
}
}""";

private String suggesterParametersSynonymsObject = """
{
"fields": [
{
"name": "foo",
"synonyms": {
"foo": [
"BAR"
],
"example": [
"INSTANCE",
"SAMPLE",
"ILLUSTRATION"
]
}
}
]
}
""";


private String suggesterParametersSynonymsArray = """
{
"fields": [
{
"name": "foo",
"synonyms": [
{
"source": "foo",
"target": [
"BAR"
]
},
{
"source": "example",
"target": [
"INSTANCE",
"SAMPLE",
"ILLUSTRATION"
]
}
]
}
]
}
""";

@Test
void preProcessingTransformSynonyms() throws JSONException {
JSONSynonymsPreProcessor jsonSynonymsPreProcessor = new JSONSynonymsPreProcessor();
Expand Down Expand Up @@ -104,4 +151,11 @@ void identityPostPrePocessingTransformSynonyms() throws JSONException {
JSONAssert.assertEquals(questionnaireSynonymsArray, result, JSONCompareMode.STRICT);
}

@Test
void preProcessingTransformSuggesterParameters() throws JSONException {
JSONSynonymsPreProcessor jsonSynonymsPreProcessor = new JSONSynonymsPreProcessor();
String result = jsonSynonymsPreProcessor.transformSuggesterParameters(suggesterParametersSynonymsObject);
JSONAssert.assertEquals(suggesterParametersSynonymsArray, result, JSONCompareMode.STRICT);
}

}