Fix issue with xml export of autocomplete and multiautocomplete fields

This commit is contained in:
George Kalampokis 2021-07-20 12:23:55 +03:00
parent 15af25e292
commit 75d9726bca
1 changed files with 25 additions and 5 deletions

View File

@ -1,5 +1,7 @@
package eu.eudat.logic.utilities.documents.xml;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import eu.eudat.logic.services.forms.VisibilityRuleService;
import eu.eudat.logic.utilities.builders.XmlBuilder;
import eu.eudat.models.data.components.commons.datafield.ExternalDatasetsData;
@ -19,6 +21,7 @@ import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class ExportXmlBuilder {
@ -98,16 +101,33 @@ public class ExportXmlBuilder {
}
if (field.getValue() != null) {
Element valueField = element.createElement("value");
ObjectMapper mapper = new ObjectMapper();
try {
JSONArray jsonArray = new JSONArray(field.getValue().toString());
List<Map<String, Object>> jsonArray = mapper.readValue(field.getValue().toString(), List.class);
// JSONArray jsonArray = new JSONArray(field.getValue().toString());
StringBuilder sb = new StringBuilder();
for (int i = 0; i < jsonArray.length(); i++) {
boolean firstTime = true;
for (Map<String, Object> jsonElement: jsonArray) {
if (!firstTime) {
sb.append(", ");
}
sb.append(jsonElement.get("label") != null ? jsonElement.get("label") : jsonElement.get("name"));
firstTime = false;
}
/*for (int i = 0; i < jsonArray.length(); i++) {
sb.append(jsonArray.getJSONObject(i).get("label").toString());
if (i != jsonArray.length() - 1) sb.append(", ");
}
}*/
valueField.setTextContent(sb.toString());
} catch (JSONException ex) {
valueField.setTextContent(field.getValue().toString());
} catch (IOException ex) {
try {
Map<String, Object> jsonElement = mapper.readValue(field.getValue().toString(), Map.class);
valueField.setTextContent((jsonElement.get("label") != null ? jsonElement.get("label").toString() : jsonElement.get("name").toString()));
} catch (IOException e) {
valueField.setTextContent(field.getValue().toString());
}
}
elementField.appendChild(valueField);
}