Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java @ 127:f6f0e4ce4a35
merged gnv-artifacts/0.1
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:13:41 +0200 |
parents | 4841808819d9 |
children | 7fb9441dd8af |
comparison
equal
deleted
inserted
replaced
49:94a07d1d9316 | 127:f6f0e4ce4a35 |
---|---|
1 /** | |
2 * | |
3 */ | |
4 package de.intevation.gnv.transition.timeseries; | |
5 | |
6 import java.awt.Color; | |
7 import java.awt.Dimension; | |
8 import java.io.IOException; | |
9 import java.io.OutputStream; | |
10 import java.io.OutputStreamWriter; | |
11 import java.util.Collection; | |
12 import java.util.Iterator; | |
13 | |
14 import javax.xml.transform.Transformer; | |
15 import javax.xml.transform.TransformerConfigurationException; | |
16 import javax.xml.transform.TransformerException; | |
17 import javax.xml.transform.TransformerFactory; | |
18 import javax.xml.transform.TransformerFactoryConfigurationError; | |
19 import javax.xml.transform.dom.DOMSource; | |
20 import javax.xml.transform.stream.StreamResult; | |
21 | |
22 import org.apache.log4j.Logger; | |
23 import org.w3c.dom.Document; | |
24 import org.w3c.dom.Element; | |
25 import org.w3c.dom.Node; | |
26 | |
27 import au.com.bytecode.opencsv.CSVWriter; | |
28 import de.intevation.artifactdatabase.Config; | |
29 import de.intevation.artifactdatabase.XMLUtils; | |
30 import de.intevation.artifacts.CallMeta; | |
31 import de.intevation.gnv.chart.ChartFactory; | |
32 import de.intevation.gnv.chart.ChartLabels; | |
33 import de.intevation.gnv.chart.ChartStyle; | |
34 import de.intevation.gnv.chart.exception.TechnicalChartException; | |
35 import de.intevation.gnv.geobackend.base.Result; | |
36 import de.intevation.gnv.statistics.Statistic; | |
37 import de.intevation.gnv.statistics.Statistics; | |
38 import de.intevation.gnv.statistics.TimeseriesStatistics; | |
39 import de.intevation.gnv.statistics.exception.StatisticsException; | |
40 import de.intevation.gnv.transition.InputData; | |
41 import de.intevation.gnv.transition.OutputTransitionBase; | |
42 import de.intevation.gnv.transition.describedata.KeyValueDescibeData; | |
43 import de.intevation.gnv.transition.describedata.NamedCollection; | |
44 import de.intevation.gnv.transition.exception.TransitionException; | |
45 import de.intevation.gnv.utils.ArtifactXMLUtilities; | |
46 | |
47 /** | |
48 * @author Tim Englich <tim.englich@intevation.de> | |
49 * | |
50 */ | |
51 public class TimeSeriesOutputTransition extends OutputTransitionBase { | |
52 | |
53 /** | |
54 * The UID of this Class | |
55 */ | |
56 private static final long serialVersionUID = 4178407570503098858L; | |
57 | |
58 /** | |
59 * the logger, used to log exceptions and additonaly information | |
60 */ | |
61 private static Logger log = Logger | |
62 .getLogger(TimeSeriesOutputTransition.class); | |
63 | |
64 protected String domainLable = "Zeit [UTC]"; | |
65 | |
66 protected String featureValuesName = "featureid"; | |
67 protected String parameterValuesName = "parameterid"; | |
68 protected String measuremenValueName = "measurementid"; | |
69 | |
70 /** | |
71 * Constructor | |
72 */ | |
73 public TimeSeriesOutputTransition() { | |
74 super(); | |
75 } | |
76 | |
77 /** | |
78 * @see de.intevation.gnv.transition.Transition#validate() | |
79 */ | |
80 public boolean validate() { | |
81 return true; | |
82 } | |
83 | |
84 /** | |
85 * @see de.intevation.gnv.transition.OutputTransition#out(java.lang.String, | |
86 * java.util.Collection, java.io.OutputStream, java.lang.String, | |
87 * de.intevation.artifacts.CallMeta) | |
88 */ | |
89 public void out(String outputMode, Collection<InputData> inputData, | |
90 OutputStream outputStream, String uuid, CallMeta callMeta) | |
91 throws TransitionException { | |
92 log.debug("TimeSeriesOutputTransition.out"); | |
93 try { | |
94 | |
95 this.advance(uuid, callMeta); | |
96 Collection<Result> chartResult = this.getChartResult(uuid); | |
97 if (outputMode.equalsIgnoreCase("chart")) { | |
98 log.debug("Chart will be generated."); | |
99 int chartWidth = 600; | |
100 int chartHeight = 400; | |
101 try { | |
102 if (inputData != null) { | |
103 Iterator<InputData> it = inputData.iterator(); | |
104 while (it.hasNext()) { | |
105 InputData ip = it.next(); | |
106 if (ip.getName().equalsIgnoreCase("width")) { | |
107 chartWidth = Integer.parseInt(ip.getValue()); | |
108 } else if (ip.getName().equalsIgnoreCase("height")) { | |
109 chartHeight = Integer.parseInt(ip.getValue()); | |
110 } | |
111 } | |
112 } | |
113 } catch (NumberFormatException e) { | |
114 log.error(e, e); | |
115 throw new TransitionException(e); | |
116 } | |
117 Collection<KeyValueDescibeData> parameters = this | |
118 .getParameters(); | |
119 Collection<KeyValueDescibeData> measurements = this | |
120 .getMeasurements(); | |
121 ChartStyle chartStyle = this | |
122 .creatStyle(chartWidth, chartHeight); | |
123 ChartLabels chartLables = new ChartLabels(this | |
124 .getSelectedFeatureName(), this.domainLable); | |
125 this.createChart(outputStream, parameters, measurements, | |
126 chartStyle, chartLables, uuid); | |
127 } else if (outputMode.equalsIgnoreCase("csv")) { | |
128 log.debug("CSV-File will be generated."); | |
129 if (chartResult != null) { | |
130 CSVWriter writer = new CSVWriter(new OutputStreamWriter( | |
131 outputStream, "ISO-8859-1"), ','); // USE THIS | |
132 // ENCODING | |
133 // BECAUSE OF | |
134 // PROBLEMS WITH | |
135 // EXCEL AND | |
136 // UTF-8 | |
137 Iterator<Result> it = chartResult.iterator(); | |
138 while (it.hasNext()) { | |
139 Result result = it.next(); | |
140 int i = 0; | |
141 String[] entries = new String[5]; | |
142 entries[i++] = result.getString("XORDINATE"); | |
143 entries[i++] = result.getString("YORDINATE"); | |
144 entries[i++] = result.getString("GROUP1"); | |
145 entries[i++] = result.getString("GROUP2"); | |
146 entries[i++] = result.getString("GROUP3"); | |
147 writer.writeNext(entries); | |
148 } | |
149 writer.close(); | |
150 } else { | |
151 log.error("No Data given for generation an CSV-File."); | |
152 throw new TransitionException( | |
153 "No Data given for generation an CSV-File."); | |
154 } | |
155 } else if (outputMode.equalsIgnoreCase("statistics")) { | |
156 log.debug("Statistics will be generated."); | |
157 Statistics s = getStatisticsGenerator(); | |
158 Collection<Statistic> statistics = s | |
159 .calculateStatistics(chartResult); | |
160 Document doc = this.writeStatistics2XML(statistics); | |
161 this.writeDocument2OutputStream(doc, outputStream); | |
162 } | |
163 } catch (IOException e) { | |
164 log.error(e, e); | |
165 throw new TransitionException(e); | |
166 } catch (TechnicalChartException e) { | |
167 log.error(e, e); | |
168 throw new TransitionException(e); | |
169 } catch (StatisticsException e) { | |
170 log.error(e, e); | |
171 throw new TransitionException(e); | |
172 } | |
173 } | |
174 | |
175 /** | |
176 * @return | |
177 */ | |
178 protected Statistics getStatisticsGenerator() { | |
179 Statistics s = new TimeseriesStatistics(); | |
180 return s; | |
181 } | |
182 | |
183 protected void writeDocument2OutputStream(Document document, OutputStream os) { | |
184 | |
185 try { | |
186 TransformerFactory transformerFactory = TransformerFactory | |
187 .newInstance(); | |
188 Transformer transformer = transformerFactory.newTransformer(); | |
189 DOMSource source = new DOMSource(document); | |
190 StreamResult result = new StreamResult(os); | |
191 transformer.transform(source, result); | |
192 } catch (TransformerConfigurationException e) { | |
193 log.error(e, e); | |
194 } catch (TransformerFactoryConfigurationError e) { | |
195 log.error(e, e); | |
196 } catch (TransformerException e) { | |
197 log.error(e, e); | |
198 } | |
199 } | |
200 | |
201 protected Document writeStatistics2XML(Collection<Statistic> statistic) { | |
202 ArtifactXMLUtilities xmlUtilities = new ArtifactXMLUtilities(); | |
203 Document doc = XMLUtils.newDocument(); | |
204 if (statistic != null) { | |
205 Node statisticResults = xmlUtilities.createArtifactElement(doc, | |
206 "statistic-values"); | |
207 doc.appendChild(statisticResults); | |
208 Iterator<Statistic> it = statistic.iterator(); | |
209 while (it.hasNext()) { | |
210 Statistic s = it.next(); | |
211 Element result = xmlUtilities.createArtifactElement(doc, | |
212 "statistic"); | |
213 result.setAttribute("name", s.getKey()); | |
214 result.setAttribute("value", s.getStringValue()); | |
215 statisticResults.appendChild(result); | |
216 } | |
217 | |
218 } | |
219 return doc; | |
220 } | |
221 | |
222 protected String getSelectedFeatureName() { | |
223 Collection<KeyValueDescibeData> values = this | |
224 .getCollection(featureValuesName); | |
225 if (values != null) { | |
226 Iterator<KeyValueDescibeData> it = values.iterator(); | |
227 while (it.hasNext()) { | |
228 KeyValueDescibeData data = it.next(); | |
229 if (data.isSelected()) { | |
230 return data.getValue(); | |
231 } | |
232 } | |
233 } | |
234 return null; | |
235 } | |
236 | |
237 /** | |
238 * @param outputStream | |
239 * @param parameters | |
240 * @param measurements | |
241 * @param timeSeriesName | |
242 * @param chartStyle | |
243 * @param chartLables | |
244 * @throws IOException | |
245 * @throws TechnicalChartException | |
246 */ | |
247 protected void createChart(OutputStream outputStream, | |
248 Collection<KeyValueDescibeData> parameters, | |
249 Collection<KeyValueDescibeData> measurements, | |
250 ChartStyle chartStyle, ChartLabels chartLables, | |
251 String uuid) throws IOException, | |
252 TechnicalChartException { | |
253 ChartFactory chartFactory = new ChartFactory(); | |
254 chartFactory.createSimpleTimeSeriesChart(chartLables, chartStyle, | |
255 parameters, measurements, outputStream, this | |
256 .getChartResult(uuid)); | |
257 } | |
258 | |
259 protected ChartStyle creatStyle(int witdh, int height) { | |
260 // TODO Konfigurierbar machen | |
261 de.intevation.gnv.chart.Insets lInsets = new de.intevation.gnv.chart.Insets( | |
262 5d, 5d, 5d, 5d); | |
263 Dimension lChartSize = new Dimension(witdh, height); | |
264 return new ChartStyle(Color.white, new Color(230, 230, 230), | |
265 Color.white, Color.white, true, true, lInsets, lChartSize); | |
266 } | |
267 | |
268 protected Collection<KeyValueDescibeData> getParameters() { | |
269 return this.getCollection(parameterValuesName); | |
270 } | |
271 | |
272 protected Collection<KeyValueDescibeData> getMeasurements() { | |
273 return this.getCollection(measuremenValueName); | |
274 } | |
275 | |
276 @Override | |
277 public void setup(Node configuration) { | |
278 super.setup(configuration); | |
279 String featureNameValue = Config.getStringXPath(configuration, "value-names/value-name[@name='feature']/@value"); | |
280 if (featureNameValue != null){ | |
281 this.featureValuesName = featureNameValue; | |
282 } | |
283 String parameterNameValue = Config.getStringXPath(configuration, "value-names/value-name[@name='parameter']/@value"); | |
284 if (parameterNameValue != null){ | |
285 this.parameterValuesName = parameterNameValue; | |
286 } | |
287 String measurementNameValue = Config.getStringXPath(configuration, "value-names/value-name[@name='measurement']/@value"); | |
288 if (measurementNameValue != null){ | |
289 this.measuremenValueName = measurementNameValue; | |
290 } | |
291 } | |
292 | |
293 /** | |
294 * @param collectionName | |
295 * @return | |
296 */ | |
297 protected Collection<KeyValueDescibeData> getCollection( | |
298 String collectionName) { | |
299 Iterator<Object> it = this.descibeData.iterator(); | |
300 while (it.hasNext()) { | |
301 | |
302 Object o = it.next(); | |
303 | |
304 if (o instanceof NamedCollection<?>) { | |
305 NamedCollection<KeyValueDescibeData> nc = (NamedCollection<KeyValueDescibeData>) o; | |
306 if (nc.getName().equals(collectionName)) { | |
307 return nc; | |
308 } | |
309 } | |
310 } | |
311 return null; | |
312 } | |
313 } |