From 93ba59ef5a5881879c744b9eaf07064c436d8e51 Mon Sep 17 00:00:00 2001 From: George Kalampokis Date: Fri, 27 Aug 2021 12:58:06 +0300 Subject: [PATCH] Fixed word parsing issue with datasetIdentifier and validation fields --- .../utilities/documents/word/WordBuilder.java | 77 +++++++------------ 1 file changed, 27 insertions(+), 50 deletions(-) diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/word/WordBuilder.java b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/word/WordBuilder.java index 9fc414975..035c0a2a0 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/word/WordBuilder.java +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/word/WordBuilder.java @@ -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 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 exMap = mapper.readValue(field.getValue().toString(), new TypeReference>() { - }); - return exMap.get("label"); - }*/ } StringBuilder sb = new StringBuilder(); int index = 0; for (Map 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 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.Option selectedOption = null; - for (ComboBoxData.Option option: wordListData.getOptions()) { - if (option.getValue().equals(field.getValue())) { - selectedOption = option; + if (!wordListData.getOptions().isEmpty()) { + for (ComboBoxData.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 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 customParse(String value) { + Map 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; + } }