view artifacts/src/main/java/org/dive4elements/river/artifacts/common/AbstractCommonExporter.java @ 9425:3f49835a00c3

Extended CrossSectionFacet so it may fetch different data from within the artifact result. Also allows to have acces to the potentially already computed artifact result via its normal computation cache.
author gernotbelger
date Fri, 17 Aug 2018 15:31:02 +0200
parents 217e8e59c386
children 5f81d3f7b82b
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();

        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[] { "#" });

            lastResultType = resultType;
            result.writeCsv(exportContext);
        }
    }

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

    @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