diff --git a/pom.xml b/pom.xml
index 63c1e0d..63ef83b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -4,7 +4,7 @@
fr.insee.pogues
pogues-model
- 1.16.0
+ 1.17.0
jar
Pogues Model
diff --git a/src/main/java/fr/insee/pogues/conversion/JSONSynonymsPreProcessor.java b/src/main/java/fr/insee/pogues/conversion/JSONSynonymsPreProcessor.java
index 2b6dd9b..e5fddeb 100644
--- a/src/main/java/fr/insee/pogues/conversion/JSONSynonymsPreProcessor.java
+++ b/src/main/java/fr/insee/pogues/conversion/JSONSynonymsPreProcessor.java
@@ -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) {
@@ -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) -> {
@@ -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);
@@ -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) {
diff --git a/src/test/java/fr/insee/pogues/test/JSONSynonymsProcessorTest.java b/src/test/java/fr/insee/pogues/test/JSONSynonymsProcessorTest.java
index aeedd21..1ab6052 100644
--- a/src/test/java/fr/insee/pogues/test/JSONSynonymsProcessorTest.java
+++ b/src/test/java/fr/insee/pogues/test/JSONSynonymsProcessorTest.java
@@ -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();
@@ -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);
+ }
+
}