comparison gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java @ 303:9ba2cf8cad8d

Export format of charts is related to mime-type. gnv-artifacts/trunk@358 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 20 Nov 2009 15:45:25 +0000
parents 3ea030aafe65
children a16d337c5678
comparison
equal deleted inserted replaced
302:dc2713a3eaa7 303:9ba2cf8cad8d
70 public class TimeSeriesOutputTransition extends OutputTransitionBase { 70 public class TimeSeriesOutputTransition extends OutputTransitionBase {
71 71
72 protected static final boolean CACHE_CHART = 72 protected static final boolean CACHE_CHART =
73 Boolean.parseBoolean(System.getProperty("cache.chart", "false")); 73 Boolean.parseBoolean(System.getProperty("cache.chart", "false"));
74 74
75 protected static final String[] IMG_EXPORT_FORMAT = {
76 "PNG", "JPEG", "GIF"
77 };
78
75 /** 79 /**
76 * The UID of this Class 80 * The UID of this Class
77 */ 81 */
78 private static final long serialVersionUID = 4178407570503098858L; 82 private static final long serialVersionUID = 4178407570503098858L;
79 83
161 /** 165 /**
162 * @see de.intevation.gnv.transition.OutputTransition#out(java.lang.String, 166 * @see de.intevation.gnv.transition.OutputTransition#out(java.lang.String,
163 * java.util.Collection, java.io.OutputStream, java.lang.String, 167 * java.util.Collection, java.io.OutputStream, java.lang.String,
164 * de.intevation.artifacts.CallMeta) 168 * de.intevation.artifacts.CallMeta)
165 */ 169 */
166 public void out(Document format, Collection<InputData> inputData, 170 public void out(
167 OutputStream outputStream, String uuid, CallMeta callMeta) 171 Document format,
168 throws TransitionException { 172 Collection<InputData> inputData,
173 OutputStream outputStream,
174 String uuid,
175 CallMeta callMeta
176 ) throws TransitionException
177 {
169 log.debug("TimeSeriesOutputTransition.out"); 178 log.debug("TimeSeriesOutputTransition.out");
170 String outputMode = Config.getStringXPath(format, "action/out/@name"); 179
180 String outputMode = Config.getStringXPath(
181 format,
182 "action/out/@name"
183 );
184 String mimeType = Config.getStringXPath(
185 format,
186 "action/out/mime-type/@value"
187 );
188
171 try { 189 try {
172 190
173 this.advance(uuid, callMeta); // TODO This hsould only be done if it is nessessary 191 this.advance(uuid, callMeta); // TODO This hsould only be done if it is nessessary
174 192
175 if (outputMode.equalsIgnoreCase("chart")) { 193 if (outputMode.equalsIgnoreCase("chart")) {
176 log.debug("Chart will be generated."); 194 log.debug("Chart will be generated.");
177 int chartWidth = 600; 195 int chartWidth = 600;
178 int chartHeight = 400; 196 int chartHeight = 400;
179 try { 197 try {
197 Collection<KeyValueDescibeData> dates = this.getDates(uuid); 215 Collection<KeyValueDescibeData> dates = this.getDates(uuid);
198 ChartStyle chartStyle = this 216 ChartStyle chartStyle = this
199 .creatStyle(chartWidth, chartHeight); 217 .creatStyle(chartWidth, chartHeight);
200 ChartLabels chartLables = new ChartLabels(this.getFisName(callMeta.getLanguages())+" "+this 218 ChartLabels chartLables = new ChartLabels(this.getFisName(callMeta.getLanguages())+" "+this
201 .getSelectedFeatureName(uuid), this.domainLable); 219 .getSelectedFeatureName(uuid), this.domainLable);
202 this.createChart(outputStream, parameters, measurements,dates, 220
203 chartStyle, chartLables, uuid); 221 String exportFormat = getExportFormat(mimeType);
222
223 this.createChart(
224 outputStream,
225 parameters,
226 measurements,
227 dates,
228 chartStyle,
229 chartLables,
230 uuid,
231 exportFormat
232 );
204 } else if (outputMode.equalsIgnoreCase("csv")) { 233 } else if (outputMode.equalsIgnoreCase("csv")) {
205 log.debug("CSV-File will be generated."); 234 log.debug("CSV-File will be generated.");
206 Collection<Result> chartResult = this.getChartResult(uuid); 235 Collection<Result> chartResult = this.getChartResult(uuid);
207 this.createCSV(outputStream, chartResult); 236 this.createCSV(outputStream, chartResult);
208 } else if (outputMode.equalsIgnoreCase("statistics")) { 237 } else if (outputMode.equalsIgnoreCase("statistics")) {
237 log.error(e, e); 266 log.error(e, e);
238 throw new TransitionException(e); 267 throw new TransitionException(e);
239 } 268 }
240 } 269 }
241 270
242 271
272 protected String getExportFormat(String mime) {
273 for(int i = 0; i < IMG_EXPORT_FORMAT.length; i++) {
274 if (mime.trim().toUpperCase().indexOf(IMG_EXPORT_FORMAT[i]) > 0)
275 return IMG_EXPORT_FORMAT[i];
276 }
277
278 // no format found relating to mimeType, default export as PNG
279 return IMG_EXPORT_FORMAT[0];
280 }
281
282
243 /** 283 /**
244 * @param outputStream 284 * @param outputStream
245 * @param chartResult 285 * @param chartResult
246 * @throws UnsupportedEncodingException 286 * @throws UnsupportedEncodingException
247 * @throws IOException 287 * @throws IOException
358 Collection parameters, 398 Collection parameters,
359 Collection measurements, 399 Collection measurements,
360 Collection dates, 400 Collection dates,
361 ChartStyle chartStyle, 401 ChartStyle chartStyle,
362 ChartLabels chartLables, 402 ChartLabels chartLables,
363 String uuid 403 String uuid,
404 String exportFormat
364 ) 405 )
365 throws IOException, TechnicalChartException 406 throws IOException, TechnicalChartException
366 { 407 {
367 log.debug("Create chart."); 408 log.debug("Create chart.");
368 Chart chart = getChart( 409 Chart chart = getChart(
379 if (chart == null) { 420 if (chart == null) {
380 log.error("Could not initialize chart."); 421 log.error("Could not initialize chart.");
381 return; 422 return;
382 } 423 }
383 424
384 // TODO generic format
385 Dimension dim = chartStyle.getChartSize(); 425 Dimension dim = chartStyle.getChartSize();
386 String format = "PNG";
387 int width = new Double(dim.getWidth()).intValue(); 426 int width = new Double(dim.getWidth()).intValue();
388 int height = new Double(dim.getHeight()).intValue(); 427 int height = new Double(dim.getHeight()).intValue();
389 428
390 log.debug("export chart as " + format + " in " + width + "x" + height); 429 log.debug(
391 ImageIO.write(chart.exportPNG(width, height), format, outputStream); 430 "export chart as " + exportFormat +
431 " in " + width + "x" + height
432 );
433
434 ImageIO.write(
435 chart.exportImage(width, height),
436 exportFormat,
437 outputStream
438 );
392 } 439 }
393 440
394 protected Chart getChart( 441 protected Chart getChart(
395 ChartLabels chartLables, 442 ChartLabels chartLables,
396 ChartTheme chartTheme, 443 ChartTheme chartTheme,

http://dive4elements.wald.intevation.org