fix tags - distribution_format word export

This commit is contained in:
Bernaldo Mihasi 2022-03-22 14:03:22 +02:00
parent 8734d7c1ba
commit 116f9b99ea
5 changed files with 33 additions and 17 deletions

View File

@ -5,10 +5,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.logic.services.forms.VisibilityRuleService;
import eu.eudat.logic.utilities.documents.types.ParagraphStyle;
import eu.eudat.logic.utilities.interfaces.ApplierWithValue;
import eu.eudat.models.data.components.commons.datafield.CheckBoxData;
import eu.eudat.models.data.components.commons.datafield.ComboBoxData;
import eu.eudat.models.data.components.commons.datafield.UploadData;
import eu.eudat.models.data.components.commons.datafield.WordListData;
import eu.eudat.logic.utilities.json.JavaToJson;
import eu.eudat.models.data.components.commons.datafield.*;
import eu.eudat.models.data.user.components.datasetprofile.Field;
import eu.eudat.models.data.user.components.datasetprofile.FieldSet;
import eu.eudat.models.data.user.components.datasetprofile.Section;
@ -17,13 +15,13 @@ import eu.eudat.models.data.user.composite.PagedDatasetProfile;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
import org.json.JSONArray;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import org.springframework.http.MediaType;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
@ -328,7 +326,7 @@ public class WordBuilder {
boolean isImage = false;
for(UploadData.Option type: ((UploadData)field.getData()).getTypes()){
String fileFormat = type.getValue();
if(fileFormat.equals(MediaType.IMAGE_JPEG_VALUE) || fileFormat.equals(MediaType.IMAGE_PNG_VALUE) || fileFormat.equals(MediaType.IMAGE_GIF_VALUE)){
if(IMAGE_TYPE_MAP.containsKey(fileFormat)){
isImage = true;
break;
}
@ -347,6 +345,11 @@ public class WordBuilder {
else if (field.getValue() != null && !field.getValue().toString().isEmpty()) {
this.indent = indent;
String format = this.formatter(field);
if (field.getViewStyle().getRenderStyle().equals("tags")) {
format = getCommaSeparatedFormatsFromJson(format, "name");
} else if (field.getViewStyle().getRenderStyle().equals("combobox") && field.getData() instanceof AutoCompleteData) {
format = getCommaSeparatedFormatsFromJson(format, "label");
}
if(format != null && !format.isEmpty()){
if(format.charAt(0) == '['){
format = format.substring(1, format.length() - 1).replaceAll(",", ", ");
@ -371,6 +374,21 @@ public class WordBuilder {
return hasValue;
}
private String getCommaSeparatedFormatsFromJson(String format, String attribute){
if((format == null || format.isEmpty()) || (attribute == null || attribute.isEmpty())){
return null;
}
JSONArray array = new JSONArray(JavaToJson.objectStringToJson(format));
StringBuilder multipleFormats = new StringBuilder();
for (int i = 0; i < array.length(); i++) {
multipleFormats.append(array.getJSONObject(i).getString(attribute)).append(", ");
}
if (multipleFormats.length() > 0) {
multipleFormats.setLength(multipleFormats.length() - 2);
}
return multipleFormats.toString();
}
public XWPFParagraph addParagraphContent(Object content, XWPFDocument mainDocumentPart, ParagraphStyle style, BigInteger numId) {
if (content != null) {
if (content instanceof String && ((String)content).isEmpty()) {

View File

@ -4,9 +4,9 @@ public class JavaToJson {
public static String objectStringToJson(String object) {
String result = object.replaceAll("=", "\":\"")
//.replaceAll("\\{", "{\"")
.replaceAll("\\{", "{\"")
.replaceAll(", ", "\", \"")
//.replaceAll("}", "\"}" ).
.replaceAll("}", "\"}" )
.replaceAll("}\", \"\\{", "}, {");
return result;
}

View File

@ -55,9 +55,8 @@ public class CostRDAMapper {
rda.setValue(Double.valueOf(rdaValue));
}
else if(rdaProperty.contains("currency_code")){
String json = JavaToJson.objectStringToJson(rdaValue);
HashMap<String,String> result =
new ObjectMapper().readValue(json, HashMap.class);
new ObjectMapper().readValue(rdaValue, HashMap.class);
rda.setCurrencyCode(Cost.CurrencyCode.fromValue(result.get("value")));
}
else if(rdaProperty.contains("title")){

View File

@ -18,6 +18,7 @@ import java.util.stream.Collectors;
public class DistributionRDAMapper {
private static final Logger logger = LoggerFactory.getLogger(DistributionRDAMapper.class);
private static final ObjectMapper mapper = new ObjectMapper();
public static List<Distribution> toRDAList(List<JsonNode> nodes) {
Map<String, Distribution> rdaMap = new HashMap<>();
@ -89,12 +90,12 @@ public class DistributionRDAMapper {
int i = 1;
while(iter.hasNext()) {
JsonNode current = iter.next();
String format = JavaToJson.objectStringToJson(current.toString());
String format = current.toString();
try {
Map<String, String> result = new ObjectMapper().readValue(format, HashMap.class);
Map<String, String> result = mapper.readValue(format, HashMap.class);
format = result.get("label");
formats.add(format);
rda.setAdditionalProperty("format" + i++, new ObjectMapper().readTree(current.toString()));
rda.setAdditionalProperty("format" + i++, mapper.readTree(current.toString()));
}
catch(JsonProcessingException e){
logger.warn(e.getMessage());
@ -219,7 +220,6 @@ public class DistributionRDAMapper {
if (distributionNode.get("data").get("type").asText().equals("autocomplete")) {
Map<String, Object> additionalProperties = rda.getAdditionalProperties();
List<Object> standardFormats = new ArrayList<>();
ObjectMapper mapper = new ObjectMapper();
rda.getAdditionalProperties().forEach((key, value) -> {
try {
if (key.matches("format\\d+")) {

View File

@ -15,13 +15,12 @@ import java.util.stream.Collectors;
public class KeywordRDAMapper {
private static final Logger logger = LoggerFactory.getLogger(KeywordRDAMapper.class);
private static final ObjectMapper mapper = new ObjectMapper();
public static List<String> toRDA(String value) {
if (!value.isEmpty() && !value.equals("null")) {
try {
ObjectMapper mapper = new ObjectMapper();
String valueJson = JavaToJson.objectStringToJson(value);
Tag tag = mapper.readValue(valueJson, Tag.class);
Tag tag = mapper.readValue(value, Tag.class);
return new ArrayList<>(Collections.singletonList(tag.getName()));
} catch (JsonProcessingException e) {
logger.warn(e.getMessage() + ". Attempting to parse it as a String since its a new tag.");