view gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/vertical/VerticalProfileOutputState.java @ 439:8975de9d7483

Loop through configuration to chart generation. gnv-artifacts/trunk@487 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 28 Dec 2009 16:24:05 +0000
parents bed9735adf84
children b3f922908a31
line wrap: on
line source
/**
 *
 */
package de.intevation.gnv.state.profile.vertical;

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;

import de.intevation.gnv.chart.Chart;
import de.intevation.gnv.chart.ChartLabels;
import de.intevation.gnv.chart.VerticalProfileChart;
import de.intevation.gnv.exports.DefaultExport;
import de.intevation.gnv.exports.DefaultDataCollector;
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 de.intevation.artifacts.CallContext;

/**
 * @author Tim Englich <tim.englich@intevation.de>
 * 
 */
public class VerticalProfileOutputState extends TimeSeriesOutputState {

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


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


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


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


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

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

    /**
     * Constructor
     */
    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;
    }


    @Override
    protected void createCSV(OutputStream out, Collection<Result> results)
    throws UnsupportedEncodingException, IOException, StateException
    {
        Iterator iter = results.iterator();
        Result   res  = iter.hasNext() ? (Result) iter.next() : null;

        if (res == null)
            return;

        Profile profile = null;
        int     dataid  = res.getInteger("DATAID").intValue();

        // on meshes
        if (dataid == 2) {
            profile =  new DefaultProfile(
                VERTICAL_MESH_CSV_COLUMN_LABEL,
                ',',
                '"',
                '"',
                "CSV",
                "ISO-8859-1");
        }

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

        // on measurements
        else {
            profile =  new DefaultProfile(
                VERTICAL_MEASUREMENT_CSV_COLUMN_LABEL,
                ',',
                '"',
                '"',
                "CSV",
                "ISO-8859-1");
        }

        DefaultExport export = new DefaultExport(
            new DefaultDataCollector(VERTICAL_PROFILE_COLUMNS));
        export.create(profile, out, results);
    }


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



    /**
     * @see de.intevation.gnv.state.timeseries.TimeSeriesOutputState#getStatisticsGenerator()
     */
    @Override
    protected Statistics getStatisticsGenerator() {
        return new VerticalProfileStatistics();
    }

}

http://dive4elements.wald.intevation.org