Improvements over MS Word Document export

This commit is contained in:
George Kalampokis 2021-06-30 17:17:17 +03:00
parent 295750684f
commit 4e3b98f1ff
2 changed files with 59 additions and 32 deletions

View File

@ -1257,7 +1257,7 @@ public class DataManagementPlanManager {
if (versioned) { if (versioned) {
fileName += "_" + dmpEntity.getVersion(); fileName += "_" + dmpEntity.getVersion();
} }
fileName = fileName.replaceAll("[^a-zA-Z0-9+ ]", ""); // fileName = fileName.replaceAll("[^a-zA-Z0-9+ ]", "");
FileEnvelope exportEnvelope = new FileEnvelope(); FileEnvelope exportEnvelope = new FileEnvelope();
exportEnvelope.setFilename(fileName + ".docx"); exportEnvelope.setFilename(fileName + ".docx");
String uuid = UUID.randomUUID().toString(); String uuid = UUID.randomUUID().toString();

View File

@ -144,68 +144,90 @@ public class WordBuilder {
private void createSections(List<Section> sections, XWPFDocument mainDocumentPart, ParagraphStyle style, Integer indent, Boolean createListing, VisibilityRuleService visibilityRuleService, Integer page, String sectionString) { private void createSections(List<Section> sections, XWPFDocument mainDocumentPart, ParagraphStyle style, Integer indent, Boolean createListing, VisibilityRuleService visibilityRuleService, Integer page, String sectionString) {
if (createListing) this.addListing(mainDocumentPart, indent, false, true); if (createListing) this.addListing(mainDocumentPart, indent, false, true);
sections.forEach(section -> { boolean hasValue = false;
for (Section section: sections) {
int paragraphPos = -1;
String tempSectionString = sectionString != null ? sectionString + "." + (section.getOrdinal() + 1) : "" + (section.getOrdinal() + 1); String tempSectionString = sectionString != null ? sectionString + "." + (section.getOrdinal() + 1) : "" + (section.getOrdinal() + 1);
if (visibilityRuleService.isElementVisible(section.getId())) { if (visibilityRuleService.isElementVisible(section.getId())) {
if (!createListing) { if (!createListing) {
XWPFParagraph paragraph = addParagraphContent(page + "." + tempSectionString + " " + section.getTitle(), mainDocumentPart, style, numId); XWPFParagraph paragraph = addParagraphContent(page + "." + tempSectionString + " " + section.getTitle(), mainDocumentPart, style, numId);
CTDecimalNumber number = paragraph.getCTP().getPPr().getNumPr().addNewIlvl(); CTDecimalNumber number = paragraph.getCTP().getPPr().getNumPr().addNewIlvl();
number.setVal(BigInteger.valueOf(indent)); number.setVal(BigInteger.valueOf(indent));
paragraphPos = mainDocumentPart.getPosOfParagraph(paragraph);
} }
createSections(section.getSections(), mainDocumentPart, ParagraphStyle.HEADER5, 1, createListing, visibilityRuleService, page, tempSectionString); createSections(section.getSections(), mainDocumentPart, ParagraphStyle.HEADER5, 1, createListing, visibilityRuleService, page, tempSectionString);
createCompositeFields(section.getCompositeFields(), mainDocumentPart, 2, createListing, visibilityRuleService, page, tempSectionString); hasValue = createCompositeFields(section.getCompositeFields(), mainDocumentPart, 2, createListing, visibilityRuleService, page, tempSectionString);
if (!hasValue && paragraphPos > -1) {
mainDocumentPart.removeBodyElement(paragraphPos);
}
}
} }
});
} }
private void createCompositeFields(List<FieldSet> compositeFields, XWPFDocument mainDocumentPart, Integer indent, Boolean createListing, VisibilityRuleService visibilityRuleService, Integer page, String section) { private Boolean createCompositeFields(List<FieldSet> compositeFields, XWPFDocument mainDocumentPart, Integer indent, Boolean createListing, VisibilityRuleService visibilityRuleService, Integer page, String section) {
if (createListing) this.addListing(mainDocumentPart, indent, true, true); if (createListing) this.addListing(mainDocumentPart, indent, true, true);
compositeFields.forEach(compositeField -> { boolean hasValue = false;
for (FieldSet compositeField: compositeFields) {
if (visibilityRuleService.isElementVisible(compositeField.getId()) && hasVisibleFields(compositeField, visibilityRuleService)) { if (visibilityRuleService.isElementVisible(compositeField.getId()) && hasVisibleFields(compositeField, visibilityRuleService)) {
int paragraphPos = -1;
if (compositeField.getTitle() != null && !compositeField.getTitle().isEmpty() && !createListing) { if (compositeField.getTitle() != null && !compositeField.getTitle().isEmpty() && !createListing) {
XWPFParagraph paragraph = addParagraphContent(page + "." + section + "." + (compositeField.getOrdinal() +1) + " " + compositeField.getTitle(), mainDocumentPart, ParagraphStyle.HEADER6, numId); XWPFParagraph paragraph = addParagraphContent(page + "." + section + "." + (compositeField.getOrdinal() +1) + " " + compositeField.getTitle(), mainDocumentPart, ParagraphStyle.HEADER6, numId);
CTDecimalNumber number = paragraph.getCTP().getPPr().getNumPr().addNewIlvl(); CTDecimalNumber number = paragraph.getCTP().getPPr().getNumPr().addNewIlvl();
number.setVal(BigInteger.valueOf(indent)); number.setVal(BigInteger.valueOf(indent));
paragraphPos = mainDocumentPart.getPosOfParagraph(paragraph);
} }
createFields(compositeField.getFields(), mainDocumentPart, 3, createListing, visibilityRuleService); hasValue = createFields(compositeField.getFields(), mainDocumentPart, 3, createListing, visibilityRuleService);
if (compositeField.getMultiplicityItems() != null && !compositeField.getMultiplicityItems().isEmpty()) { if (compositeField.getMultiplicityItems() != null && !compositeField.getMultiplicityItems().isEmpty()) {
for (FieldSet multiplicityFieldset : compositeField.getMultiplicityItems()) { for (FieldSet multiplicityFieldset : compositeField.getMultiplicityItems()) {
createFields(multiplicityFieldset.getFields(), mainDocumentPart, 3, createListing, visibilityRuleService); hasValue = createFields(multiplicityFieldset.getFields(), mainDocumentPart, 3, createListing, visibilityRuleService);
} }
} }
if (compositeField.getHasCommentField() && compositeField.getCommentFieldValue() != null && !compositeField.getCommentFieldValue().isEmpty() && !createListing) { if (hasValue && compositeField.getHasCommentField() && compositeField.getCommentFieldValue() != null && !compositeField.getCommentFieldValue().isEmpty() && !createListing) {
XWPFParagraph paragraph = addParagraphContent("Comment: " + compositeField.getCommentFieldValue(), mainDocumentPart, ParagraphStyle.COMMENT, numId); XWPFParagraph paragraph = addParagraphContent("Comment: " + compositeField.getCommentFieldValue(), mainDocumentPart, ParagraphStyle.COMMENT, numId);
CTDecimalNumber number = paragraph.getCTP().getPPr().getNumPr().addNewIlvl(); CTDecimalNumber number = paragraph.getCTP().getPPr().getNumPr().addNewIlvl();
number.setVal(BigInteger.valueOf(indent)); number.setVal(BigInteger.valueOf(indent));
} }
if (!hasValue && paragraphPos > -1) {
mainDocumentPart.removeBodyElement(paragraphPos);
} }
}); }
}
return hasValue;
} }
private void createFields(List<Field> fields, XWPFDocument mainDocumentPart, Integer indent, Boolean createListing, VisibilityRuleService visibilityRuleService) { private Boolean createFields(List<Field> fields, XWPFDocument mainDocumentPart, Integer indent, Boolean createListing, VisibilityRuleService visibilityRuleService) {
if (createListing) this.addListing(mainDocumentPart, indent, false, false); if (createListing) this.addListing(mainDocumentPart, indent, false, false);
fields.forEach(field -> { boolean hasValue = false;
for (Field field: fields) {
if (visibilityRuleService.isElementVisible(field.getId())) { if (visibilityRuleService.isElementVisible(field.getId())) {
if (!createListing) { if (!createListing) {
try { try {
XWPFParagraph paragraph = addParagraphContent(this.formatter(field), mainDocumentPart, ParagraphStyle.TEXT, numId); XWPFParagraph paragraph = addParagraphContent(this.formatter(field), mainDocumentPart, ParagraphStyle.TEXT, numId);
if (paragraph != null) {
CTDecimalNumber number = paragraph.getCTP().getPPr().getNumPr().addNewIlvl(); CTDecimalNumber number = paragraph.getCTP().getPPr().getNumPr().addNewIlvl();
number.setVal(BigInteger.valueOf(indent)); number.setVal(BigInteger.valueOf(indent));
hasValue = true;
}
} catch (IOException e) { } catch (IOException e) {
logger.error(e.getMessage(), e); logger.error(e.getMessage(), e);
} }
} }
} }
}); }
return hasValue;
} }
public XWPFParagraph addParagraphContent(String text, XWPFDocument mainDocumentPart, ParagraphStyle style, BigInteger numId) { public XWPFParagraph addParagraphContent(String text, XWPFDocument mainDocumentPart, ParagraphStyle style, BigInteger numId) {
if (text != null && !text.isEmpty()) {
XWPFParagraph paragraph = this.options.get(style).apply(mainDocumentPart, text); XWPFParagraph paragraph = this.options.get(style).apply(mainDocumentPart, text);
if (numId != null) { if (numId != null) {
paragraph.setNumID(numId); paragraph.setNumID(numId);
} }
return paragraph; return paragraph;
} }
return null;
}
private void addListing(XWPFDocument document, int indent, Boolean question, Boolean hasIndication) { private void addListing(XWPFDocument document, int indent, Boolean question, Boolean hasIndication) {
CTLvl cTLvl = this.cTAbstractNum.addNewLvl(); CTLvl cTLvl = this.cTAbstractNum.addNewLvl();
@ -254,6 +276,7 @@ public class WordBuilder {
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");
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);
@ -304,6 +327,7 @@ public class WordBuilder {
WordListData wordListData = (WordListData) field.getData(); WordListData wordListData = (WordListData) field.getData();
if (wordListData.getOptions().isEmpty() && field.getValue() != null) { if (wordListData.getOptions().isEmpty() && field.getValue() != null) {
logger.warn("World List has no values but the field has"); logger.warn("World List has no values but the field has");
logger.info("Return value as is");
return field.getValue().toString(); return field.getValue().toString();
} else if (field.getValue() != null){ } else if (field.getValue() != null){
ComboBoxData<WordListData>.Option selectedOption = null; ComboBoxData<WordListData>.Option selectedOption = null;
@ -332,6 +356,7 @@ 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) {
Map<String, String> identifierData; Map<String, String> identifierData;
try { try {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
@ -348,6 +373,8 @@ public class WordBuilder {
} }
return "id: " + identifierData.get("identifier") + ", Validation Type: " + identifierData.get("type"); return "id: " + identifierData.get("identifier") + ", Validation Type: " + identifierData.get("type");
} }
return "";
}
return null; return null;
} }