view artifacts/src/main/java/org/dive4elements/river/artifacts/common/AbstractCommonListExporter.java @ 9150:23945061daec

gigantic refactoring: exporter, result, results to support multiple jaspers -> collisions
author gernotbelger
date Thu, 14 Jun 2018 16:56:31 +0200
parents artifacts/src/main/java/org/dive4elements/river/artifacts/common/AbstractCommonExporter.java@460fcc128794
children
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.util.Collection;

import org.dive4elements.river.artifacts.sinfo.util.RiverInfo;

import au.com.bytecode.opencsv.CSVWriter;

/**
 * @author Gernot Belger
 */
public abstract class AbstractCommonListExporter<RESULT extends AbstractCalculationResult, RESULTS extends AbstractCalculationListResults<RESULT>> extends AbstractCommonExporter<RESULTS> {

    /** The storage that contains the current calculation result. */
    protected static enum ExportMode {
        pdf, csv
    }

    @Override
    protected void doWriteCSVData(final CSVWriter writer, final RESULTS results) {

        getLog().info("writeCSVData");

        /* write as csv */
        writeCSVGlobalMetadata(writer, results);

        writer.writeNext(new String[] { "" }); // break line HERE to avoid redundance
        final RiverInfo river = results.getRiver();
        // FIXME :with comment if not first result
        writeCSVHeader(writer, results, river);
        writer.writeNext(new String[] { "" }); // break line HERE to avoid redundance

        for (final RESULT result : results.getResults()) {
            writeCSVResult(writer, results, result);
            writer.writeNext(new String[] { "" }); // break line HERE after each resultset
        }
    }

    protected abstract void writeCSVHeader(final CSVWriter writer, final RESULTS results, final RiverInfo river);

    /**
     * Add metadata that is once written to the top of the file.
     */
    protected abstract void writeCSVGlobalMetadata(final CSVWriter writer, final RESULTS results);

    private final void writeCSVResult(final CSVWriter writer, final RESULTS results, final RESULT result) {

        writeCSVResultMetadata(writer, results, result);
        // wenn resultsmetadata null sind!? keine neue zeile
        // writer.writeNext(new String[] { "" }); // break line in den Implementationen,
        // weil es sein kann, dass KEINE ResultMetadata geschrieben werden; dann wäre eine Leerzeile überflüssig

        /* now the value rows */
        final Collection<ResultRow> rows = result.getRows();
        for (final ResultRow row : rows)
            writeCSVRow(writer, results, result, row);
    }

    /**
     * Add metadata that is written once per result set.
     */
    protected abstract void writeCSVResultMetadata(CSVWriter writer, RESULTS results, RESULT result);

    protected void writeCSVRow(final CSVWriter writer, final RESULTS results, final RESULT result, final ResultRow row) {
        getLog().debug("writeCSVFlowDepthRow");

        final String[] formattedRow = formatCSVRow(results, row);
        writer.writeNext(formattedRow);
    }

    protected final String[] formatCSVRow(final RESULTS results, final ResultRow row) {
        return formatRow(results, row, ExportMode.csv);
    }

    protected final MetaAndTableJRDataSource createJRData() {

        /* fetch calculation results */
        final RESULTS results = getResults();

        final MetaAndTableJRDataSource source = new MetaAndTableJRDataSource();

        addJRMetaData(source, results);

        for (final RESULT result : results.getResults())
            addJRTableData(source, results, result);

        return source;
    }

    protected abstract void addJRMetaData(final MetaAndTableJRDataSource source, final RESULTS results);

    private void addJRTableData(final MetaAndTableJRDataSource source, final RESULTS results, final RESULT result) {

        final Collection<ResultRow> rows = result.getRows();

        for (final ResultRow row : rows) {

            final String[] formattedRow = formatPDFRow(results, row);
            source.addData(formattedRow);
        }
    }

    protected abstract String[] formatRow(RESULTS results, ResultRow row, ExportMode mode);

    private final String[] formatPDFRow(final RESULTS results, final ResultRow row) {
        return formatRow(results, row, ExportMode.pdf);
    }
}

http://dive4elements.wald.intevation.org