view artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flood_duration/FloodDurationExporter.java @ 9176:1614cb14308f

Work on calculations for S-Info flood duration workflow
author mschaefer
date Mon, 25 Jun 2018 19:21:11 +0200
parents 23945061daec
children a4121ec450d6
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * 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.sinfo.flood_duration;

import java.io.OutputStream;

import org.dive4elements.river.artifacts.common.AbstractCalculationExportableResult;
import org.dive4elements.river.artifacts.common.AbstractCommonExporter;
import org.dive4elements.river.artifacts.common.ExportContextCSV;
import org.dive4elements.river.artifacts.common.JasperDesigner;
import org.dive4elements.river.artifacts.common.JasperReporter;
import org.dive4elements.river.artifacts.common.MetaAndTableJRDataSource;
import org.dive4elements.river.artifacts.sinfo.util.RiverInfo;

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

/**
 * Generates different output formats (csv, pdf) of data that resulted from a flow depths min/max computation.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 * @author Gernot Belger
 */
// REMARK: must be public because its registered in generators.xml
public class FloodDurationExporter extends AbstractCommonExporter<FloodDurationCalculationResults> {

    @Override
    protected void doWriteCSVData(final CSVWriter writer, final FloodDurationCalculationResults results) {
        // TODO: Diesen Ablauf in super?

        // TODO: move results into context?
        final ExportContextCSV exportContextCSV = new ExportContextCSV(this.context, writer);

        getLog().info("writeCSVData");

        /* write as csv */
        exportContextCSV.writeCSVGlobalMetadataDefaults(results); // ggf auslagern innerhalb dieser Klasse

        // writer.writeNext(new String[] { "" }); // break line HERE to avoid redundance

        final RiverInfo river = results.getRiver();

        final Class<?> lastResultType = null;

        for (final AbstractCalculationExportableResult<FloodDurationCalculationResults> result : results.getResults()) {

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

            result.writeCsv(exportContextCSV, results);
        }

    }

    @Override
    protected void doWritePdf(final OutputStream out, final FloodDurationCalculationResults results) {

        // TODO: Move to super (hier ist aber spezieller code drin...)
        try {
            final ExportContextCSV exportContextCSV = new ExportContextCSV(this.context, null);

            final JasperReporter reporter = new JasperReporter();

            for (final AbstractCalculationExportableResult<FloodDurationCalculationResults> result : results.getResults()) {
                final MetaAndTableJRDataSource source = new MetaAndTableJRDataSource();
                getHelper().addJRMetaDataUSINFO(source, results);

                final JasperDesigner design = result.addReport(exportContextCSV, results, reporter, source);
                if (result instanceof FloodDurationCalculationResult) {
                    // final int wlCount = ((FloodDurationCalculationResult) result).getWaterlevelCount();
                    // if (wlCount == 0 || wlCount == 2) {
                    design.removeColumn("wOpt");
                    design.removeColumn("qOpt");
                    design.removeColumn("bezOpt");
                    design.removeColumn("durOpt");
                    // }
                }
            }

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

    }
}

http://dive4elements.wald.intevation.org