teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5863: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5863: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.artifacts.services; raimund@3274: teichmann@5831: import org.dive4elements.artifactdatabase.DefaultService; raimund@3274: teichmann@5831: import org.dive4elements.artifacts.CallMeta; teichmann@5831: import org.dive4elements.artifacts.GlobalContext; teichmann@5831: import org.dive4elements.artifacts.Service; raimund@3274: teichmann@5831: import org.dive4elements.river.artifacts.model.SQOverview; teichmann@5831: import org.dive4elements.river.artifacts.model.SQOverviewFactory; raimund@3274: teichmann@5831: import org.dive4elements.river.artifacts.resources.Resources; teichmann@5831: teichmann@5831: import org.dive4elements.river.backend.SedDBSessionHolder; teichmann@5831: teichmann@5831: import org.dive4elements.river.utils.KMIndex; raimund@3274: raimund@3274: import java.awt.Color; raimund@3274: import java.awt.Dimension; raimund@3274: import java.awt.Transparency; raimund@3274: raimund@3274: import java.awt.image.BufferedImage; raimund@3274: raimund@3274: import java.io.ByteArrayOutputStream; raimund@3274: import java.io.IOException; raimund@3274: sascha@3276: import java.util.Date; raimund@3274: import java.util.List; raimund@3274: raimund@3274: import javax.imageio.ImageIO; raimund@3274: raimund@3274: import org.apache.log4j.Logger; raimund@3274: raimund@3274: import org.jfree.chart.ChartFactory; raimund@3274: import org.jfree.chart.JFreeChart; raimund@3274: raimund@3274: import org.jfree.chart.axis.DateAxis; raimund@3274: raimund@3274: import org.jfree.chart.plot.PlotOrientation; raimund@3274: import org.jfree.chart.plot.XYPlot; sascha@3276: raimund@3274: import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; raimund@3274: raimund@3274: import org.jfree.data.xy.XYSeries; raimund@3274: import org.jfree.data.xy.XYSeriesCollection; raimund@3274: raimund@3274: import org.w3c.dom.Document; raimund@3274: import org.w3c.dom.Element; raimund@3274: import org.w3c.dom.NodeList; raimund@3274: raimund@3274: public class SQKMChartService raimund@3274: extends DefaultService { raimund@3274: raimund@3274: private static final Logger log = sascha@3288: Logger.getLogger(SQKMChartService.class); raimund@3274: raimund@3274: public static final int DEFAULT_WIDTH = 240; raimund@3274: public static final int DEFAULT_HEIGHT = 180; raimund@3274: raimund@3274: public static final String I18N_CHART_LABEL = raimund@3274: "sq.km.chart.label"; raimund@3274: raimund@3274: public static final String DEFAULT_CHART_LABEL = raimund@3274: "Measuring Points"; raimund@3274: raimund@3274: public static final String I18N_CHART_TITLE = raimund@3274: "sq.km.chart.title"; raimund@3274: raimund@3274: public static final String DEFAULT_CHART_TITLE = raimund@3274: "Measuring points"; raimund@3274: raimund@3274: public static final String I18N_KM_AXIS = raimund@3274: "sq.km.chart.km.axis"; raimund@3274: raimund@3274: public static final String DEFAULT_KM_AXIS = raimund@3274: "km"; raimund@3274: raimund@3274: public static final String I18N_DATE_AXIS = raimund@3274: "sq.km.chart.date.axis"; raimund@3274: raimund@3274: public static final String DEFAULT_DATE_AXIS = raimund@3274: "Date"; raimund@3274: raimund@3274: public static final String DEFAULT_FORMAT = "png"; raimund@3274: raimund@3274: // TODO: Load fancy image from resources. raimund@3274: public static final byte [] EMPTY = { raimund@3274: (byte)0x89, (byte)0x50, (byte)0x4e, (byte)0x47, raimund@3274: (byte)0x0d, (byte)0x0a, (byte)0x1a, (byte)0x0a, raimund@3274: (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x0d, raimund@3274: (byte)0x49, (byte)0x48, (byte)0x44, (byte)0x52, raimund@3274: (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, raimund@3274: (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, raimund@3274: (byte)0x08, (byte)0x00, (byte)0x00, (byte)0x00, raimund@3274: (byte)0x00, (byte)0x3a, (byte)0x7e, (byte)0x9b, raimund@3274: (byte)0x55, (byte)0x00, (byte)0x00, (byte)0x00, raimund@3274: (byte)0x01, (byte)0x73, (byte)0x52, (byte)0x47, raimund@3274: (byte)0x42, (byte)0x00, (byte)0xae, (byte)0xce, raimund@3274: (byte)0x1c, (byte)0xe9, (byte)0x00, (byte)0x00, raimund@3274: (byte)0x00, (byte)0x09, (byte)0x70, (byte)0x48, raimund@3274: (byte)0x59, (byte)0x73, (byte)0x00, (byte)0x00, raimund@3274: (byte)0x0b, (byte)0x13, (byte)0x00, (byte)0x00, raimund@3274: (byte)0x0b, (byte)0x13, (byte)0x01, (byte)0x00, raimund@3274: (byte)0x9a, (byte)0x9c, (byte)0x18, (byte)0x00, raimund@3274: (byte)0x00, (byte)0x00, (byte)0x07, (byte)0x74, raimund@3274: (byte)0x49, (byte)0x4d, (byte)0x45, (byte)0x07, raimund@3274: (byte)0xdc, (byte)0x04, (byte)0x04, (byte)0x10, raimund@3274: (byte)0x30, (byte)0x15, (byte)0x7d, (byte)0x77, raimund@3274: (byte)0x36, (byte)0x0b, (byte)0x00, (byte)0x00, raimund@3274: (byte)0x00, (byte)0x08, (byte)0x74, (byte)0x45, raimund@3274: (byte)0x58, (byte)0x74, (byte)0x43, (byte)0x6f, raimund@3274: (byte)0x6d, (byte)0x6d, (byte)0x65, (byte)0x6e, raimund@3274: (byte)0x74, (byte)0x00, (byte)0xf6, (byte)0xcc, raimund@3274: (byte)0x96, (byte)0xbf, (byte)0x00, (byte)0x00, raimund@3274: (byte)0x00, (byte)0x0a, (byte)0x49, (byte)0x44, raimund@3274: (byte)0x41, (byte)0x54, (byte)0x08, (byte)0xd7, raimund@3274: (byte)0x63, (byte)0xf8, (byte)0x0f, (byte)0x00, raimund@3274: (byte)0x01, (byte)0x01, (byte)0x01, (byte)0x00, raimund@3274: (byte)0x1b, (byte)0xb6, (byte)0xee, (byte)0x56, raimund@3274: (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, raimund@3274: (byte)0x49, (byte)0x45, (byte)0x4e, (byte)0x44, raimund@3274: (byte)0xae, (byte)0x42, (byte)0x60, (byte)0x82 raimund@3274: }; raimund@3274: raimund@3274: private static final Output empty() { raimund@3274: return new Output(EMPTY, "image/png"); raimund@3274: } raimund@3274: raimund@3274: @Override raimund@3274: public Service.Output process( raimund@3274: Document data, raimund@3274: GlobalContext globalContext, raimund@3274: CallMeta callMeta raimund@3274: ) { raimund@3274: log.debug("SQKMChartService.process"); raimund@3274: raimund@3274: SedDBSessionHolder.acquire(); raimund@3274: try { raimund@3274: return doProcess(data, globalContext, callMeta); raimund@3274: } raimund@3274: finally { raimund@3274: SedDBSessionHolder.HOLDER.get().close(); raimund@3274: SedDBSessionHolder.release(); raimund@3274: } raimund@3274: } raimund@3274: raimund@3274: protected Service.Output doProcess( raimund@3274: Document input, raimund@3274: GlobalContext globalContext, raimund@3274: CallMeta callMeta raimund@3274: ) { raimund@3274: String river = getRiverName(input); raimund@3274: Dimension extent = getExtent(input); raimund@3274: String format = getFormat(input); raimund@3274: raimund@3274: if (river == null) { raimund@3274: log.warn("River invalid."); raimund@3274: return empty(); raimund@3274: } raimund@3274: raimund@3274: SQOverview overview = SQOverviewFactory.getOverview(river); raimund@3274: raimund@3274: if (overview == null) { raimund@3274: log.warn("No overview found for river '" + river + "'"); raimund@3274: return empty(); raimund@3274: } raimund@3274: raimund@3274: KMIndex> entries = overview.filter(SQOverview.ACCEPT); raimund@3274: raimund@3274: JFreeChart chart = createChart(entries, river, callMeta); raimund@3274: raimund@3274: return encode(chart, extent, format); raimund@3274: } raimund@3274: raimund@3274: protected static Output encode( raimund@3274: JFreeChart chart, raimund@3274: Dimension extent, raimund@3274: String format raimund@3274: ) { raimund@3274: BufferedImage image = chart.createBufferedImage( raimund@3274: extent.width, extent.height, raimund@3274: Transparency.BITMASK, raimund@3274: null); raimund@3274: raimund@3274: ByteArrayOutputStream out = new ByteArrayOutputStream(); raimund@3274: raimund@3274: try { raimund@3274: ImageIO.write(image, format, out); raimund@3274: } raimund@3274: catch (IOException ioe) { raimund@3274: log.warn("writing image failed", ioe); raimund@3274: return empty(); raimund@3274: } raimund@3274: raimund@3274: return new Output(out.toByteArray(), "image/" + format); raimund@3274: } raimund@3274: raimund@3274: protected static JFreeChart createChart( raimund@3274: KMIndex> entries, raimund@3274: String river, raimund@3274: CallMeta callMeta raimund@3274: ) { raimund@3274: raimund@3274: XYSeriesCollection dataset = new XYSeriesCollection(); raimund@3274: String key = Resources.format( raimund@3274: callMeta, I18N_CHART_LABEL, DEFAULT_CHART_LABEL, river); raimund@3274: raimund@3274: XYSeries series = new XYSeries(key); raimund@3274: for (KMIndex.Entry> e: entries) { raimund@3274: double km = e.getKm(); raimund@3274: List ds = e.getValue(); raimund@3274: for (Date d: ds) { raimund@3274: series.add(km, d.getTime()); raimund@3274: } raimund@3274: } raimund@3274: raimund@3274: dataset.addSeries(series); raimund@3274: String title = Resources.format( raimund@3274: callMeta, I18N_CHART_TITLE, DEFAULT_CHART_TITLE, river); raimund@3274: raimund@3274: String kmAxis = Resources.getMsg( raimund@3274: callMeta, I18N_KM_AXIS, DEFAULT_KM_AXIS); raimund@3274: raimund@3274: String dateAxis = Resources.getMsg( raimund@3274: callMeta, I18N_DATE_AXIS, DEFAULT_DATE_AXIS); raimund@3274: raimund@3274: JFreeChart chart = ChartFactory.createXYLineChart( raimund@3274: title, raimund@3274: kmAxis, raimund@3274: dateAxis, raimund@3274: null, raimund@3274: PlotOrientation.VERTICAL, raimund@3274: true, raimund@3274: true, raimund@3274: false); raimund@3274: raimund@3274: XYPlot plot = (XYPlot)chart.getPlot(); raimund@3274: raimund@3274: DateAxis dA = new DateAxis(); raimund@3274: plot.setRangeAxis(dA); raimund@3274: plot.setDataset(0, dataset); raimund@3274: raimund@3274: chart.setBackgroundPaint(Color.white); raimund@3274: plot.setBackgroundPaint(Color.white); raimund@3274: plot.setDomainGridlinePaint(Color.gray); raimund@3274: plot.setRangeGridlinePaint(Color.gray); raimund@3274: plot.setDomainGridlinesVisible(true); raimund@3274: plot.setRangeGridlinesVisible(true); raimund@3274: XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer(); raimund@3274: raimund@3274: renderer.setSeriesPaint(0, Color.gray); raimund@3274: renderer.setSeriesLinesVisible(0, false); raimund@3274: renderer.setSeriesShapesVisible(0, true); raimund@3274: renderer.setDrawOutlines(true); raimund@3274: return chart; raimund@3274: } raimund@3274: raimund@3274: raimund@3274: protected static String getRiverName(Document input) { raimund@3274: NodeList rivers = input.getElementsByTagName("river"); raimund@3274: raimund@3274: if (rivers.getLength() == 0) { raimund@3274: return null; raimund@3274: } raimund@3274: raimund@3274: String river = ((Element)rivers.item(0)).getAttribute("name"); raimund@3274: raimund@3274: return river.length() > 0 ? river : null; raimund@3274: } raimund@3274: raimund@3274: protected static Dimension getExtent(Document input) { raimund@3274: raimund@3274: int width = DEFAULT_WIDTH; raimund@3274: int height = DEFAULT_HEIGHT; raimund@3274: raimund@3274: NodeList extents = input.getElementsByTagName("extent"); raimund@3274: raimund@3274: if (extents.getLength() > 0) { raimund@3274: Element element = (Element)extents.item(0); raimund@3274: String w = element.getAttribute("width"); raimund@3274: String h = element.getAttribute("height"); raimund@3274: raimund@3274: try { raimund@3274: width = Math.max(1, Integer.parseInt(w)); raimund@3274: } raimund@3274: catch (NumberFormatException nfe) { raimund@3274: log.warn("width '" + w + "' is not a valid."); raimund@3274: } raimund@3274: raimund@3274: try { raimund@3274: height = Math.max(1, Integer.parseInt(h)); raimund@3274: } raimund@3274: catch (NumberFormatException nfe) { raimund@3274: log.warn("height '" + h + "' is not a valid"); raimund@3274: } raimund@3274: } raimund@3274: raimund@3274: return new Dimension(width, height); raimund@3274: } raimund@3274: raimund@3274: protected static String getFormat(Document input) { raimund@3274: String format = DEFAULT_FORMAT; raimund@3274: raimund@3274: NodeList formats = input.getElementsByTagName("format"); raimund@3274: raimund@3274: if (formats.getLength() > 0) { raimund@3274: String type = ((Element)formats.item(0)).getAttribute("type"); raimund@3274: if (type.length() > 0) { raimund@3274: format = type; raimund@3274: } raimund@3274: } raimund@3274: raimund@3274: return format; raimund@3274: } raimund@3274: } raimund@3274: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :