diff --git a/mica-core/src/main/java/org/obiba/mica/access/export/DataAccessEntityExporter.java b/mica-core/src/main/java/org/obiba/mica/access/export/DataAccessEntityExporter.java index f1929adf4b..fbb43a749b 100644 --- a/mica-core/src/main/java/org/obiba/mica/access/export/DataAccessEntityExporter.java +++ b/mica-core/src/main/java/org/obiba/mica/access/export/DataAccessEntityExporter.java @@ -15,10 +15,14 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.Splitter; import com.google.common.base.Strings; +import com.google.common.collect.Maps; import org.apache.commons.compress.utils.Lists; import org.apache.poi.wp.usermodel.HeaderFooterType; import org.apache.poi.xwpf.usermodel.*; +import org.json.JSONException; +import org.json.JSONObject; import org.jsoup.Jsoup; +import org.jsoup.nodes.Element; import org.obiba.mica.access.domain.DataAccessEntity; import org.obiba.mica.micaConfig.domain.AbstractDataAccessEntityForm; import org.obiba.mica.micaConfig.service.helper.SchemaFormConfig; @@ -34,6 +38,7 @@ import java.util.Arrays; import java.util.Date; import java.util.List; +import java.util.Map; import static org.slf4j.LoggerFactory.getLogger; @@ -50,29 +55,33 @@ public class DataAccessEntityExporter { private JsonNode model; + private JSONObject wordConfig; + private String prefix = ""; + private Map tablesByPrefix = Maps.newHashMap(); + private DataAccessEntityExporter() { } public ByteArrayOutputStream export(String titleStr, String status, String id) throws IOException { - try (XWPFDocument document = new XWPFDocument();) { + try (XWPFDocument document = new XWPFDocument()) { + // document title XWPFParagraph title = document.createParagraph(); title.setAlignment(ParagraphAlignment.LEFT); title.setSpacingAfter(400); XWPFRun titleRun = title.createRun(); titleRun.setText(String.format("%s [%s] - %s", titleStr, status, id)); - titleRun.setBold(true); - titleRun.setFontSize(20); + applyFontConfig(titleRun, getItemConfig("documentTitle")); traverseDefinitionTree(document, definition, model); + // footer XWPFFooter footer = document.createFooter(HeaderFooterType.DEFAULT); XWPFParagraph footerParagraph = footer.createParagraph(); XWPFRun footerRun = footerParagraph.createRun(); - footerRun.setFontSize(10); - footerRun.setColor("757575"); footerRun.setText(String.format("%s [%s] - %s - %s", titleStr, status, id, ISO_8601.format(new Date()))); + applyFontConfig(footerRun, getItemConfig("footer")); ByteArrayOutputStream ba = new ByteArrayOutputStream(); document.write(ba); @@ -180,13 +189,74 @@ private String getKeyField(String key) { */ private void appendHelp(XWPFDocument document, JsonNode item) { if (item.has("title")) { - XWPFParagraph paragraph = document.createParagraph(); - addTextWithLineBreak(paragraph, Jsoup.parse(item.get("title").asText()).wholeText(), "757575"); + String htmlText = item.get("title").asText(); + parseAndRenderHTML(htmlText, document, "title"); } if (item.has("helpvalue")) { - XWPFParagraph paragraph = document.createParagraph(); - addTextWithLineBreak(paragraph, Jsoup.parse(item.get("helpvalue").asText()).wholeText(), "757575"); + String htmlText = item.get("helpvalue").asText(); + parseAndRenderHTML(htmlText, document, "help"); + } + } + + private void parseAndRenderHTML(String html, XWPFDocument document, String itemKey) { + for (Element element : Jsoup.parse(html).body().children()) { + processElement(element, document, itemKey); + } + } + + private XWPFParagraph processElement(Element element, XWPFDocument document, String itemKey) { + if ("ul".equals(element.tagName()) || "ol".equals(element.tagName())) { + XWPFParagraph paragraph = null; + for (Element listItem : element.children()) { + paragraph = processElement(listItem, document, itemKey); // Process each
  • item in