view gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java @ 231:74255064a4c5

Added the Region-Filter to the Workflow of the Artifacttescases to be able to test it automatically. Checkin laer because of trouble with checksum of these file gnv-artifacts/trunk@299 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Fri, 06 Nov 2009 11:12:45 +0000
parents f68ffbe974a0
children 3d6d89bcbf42
line wrap: on
line source
/**
 *
 */
package de.intevation.gnv.transition.timeseries;

import java.awt.Color;
import java.awt.Dimension;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import au.com.bytecode.opencsv.CSVWriter;
import de.intevation.artifactdatabase.Config;
import de.intevation.artifactdatabase.XMLUtils;
import de.intevation.artifacts.CallMeta;
import de.intevation.artifacts.PreferredLocale;
import de.intevation.gnv.artifacts.ressource.RessourceFactory;
import de.intevation.gnv.chart.ChartFactory;
import de.intevation.gnv.chart.ChartLabels;
import de.intevation.gnv.chart.ChartStyle;
import de.intevation.gnv.chart.exception.TechnicalChartException;
import de.intevation.gnv.geobackend.base.Result;
import de.intevation.gnv.statistics.Statistic;
import de.intevation.gnv.statistics.Statistics;
import de.intevation.gnv.statistics.TimeseriesStatistics;
import de.intevation.gnv.statistics.exception.StatisticsException;
import de.intevation.gnv.timeseries.gap.DefaultTimeGap;
import de.intevation.gnv.timeseries.gap.TimeGap;
import de.intevation.gnv.transition.InputData;
import de.intevation.gnv.transition.OutputTransitionBase;
import de.intevation.gnv.transition.describedata.KeyValueDescibeData;
import de.intevation.gnv.transition.describedata.NamedCollection;
import de.intevation.gnv.transition.exception.TransitionException;

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

