Mercurial > dive4elements > gnv-client
view gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java @ 97:fce237b6fdb8
Bug fixed. Now all Values will be used to calculate the Statistics
gnv-artifacts/trunk@143 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Tim Englich <tim.englich@intevation.de> |
---|---|
date | Mon, 28 Sep 2009 09:53:17 +0000 |
parents | 13402ac8d8fe |
children | 156db25ad4b4 |
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.util.Collection; import java.util.Iterator; 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 au.com.bytecode.opencsv.CSVWriter; import de.intevation.artifactdatabase.XMLUtils; 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.TimeseriesStatistics; import de.intevation.gnv.statistics.exception.StatisticsException; 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.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); protected String domainLable = "Zeit [UTC]"; /** * Constructor */ public TimeSeriesOutputTransition() { super(); } /** * @see de.intevation.gnv.transition.Transition#validate() */ public boolean validate() { return true; } /** * @see de.intevation.gnv.transition.OutputTransition#out(java.lang.String, java.util.Collection, java.io.OutputStream) */ public void out(String outputMode, Collection<InputData> inputData, OutputStream outputStream) throws TransitionException { log.debug("TimeSeriesOutputTransition.out"); try { this.advance(); 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(); Collection<KeyValueDescibeData> measurements = this.getMeasurements(); ChartStyle chartStyle = this.creatStyle(chartWidth,chartHeight); ChartLabels chartLables = new ChartLabels(this.getSelectedFeatureName(),this.domainLable); this.createChart(outputStream, parameters, measurements, chartStyle, chartLables); }else if (outputMode.equalsIgnoreCase("csv")){ log.debug("CSV-File will be generated."); if(this.chartResult != null){ CSVWriter writer = new CSVWriter(new OutputStreamWriter(outputStream,"ISO-8859-1"), ','); // USE THIS ENCODING BECAUSE OF PROBLEMS WITH EXCEL AND UTF-8 Iterator<Result>it = this.chartResult.iterator(); while (it.hasNext()){ Result result = it.next(); int i = 0; String[] entries = new String[5]; entries[i++] = result.getString("XORDINATE"); entries[i++] = result.getString("YORDINATE"); entries[i++] = result.getString("GROUP1"); entries[i++] = result.getString("GROUP2"); entries[i++] = result.getString("GROUP3"); writer.writeNext(entries); } writer.close(); }else{ log.error("No Data given for generation an CSV-File."); throw new TransitionException("No Data given for generation an CSV-File."); } }else if (outputMode.equalsIgnoreCase("statistics")){ log.debug("Statistics will be generated."); TimeseriesStatistics tss = new TimeseriesStatistics(); Collection<Statistic> statistics = tss.calculateStatistics(this.chartResult); Document doc = this.writeStatistics2XML(statistics); this.writeDocument2OutputStream(doc, outputStream); } } 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); } } 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 collectionName = "featureid"; // TODO: Konfigurierbar machen Collection<KeyValueDescibeData> values = this.getCollection(collectionName); 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, ChartStyle chartStyle, ChartLabels chartLables) throws IOException, TechnicalChartException { ChartFactory chartFactory = new ChartFactory(); chartFactory.createSimpleTimeSeriesChart(chartLables, chartStyle, parameters, measurements, outputStream, this.chartResult); } /** * @see de.intevation.gnv.transition.TransitionBase#purifyResult(java.util.Collection) */ @Override protected void purifyResult(Collection<Result> result) { this.chartResult = result; } 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 Collection<KeyValueDescibeData> getParameters(){ String collectionName = "parameterid"; // TODO: Konfigurierbar machen return this.getCollection(collectionName); } protected Collection<KeyValueDescibeData> getMeasurements(){ String collectionName = "measurementid"; // TODO: Konfigurierbar machen return this.getCollection(collectionName); } /** * @param collectionName * @return */ protected Collection<KeyValueDescibeData> getCollection( String collectionName) { Iterator<Object> it = this.descibeData.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; } }