no message
parent
1715e8629f
commit
52bceed971
@ -0,0 +1,41 @@
|
||||
package eu.eudat.documents.types;
|
||||
|
||||
/**
|
||||
* Created by ikalyvas on 2/26/2018.
|
||||
*/
|
||||
public enum ParagraphStyle {
|
||||
TEXT(0), HEADER1(1), HEADER2(2), HEADER3(3), HEADER4(4), TITLE(5), FOOTER(6), COMMENT(7);
|
||||
|
||||
private Integer value;
|
||||
|
||||
private ParagraphStyle(Integer value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public Integer getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public static ParagraphStyle fromInteger(Integer value) {
|
||||
switch (value) {
|
||||
case 0:
|
||||
return TEXT;
|
||||
case 1:
|
||||
return HEADER1;
|
||||
case 2:
|
||||
return HEADER2;
|
||||
case 3:
|
||||
return HEADER3;
|
||||
case 4:
|
||||
return HEADER4;
|
||||
case 5:
|
||||
return TITLE;
|
||||
case 6:
|
||||
return FOOTER;
|
||||
case 7:
|
||||
return COMMENT;
|
||||
default:
|
||||
throw new RuntimeException("Unsupported ParagraphStyle Code");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package eu.eudat.documents.types;
|
||||
|
||||
/**
|
||||
* Created by ikalyvas on 2/27/2018.
|
||||
*/
|
||||
public enum TextStyle {
|
||||
ITALIC(0), BOLD(1), CAPS(2);
|
||||
|
||||
private Integer value;
|
||||
|
||||
private TextStyle(Integer value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public Integer getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public static TextStyle fromInteger(Integer value) {
|
||||
switch (value) {
|
||||
case 0:
|
||||
return ITALIC;
|
||||
case 1:
|
||||
return BOLD;
|
||||
case 2:
|
||||
return CAPS;
|
||||
default:
|
||||
throw new RuntimeException("Unsupported TextStyle Code");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,193 @@
|
||||
package eu.eudat.documents.word;
|
||||
|
||||
import eu.eudat.documents.types.ParagraphStyle;
|
||||
import eu.eudat.documents.types.TextStyle;
|
||||
import eu.eudat.models.user.components.datasetprofile.Field;
|
||||
import eu.eudat.models.user.components.datasetprofile.FieldSet;
|
||||
import eu.eudat.models.user.components.datasetprofile.Section;
|
||||
import eu.eudat.models.user.composite.DatasetProfilePage;
|
||||
import eu.eudat.models.user.composite.PagedDatasetProfile;
|
||||
import eu.eudat.utilities.interfaces.Applier;
|
||||
import org.apache.poi.xwpf.usermodel.*;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTAbstractNum;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLvl;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STNumberFormat;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by ikalyvas on 2/26/2018.
|
||||
*/
|
||||
public class WordBuilder {
|
||||
|
||||
private Map<ParagraphStyle, Applier<XWPFDocument, String, XWPFParagraph>> options = new HashMap<>();
|
||||
private CTAbstractNum cTAbstractNum;
|
||||
private BigInteger numId;
|
||||
|
||||
public WordBuilder() {
|
||||
this.cTAbstractNum = CTAbstractNum.Factory.newInstance();
|
||||
this.cTAbstractNum.setAbstractNumId(BigInteger.valueOf(1));
|
||||
}
|
||||
|
||||
private void buildOptions() {
|
||||
this.options.put(ParagraphStyle.TEXT, (mainDocumentPart, item) -> {
|
||||
XWPFParagraph paragraph = mainDocumentPart.createParagraph();
|
||||
XWPFRun run = paragraph.createRun();
|
||||
run.setText(item);
|
||||
run.setFontSize(11);
|
||||
return paragraph;
|
||||
});
|
||||
this.options.put(ParagraphStyle.TITLE, (mainDocumentPart, item) -> {
|
||||
XWPFParagraph paragraph = mainDocumentPart.createParagraph();
|
||||
XWPFRun run = paragraph.createRun();
|
||||
run.setText(item);
|
||||
run.setBold(true);
|
||||
run.setFontSize(14);
|
||||
return paragraph;
|
||||
});
|
||||
this.options.put(ParagraphStyle.HEADER1, (mainDocumentPart, item) -> {
|
||||
XWPFParagraph paragraph = mainDocumentPart.createParagraph();
|
||||
XWPFRun run = paragraph.createRun();
|
||||
run.setText(item);
|
||||
run.setBold(true);
|
||||
run.setFontSize(12);
|
||||
run.setUnderline(UnderlinePatterns.SINGLE);
|
||||
return paragraph;
|
||||
});
|
||||
this.options.put(ParagraphStyle.HEADER2, (mainDocumentPart, item) -> {
|
||||
XWPFParagraph paragraph = mainDocumentPart.createParagraph();
|
||||
XWPFRun run = paragraph.createRun();
|
||||
run.setText(item);
|
||||
run.setBold(true);
|
||||
run.setFontSize(12);
|
||||
run.setUnderline(UnderlinePatterns.SINGLE);
|
||||
return paragraph;
|
||||
});
|
||||
this.options.put(ParagraphStyle.HEADER3, (mainDocumentPart, item) -> {
|
||||
XWPFParagraph paragraph = mainDocumentPart.createParagraph();
|
||||
XWPFRun run = paragraph.createRun();
|
||||
run.setText(item);
|
||||
run.setBold(true);
|
||||
run.setFontSize(11);
|
||||
run.setUnderline(UnderlinePatterns.SINGLE);
|
||||
return paragraph;
|
||||
});
|
||||
this.options.put(ParagraphStyle.FOOTER, (mainDocumentPart, item) -> {
|
||||
XWPFParagraph paragraph = mainDocumentPart.createParagraph();
|
||||
XWPFRun run = paragraph.createRun();
|
||||
run.setText(item);
|
||||
return paragraph;
|
||||
});
|
||||
}
|
||||
|
||||
public File build(PagedDatasetProfile pagedDatasetProfile) throws IOException {
|
||||
XWPFDocument document = new XWPFDocument();
|
||||
|
||||
this.buildOptions();
|
||||
createPages(pagedDatasetProfile.getPages(), document, true);
|
||||
XWPFAbstractNum abstractNum = new XWPFAbstractNum(cTAbstractNum);
|
||||
XWPFNumbering numbering = document.createNumbering();
|
||||
BigInteger abstractNumID = numbering.addAbstractNum(abstractNum);
|
||||
this.numId = numbering.addNum(abstractNumID);
|
||||
createPages(pagedDatasetProfile.getPages(), document, false);
|
||||
File exportFile = new File("welcome.docx");
|
||||
FileOutputStream out = new FileOutputStream(exportFile);
|
||||
document.write(out);
|
||||
out.close();
|
||||
return exportFile;
|
||||
}
|
||||
|
||||
public void createPages(List<DatasetProfilePage> datasetProfilePages, XWPFDocument mainDocumentPart, Boolean createListing) {
|
||||
//if (createListing) this.addListing(mainDocumentPart, 0, false, true);
|
||||
datasetProfilePages.forEach(item -> {
|
||||
createSections(item.getSections(), mainDocumentPart, ParagraphStyle.TITLE, 0, createListing);
|
||||
});
|
||||
}
|
||||
|
||||
public void createSections(List<Section> sections, XWPFDocument mainDocumentPart, ParagraphStyle style, Integer indent, Boolean createListing) {
|
||||
if (createListing) this.addListing(mainDocumentPart, indent, false, true);
|
||||
BigInteger listing = numId;
|
||||
sections.forEach(section -> {
|
||||
if (!createListing) {
|
||||
XWPFParagraph paragraph = addParagraphContent(section.getTitle(), mainDocumentPart, style, listing);
|
||||
CTDecimalNumber number = paragraph.getCTP().getPPr().getNumPr().addNewIlvl();
|
||||
number.setVal(BigInteger.valueOf(indent));
|
||||
}
|
||||
createSections(section.getSections(), mainDocumentPart, ParagraphStyle.HEADER2, 1, createListing);
|
||||
createCompositeFields(section.getCompositeFields(), mainDocumentPart, 2, createListing);
|
||||
});
|
||||
}
|
||||
|
||||
public void createCompositeFields(List<FieldSet> compositeFields, XWPFDocument mainDocumentPart, Integer indent, Boolean createListing) {
|
||||
if (createListing) this.addListing(mainDocumentPart, indent, true, true);
|
||||
compositeFields.forEach(compositeField -> {
|
||||
if (compositeField.getTitle() != null && !compositeField.getTitle().isEmpty() && !createListing) {
|
||||
XWPFParagraph paragraph = addParagraphContent(compositeField.getTitle(), mainDocumentPart, ParagraphStyle.HEADER3, numId);
|
||||
CTDecimalNumber number = paragraph.getCTP().getPPr().getNumPr().addNewIlvl();
|
||||
number.setVal(BigInteger.valueOf(indent));
|
||||
}
|
||||
createFields(compositeField.getFields(), mainDocumentPart, 3, createListing);
|
||||
});
|
||||
}
|
||||
|
||||
public void createFields(List<Field> fields, XWPFDocument mainDocumentPart, Integer indent, Boolean createListing) {
|
||||
if (createListing) this.addListing(mainDocumentPart, indent, false, false);
|
||||
fields.forEach(field -> {
|
||||
if (!createListing) {
|
||||
XWPFParagraph paragraph = addParagraphContent(field.getValue(), mainDocumentPart, ParagraphStyle.TEXT, numId);
|
||||
CTDecimalNumber number = paragraph.getCTP().getPPr().getNumPr().addNewIlvl();
|
||||
number.setVal(BigInteger.valueOf(indent));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public XWPFParagraph addParagraphContent(String text, XWPFDocument mainDocumentPart, ParagraphStyle style, BigInteger numId) {
|
||||
XWPFParagraph paragraph = this.options.get(style).apply(mainDocumentPart, text);
|
||||
if (numId != null) paragraph.setNumID(numId);
|
||||
return paragraph;
|
||||
}
|
||||
|
||||
|
||||
public void addStyling(List<TextStyle> styles, String color) {
|
||||
|
||||
}
|
||||
|
||||
public void addListing(XWPFDocument document, int indent, Boolean question, Boolean hasIndication) {
|
||||
|
||||
//this.cTAbstractNum.setAbstractNumId(BigInteger.valueOf(indent));
|
||||
CTLvl cTLvl = this.cTAbstractNum.addNewLvl();
|
||||
|
||||
String textLevel = "";
|
||||
for (int i = 0; i <= indent; i++) {
|
||||
textLevel += "%" + (i + 1) + ".";
|
||||
}
|
||||
if (question) {
|
||||
cTLvl.addNewNumFmt().setVal(STNumberFormat.DECIMAL);
|
||||
cTLvl.addNewLvlText().setVal("Q " + textLevel);
|
||||
cTLvl.addNewStart().setVal(BigInteger.valueOf(1));
|
||||
} else if (!question && hasIndication) {
|
||||
cTLvl.addNewNumFmt().setVal(STNumberFormat.DECIMAL);
|
||||
cTLvl.addNewLvlText().setVal(textLevel);
|
||||
cTLvl.addNewStart().setVal(BigInteger.valueOf(1));
|
||||
}
|
||||
if (!question && !hasIndication) {
|
||||
cTLvl.addNewNumFmt().setVal(STNumberFormat.NONE);
|
||||
}
|
||||
|
||||
/*if (this.numId == null) {
|
||||
XWPFAbstractNum abstractNum = new XWPFAbstractNum(cTAbstractNum);
|
||||
XWPFNumbering numbering = document.createNumbering();
|
||||
BigInteger abstractNumID = numbering.addAbstractNum(abstractNum);
|
||||
this.numId = numbering.addNum(abstractNumID);
|
||||
}*/
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package eu.eudat.utilities.interfaces;
|
||||
|
||||
/**
|
||||
* Created by ikalyvas on 2/27/2018.
|
||||
*/
|
||||
public interface Applier<A, V, R> {
|
||||
R apply(A applier, V value);
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package eu.eudat.utilities;
|
||||
package eu.eudat.utilities.interfaces;
|
||||
|
||||
/**
|
||||
* Created by ikalyvas on 2/5/2018.
|
@ -1,4 +1,4 @@
|
||||
package eu.eudat.utilities;
|
||||
package eu.eudat.utilities.interfaces;
|
||||
|
||||
|
||||
public interface DatabaseDefinition {
|
@ -1,4 +1,4 @@
|
||||
package eu.eudat.utilities;
|
||||
package eu.eudat.utilities.interfaces;
|
||||
|
||||
import eu.eudat.entities.xmlmodels.modeldefinition.DatabaseModelDefinition;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package eu.eudat.utilities;
|
||||
package eu.eudat.utilities.interfaces;
|
||||
|
||||
|
||||
import eu.eudat.entities.xmlmodels.datasetprofiledefinition.DatabaseViewStyleDefinition;
|
@ -1,4 +1,4 @@
|
||||
package eu.eudat.utilities;
|
||||
package eu.eudat.utilities.interfaces;
|
||||
|
||||
|
||||
import eu.eudat.entities.xmlmodels.datasetprofiledefinition.DatabaseViewStyleDefinition;
|
@ -1,4 +1,4 @@
|
||||
package eu.eudat.utilities;
|
||||
package eu.eudat.utilities.interfaces;
|
||||
|
||||
|
||||
import org.w3c.dom.Document;
|
Loading…
Reference in New Issue