OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 132 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 /*
15
 * Créé le 15 nov. 2004
16
 */
17
package org.openconcerto.odtemplate;
18
 
19
import org.openconcerto.odtemplate.engine.OGNLDataModel;
20
import org.openconcerto.openoffice.ODPackage;
21
import org.openconcerto.openoffice.ODSingleXMLDocument;
22
import org.openconcerto.openoffice.generation.DocumentGenerator;
23
import org.openconcerto.openoffice.generation.ReportGeneration;
24
import org.openconcerto.utils.ExceptionUtils;
25
 
26
import java.io.File;
27
import java.io.FileNotFoundException;
28
import java.io.IOException;
29
import java.util.HashMap;
30
import java.util.Map;
31
 
32
/**
33
 * Un générateur se servant de d'une template. In this implementation only
34
 * {@link ReportGeneration#getCommonData()} is provided to the template, subclass {@link #getData()}
35
 * to add other objects.
36
 *
37
 * @author Sylvain CUAZ
38
 * @param <R> type of generation
39
 */
40
public class TemplateGenerator<R extends ReportGeneration<?>> extends DocumentGenerator<R> {
41
 
42
    private final File file;
132 ilm 43
    private final ODPackage pkg;
17 ilm 44
 
45
    public TemplateGenerator(final R rg, final File f) {
132 ilm 46
        this(rg, f, null);
47
    }
48
 
49
    public TemplateGenerator(final R rg, final ODPackage pkg) {
50
        this(rg, null, pkg);
51
    }
52
 
53
    private TemplateGenerator(final R rg, final File f, final ODPackage pkg) {
17 ilm 54
        super(rg);
132 ilm 55
        if ((f == null) == (pkg == null))
56
            throw new IllegalArgumentException();
17 ilm 57
        this.file = f;
132 ilm 58
        this.pkg = pkg;
17 ilm 59
    }
60
 
132 ilm 61
    @Override
17 ilm 62
    public final ODSingleXMLDocument generate() throws IOException, InterruptedException {
63
        return this.substitute(this.getAllData());
64
    }
65
 
66
    protected final Map<String, Object> getAllData() throws IOException, InterruptedException {
67
        // ce qui y est toujours
68
        final Map<String, Object> res = new HashMap<String, Object>(this.getRg().getCommonData());
69
        // plus les données de ce generateur en particulier
70
        res.putAll(this.getData());
71
        return res;
72
    }
73
 
74
    protected Map<String, Object> getData() throws InterruptedException {
75
        return new HashMap<String, Object>();
76
    }
77
 
132 ilm 78
    private final ODSingleXMLDocument substitute(Map<String, Object> data) throws FileNotFoundException, IOException {
79
        final ODPackage pkg = this.pkg == null ? new ODPackage(this.file) : this.pkg;
17 ilm 80
        try {
81
            this.transform(pkg.toSingle());
82
            // MAYBE fireStatusChange with the number of tag done out of the total
180 ilm 83
            try (final Template template = new Template(pkg)) {
84
                return template.createDocument(new OGNLDataModel(data));
85
            }
17 ilm 86
        } catch (Exception exn) {
87
            throw ExceptionUtils.createExn(IOException.class, "generation error in " + this, exn);
88
        }
89
    }
90
 
91
    protected void transform(ODSingleXMLDocument single) throws Exception {
92
    }
93
 
132 ilm 94
    @Override
17 ilm 95
    public String toString() {
132 ilm 96
        return this.getClass() + (this.file != null ? (" with file " + this.file) : (" with package " + this.pkg));
17 ilm 97
    }
98
 
99
}