1
|
/**
|
2
|
* Copyright (C) 2017 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.io.descriptive.word.out;
|
10
|
|
11
|
import java.io.ByteArrayOutputStream;
|
12
|
import java.io.File;
|
13
|
import java.io.InputStream;
|
14
|
|
15
|
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
|
16
|
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
|
17
|
import org.springframework.stereotype.Component;
|
18
|
import org.springframework.transaction.TransactionStatus;
|
19
|
|
20
|
import eu.etaxonomy.cdm.io.common.CdmExportBase;
|
21
|
import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;
|
22
|
import eu.etaxonomy.cdm.model.description.Feature;
|
23
|
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
|
24
|
import eu.etaxonomy.cdm.model.term.TermNode;
|
25
|
import eu.etaxonomy.cdm.model.term.TermTree;
|
26
|
|
27
|
/**
|
28
|
* @author pplitzner
|
29
|
* @since Oct 18, 2018
|
30
|
*/
|
31
|
@Component
|
32
|
public class WordTermExport extends CdmExportBase<WordTermExportConfigurator, WordTermExportState, IExportTransformer, File> {
|
33
|
|
34
|
private static final long serialVersionUID = 3197379920692366008L;
|
35
|
|
36
|
@Override
|
37
|
protected boolean doCheck(WordTermExportState state) {
|
38
|
return false;
|
39
|
}
|
40
|
|
41
|
@Override
|
42
|
protected void doInvoke(WordTermExportState state) {
|
43
|
|
44
|
TransactionStatus txStatus = startTransaction(true);
|
45
|
|
46
|
@SuppressWarnings("unchecked")
|
47
|
TermTree<Feature> featureTree = state.getConfig().getFeatureTree();
|
48
|
featureTree = getTermTreeService().load(featureTree.getUuid());
|
49
|
TermNode<Feature> rootNode = featureTree.getRoot();
|
50
|
|
51
|
try {
|
52
|
exportStream = generateDocx4JDocument(rootNode);
|
53
|
state.getResult().addExportData(getByteArray());
|
54
|
} catch (Exception e) {
|
55
|
e.printStackTrace();
|
56
|
}
|
57
|
|
58
|
commitTransaction(txStatus);
|
59
|
return;
|
60
|
}
|
61
|
|
62
|
private ByteArrayOutputStream generateDocx4JDocument(TermNode<?> rootNode) throws Exception {
|
63
|
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("eu/etaxonomy/cdm/io/word/out/template.docx");
|
64
|
WordprocessingMLPackage wordPackage = WordprocessingMLPackage.load(resourceAsStream);
|
65
|
MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart();
|
66
|
|
67
|
|
68
|
addChildNode(rootNode, mainDocumentPart, 1);
|
69
|
|
70
|
// createTOC(mainDocumentPart);
|
71
|
|
72
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
73
|
wordPackage.save(out);
|
74
|
return out;
|
75
|
}
|
76
|
|
77
|
// public void createTOC(MainDocumentPart mainDocumentPart) throws Exception {
|
78
|
//
|
79
|
// ObjectFactory factory = Context.getWmlObjectFactory();
|
80
|
//
|
81
|
//
|
82
|
// P p = factory.createP();
|
83
|
// R r = factory.createR();
|
84
|
//
|
85
|
// FldChar fldchar = factory.createFldChar();
|
86
|
// fldchar.setFldCharType(org.docx4j.wml.STFldCharType.BEGIN);
|
87
|
// r.getContent().add(getWrappedFldChar(fldchar));
|
88
|
// p.getContent().add(r);
|
89
|
//
|
90
|
// R r1 = factory.createR();
|
91
|
// Text txt = new Text();
|
92
|
// txt.setSpace("preserve");
|
93
|
// txt.setValue("TOC \\o \"1-3\" \\h \\z \\u \\h");
|
94
|
// r.getContent().add(factory.createRInstrText(txt) );
|
95
|
// p.getContent().add(r1);
|
96
|
//
|
97
|
// FldChar fldcharend = factory.createFldChar();
|
98
|
// fldcharend.setFldCharType(org.docx4j.wml.STFldCharType.END);
|
99
|
// R r2 = factory.createR();
|
100
|
// r2.getContent().add(getWrappedFldChar(fldcharend));
|
101
|
// p.getContent().add(r2);
|
102
|
//
|
103
|
// mainDocumentPart.addObject(p);
|
104
|
//
|
105
|
// }
|
106
|
//
|
107
|
// @SuppressWarnings("unchecked")
|
108
|
// public JAXBElement getWrappedFldChar(FldChar fldchar) {
|
109
|
// return new JAXBElement( new QName(Namespaces.NS_WORD12, "fldChar"), org.docx4j.wml.FldChar.class, fldchar);
|
110
|
// }
|
111
|
|
112
|
private void addChildNode(TermNode<?> node, MainDocumentPart mainDocumentPart, int indent) throws Exception{
|
113
|
String styleId = "Heading"+indent;
|
114
|
|
115
|
for (TermNode<?> childNode : node.getChildNodes()) {
|
116
|
DefinedTermBase<?> term = childNode.getTerm();
|
117
|
mainDocumentPart.addStyledParagraphOfText(styleId, term.getLabel());
|
118
|
if(term.getDescription()!=null){
|
119
|
mainDocumentPart.addParagraphOfText("Description: "+term.getDescription());
|
120
|
}
|
121
|
if(term.getUri()!=null){
|
122
|
mainDocumentPart.addParagraphOfText("URI: "+term.getUri().toString());
|
123
|
}
|
124
|
addChildNode(childNode, mainDocumentPart, indent+1);
|
125
|
}
|
126
|
|
127
|
}
|
128
|
|
129
|
@Override
|
130
|
protected boolean isIgnore(WordTermExportState state) {
|
131
|
return false;
|
132
|
}
|
133
|
|
134
|
}
|