Commit 93ba59ef authored by George Kalampokis's avatar George Kalampokis
Browse files

Fixed word parsing issue with datasetIdentifier and validation fields

parent bfbc271b
package eu.eudat.logic.utilities.documents.word;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.logic.services.forms.VisibilityRuleService;
......@@ -275,44 +274,16 @@ public class WordBuilder {
try {
mapList = Arrays.asList(mapper.readValue(field.getValue().toString(), HashMap[].class));
}catch (Exception e) {
logger.warn(e.getMessage(), e);
logger.info("Moving to fallback parsing");
// logger.warn(e.getMessage(), e);
// logger.info("Moving to fallback parsing");
Map <String, Object> map = new HashMap<>();
map.put("label", field.getValue().toString());
mapList.add(map);
}
/*try {
if (field.getValue().toString().startsWith("[")) {
JSONArray jsonarray = new JSONArray(field.getValue().toString());
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonObject = jsonarray.getJSONObject(i);
String id = jsonObject.get("id").toString();
String label = jsonObject.getString("label");
if (id != null && label != null) {
map.put(id, label);
}
}
} else if (field.getValue().toString().startsWith("{")) {
JSONObject jsonObject = new JSONObject(field.getValue().toString());
String id = jsonObject.get("id").toString();
String label = jsonObject.getString("label");
if (id != null && label != null) {
map.put(id, label);
}
}
} catch (Exception e) {
Map<String, String> exMap = mapper.readValue(field.getValue().toString(), new TypeReference<Map<String, String>>() {
});
return exMap.get("label");
}*/
}
StringBuilder sb = new StringBuilder();
int index = 0;
for (Map<String, Object> map: mapList) {
/*if (!map.containsKey("label") && !map.containsKey("description")) {
logger.error("Value is missing the \"label\" and the \"description\" attributes");
map.put("label", "unknown Name");
}*/
for (Map.Entry<String, Object> entry : map.entrySet()) {
if (entry.getValue() != null && (entry.getKey().equals("label") || entry.getKey().equals("description") || entry.getKey().equals("name"))) {
sb.append(entry.getValue().toString());
......@@ -325,15 +296,13 @@ public class WordBuilder {
return sb.toString();
} else if (comboboxType.equals("wordlist")) {
WordListData wordListData = (WordListData) field.getData();
if (wordListData.getOptions().isEmpty() && field.getValue() != null) {
logger.warn("World List has no values but the field has");
logger.info("Return value as is");
return field.getValue().toString();
} else if (field.getValue() != null){
if (field.getValue() != null){
ComboBoxData<WordListData>.Option selectedOption = null;
for (ComboBoxData<WordListData>.Option option: wordListData.getOptions()) {
if (option.getValue().equals(field.getValue())) {
selectedOption = option;
if (!wordListData.getOptions().isEmpty()) {
for (ComboBoxData<WordListData>.Option option : wordListData.getOptions()) {
if (option.getValue().equals(field.getValue())) {
selectedOption = option;
}
}
}
return selectedOption != null ? selectedOption.getLabel() : field.getValue().toString();
......@@ -357,20 +326,15 @@ public class WordBuilder {
return field.getValue() != null ? field.getValue().toString(): "";
case "datasetIdentifier":
case "validation":
if (field.getValue() != null) {
if (field.getValue() != null && !field.getValue().toString().isEmpty()) {
Map<String, String> identifierData;
ObjectMapper mapper = new ObjectMapper();
try {
ObjectMapper mapper = new ObjectMapper();
identifierData = mapper.readValue(field.getValue().toString(), HashMap.class);
} catch (JsonParseException ex) {
identifierData = new HashMap<>();
String parsedData = field.getValue().toString().substring(1, field.getValue().toString().length() - 1);
StringTokenizer commaTokens = new StringTokenizer(parsedData, ", ");
while (commaTokens.hasMoreTokens()) {
String token = commaTokens.nextToken();
StringTokenizer equalTokens = new StringTokenizer(token, "=");
identifierData.put(equalTokens.nextToken(), equalTokens.nextToken());
}
} catch (Exception ex) {
// logger.warn(ex.getLocalizedMessage(), ex);
// logger.info("Reverting to custom parsing");
identifierData = customParse(field.getValue().toString());
}
return "id: " + identifierData.get("identifier") + ", Validation Type: " + identifierData.get("type");
}
......@@ -382,4 +346,17 @@ public class WordBuilder {
private boolean hasVisibleFields(FieldSet compositeFields, VisibilityRuleService visibilityRuleService) {
return compositeFields.getFields().stream().anyMatch(field -> visibilityRuleService.isElementVisible(field.getId()));
}
private Map<String, String> customParse(String value) {
Map<String, String> result = new LinkedHashMap<>();
String parsedValue = value.replaceAll("[^a-zA-Z0-9\\s:=,]", "");
StringTokenizer commaTokens = new StringTokenizer(parsedValue, ", ");
String delimeter = parsedValue.contains("=") ? "=" : ":";
while (commaTokens.hasMoreTokens()) {
String token = commaTokens.nextToken();
StringTokenizer delimiterTokens = new StringTokenizer(token, delimeter);
result.put(delimiterTokens.nextToken(), delimiterTokens.nextToken());
}
return result;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment