view artifacts/src/main/java/org/dive4elements/river/exports/minfo/BedDifferenceExporter.java @ 7691:fa4fbd66e752

(issue1579) Fix axes syncronisation at Gauges The SyncNumberAxis was completely broken. It only synced in one direction and even that did not work correctly when data was added to the axis (and the syncAxis rescaled but forgot the old axis) then there were lots of ways to bypass that scaling. And i also think the trans calculation was wrong. It has been replaced by a "mostly" simple method to just keep the W in M and W in CM+Datum axes in sync. I say "Mostly" because it had to deal with the Bounds interface.
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 13 Dec 2013 19:03:00 +0100
parents e79e231c864d
children b4f806999173
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.exports.minfo;

import java.io.IOException;
import java.io.OutputStream;
import java.text.NumberFormat;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Locale;
import java.util.Date;
import java.text.DateFormat;

import org.apache.log4j.Logger;
import org.apache.commons.lang.StringUtils;

import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JRException;

import org.dive4elements.artifacts.CallMeta;
import org.dive4elements.river.artifacts.D4EArtifact;
import org.dive4elements.river.artifacts.access.BedDifferencesAccess;
import org.dive4elements.artifacts.common.utils.Config;
import org.dive4elements.river.artifacts.resources.Resources;
import org.dive4elements.river.artifacts.model.CalculationResult;
import org.dive4elements.river.artifacts.model.minfo.BedDifferencesResult;
import org.dive4elements.river.artifacts.model.minfo.BedDifferenceJRDataSource;
import org.dive4elements.river.exports.AbstractExporter;
import org.dive4elements.river.utils.Formatter;
import org.dive4elements.river.utils.RiverUtils;

import au.com.bytecode.opencsv.CSVWriter;

public class BedDifferenceExporter
extends AbstractExporter
{

    /** Private logger. */
    private static Logger logger =
        Logger.getLogger(BedDifferenceExporter.class);

    private static final String CSV_HEADER_KM =
        "export.minfo.beddifference.km";

    private static final String CSV_HEADER_DIFF =
        "export.minfo.beddifference.diff";

    public static final String JASPER_FILE =
        "export.minfo.beddifference.pdf.file";

    public static final String PDF_TITLE=
        "export.minfo.beddifference.pdf.title";

    public static final String PDF_HEADER_MODE=
        "export.minfo.beddifference.pdf.mode";

    private BedDifferencesResult[] results;

    public BedDifferenceExporter() {
        results = new BedDifferencesResult[0];
    }

    @Override
    protected void writeCSVData(CSVWriter writer) throws IOException {
        writeCSVHeader(writer);

        NumberFormat kmf = Formatter.getCalculationKm(context.getMeta());
        NumberFormat mf = Formatter.getMeterFormat(context);
        for (BedDifferencesResult result : results) {
            double[][] kms = result.getDifferencesData();
            for (int j = 0; j < kms[0].length; j++) {
                writer.writeNext(new String[] {
                    kmf.format(kms[0][j]), mf.format(kms[1][j])});
            }
        }
    }

    @Override
    protected void addData(Object data) {
        if (!(data instanceof CalculationResult)) {
            logger.warn("Invalid data type.");
            return;
        }
        Object[] d = (Object[])((CalculationResult)data).getData();

        if (!(d instanceof BedDifferencesResult[])) {
            logger.warn("Invalid result object.");
            return;
        }
        results = (BedDifferencesResult[])d;
    }

    protected void addMetaData(BedDifferenceJRDataSource source) {
        CallMeta meta = context.getMeta();

        D4EArtifact arti = (D4EArtifact) master;

        source.addMetaData ("river", RiverUtils.getRivername(arti));

        Locale locale = Resources.getLocale(meta);
        DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);

        source.addMetaData("date", df.format(new Date()));

        source.addMetaData("calculation", Resources.getMsg(
                                            locale,
                                            PDF_HEADER_MODE,
                                            "Bedheight difference"));

        // Now the dynamic parts

        BedDifferencesAccess access = new BedDifferencesAccess(arti);
        source.addMetaData("ye_mode", Resources.getMsg(locale,
                    "state.minfo." + access.getYearEpoch(),
                    access.getYearEpoch()));

        source.addMetaData("differences", StringUtils.join(
                access.getDifferenceArtifactNamePairs(), "\n"));
    }


    protected void writeCSVHeader(CSVWriter writer) {
        logger.debug("writeCSVHeader()");

        List<String> header = new LinkedList<String>();
        if (results != null)  {
            header.add(msg(CSV_HEADER_KM, "km"));
            header.add(msg(CSV_HEADER_DIFF, "m"));
        }
        writer.writeNext(header.toArray(new String[header.size()]));
    }

    protected BedDifferenceJRDataSource createJRData() {
        BedDifferenceJRDataSource source = new BedDifferenceJRDataSource();

        addMetaData(source);
        NumberFormat kmf = Formatter.getCalculationKm(context.getMeta());
        NumberFormat mf = Formatter.getMeterFormat(context);
        for (BedDifferencesResult result: results) {
            double[][] kms = result.getDifferencesData();
            for (int j = 0; j < kms[0].length; j++) {
                source.addData(new String[] {
                    kmf.format(kms[0][j]), mf.format(kms[1][j])});
            }
        }
        return source;
    }

    @Override
    protected void writePDF(OutputStream out) {
        logger.debug("write PDF");
        BedDifferenceJRDataSource source = createJRData();

        String jasperFile = Resources.getMsg(
                                context.getMeta(),
                                JASPER_FILE,
                                "/jasper/beddifference_en.jasper");
        String confPath = Config.getConfigDirectory().toString();


        Map parameters = new HashMap();
        parameters.put("ReportTitle", Resources.getMsg(
                    context.getMeta(), PDF_TITLE, "Exported Data"));
        try {
            JasperPrint print = JasperFillManager.fillReport(
                confPath + jasperFile,
                parameters,
                source);
            JasperExportManager.exportReportToPdfStream(print, out);
        }
        catch(JRException je) {
            logger.warn("Error generating PDF Report!", je);
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org