Mercurial > dive4elements > gnv-client
diff gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java @ 117:ef157bd2fa92
LanguageSupport integrated
gnv-artifacts/trunk@178 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Tim Englich <tim.englich@intevation.de> |
---|---|
date | Fri, 02 Oct 2009 14:24:47 +0000 |
parents | bb45c5097cb6 |
children | 4841808819d9 |
line wrap: on
line diff
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java Fri Oct 02 08:07:00 2009 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java Fri Oct 02 14:24:47 2009 +0000 @@ -26,6 +26,7 @@ import au.com.bytecode.opencsv.CSVWriter; import de.intevation.artifactdatabase.XMLUtils; +import de.intevation.artifacts.CallMeta; import de.intevation.gnv.chart.ChartFactory; import de.intevation.gnv.chart.ChartLabels; import de.intevation.gnv.chart.ChartStyle; @@ -44,22 +45,23 @@ /** * @author Tim Englich <tim.englich@intevation.de> - * + * */ -public class TimeSeriesOutputTransition extends OutputTransitionBase{ +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]"; - + private static Logger log = Logger + .getLogger(TimeSeriesOutputTransition.class); + + protected String domainLable = "Zeit [UTC]"; + /** * Constructor */ @@ -74,47 +76,61 @@ 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, String uuid) throws TransitionException { + /** + * @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); - Collection<Result> chartResult = this.getChartResult(uuid); - if (outputMode.equalsIgnoreCase("chart")){ + + 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){ + if (inputData != null) { Iterator<InputData> it = inputData.iterator(); - while (it.hasNext()){ + while (it.hasNext()) { InputData ip = it.next(); - if (ip.getName().equalsIgnoreCase("width")){ + if (ip.getName().equalsIgnoreCase("width")) { chartWidth = Integer.parseInt(ip.getValue()); - }else if (ip.getName().equalsIgnoreCase("height")){ + } else if (ip.getName().equalsIgnoreCase("height")) { chartHeight = Integer.parseInt(ip.getValue()); } } } } catch (NumberFormatException e) { - log.error(e,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); + 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,uuid); - }else if (outputMode.equalsIgnoreCase("csv")){ + chartStyle, chartLables, uuid); + } else if (outputMode.equalsIgnoreCase("csv")) { log.debug("CSV-File will be generated."); - if(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 = chartResult.iterator(); - while (it.hasNext()){ + if (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 = chartResult.iterator(); + while (it.hasNext()) { Result result = it.next(); int i = 0; String[] entries = new String[5]; @@ -126,25 +142,27 @@ writer.writeNext(entries); } writer.close(); - }else{ + } else { log.error("No Data given for generation an CSV-File."); - throw new TransitionException("No Data given for generation an CSV-File."); + throw new TransitionException( + "No Data given for generation an CSV-File."); } - }else if (outputMode.equalsIgnoreCase("statistics")){ + } else if (outputMode.equalsIgnoreCase("statistics")) { log.debug("Statistics will be generated."); Statistics s = getStatisticsGenerator(); - Collection<Statistic> statistics = s.calculateStatistics(chartResult); + Collection<Statistic> statistics = s + .calculateStatistics(chartResult); Document doc = this.writeStatistics2XML(statistics); this.writeDocument2OutputStream(doc, outputStream); } } catch (IOException e) { - log.error(e,e); + log.error(e, e); throw new TransitionException(e); } catch (TechnicalChartException e) { - log.error(e,e); + log.error(e, e); throw new TransitionException(e); - }catch (StatisticsException e) { - log.error(e,e); + } catch (StatisticsException e) { + log.error(e, e); throw new TransitionException(e); } } @@ -156,58 +174,62 @@ Statistics s = new TimeseriesStatistics(); return s; } - - protected void writeDocument2OutputStream(Document document, OutputStream os){ - + + protected void writeDocument2OutputStream(Document document, OutputStream os) { + try { - TransformerFactory transformerFactory = TransformerFactory.newInstance(); + TransformerFactory transformerFactory = TransformerFactory + .newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(document); - StreamResult result = new StreamResult(os); + StreamResult result = new StreamResult(os); transformer.transform(source, result); } catch (TransformerConfigurationException e) { - log.error(e,e); + log.error(e, e); } catch (TransformerFactoryConfigurationError e) { - log.error(e,e); - }catch (TransformerException e) { - log.error(e,e); + log.error(e, e); + } catch (TransformerException e) { + log.error(e, e); } } - - protected Document writeStatistics2XML(Collection<Statistic> statistic){ + + protected Document writeStatistics2XML(Collection<Statistic> statistic) { ArtifactXMLUtilities xmlUtilities = new ArtifactXMLUtilities(); Document doc = XMLUtils.newDocument(); - if (statistic != null){ - Node statisticResults = xmlUtilities.createArtifactElement(doc, "statistic-values"); + if (statistic != null) { + Node statisticResults = xmlUtilities.createArtifactElement(doc, + "statistic-values"); doc.appendChild(statisticResults); Iterator<Statistic> it = statistic.iterator(); - while (it.hasNext()){ + while (it.hasNext()) { Statistic s = it.next(); - Element result = xmlUtilities.createArtifactElement(doc, "statistic"); + Element result = xmlUtilities.createArtifactElement(doc, + "statistic"); result.setAttribute("name", s.getKey()); result.setAttribute("value", s.getStringValue()); statisticResults.appendChild(result); } - + } return doc; } - - protected String getSelectedFeatureName(){ + protected String getSelectedFeatureName() { String collectionName = "featureid"; // TODO: Konfigurierbar machen - Collection<KeyValueDescibeData> values = this.getCollection(collectionName); - if (values != null){ + Collection<KeyValueDescibeData> values = this + .getCollection(collectionName); + if (values != null) { Iterator<KeyValueDescibeData> it = values.iterator(); - while(it.hasNext()){ + while (it.hasNext()) { KeyValueDescibeData data = it.next(); - if (data.isSelected()){ + if (data.isSelected()) { return data.getValue(); } } } return null; } + /** * @param outputStream * @param parameters @@ -219,29 +241,32 @@ * @throws TechnicalChartException */ protected void createChart(OutputStream outputStream, - Collection<KeyValueDescibeData> parameters, - Collection<KeyValueDescibeData> measurements, - ChartStyle chartStyle, ChartLabels chartLables, String uuid) throws IOException, - TechnicalChartException { + Collection<KeyValueDescibeData> parameters, + Collection<KeyValueDescibeData> measurements, + ChartStyle chartStyle, ChartLabels chartLables, + String uuid) throws IOException, + TechnicalChartException { ChartFactory chartFactory = new ChartFactory(); chartFactory.createSimpleTimeSeriesChart(chartLables, chartStyle, - parameters, measurements, - outputStream, this.getChartResult(uuid)); + parameters, measurements, outputStream, this + .getChartResult(uuid)); } - - protected ChartStyle creatStyle(int witdh, int height){ + + 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); + 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); + return new ChartStyle(Color.white, new Color(230, 230, 230), + Color.white, Color.white, true, true, lInsets, lChartSize); } - - protected Collection<KeyValueDescibeData> getParameters(){ + + protected Collection<KeyValueDescibeData> getParameters() { String collectionName = "parameterid"; // TODO: Konfigurierbar machen return this.getCollection(collectionName); } - - protected Collection<KeyValueDescibeData> getMeasurements(){ + + protected Collection<KeyValueDescibeData> getMeasurements() { String collectionName = "measurementid"; // TODO: Konfigurierbar machen return this.getCollection(collectionName); } @@ -251,18 +276,18 @@ * @return */ protected Collection<KeyValueDescibeData> getCollection( - String collectionName) { + 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; - } - } + 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; }