import de.intevation.gnv.utils.ArtifactXMLUtilities;


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

    /**
     * The UID of this Class
     */
    private static final long serialVersionUID = 4178407570503098858L;

    /**
     * the logger, used to log exceptions and additonaly information
     */
    private static Logger log = Logger
            .getLogger(TimeSeriesOutputTransition.class);
    
    
    private static List<TimeGap> timeGapDefinitions = null;

    protected String domainLable = "Zeit [UTC]";

    protected String featureValuesName = "featureid";
    protected String parameterValuesName = "parameterid";
    protected String measuremenValueName = "measurementid";
    protected String dateValueName = "dateid";
    
    public static final String [] TIMESERIES_CSV_PROFILE_NAMES = {
        "XORDINATE",
        "YORDINATE",
        "GROUP1",
        "GROUP2",
        "GROUP3"
    };

    /**
     * Profile for exporting data to cvs
     */
    public static final Profile TIMESERIES_CSV_PROFILE =
        new DefaultProfile(
            TIMESERIES_CSV_PROFILE_NAMES,
            ',',
            '"',
            '"',
            "CSV",
            "ISO-8859-1");

    /**
     * Profile for exporting data to odv
     * TODO Change TIMESERIES_PROFILE_NAMES, which belong to CSV exports
     */
    public static final Profile TIMESERIES_ODV_PROFILE =
        new DefaultProfile(
            TIMESERIES_CSV_PROFILE_NAMES,
            '\t',
            CSVWriter.NO_QUOTE_CHARACTER,
            CSVWriter.NO_ESCAPE_CHARACTER,
            "ODV",
            "ISO-8859-1");

    /**
     * Constructor
     */
    public TimeSeriesOutputTransition() {
        super();
    }

    /**
     * @see de.intevation.gnv.transition.OutputTransition#out(java.lang.String,
     *      java.util.Collection, java.io.OutputStream, java.lang.String,
     *      de.intevation.artifacts.CallMeta)
     */
    public void out(String outputMode, Collection<InputData> inputData,
                    OutputStream outputStream, String uuid, CallMeta callMeta)
                                                                              throws TransitionException {
        log.debug("TimeSeriesOutputTransition.out");
        try {

            this.advance(uuid, callMeta);
            Collection<Result> chartResult = this.getChartResult(uuid);
            if (outputMode.equalsIgnoreCase("chart")) {
                log.debug("Chart will be generated.");
                int chartWidth = 600;
                int chartHeight = 400;
                try {
                    if (inputData != null) {
                        Iterator<InputData> it = inputData.iterator();
                        while (it.hasNext()) {
                            InputData ip = it.next();
                            if (ip.getName().equalsIgnoreCase("width")) {
                                chartWidth = Integer.parseInt(ip.getValue());
                            } else if (ip.getName().equalsIgnoreCase("height")) {
                                chartHeight = Integer.parseInt(ip.getValue());
                            }
                        }
                    }
                } catch (NumberFormatException e) {
                    log.error(e, e);
                    throw new TransitionException(e);
                }
                Collection<KeyValueDescibeData> parameters = this.getParameters(uuid);
                Collection<KeyValueDescibeData> measurements = this.getMeasurements(uuid);
                Collection<KeyValueDescibeData> dates = this.getDates(uuid);
                ChartStyle chartStyle = this
                        .creatStyle(chartWidth, chartHeight);
                ChartLabels chartLables = new ChartLabels(this.getFisName(callMeta.getLanguages())+" "+this
                        .getSelectedFeatureName(uuid), this.domainLable);
                this.createChart(outputStream, parameters, measurements,dates,
                        chartStyle, chartLables, uuid);
            } else if (outputMode.equalsIgnoreCase("csv")) {
                log.debug("CSV-File will be generated.");
                this.createCSV(outputStream, chartResult);
            } else if (outputMode.equalsIgnoreCase("statistics")) {
                log.debug("Statistics will be generated.");
                Statistics s = getStatisticsGenerator();
                Collection<Statistic> statistics = s
                        .calculateStatistics(chartResult);
                Document doc = this.writeStatistics2XML(statistics);
                this.writeDocument2OutputStream(doc, outputStream);
            } else if (outputMode.equalsIgnoreCase("odv")) {
                //TimeSeriesExport export = new TimeSeriesExport();
                //export.createODV(outputStream, chartResult);
                createODV(outputStream, chartResult);
            }
        } catch (IOException e) {
            log.error(e, e);
            throw new TransitionException(e);
        } catch (TechnicalChartException e) {
            log.error(e, e);
            throw new TransitionException(e);
        } catch (StatisticsException e) {
            log.error(e, e);
            throw new TransitionException(e);
        }
    }

    
    /**
     * @param outputStream
     * @param chartResult
     * @throws UnsupportedEncodingException
     * @throws IOException
     * @throws TransitionException
     */
    protected void createCSV(OutputStream outputStream,
                            Collection<Result> chartResult)
                                                          throws UnsupportedEncodingException,
                                                          IOException,
                                                          TransitionException {
        DefaultExport export = new DefaultExport();

        export.create(TIMESERIES_CSV_PROFILE, outputStream, chartResult);
    }

    /**
     * TODO Result is not used at the moment. Change result with correct data.
     */
    protected void createODV(OutputStream outputStream, Collection result)
    throws IOException, TransitionException {

        DefaultExport export = new DefaultExport();

        export.create(TIMESERIES_ODV_PROFILE, outputStream, result);
    }

    /**
     * @return
     */
    protected Statistics getStatisticsGenerator() {
        Statistics s = new TimeseriesStatistics();
        return s;
    }

    protected void writeDocument2OutputStream(Document document, OutputStream os) {

        try {
            TransformerFactory transformerFactory = TransformerFactory
                    .newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(document);
            StreamResult result = new StreamResult(os);
            transformer.transform(source, result);
        } catch (TransformerConfigurationException e) {
            log.error(e, e);
        } catch (TransformerFactoryConfigurationError e) {
            log.error(e, e);
        } catch (TransformerException e) {
            log.error(e, e);
        }
    }

    protected Document writeStatistics2XML(Collection<Statistic> statistic) {
        ArtifactXMLUtilities xmlUtilities = new ArtifactXMLUtilities();
        Document doc = XMLUtils.newDocument();
        if (statistic != null) {
            Node statisticResults = xmlUtilities.createArtifactElement(doc,
                    "statistic-values");
            doc.appendChild(statisticResults);
            Iterator<Statistic> it = statistic.iterator();
            while (it.hasNext()) {
                Statistic s = it.next();
                Element result = xmlUtilities.createArtifactElement(doc,
                        "statistic");
                result.setAttribute("name", s.getKey());
                result.setAttribute("value", s.getStringValue());
                statisticResults.appendChild(result);
            }

        }
        return doc;
    }

    protected String getSelectedFeatureName(String uuid) {
        Collection<KeyValueDescibeData> values = this
                .getCollection(featureValuesName, uuid);
        if (values != null) {
            Iterator<KeyValueDescibeData> it = values.iterator();
            while (it.hasNext()) {
                KeyValueDescibeData data = it.next();
                if (data.isSelected()) {
                    return data.getValue();
                }
            }
        }
        return null;
    }

    /**
     * @param outputStream
     * @param parameters
     * @param measurements
     * @param timeSeriesName
     * @param chartStyle
     * @param chartLables
     * @throws IOException
     * @throws TechnicalChartException
     */
    protected void createChart(OutputStream outputStream,
                               Collection<KeyValueDescibeData> parameters,
                               Collection<KeyValueDescibeData> measurements,
                               Collection<KeyValueDescibeData> dates,
                               ChartStyle chartStyle, ChartLabels chartLables,
                               String uuid) throws IOException,
                                           TechnicalChartException {
        ChartFactory chartFactory = new ChartFactory();
        chartFactory.createSimpleTimeSeriesChart(chartLables, chartStyle,
                parameters, measurements, outputStream, this
                        .getChartResult(uuid),timeGapDefinitions);
    }

    protected ChartStyle creatStyle(int witdh, int height) {
        // TODO Konfigurierbar machen
        de.intevation.gnv.chart.Insets lInsets = new de.intevation.gnv.chart.Insets(
                5d, 5d, 5d, 5d);
        Dimension lChartSize = new Dimension(witdh, height);
        return new ChartStyle(Color.white, new Color(230, 230, 230),
                Color.white, Color.white, true, true, lInsets, lChartSize);
    }
    
    protected String getFisName(PreferredLocale[] preferredLocales){
        String returnValue = "";
        InputData inputData = this.inputData.get("fisname");
        if (inputData != null){
            returnValue = RessourceFactory.getInstance()
                                          .getRessource(preferredLocales, 
                                                        inputData.getValue(), 
                                                        inputData.getValue());
        }
        return returnValue;
    }

    protected Collection<KeyValueDescibeData> getParameters(String uuid) {
        return this.getCollection(parameterValuesName, uuid);
    }

    protected Collection<KeyValueDescibeData> getMeasurements(String uuid) {
        return this.getCollection(measuremenValueName, uuid);
    }
    protected Collection<KeyValueDescibeData> getDates(String uuid) {
        return this.getCollection(dateValueName,uuid);
    }

    @Override
    public void setup(Node configuration) {
        super.setup(configuration);
        String featureNameValue = Config.getStringXPath(configuration,
                "value-names/value-name[@name='feature']/@value");
        if (featureNameValue != null) {
            this.featureValuesName = featureNameValue;
        }
        String parameterNameValue = Config.getStringXPath(configuration,
                "value-names/value-name[@name='parameter']/@value");
        if (parameterNameValue != null) {
            this.parameterValuesName = parameterNameValue;
        }
        String measurementNameValue = Config.getStringXPath(configuration,
                "value-names/value-name[@name='measurement']/@value");
        if (measurementNameValue != null) {
            this.measuremenValueName = measurementNameValue;
        }
        
        String dateNameValue = Config.getStringXPath(configuration,
        "value-names/value-name[@name='date']/@value");
        if (dateNameValue != null) {
            this.dateValueName = dateNameValue;
        }
        if (timeGapDefinitions == null){
            Element gapDefinition =  (Element)Config.getNodeXPath(configuration,
                                                                 "time-gap-definition");
            synchronized (this.getClass()) {
                if (gapDefinition != null){
                    String link = gapDefinition.getAttribute("xlink:href");
                    if (link != null ){
                        String absolutFileName = Config.replaceConfigDir(link);
                        gapDefinition = (Element)new ArtifactXMLUtilities().
                                                     readConfiguration(absolutFileName);
                    }
                    
                    NodeList gapDefinitions = Config.getNodeSetXPath(gapDefinition, 
                                                                    "/time-gaps/time-gap");
                    if (gapDefinition != null){
                        timeGapDefinitions = new ArrayList<TimeGap>(gapDefinitions.
                                                                    getLength());
                        for (int i = 0; i < gapDefinitions.getLength(); i++){
                            Element gapNode = (Element)gapDefinitions.item(i);
                            String unit = gapNode.getAttribute("unit");
                            int key = Integer.parseInt(gapNode.getAttribute("key"));
                            int value = Integer.parseInt(gapNode.getAttribute("gap"));
                            log.info("Add new Timegap: "+key+" "+value+" "+ unit);
                            timeGapDefinitions.add(new DefaultTimeGap(unit, 
                                                                      key, 
                                                                      value));
                        }
                    }
                    
                }
            }
        }
    }
    
    /**
     * @param collectionName
     * @return
     */
    protected Collection<KeyValueDescibeData> getCollection(
                                                            String collectionName, 
                                                            String uuid) {
        Iterator<Object> it = this.getDescibeData(uuid).iterator();
        while (it.hasNext()) {

            Object o = it.next();

            if (o instanceof NamedCollection<?>) {
                NamedCollection<KeyValueDescibeData> nc = (NamedCollection<KeyValueDescibeData>) o;
                if (nc.getName().equals(collectionName)) {
                    return nc;
                }
            }
        }
        return null;
    }
}

http://dive4elements.wald.intevation.org