Project

General

Profile

Download (4.49 KB) Statistics
| Branch: | Tag: | Revision:
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.common.DefinedTermBase;
23
import eu.etaxonomy.cdm.model.description.FeatureNode;
24
import eu.etaxonomy.cdm.model.description.FeatureTree;
25

    
26
/**
27
 *
28
 * @author pplitzner
29
 * @since Oct 18, 2018
30
 *
31
 */
32
@Component
33
public class WordExport extends CdmExportBase<WordExportConfigurator, WordExportState, IExportTransformer, File> {
34

    
35
    private static final long serialVersionUID = 3197379920692366008L;
36

    
37
    @Override
38
    protected boolean doCheck(WordExportState state) {
39
        return false;
40
    }
41

    
42
    @Override
43
    protected void doInvoke(WordExportState state) {
44

    
45
        TransactionStatus txStatus = startTransaction(true);
46

    
47
        FeatureTree featureTree = state.getConfig().getFeatureTree();
48
        featureTree = getFeatureTreeService().load(featureTree.getUuid());
49
        FeatureNode 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

    
60
        return;
61
    }
62

    
63
    private ByteArrayOutputStream generateDocx4JDocument(FeatureNode rootNode) throws Exception {
64
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("eu/etaxonomy/cdm/io/word/out/template.docx");
65
        WordprocessingMLPackage wordPackage = WordprocessingMLPackage.load(resourceAsStream);
66
        MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart();
67

    
68

    
69
        addChildNode(rootNode, mainDocumentPart, 1);
70

    
71
//        createTOC(mainDocumentPart);
72

    
73
        ByteArrayOutputStream out = new ByteArrayOutputStream();
74
        wordPackage.save(out);
75
        return out;
76
    }
77

    
78
//    public void createTOC(MainDocumentPart mainDocumentPart) throws Exception {
79
//
80
//        ObjectFactory factory = Context.getWmlObjectFactory();
81
//
82
//
83
//        P p = factory.createP();
84
//        R r = factory.createR();
85
//
86
//        FldChar fldchar = factory.createFldChar();
87
//        fldchar.setFldCharType(org.docx4j.wml.STFldCharType.BEGIN);
88
//        r.getContent().add(getWrappedFldChar(fldchar));
89
//        p.getContent().add(r);
90
//
91
//        R r1 = factory.createR();
92
//        Text txt = new Text();
93
//        txt.setSpace("preserve");
94
//        txt.setValue("TOC \\o \"1-3\" \\h \\z \\u \\h");
95
//        r.getContent().add(factory.createRInstrText(txt) );
96
//        p.getContent().add(r1);
97
//
98
//        FldChar fldcharend = factory.createFldChar();
99
//        fldcharend.setFldCharType(org.docx4j.wml.STFldCharType.END);
100
//        R r2 = factory.createR();
101
//        r2.getContent().add(getWrappedFldChar(fldcharend));
102
//        p.getContent().add(r2);
103
//
104
//        mainDocumentPart.addObject(p);
105
//
106
//    }
107
//
108
//    @SuppressWarnings("unchecked")
109
//    public JAXBElement getWrappedFldChar(FldChar fldchar) {
110
//        return new JAXBElement( new QName(Namespaces.NS_WORD12, "fldChar"), org.docx4j.wml.FldChar.class, fldchar);
111
//    }
112

    
113
    private void addChildNode(FeatureNode node, MainDocumentPart mainDocumentPart, int indent) throws Exception{
114
        String styleId = "Heading"+indent;
115

    
116
        for (FeatureNode childNode : node.getChildNodes()) {
117
            DefinedTermBase term = childNode.getTerm();
118
            mainDocumentPart.addStyledParagraphOfText(styleId, term.getLabel());
119
            if(term.getDescription()!=null){
120
                mainDocumentPart.addParagraphOfText("Description: "+term.getDescription());
121
            }
122
            if(term.getUri()!=null){
123
                mainDocumentPart.addParagraphOfText("URI: "+term.getUri().toString());
124
            }
125
            addChildNode(childNode, mainDocumentPart, indent+1);
126
        }
127

    
128
    }
129

    
130
    @Override
131
    protected boolean isIgnore(WordExportState state) {
132
        return false;
133
    }
134

    
135
}
(1-1/3)