view artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhstate/TkhCalculationResult.java @ 9195:a4121ec450d6

'ca.'-issue ExportContextCSV+PDF separated uinfo.inundationduration url export
author gernotbelger
date Fri, 29 Jun 2018 14:52:54 +0200
parents 9b2e46090099
children 740d65e4aa14
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.sinfo.tkhstate;

import java.util.ArrayList;
import java.util.Collection;

import org.dive4elements.river.artifacts.common.ExportContextCSV;
import org.dive4elements.river.artifacts.common.ExportContextPDF;
import org.dive4elements.river.artifacts.common.GeneralResultType;
import org.dive4elements.river.artifacts.common.IExportContext;
import org.dive4elements.river.artifacts.common.MetaAndTableJRDataSource;
import org.dive4elements.river.artifacts.common.ResultRow;
import org.dive4elements.river.artifacts.sinfo.common.AbstractTkhCalculationResult;
import org.dive4elements.river.artifacts.sinfo.common.SInfoResultType;
import org.dive4elements.river.artifacts.sinfo.util.RiverInfo;
import org.dive4elements.river.artifacts.sinfo.util.WstInfo;

/**
 * Contains the results of a {@link FloodDurationCalculation}.
 *
 * @author Gernot Belger
 */
final class TkhCalculationResult extends AbstractTkhCalculationResult {

    private static final long serialVersionUID = 1L;
    private static final String JASPER_FILE = "/jasper/templates/sinfo.tkh.jrxml";

    private enum ExportMode {
        pdf, csv
    }

    public TkhCalculationResult(final String label, final WstInfo wst, final boolean hasTkh, final Collection<ResultRow> rows) {
        super(label, wst, hasTkh, rows);

    }

    @Override
    public void writeCSVHeader(final ExportContextCSV exportContext, final RiverInfo river) {

        final Collection<String> header = new ArrayList<>(11);

        header.add(exportContext.formatCsvHeader(GeneralResultType.station));
        header.add(exportContext.msgUnitCSV(SInfoResultType.tkh));
        header.add(exportContext.formatCsvHeader(SInfoResultType.soilkind));
        header.add(exportContext.msgUnitCSV(SInfoResultType.meanBedHeight, river.getWstUnit()));
        header.add(exportContext.msgUnitCSV(SInfoResultType.waterlevel, river.getWstUnit()));
        header.add(exportContext.msgUnitCSV(SInfoResultType.discharge));

        final TkhCalculationResults results = exportContext.getResults();
        final String descriptionHeader = results.getDescriptionHeader();
        if (descriptionHeader != null)
            header.add(descriptionHeader);

        header.add(exportContext.formatCsvHeader(SInfoResultType.gaugeLabel));
        header.add(exportContext.formatCsvHeader(SInfoResultType.location));

        exportContext.writeCSVLine(header.toArray(new String[header.size()]));

    }

    @Override
    protected void writeCSVResultMetadata(final ExportContextCSV exportContextCSV) {
        exportContextCSV.writeCSVWaterlevelMetadata(super.getWst());// TODO: move to super
        exportContextCSV.writeBlankLine();
    }

    @Override
    protected String[] formatCSVRow(final ExportContextCSV exportContextCSV, final ResultRow row) {

        return this.formatRow(exportContextCSV, row, ExportMode.csv);
    }

    @Override
    protected String[] formatPDFRow(final ExportContextPDF exportContext, final ResultRow row) {

        return this.formatRow(exportContext, row, ExportMode.pdf);
    }

    @Override
    protected String getJasperFile() {
        return JASPER_FILE;
    }

    @Override
    protected void addJRTableHeader(final ExportContextPDF exportContext, final MetaAndTableJRDataSource source) {
        /* column headings */
        exportContext.addJRMetadata(source, "station_header", GeneralResultType.station);
        exportContext.addJRMetadata(source, "tkh_header", SInfoResultType.tkh);
        exportContext.addJRMetadata(source, "tkhkind_header", SInfoResultType.soilkind);
        exportContext.addJRMetadata(source, "bedheight_header", SInfoResultType.meanBedHeight);
        exportContext.addJRMetadata(source, "waterlevel_header", SInfoResultType.waterlevel);
        exportContext.addJRMetadata(source, "discharge_header", SInfoResultType.discharge);

        // REMARK: actually the column makes no sense if description header is null. But (software symmetry...) WINFO also
        // writes an empty column into the pdf in that case (most probably to avoid the need for two jasper templates).

        final TkhCalculationResults results = exportContext.getResults();
        final String descriptionHeader = results.getDescriptionHeader();

        final String waterlevelNameHeader = descriptionHeader == null ? exportContext.msgPdf(SInfoResultType.waterlevelLabel) : descriptionHeader;
        exportContext.addJRMetadata(source, "waterlevel_name_header", waterlevelNameHeader);

        exportContext.addJRMetadata(source, "gauge_header", SInfoResultType.gaugeLabel);
        exportContext.addJRMetadata(source, "location_header", SInfoResultType.location);

    }

    private String[] formatRow(final IExportContext exportContext, final ResultRow row, final ExportMode mode) {

        final Collection<String> lines = new ArrayList<>(11);

        lines.add(exportContext.formatRowValue(row, GeneralResultType.station));
        lines.add(exportContext.formatRowValue(row, SInfoResultType.tkh));
        lines.add(exportContext.formatRowValue(row, SInfoResultType.soilkind));
        lines.add(exportContext.formatRowValue(row, SInfoResultType.meanBedHeight));
        lines.add(exportContext.formatRowValue(row, SInfoResultType.waterlevel));
        lines.add(exportContext.formatRowValue(row, SInfoResultType.discharge));

        // REMARK: always export this column in pdf-mode, because WInfo also does it (no need for two jasper-templates).
        final TkhCalculationResults results = exportContext.getResults();
        final String descriptionHeader = results.getDescriptionHeader();
        if (descriptionHeader != null || mode == ExportMode.pdf)
            lines.add(exportContext.formatRowValue(row, SInfoResultType.waterlevelLabel));

        lines.add(exportContext.formatRowValue(row, SInfoResultType.gaugeLabel));
        lines.add(exportContext.formatRowValue(row, SInfoResultType.location));

        return lines.toArray(new String[lines.size()]);
    }
}

http://dive4elements.wald.intevation.org