Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesOutputState.java @ 376:d8f3ef441bf2
merged gnv-artifacts/0.3
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:13:47 +0200 |
parents | 6491000407dd |
children | bed9735adf84 |
comparison
equal
deleted
inserted
replaced
293:6b0ef2324d02 | 376:d8f3ef441bf2 |
---|---|
1 /** | |
2 * | |
3 */ | |
4 package de.intevation.gnv.state.timeseries; | |
5 | |
6 import java.io.IOException; | |
7 import java.io.File; | |
8 import java.io.OutputStream; | |
9 import java.io.FileOutputStream; | |
10 import java.io.UnsupportedEncodingException; | |
11 import java.util.ArrayList; | |
12 import java.util.Collection; | |
13 import java.util.Iterator; | |
14 import java.util.List; | |
15 import java.util.Locale; | |
16 import java.util.Vector; | |
17 | |
18 import javax.xml.transform.Transformer; | |
19 import javax.xml.transform.TransformerConfigurationException; | |
20 import javax.xml.transform.TransformerException; | |
21 import javax.xml.transform.TransformerFactory; | |
22 import javax.xml.transform.TransformerFactoryConfigurationError; | |
23 import javax.xml.transform.dom.DOMSource; | |
24 import javax.xml.transform.stream.StreamResult; | |
25 | |
26 import org.apache.log4j.Logger; | |
27 import org.w3c.dom.Document; | |
28 import org.w3c.dom.Element; | |
29 import org.w3c.dom.Node; | |
30 import org.w3c.dom.NodeList; | |
31 | |
32 import org.jfree.chart.ChartTheme; | |
33 | |
34 import au.com.bytecode.opencsv.CSVWriter; | |
35 import de.intevation.artifactdatabase.Config; | |
36 import de.intevation.artifactdatabase.XMLUtils; | |
37 import de.intevation.artifacts.CallMeta; | |
38 import de.intevation.artifacts.CallContext; | |
39 import de.intevation.artifacts.PreferredLocale; | |
40 import de.intevation.gnv.artifacts.context.GNVArtifactContext; | |
41 import de.intevation.gnv.artifacts.context.GNVArtifactContextFactory; | |
42 import de.intevation.gnv.artifacts.ressource.RessourceFactory; | |
43 import de.intevation.gnv.chart.Chart; | |
44 import de.intevation.gnv.chart.ChartLabels; | |
45 import de.intevation.gnv.chart.TimeSeriesChart; | |
46 import de.intevation.gnv.chart.XMLChartTheme; | |
47 import de.intevation.gnv.chart.exception.TechnicalChartException; | |
48 import de.intevation.gnv.exports.ChartExportHelper; | |
49 import de.intevation.gnv.exports.DefaultExport; | |
50 import de.intevation.gnv.exports.DefaultDataCollector; | |
51 import de.intevation.gnv.exports.SimpleOdvDataCollector; | |
52 import de.intevation.gnv.exports.DefaultProfile; | |
53 import de.intevation.gnv.exports.Export.Profile; | |
54 import de.intevation.gnv.geobackend.base.Result; | |
55 import de.intevation.gnv.state.InputData; | |
56 import de.intevation.gnv.state.OutputStateBase; | |
57 import de.intevation.gnv.state.describedata.KeyValueDescibeData; | |
58 import de.intevation.gnv.state.describedata.NamedCollection; | |
59 import de.intevation.gnv.state.exception.StateException; | |
60 import de.intevation.gnv.statistics.Statistic; | |
61 import de.intevation.gnv.statistics.StatisticSet; | |
62 import de.intevation.gnv.statistics.Statistics; | |
63 import de.intevation.gnv.statistics.TimeseriesStatistics; | |
64 import de.intevation.gnv.statistics.exception.StatisticsException; | |
65 import de.intevation.gnv.timeseries.gap.DefaultTimeGap; | |
66 import de.intevation.gnv.timeseries.gap.TimeGap; | |
67 import de.intevation.gnv.utils.ArtifactXMLUtilities; | |
68 | |
69 | |
70 /** | |
71 * @author Tim Englich <tim.englich@intevation.de> | |
72 * | |
73 */ | |
74 public class TimeSeriesOutputState extends OutputStateBase { | |
75 | |
76 protected static final boolean CACHE_CHART = | |
77 Boolean.parseBoolean(System.getProperty("cache.chart", "false")); | |
78 | |
79 protected static final boolean PDF_FORMAT_LANDSCAPE = | |
80 Boolean.parseBoolean(System.getProperty("export.pdf.landscape","true")); | |
81 | |
82 protected static final String[] IMG_EXPORT_FORMAT = { | |
83 "PNG", "JPEG", "GIF" | |
84 }; | |
85 | |
86 /** | |
87 * The UID of this Class | |
88 */ | |
89 private static final long serialVersionUID = 4178407570503098858L; | |
90 | |
91 /** | |
92 * the logger, used to log exceptions and additonaly information | |
93 */ | |
94 private static Logger log = Logger | |
95 .getLogger(TimeSeriesOutputState.class); | |
96 | |
97 private static List<TimeGap> timeGapDefinitions = null; | |
98 | |
99 protected String domainLable = "chart.timeseries.title.xaxis"; | |
100 | |
101 protected String featureValuesName = "featureid"; | |
102 protected String parameterValuesName = "parameterid"; | |
103 protected String measuremenValueName = "measurementid"; | |
104 protected String dateValueName = "dateid"; | |
105 | |
106 public static final String [] TIMESERIES_CSV_PROFILE_COLUMNS = { | |
107 "XORDINATE", | |
108 "YORDINATE", | |
109 "GROUP1", | |
110 "GROUP2", | |
111 "GROUP3" | |
112 }; | |
113 | |
114 | |
115 public static final String [] TIMESERIES_TIMESERIES_CSV_COLUMN_LABEL = { | |
116 "Date/Time", | |
117 "Value", | |
118 "ParameterID", | |
119 "MeasurementID", | |
120 "TimeseriesID" | |
121 }; | |
122 | |
123 public static final String [] TIMESERIES_MESH_CSV_COLUMN_LABEL = { | |
124 "Date/Time", | |
125 "Value", | |
126 "ParameterID", | |
127 "FeatureID", | |
128 "MeshID" | |
129 }; | |
130 | |
131 public static final String [] TIMESERIES_ODV_PROFILE_NAMES = { | |
132 "CRUISE", | |
133 "STATION", | |
134 "TYPE", | |
135 "SHAPE", | |
136 "BOTDEPTH", | |
137 "DEPTH", | |
138 "TIMEVALUE", | |
139 "DATAVALUE", | |
140 "PARAMETER" | |
141 }; | |
142 | |
143 | |
144 public static final String [] ODV_COLUMN_HEADER = { | |
145 "Cruise", | |
146 "Station", | |
147 "Type", | |
148 "Longitude [deegrees_east]", | |
149 "Latitude [deegrees_north]", | |
150 "Bot. Depth [m]", | |
151 "Depth [m]", | |
152 "Date/Time", | |
153 "Value", | |
154 "Parameterid" | |
155 }; | |
156 | |
157 /** | |
158 * Profile for exporting data to odv | |
159 * TODO Change TIMESERIES_PROFILE_NAMES, which belong to CSV exports | |
160 */ | |
161 public static final Profile TIMESERIES_ODV_PROFILE = | |
162 new DefaultProfile( | |
163 ODV_COLUMN_HEADER, | |
164 '\t', | |
165 CSVWriter.NO_QUOTE_CHARACTER, | |
166 CSVWriter.NO_ESCAPE_CHARACTER, | |
167 "ODV", | |
168 "ISO-8859-1"); | |
169 | |
170 /** | |
171 * Constructor | |
172 */ | |
173 public TimeSeriesOutputState() { | |
174 super(); | |
175 } | |
176 | |
177 /** | |
178 * @see de.intevation.gnv.transition.OutputTransition#out(java.lang.String, | |
179 * java.util.Collection, java.io.OutputStream, java.lang.String, | |
180 * de.intevation.artifacts.CallMeta) | |
181 */ | |
182 public void out( | |
183 Document format, | |
184 Collection<InputData> inputData, | |
185 OutputStream outputStream, | |
186 String uuid, | |
187 CallContext callContext | |
188 ) throws StateException | |
189 { | |
190 log.debug("TimeSeriesOutputTransition.out"); | |
191 | |
192 String outputMode = Config.getStringXPath( | |
193 format, | |
194 "action/out/@name" | |
195 ); | |
196 String mimeType = Config.getStringXPath( | |
197 format, | |
198 "action/out/mime-type/@value" | |
199 ); | |
200 | |
201 CallMeta callMeta = callContext.getMeta(); | |
202 | |
203 try { | |
204 if (outputMode.equalsIgnoreCase("chart")) { | |
205 log.debug("Chart will be generated."); | |
206 int chartWidth = 600; | |
207 int chartHeight = 400; | |
208 try { | |
209 if (inputData != null) { | |
210 Iterator<InputData> it = inputData.iterator(); | |
211 while (it.hasNext()) { | |
212 InputData ip = it.next(); | |
213 if (ip.getName().equalsIgnoreCase("width")) { | |
214 chartWidth = Integer.parseInt(ip.getValue()); | |
215 } else if (ip.getName().equalsIgnoreCase("height")) { | |
216 chartHeight = Integer.parseInt(ip.getValue()); | |
217 } | |
218 } | |
219 } | |
220 } catch (NumberFormatException e) { | |
221 log.error(e, e); | |
222 throw new StateException(e); | |
223 } | |
224 | |
225 PreferredLocale[] locales = callMeta.getLanguages(); | |
226 Locale[] serverLocales = | |
227 RessourceFactory.getInstance().getLocales(); | |
228 Locale locale = | |
229 callMeta.getPreferredLocale(serverLocales); | |
230 | |
231 log.debug( | |
232 "Best locale - regarding intersection of server and " + | |
233 "browser locales - is " + locale.toString() | |
234 ); | |
235 | |
236 Collection parameters = this.getCleanedParameters(uuid); | |
237 Collection measurements = this.getMeasurements(uuid); | |
238 Collection dates = this.getDates(uuid); | |
239 | |
240 ChartLabels chartLables = new ChartLabels( | |
241 createChartTitle(locale, uuid), | |
242 createChartSubtitle(locale, uuid), | |
243 RessourceFactory.getInstance().getRessource( | |
244 locale, | |
245 domainLable, | |
246 domainLable | |
247 ) | |
248 ); | |
249 | |
250 String exportFormat = getExportFormat(mimeType); | |
251 | |
252 // TODO Remove this and parse input data | |
253 boolean linesVisible = true; | |
254 boolean shapesVisible = true; | |
255 | |
256 this.createChart( | |
257 outputStream, | |
258 parameters, | |
259 measurements, | |
260 dates, | |
261 chartLables, | |
262 callContext, | |
263 uuid, | |
264 exportFormat, | |
265 locale, | |
266 chartWidth, | |
267 chartHeight, | |
268 linesVisible, | |
269 shapesVisible | |
270 ); | |
271 } | |
272 else if (outputMode.equalsIgnoreCase("pdf")) { | |
273 log.debug("Output mode == pdf"); | |
274 | |
275 Locale[] serverLocales = | |
276 RessourceFactory.getInstance().getLocales(); | |
277 Locale locale = | |
278 callMeta.getPreferredLocale(serverLocales); | |
279 | |
280 // TODO Remove this and parse input data | |
281 boolean linesVisible = true; | |
282 boolean shapesVisible = true; | |
283 | |
284 log.debug( | |
285 "Best locale - regarding intersection of server and " + | |
286 "browser locales - is " + locale.toString() | |
287 ); | |
288 | |
289 createPDF( | |
290 outputStream, | |
291 getCleanedParameters(uuid), | |
292 getMeasurements(uuid), | |
293 getDates(uuid), | |
294 new ChartLabels( | |
295 createChartTitle(locale, uuid), | |
296 createChartSubtitle(locale, uuid), | |
297 RessourceFactory.getInstance().getRessource( | |
298 locale, | |
299 domainLable, | |
300 domainLable | |
301 ) | |
302 ), | |
303 uuid, | |
304 "A4", | |
305 true, | |
306 linesVisible, | |
307 shapesVisible, | |
308 locale, | |
309 callContext | |
310 ); | |
311 } | |
312 else if (outputMode.equalsIgnoreCase("svg")) { | |
313 log.debug("Output mode == svg"); | |
314 int width = 600; | |
315 int height = 400; | |
316 | |
317 // TODO Remove this and parse input data | |
318 boolean linesVisible = true; | |
319 boolean shapesVisible = true; | |
320 | |
321 Locale[] serverLocales = | |
322 RessourceFactory.getInstance().getLocales(); | |
323 Locale locale = | |
324 callMeta.getPreferredLocale(serverLocales); | |
325 | |
326 log.debug( | |
327 "Best locale - regarding intersection of server and " + | |
328 "browser locales - is " + locale.toString() | |
329 ); | |
330 | |
331 createSVG( | |
332 outputStream, | |
333 getCleanedParameters(uuid), | |
334 getMeasurements(uuid), | |
335 getDates(uuid), | |
336 new ChartLabels( | |
337 createChartTitle(locale, uuid), | |
338 createChartSubtitle(locale, uuid), | |
339 RessourceFactory.getInstance().getRessource( | |
340 locale, | |
341 domainLable, | |
342 domainLable | |
343 ) | |
344 ), | |
345 uuid, | |
346 locale, | |
347 width, | |
348 height, | |
349 linesVisible, | |
350 shapesVisible, | |
351 callContext | |
352 ); | |
353 } | |
354 else if (outputMode.equalsIgnoreCase("csv")) { | |
355 log.debug("CSV-File will be generated."); | |
356 Collection<Result> chartResult = this.getChartResult(uuid); | |
357 this.createCSV(outputStream, chartResult); | |
358 } else if (outputMode.equalsIgnoreCase("statistics")) { | |
359 log.debug("Statistics will be generated."); | |
360 Statistics s = getStatisticsGenerator(); | |
361 Collection<Result> chartResult = this.getChartResult(uuid); | |
362 Collection<KeyValueDescibeData> parameters = | |
363 this.getParameters(uuid); | |
364 Collection<KeyValueDescibeData> measurements = | |
365 this.getMeasurements(uuid); | |
366 Collection<KeyValueDescibeData> dates = | |
367 this.getDates(uuid); | |
368 Collection<StatisticSet> statistics = | |
369 s.calculateStatistics(chartResult, | |
370 parameters, | |
371 measurements, | |
372 dates); | |
373 Document doc = this.writeStatistics2XML(statistics); | |
374 this.writeDocument2OutputStream(doc, outputStream); | |
375 } else if (outputMode.equalsIgnoreCase("odv")) { | |
376 | |
377 Collection<Result> odvResult = this.getODVResult(uuid); | |
378 this.createODV(outputStream, odvResult); | |
379 } | |
380 } catch (IOException e) { | |
381 log.error(e, e); | |
382 throw new StateException(e); | |
383 } catch (TechnicalChartException e) { | |
384 log.error(e, e); | |
385 throw new StateException(e); | |
386 } catch (StatisticsException e) { | |
387 log.error(e, e); | |
388 throw new StateException(e); | |
389 } | |
390 } | |
391 | |
392 | |
393 protected String getExportFormat(String mime) { | |
394 for(int i = 0; i < IMG_EXPORT_FORMAT.length; i++) { | |
395 if (mime.trim().toUpperCase().indexOf(IMG_EXPORT_FORMAT[i]) > 0) | |
396 return IMG_EXPORT_FORMAT[i]; | |
397 } | |
398 | |
399 // no format found relating to mimeType, default export as PNG | |
400 return IMG_EXPORT_FORMAT[0]; | |
401 } | |
402 | |
403 | |
404 protected Collection getCleanedParameters(Collection parameters) { | |
405 Iterator iter = parameters.iterator(); | |
406 Collection parameter = new Vector(parameters); | |
407 while (iter.hasNext()) { | |
408 KeyValueDescibeData data = (KeyValueDescibeData)iter.next(); | |
409 if (!data.isSelected()) | |
410 parameter.remove(data); | |
411 } | |
412 | |
413 return parameter; | |
414 } | |
415 | |
416 | |
417 protected Collection getCleanedParameters(String uuid) { | |
418 return getCleanedParameters(getParameters(uuid)); | |
419 } | |
420 | |
421 | |
422 protected void createCSV(OutputStream out, Collection<Result> results) | |
423 throws UnsupportedEncodingException, IOException, StateException | |
424 { | |
425 Iterator iter = results.iterator(); | |
426 Result res = iter.hasNext() ? (Result) iter.next() : null; | |
427 | |
428 if (res == null) | |
429 return; | |
430 | |
431 Profile profile = null; | |
432 int dataid = res.getInteger("DATAID").intValue(); | |
433 | |
434 // on meshes | |
435 if (dataid == 2) { | |
436 profile = new DefaultProfile( | |
437 TIMESERIES_MESH_CSV_COLUMN_LABEL, | |
438 ',', | |
439 '"', | |
440 '"', | |
441 "CSV", | |
442 "ISO-8859-1"); | |
443 } | |
444 | |
445 // on timeseries | |
446 else { | |
447 profile = new DefaultProfile( | |
448 TIMESERIES_TIMESERIES_CSV_COLUMN_LABEL, | |
449 ',', | |
450 '"', | |
451 '"', | |
452 "CSV", | |
453 "ISO-8859-1"); | |
454 } | |
455 | |
456 DefaultExport export = new DefaultExport( | |
457 new DefaultDataCollector(TIMESERIES_CSV_PROFILE_COLUMNS)); | |
458 export.create(profile, out, results); | |
459 } | |
460 | |
461 | |
462 /** | |
463 * TODO Result is not used at the moment. Change result with correct data. | |
464 */ | |
465 protected void createODV(OutputStream outputStream, Collection result) | |
466 throws IOException, StateException { | |
467 | |
468 DefaultExport export = new DefaultExport(new SimpleOdvDataCollector( | |
469 TIMESERIES_ODV_PROFILE_NAMES)); | |
470 | |
471 if (result == null) | |
472 log.error("#################### RESULT == NULL #################"); | |
473 export.create(TIMESERIES_ODV_PROFILE, outputStream, result); | |
474 } | |
475 | |
476 /** | |
477 * @return | |
478 */ | |
479 protected Statistics getStatisticsGenerator() { | |
480 Statistics s = new TimeseriesStatistics(); | |
481 return s; | |
482 } | |
483 | |
484 protected void writeDocument2OutputStream(Document document, OutputStream os) { | |
485 | |
486 try { | |
487 TransformerFactory transformerFactory = TransformerFactory | |
488 .newInstance(); | |
489 Transformer transformer = transformerFactory.newTransformer(); | |
490 DOMSource source = new DOMSource(document); | |
491 StreamResult result = new StreamResult(os); | |
492 transformer.transform(source, result); | |
493 } catch (TransformerConfigurationException e) { | |
494 log.error(e, e); | |
495 } catch (TransformerFactoryConfigurationError e) { | |
496 log.error(e, e); | |
497 } catch (TransformerException e) { | |
498 log.error(e, e); | |
499 } | |
500 } | |
501 | |
502 protected Document writeStatistics2XML( Collection<StatisticSet> statistic) { | |
503 ArtifactXMLUtilities xmlUtilities = new ArtifactXMLUtilities(); | |
504 Document doc = XMLUtils.newDocument(); | |
505 if (statistic != null) { | |
506 Node statisticResults = xmlUtilities.createArtifactElement(doc, | |
507 "statistics"); | |
508 doc.appendChild(statisticResults); | |
509 Iterator<StatisticSet> it = statistic.iterator(); | |
510 while (it.hasNext()) { | |
511 StatisticSet set = it.next(); | |
512 Element setElement = xmlUtilities.createArtifactElement(doc, | |
513 "statistic"); | |
514 setElement.setAttribute("name", set.getName()); | |
515 | |
516 Iterator<Statistic> sit = set.getStatistics().iterator(); | |
517 while (sit.hasNext()){ | |
518 Statistic s = sit.next(); | |
519 Element result = xmlUtilities.createArtifactElement(doc, | |
520 "statistic-value"); | |
521 result.setAttribute("name", s.getKey()); | |
522 result.setAttribute("value", s.getStringValue()); | |
523 setElement.appendChild(result); | |
524 } | |
525 statisticResults.appendChild(setElement); | |
526 } | |
527 | |
528 } | |
529 return doc; | |
530 } | |
531 | |
532 | |
533 protected String getSelectedFeatureName(String uuid) { | |
534 Collection values = getCollection(featureValuesName, uuid); | |
535 | |
536 if (values != null) { | |
537 Iterator it = values.iterator(); | |
538 | |
539 while (it.hasNext()) { | |
540 KeyValueDescibeData data = (KeyValueDescibeData) it.next(); | |
541 | |
542 if (data.isSelected()) { | |
543 return data.getValue(); | |
544 } | |
545 } | |
546 } | |
547 return null; | |
548 } | |
549 | |
550 | |
551 /** | |
552 * @param outputStream | |
553 * @param parameters | |
554 * @param measurements | |
555 * @param timeSeriesName | |
556 * @param chartStyle | |
557 * @param chartLables | |
558 * @throws IOException | |
559 * @throws TechnicalChartException | |
560 */ | |
561 protected void createChart( | |
562 OutputStream outputStream, | |
563 Collection parameters, | |
564 Collection measurements, | |
565 Collection dates, | |
566 ChartLabels chartLables, | |
567 CallContext context, | |
568 String uuid, | |
569 String exportFormat, | |
570 Locale locale, | |
571 int width, | |
572 int height, | |
573 boolean linesVisible, | |
574 boolean shapesVisible | |
575 ) | |
576 throws IOException, TechnicalChartException | |
577 { | |
578 log.debug("Create chart."); | |
579 Chart chart = getChart( | |
580 chartLables, | |
581 createStyle(context), | |
582 parameters, | |
583 measurements, | |
584 dates, | |
585 getChartResult(uuid), | |
586 locale, // Locale | |
587 uuid, | |
588 linesVisible, | |
589 shapesVisible | |
590 ); | |
591 | |
592 if (chart == null) { | |
593 log.error("Could not initialize chart."); | |
594 return; | |
595 } | |
596 | |
597 log.debug( | |
598 "export chart as " + exportFormat + | |
599 " in " + width + "x" + height | |
600 ); | |
601 | |
602 ChartExportHelper.exportImage( | |
603 outputStream, | |
604 chart.generateChart(), | |
605 exportFormat, | |
606 width, | |
607 height | |
608 ); | |
609 } | |
610 | |
611 | |
612 protected void createPDF( | |
613 OutputStream outputStream, | |
614 Collection parameters, | |
615 Collection measurements, | |
616 Collection dates, | |
617 ChartLabels chartLables, | |
618 String uuid, | |
619 String exportFormat, | |
620 boolean landscape, | |
621 boolean linesVisible, | |
622 boolean shapesVisible, | |
623 Locale locale, | |
624 CallContext context | |
625 ) { | |
626 Chart chart = getChart( | |
627 chartLables, | |
628 createStyle(context), | |
629 parameters, | |
630 measurements, | |
631 dates, | |
632 getChartResult(uuid), | |
633 locale, | |
634 uuid, | |
635 linesVisible, | |
636 shapesVisible | |
637 ); | |
638 | |
639 if (chart == null) { | |
640 log.error("Could not initialize chart."); | |
641 return; | |
642 } | |
643 | |
644 ChartExportHelper.exportPDF( | |
645 outputStream, | |
646 chart.generateChart(), | |
647 "A4", | |
648 PDF_FORMAT_LANDSCAPE, | |
649 50F, 50F, 50F, 50F | |
650 ); | |
651 | |
652 try { | |
653 OutputStream toFile = new FileOutputStream("/vol1/home/iweinzierl/tmp/test.svg"); | |
654 ChartExportHelper.exportSVG( | |
655 toFile, | |
656 chart.generateChart(), | |
657 null, | |
658 600, 400 | |
659 ); | |
660 toFile.flush(); | |
661 toFile.close(); | |
662 } | |
663 catch(Exception e) { log.debug("ERROR WHLILE TEST."); } | |
664 } | |
665 | |
666 | |
667 protected void createSVG( | |
668 OutputStream outputStream, | |
669 Collection parameters, | |
670 Collection measurements, | |
671 Collection dates, | |
672 ChartLabels chartLables, | |
673 String uuid, | |
674 Locale locale, | |
675 int width, | |
676 int height, | |
677 boolean linesVisible, | |
678 boolean shapesVisible, | |
679 CallContext callContext | |
680 ) { | |
681 Chart chart = getChart( | |
682 chartLables, | |
683 createStyle(callContext), | |
684 parameters, | |
685 measurements, | |
686 dates, | |
687 getChartResult(uuid), | |
688 locale, | |
689 uuid, | |
690 linesVisible, | |
691 shapesVisible | |
692 ); | |
693 | |
694 if (chart == null) { | |
695 log.error("Could not initialize chart."); | |
696 return; | |
697 } | |
698 | |
699 ChartExportHelper.exportSVG( | |
700 outputStream, | |
701 chart.generateChart(), | |
702 null, | |
703 600, 400 | |
704 ); | |
705 | |
706 log.debug("svg export finished."); | |
707 } | |
708 | |
709 | |
710 protected Chart getChart( | |
711 ChartLabels chartLables, | |
712 ChartTheme theme, | |
713 Collection parameters, | |
714 Collection measurements, | |
715 Collection dates, | |
716 Collection result, | |
717 Locale locale, | |
718 String uuid, | |
719 boolean linesVisible, | |
720 boolean shapesVisible | |
721 ) { | |
722 Chart chart = null; | |
723 | |
724 if (CACHE_CHART) { | |
725 log.info("Try to get timeseries chart from cache."); | |
726 chart = (Chart) getChartFromCache(uuid); | |
727 } | |
728 | |
729 if (chart != null) | |
730 return chart; | |
731 | |
732 log.info("Chart not in cache yet."); | |
733 chart = new TimeSeriesChart( | |
734 chartLables, | |
735 theme, | |
736 parameters, | |
737 measurements, | |
738 dates, | |
739 result, | |
740 timeGapDefinitions, | |
741 locale, | |
742 linesVisible, | |
743 shapesVisible | |
744 ); | |
745 chart.generateChart(); | |
746 | |
747 if (CACHE_CHART) { | |
748 log.info("Put chart into cache."); | |
749 purifyChart(chart, uuid); | |
750 } | |
751 | |
752 return chart; | |
753 } | |
754 | |
755 protected ChartTheme createStyle(CallContext callContext) { | |
756 log.debug("Fetch chart theme from global context"); | |
757 | |
758 GNVArtifactContext context = | |
759 (GNVArtifactContext) callContext.globalContext(); | |
760 | |
761 XMLChartTheme theme = (XMLChartTheme) context.get( | |
762 GNVArtifactContextFactory.CHARTTEMPLATE | |
763 ); | |
764 | |
765 return theme; | |
766 } | |
767 | |
768 | |
769 protected String createChartTitle(Locale locale, String uuid) { | |
770 return getFisName(locale); | |
771 | |
772 } | |
773 | |
774 | |
775 protected String createChartSubtitle(Locale locale, String uuid) { | |
776 return getSelectedFeatureName(uuid); | |
777 } | |
778 | |
779 | |
780 protected String getFisName(Locale locale) { | |
781 String returnValue = ""; | |
782 InputData input = inputData.get("fisname"); | |
783 | |
784 if (input != null) { | |
785 String value = input.getValue(); | |
786 | |
787 returnValue = RessourceFactory.getInstance().getRessource( | |
788 locale, | |
789 value, | |
790 value | |
791 ); | |
792 } | |
793 return returnValue; | |
794 } | |
795 | |
796 | |
797 protected Collection<KeyValueDescibeData> getParameters(String uuid) { | |
798 return this.getCollection(parameterValuesName, uuid); | |
799 } | |
800 | |
801 protected Collection<KeyValueDescibeData> getMeasurements(String uuid) { | |
802 return this.getCollection(measuremenValueName, uuid); | |
803 } | |
804 protected Collection<KeyValueDescibeData> getDates(String uuid) { | |
805 return this.getCollection(dateValueName,uuid); | |
806 } | |
807 | |
808 @Override | |
809 public void setup(Node configuration) { | |
810 super.setup(configuration); | |
811 String featureNameValue = Config.getStringXPath(configuration, | |
812 "value-names/value-name[@name='feature']/@value"); | |
813 if (featureNameValue != null) { | |
814 this.featureValuesName = featureNameValue; | |
815 } | |
816 String parameterNameValue = Config.getStringXPath(configuration, | |
817 "value-names/value-name[@name='parameter']/@value"); | |
818 if (parameterNameValue != null) { | |
819 this.parameterValuesName = parameterNameValue; | |
820 } | |
821 String measurementNameValue = Config.getStringXPath(configuration, | |
822 "value-names/value-name[@name='measurement']/@value"); | |
823 if (measurementNameValue != null) { | |
824 this.measuremenValueName = measurementNameValue; | |
825 } | |
826 | |
827 String dateNameValue = Config.getStringXPath(configuration, | |
828 "value-names/value-name[@name='date']/@value"); | |
829 if (dateNameValue != null) { | |
830 this.dateValueName = dateNameValue; | |
831 } | |
832 if (timeGapDefinitions == null){ | |
833 Element gapDefinition = (Element)Config.getNodeXPath(configuration, | |
834 "time-gap-definition"); | |
835 synchronized (this.getClass()) { | |
836 if (gapDefinition != null){ | |
837 String link = gapDefinition.getAttribute("xlink:href"); | |
838 if (link != null ){ | |
839 String absolutFileName = Config.replaceConfigDir(link); | |
840 gapDefinition = (Element)new ArtifactXMLUtilities(). | |
841 readConfiguration(absolutFileName); | |
842 } | |
843 | |
844 NodeList gapDefinitions = Config.getNodeSetXPath(gapDefinition, | |
845 "/time-gaps/time-gap"); | |
846 if (gapDefinition != null){ | |
847 timeGapDefinitions = new ArrayList<TimeGap>(gapDefinitions. | |
848 getLength()); | |
849 for (int i = 0; i < gapDefinitions.getLength(); i++){ | |
850 Element gapNode = (Element)gapDefinitions.item(i); | |
851 String unit = gapNode.getAttribute("unit"); | |
852 int key = Integer.parseInt(gapNode.getAttribute("key")); | |
853 int value = Integer.parseInt(gapNode.getAttribute("gap")); | |
854 log.info("Add new Timegap: "+key+" "+value+" "+ unit); | |
855 timeGapDefinitions.add(new DefaultTimeGap(unit, | |
856 key, | |
857 value)); | |
858 } | |
859 } | |
860 | |
861 } | |
862 } | |
863 } | |
864 } | |
865 | |
866 /** | |
867 * @param collectionName | |
868 * @return | |
869 */ | |
870 protected Collection<KeyValueDescibeData> getCollection( | |
871 String collectionName, | |
872 String uuid) { | |
873 Iterator<Object> it = this.getDescibeData(uuid).iterator(); | |
874 while (it.hasNext()) { | |
875 | |
876 Object o = it.next(); | |
877 | |
878 if (o instanceof NamedCollection<?>) { | |
879 NamedCollection<KeyValueDescibeData> nc = (NamedCollection<KeyValueDescibeData>) o; | |
880 if (nc.getName().equals(collectionName)) { | |
881 return nc; | |
882 } | |
883 } | |
884 } | |
885 return null; | |
886 } | |
887 } | |
888 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |