OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 /*
 * Créé le 15 nov. 2004
 */
package org.openconcerto.odtemplate;

import org.openconcerto.odtemplate.engine.OGNLDataModel;
import org.openconcerto.openoffice.ODPackage;
import org.openconcerto.openoffice.ODSingleXMLDocument;
import org.openconcerto.openoffice.generation.DocumentGenerator;
import org.openconcerto.openoffice.generation.ReportGeneration;
import org.openconcerto.utils.ExceptionUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Un générateur se servant de d'une template. In this implementation only
 * {@link ReportGeneration#getCommonData()} is provided to the template, subclass {@link #getData()}
 * to add other objects.
 * 
 * @author Sylvain CUAZ
 * @param <R> type of generation
 */
public class TemplateGenerator<R extends ReportGeneration<?>> extends DocumentGenerator<R> {

    private final File file;
    private final ODPackage pkg;

    public TemplateGenerator(final R rg, final File f) {
        this(rg, f, null);
    }

    public TemplateGenerator(final R rg, final ODPackage pkg) {
        this(rg, null, pkg);
    }

    private TemplateGenerator(final R rg, final File f, final ODPackage pkg) {
        super(rg);
        if ((f == null) == (pkg == null))
            throw new IllegalArgumentException();
        this.file = f;
        this.pkg = pkg;
    }

    @Override
    public final ODSingleXMLDocument generate() throws IOException, InterruptedException {
        return this.substitute(this.getAllData());
    }

    protected final Map<String, Object> getAllData() throws IOException, InterruptedException {
        // ce qui y est toujours
        final Map<String, Object> res = new HashMap<String, Object>(this.getRg().getCommonData());
        // plus les données de ce generateur en particulier
        res.putAll(this.getData());
        return res;
    }

    protected Map<String, Object> getData() throws InterruptedException {
        return new HashMap<String, Object>();
    }

    private final ODSingleXMLDocument substitute(Map<String, Object> data) throws FileNotFoundException, IOException {
        final ODPackage pkg = this.pkg == null ? new ODPackage(this.file) : this.pkg;
        try {
            this.transform(pkg.toSingle());
            // MAYBE fireStatusChange with the number of tag done out of the total
            try (final Template template = new Template(pkg)) {
                return template.createDocument(new OGNLDataModel(data));
            }
        } catch (Exception exn) {
            throw ExceptionUtils.createExn(IOException.class, "generation error in " + this, exn);
        }
    }

    protected void transform(ODSingleXMLDocument single) throws Exception {
    }

    @Override
    public String toString() {
        return this.getClass() + (this.file != null ? (" with file " + this.file) : (" with package " + this.pkg));
    }

}