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

http://dive4elements.wald.intevation.org