Fixed word parsing issue with datasetIdentifier and validation fields

This commit is contained in:
George Kalampokis 2021-08-27 12:58:06 +03:00
parent bfbc271b56
commit 93ba59ef5a
1 changed files with 27 additions and 50 deletions

View File

@ -1,6 +1,5 @@
package eu.eudat.logic.utilities.documents.word; package eu.eudat.logic.utilities.documents.word;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.logic.services.forms.VisibilityRuleService; import eu.eudat.logic.services.forms.VisibilityRuleService;
@ -275,44 +274,16 @@ public class WordBuilder {
try { try {
mapList = Arrays.asList(mapper.readValue(field.getValue().toString(), HashMap[].class)); mapList = Arrays.asList(mapper.readValue(field.getValue().toString(), HashMap[].class));
}catch (Exception e) { }catch (Exception e) {
logger.warn(e.getMessage(), e); // logger.warn(e.getMessage(), e);
logger.info("Moving to fallback parsing"); // logger.info("Moving to fallback parsing");
Map <String, Object> map = new HashMap<>(); Map <String, Object> map = new HashMap<>();
map.put("label", field.getValue().toString()); map.put("label", field.getValue().toString());
mapList.add(map); 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(); StringBuilder sb = new StringBuilder();
int index = 0; int index = 0;
for (Map<String, Object> map: mapList) { 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()) { for (Map.Entry<String, Object> entry : map.entrySet()) {
if (entry.getValue() != null && (entry.getKey().equals("label") || entry.getKey().equals("description") || entry.getKey().equals("name"))) { if (entry.getValue() != null && (entry.getKey().equals("label") || entry.getKey().equals("description") || entry.getKey().equals("name"))) {
sb.append(entry.getValue().toString()); sb.append(entry.getValue().toString());
@ -325,15 +296,13 @@ public class WordBuilder {
return sb.toString(); return sb.toString();
} else if (comboboxType.equals("wordlist")) { } else if (comboboxType.equals("wordlist")) {
WordListData wordListData = (WordListData) field.getData(); WordListData wordListData = (WordListData) field.getData();
if (wordListData.getOptions().isEmpty() && field.getValue() != null) { if (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){
ComboBoxData<WordListData>.Option selectedOption = null; ComboBoxData<WordListData>.Option selectedOption = null;
for (ComboBoxData<WordListData>.Option option: wordListData.getOptions()) { if (!wordListData.getOptions().isEmpty()) {
if (option.getValue().equals(field.getValue())) { for (ComboBoxData<WordListData>.Option option : wordListData.getOptions()) {
selectedOption = option; if (option.getValue().equals(field.getValue())) {
selectedOption = option;
}
} }
} }
return selectedOption != null ? selectedOption.getLabel() : field.getValue().toString(); return selectedOption != null ? selectedOption.getLabel() : field.getValue().toString();
@ -357,20 +326,15 @@ public class WordBuilder {
return field.getValue() != null ? field.getValue().toString(): ""; return field.getValue() != null ? field.getValue().toString(): "";
case "datasetIdentifier": case "datasetIdentifier":
case "validation": case "validation":
if (field.getValue() != null) { if (field.getValue() != null && !field.getValue().toString().isEmpty()) {
Map<String, String> identifierData; Map<String, String> identifierData;
ObjectMapper mapper = new ObjectMapper();
try { try {
ObjectMapper mapper = new ObjectMapper();
identifierData = mapper.readValue(field.getValue().toString(), HashMap.class); identifierData = mapper.readValue(field.getValue().toString(), HashMap.class);
} catch (JsonParseException ex) { } catch (Exception ex) {
identifierData = new HashMap<>(); // logger.warn(ex.getLocalizedMessage(), ex);
String parsedData = field.getValue().toString().substring(1, field.getValue().toString().length() - 1); // logger.info("Reverting to custom parsing");
StringTokenizer commaTokens = new StringTokenizer(parsedData, ", "); identifierData = customParse(field.getValue().toString());
while (commaTokens.hasMoreTokens()) {
String token = commaTokens.nextToken();
StringTokenizer equalTokens = new StringTokenizer(token, "=");
identifierData.put(equalTokens.nextToken(), equalTokens.nextToken());
}
} }
return "id: " + identifierData.get("identifier") + ", Validation Type: " + identifierData.get("type"); return "id: " + identifierData.get("identifier") + ", Validation Type: " + identifierData.get("type");
} }
@ -382,4 +346,17 @@ public class WordBuilder {
private boolean hasVisibleFields(FieldSet compositeFields, VisibilityRuleService visibilityRuleService) { private boolean hasVisibleFields(FieldSet compositeFields, VisibilityRuleService visibilityRuleService) {
return compositeFields.getFields().stream().anyMatch(field -> visibilityRuleService.isElementVisible(field.getId())); 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;
}
} }