OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 132 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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