view gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/vertical/VerticalProfileOutputState.java @ 1115:f953c9a559d8

Added license file and license headers. gnv-artifacts/trunk@1260 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 02 Nov 2010 17:46:55 +0000
parents 05bf8534a35a
children
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */

package de.intevation.gnv.state.profile.vertical;

import de.intevation.artifacts.CallContext;

import de.intevation.gnv.chart.Chart;
import de.intevation.gnv.chart.ChartLabels;
import de.intevation.gnv.chart.VerticalProfileChart;

import de.intevation.gnv.exports.DefaultDataCollector;
import de.intevation.gnv.exports.DefaultExport;
import de.intevation.gnv.exports.DefaultProfile;

import de.intevation.gnv.exports.Export.Profile;

import de.intevation.gnv.geobackend.base.Result;

import de.intevation.gnv.state.exception.StateException;

import de.intevation.gnv.state.timeseries.TimeSeriesOutputState;

import de.intevation.gnv.statistics.Statistics;
import de.intevation.gnv.statistics.VerticalProfileStatistics;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import java.util.Collection;
import java.util.Iterator;
import java.util.Locale;

import org.apache.log4j.Logger;

import org.jfree.chart.ChartTheme;

/**
 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
 *
 */
public class VerticalProfileOutputState extends TimeSeriesOutputState {

    public static final String [] VERTICAL_PROFILE_COLUMNS = {
        "XORDINATE", // not quite sure if this is depth
        "YORDINATE",
        "GROUP1",
        "FEATUREID",
        "TIMESERIESID"
    };

    public static final String [] VERTICAL_PROFILE_MESH_COLUMNS = {
        "XORDINATE", // not quite sure if this is depth
        "YORDINATE",
        "GROUP1",
        "FEATUREID",
        "MESHID"
    };

    public static final String [] VERTICAL_PROFILE_MEASUREMENT_COLUMNS = {
        "XORDINATE", // not quite sure if this is depth
        "YORDINATE",
        "GROUP1",
        "FEATUREID",
        "SERIESID"
    };


    public static final String [] VERTICAL_MESH_CSV_COLUMN_LABEL = {
        "CentralDepth",
        "Value",
        "ParameterID",
        "FeatureID",
        "MeshID"
    };


    public static final String [] VERTICAL_TIMESERIES_CSV_COLUMN_LABEL = {
        "Depth",
        "Value",
        "ParameterID",
        "FeatureID",
        "TimeseriesID"
    };


    public static final String [] VERTICAL_MEASUREMENT_CSV_COLUMN_LABEL = {
        "Depth",
        "Value",
        "ParameterID",
        "FeatureID",
        "SeriesID"
    };


    /**
     * The UID of this class
     */
    private static final long serialVersionUID = 4401516087492028840L;

    private static Logger log = Logger
            .getLogger(TimeSeriesOutputState.class);

    /**
     * Creates a new VerticalProfileOutputState object.
     */
    public VerticalProfileOutputState() {
        super();
        super.domainLable = "chart.verticalprofile.title.xaxis";
    }


    @Override
    protected Chart getChart(
        ChartLabels chartLables,
        ChartTheme  theme,
        Collection  parameters,
        Collection  measurements,
        Collection  dates,
        Object      result,
        Locale      locale,
        String      uuid,
        boolean     linesVisible,
        boolean     shapesVisible,
        CallContext callContext
    ) {
        Chart chart = null;

        if (CACHE_CHART) {
            log.info("Try to get verticalprofile chart from cache.");
            chart = (Chart) getChartFromCache(uuid, callContext);
        }

        if (chart != null)
            return chart;

        log.info("Chart not in cache yet.");
        chart = new VerticalProfileChart(
            chartLables,
            theme,
            parameters,
            measurements,
            dates,
            (Collection)result,
            null,
            locale,
            linesVisible,
            shapesVisible
        );
        chart.generateChart();

        if (CACHE_CHART) {
            log.info("Put chart into cache.");
            purifyChart(chart, uuid);
        }

        return chart;
    }


    /**
     * Creates a csv file of the resulting data and writes it to output stream.
     *
     * @param out The output stream.
     * @param results The data used to create the csv file.
     * @throws UnsupportedEncodingException if the encoding is not supported.
     * @throws IOException if an error occured while writing to output stream.
     * @throws StateException if an error occured while creating the csv file.
     */
    @Override
    protected void createCSV(OutputStream out, Collection<Result> results)
    throws UnsupportedEncodingException, IOException, StateException
    {
        Iterator<Result> iter = results.iterator();
        Result   res  = iter.hasNext() ? iter.next() : null;

        if (res == null)
            return;

        Profile profile = null;
        int     dataid  = res.getInteger("DATAID").intValue();
        DefaultExport export = null;
        // on meshes
        if (dataid == 2) {
            profile =  new DefaultProfile(
                VERTICAL_MESH_CSV_COLUMN_LABEL,
                ',',
                '"',
                '"',
                "CSV",
                "ISO-8859-1");
            export = new DefaultExport(
                    new DefaultDataCollector(VERTICAL_PROFILE_MESH_COLUMNS));
        }

        // on timeseries
        else if (dataid == 1) {
            profile =  new DefaultProfile(
                VERTICAL_TIMESERIES_CSV_COLUMN_LABEL,
                ',',
                '"',
                '"',
                "CSV",
                "ISO-8859-1");
            export = new DefaultExport(
                    new DefaultDataCollector(VERTICAL_PROFILE_COLUMNS));
        }

        // on measurements
        else {
            profile =  new DefaultProfile(
                VERTICAL_MEASUREMENT_CSV_COLUMN_LABEL,
                ',',
                '"',
                '"',
                "CSV",
                "ISO-8859-1");
            export = new DefaultExport(
                    new DefaultDataCollector(VERTICAL_PROFILE_MEASUREMENT_COLUMNS));
        }
        export.create(profile, out, results);
    }


    @Override
    protected String createChartSubtitle(Locale locale, String uuid) {
        return getSelectedFeatureName(uuid);
    }


    @Override
    protected Statistics getStatisticsGenerator() {
        return new VerticalProfileStatistics();
    }

}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org