view artifacts/src/main/java/org/dive4elements/river/artifacts/common/AbstractCommonExporter.java @ 9195:a4121ec450d6

'ca.'-issue ExportContextCSV+PDF separated uinfo.inundationduration url export
author gernotbelger
date Fri, 29 Jun 2018 14:52:54 +0200
parents bb929b444ea5
children 6393e05a9610
line wrap: on
line source
/** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
 * Software engineering by
 *  Björnsen Beratende Ingenieure GmbH
 *  Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */
package org.dive4elements.river.artifacts.common;

import java.io.OutputStream;

import org.apache.log4j.Logger;
import org.dive4elements.artifacts.CallContext;
import org.dive4elements.river.artifacts.model.CalculationResult;
import org.dive4elements.river.artifacts.sinfo.util.RiverInfo;
import org.dive4elements.river.exports.AbstractExporter;
import org.w3c.dom.Document;

import au.com.bytecode.opencsv.CSVWriter;
import net.sf.jasperreports.engine.JRException;

/**
 * @author Gernot Belger
 */
public abstract class AbstractCommonExporter<RESULT extends AbstractCalculationExportableResult, RESULTS extends AbstractCalculationResults<RESULT>> extends AbstractExporter {

    /** The log used in this exporter. */
    private final Logger log = Logger.getLogger(getClass());

    /** The log used in this exporter. */
    protected final Logger getLog() {
        return this.log;
    }

    private RESULTS results = null;

    @Override
    public void init(final String outName, final Document request, final OutputStream out, final CallContext context) {
        super.init(outName, request, out, context);
    }

    @Override
    protected final void addData(final Object d) {
        /* reset */
        this.results = null;

        if (d instanceof CalculationResult) {

            final Object dat = ((CalculationResult) d).getData();
            if (dat != null) {
                @SuppressWarnings("unchecked")
                final RESULTS result = (RESULTS) dat;
                this.results = result;
            }
        }
    }

    protected final RESULTS getResults() {
        return this.results;
    }

    @Override
    protected final void writeCSVData(final CSVWriter writer) {

        final ExportContextCSV exportContext = new ExportContextCSV(this.context, writer, this.results);

        doWriteCSVData(exportContext, this.results);
    }

    protected final void doWriteCSVData(final ExportContextCSV exportContext, final RESULTS results) {

        writeCSVGlobalMetadata(exportContext, results);

        final RiverInfo river = results.getRiver();

        final Class<?> lastResultType = null;

        for (final AbstractCalculationExportableResult result : results.getResults()) {

            final Class<?> resultType = result.getClass();
            if (lastResultType == null || lastResultType != resultType) {
                exportContext.writeBlankLine();
                result.writeCSVHeader(exportContext, river);
                exportContext.writeBlankLine();
            } else
                exportContext.writeCSVLine(new String[] { "#" });

            result.writeCsv(exportContext);
        }
    }

    protected abstract void writeCSVGlobalMetadata(final ExportContextCSV exportContext, final RESULTS results);

    /**
     * Formats header with unit and label: msg [unit] (label)
     */
    // TODO: REMOVE, because it has moved to ExportContextCSV
    protected final String msgUnitLabel(final String key, final String unit, final String label) {
        final String msg = msg(key);
        return String.format("%s [%s] (%s)", msg, unit, label);
    }

    @Override
    protected final void writePDF(final OutputStream out) {
        doWritePdf(out, this.results);
    }

    private final void doWritePdf(final OutputStream out, final RESULTS results) {

        try {
            final ExportContextPDF exportContext = new ExportContextPDF(this.context, results);

            final JasperReporter reporter = new JasperReporter();

            for (final RESULT result : results.getResults()) {

                final MetaAndTableJRDataSource source = new MetaAndTableJRDataSource();

                writePDFGlobalMetadata(exportContext, source);

                final JasperDesigner design = result.addReport(exportContext, reporter, source);
                configureDesign(result, design);
            }

            reporter.exportPDF(out);
        }
        catch (final JRException je) {
            getLog().warn("Error generating PDF Report!", je);
        }
    }

    /**
     * Override to implement, does nothing by default.
     */
    protected void configureDesign(final RESULT result, final JasperDesigner design) {
    }

    protected abstract void writePDFGlobalMetadata(ExportContextPDF exportContext, MetaAndTableJRDataSource source);
}

http://dive4elements.wald.intevation.org