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;
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,17 +296,15 @@ 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 (!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();
}
return "";
@ -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;
try {
ObjectMapper mapper = new ObjectMapper();
try {
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;
}
}