raimund@3751: package de.intevation.flys.artifacts.services;
raimund@3751: 
raimund@3751: import java.awt.Color;
raimund@3751: import java.awt.Dimension;
raimund@3751: import java.awt.Transparency;
raimund@3751: import java.awt.image.BufferedImage;
raimund@3751: import java.io.ByteArrayOutputStream;
raimund@3751: import java.io.IOException;
raimund@3751: import java.util.Date;
raimund@3751: import java.util.List;
raimund@3751: 
raimund@3751: import javax.imageio.ImageIO;
raimund@3751: 
raimund@3751: import org.apache.log4j.Logger;
raimund@3751: import org.jfree.chart.ChartFactory;
raimund@3751: import org.jfree.chart.JFreeChart;
raimund@3751: import org.jfree.chart.axis.DateAxis;
raimund@3751: import org.jfree.chart.plot.PlotOrientation;
raimund@3751: import org.jfree.chart.plot.XYPlot;
raimund@3751: import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
raimund@3751: import org.jfree.data.xy.XYSeries;
raimund@3751: import org.jfree.data.xy.XYSeriesCollection;
raimund@3751: import org.w3c.dom.Document;
raimund@3751: import org.w3c.dom.Element;
raimund@3751: import org.w3c.dom.NodeList;
raimund@3751: 
raimund@3751: import de.intevation.artifactdatabase.DefaultService;
raimund@3751: import de.intevation.artifacts.CallMeta;
raimund@3751: import de.intevation.artifacts.GlobalContext;
raimund@3751: import de.intevation.artifacts.Service;
raimund@3751: import de.intevation.flys.artifacts.model.minfo.BedloadOverview;
raimund@3751: import de.intevation.flys.artifacts.model.minfo.BedloadOverviewFactory;
raimund@3751: import de.intevation.flys.artifacts.resources.Resources;
raimund@3751: import de.intevation.flys.backend.SedDBSessionHolder;
raimund@3751: import de.intevation.flys.utils.KMIndex;
raimund@3751: 
raimund@3751: public class BedloadKMChartService extends DefaultService {
raimund@3751: 
raimund@3751:     private static final long serialVersionUID = 4156704841305086495L;
raimund@3751: 
raimund@3751:     private static final Logger log =
raimund@3751:         Logger.getLogger(BedloadKMChartService.class);
raimund@3751: 
raimund@3751:     public static final int DEFAULT_WIDTH  = 240;
raimund@3751:     public static final int DEFAULT_HEIGHT = 180;
raimund@3751: 
raimund@3751:     public static final String I18N_CHART_LABEL =
raimund@3751:         "bedload.km.chart.label";
raimund@3751: 
raimund@3751:     public static final String DEFAULT_CHART_LABEL =
raimund@3751:         "Measuring Points";
raimund@3751: 
raimund@3751:     public static final String I18N_CHART_TITLE =
raimund@3751:         "bedload.km.chart.title";
raimund@3751: 
raimund@3751:     public static final String DEFAULT_CHART_TITLE =
raimund@3751:         "Measuring points";
raimund@3751: 
raimund@3751:     public static final String I18N_KM_AXIS =
raimund@3751:         "bedload.km.chart.km.axis";
raimund@3751: 
raimund@3751:     public static final String DEFAULT_KM_AXIS =
raimund@3751:         "km";
raimund@3751: 
raimund@3751:     public static final String I18N_DATE_AXIS =
raimund@3751:         "bedload.km.chart.date.axis";
raimund@3751: 
raimund@3751:     public static final String DEFAULT_DATE_AXIS =
raimund@3751:         "Date";
raimund@3751: 
raimund@3751:     public static final String DEFAULT_FORMAT = "png";
raimund@3751: 
raimund@3751:     // TODO: Load fancy image from resources.
raimund@3751:     public static final byte [] EMPTY = {
raimund@3751:         (byte)0x89, (byte)0x50, (byte)0x4e, (byte)0x47,
raimund@3751:         (byte)0x0d, (byte)0x0a, (byte)0x1a, (byte)0x0a,
raimund@3751:         (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x0d,
raimund@3751:         (byte)0x49, (byte)0x48, (byte)0x44, (byte)0x52,
raimund@3751:         (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01,
raimund@3751:         (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01,
raimund@3751:         (byte)0x08, (byte)0x00, (byte)0x00, (byte)0x00,
raimund@3751:         (byte)0x00, (byte)0x3a, (byte)0x7e, (byte)0x9b,
raimund@3751:         (byte)0x55, (byte)0x00, (byte)0x00, (byte)0x00,
raimund@3751:         (byte)0x01, (byte)0x73, (byte)0x52, (byte)0x47,
raimund@3751:         (byte)0x42, (byte)0x00, (byte)0xae, (byte)0xce,
raimund@3751:         (byte)0x1c, (byte)0xe9, (byte)0x00, (byte)0x00,
raimund@3751:         (byte)0x00, (byte)0x09, (byte)0x70, (byte)0x48,
raimund@3751:         (byte)0x59, (byte)0x73, (byte)0x00, (byte)0x00,
raimund@3751:         (byte)0x0b, (byte)0x13, (byte)0x00, (byte)0x00,
raimund@3751:         (byte)0x0b, (byte)0x13, (byte)0x01, (byte)0x00,
raimund@3751:         (byte)0x9a, (byte)0x9c, (byte)0x18, (byte)0x00,
raimund@3751:         (byte)0x00, (byte)0x00, (byte)0x07, (byte)0x74,
raimund@3751:         (byte)0x49, (byte)0x4d, (byte)0x45, (byte)0x07,
raimund@3751:         (byte)0xdc, (byte)0x04, (byte)0x04, (byte)0x10,
raimund@3751:         (byte)0x30, (byte)0x15, (byte)0x7d, (byte)0x77,
raimund@3751:         (byte)0x36, (byte)0x0b, (byte)0x00, (byte)0x00,
raimund@3751:         (byte)0x00, (byte)0x08, (byte)0x74, (byte)0x45,
raimund@3751:         (byte)0x58, (byte)0x74, (byte)0x43, (byte)0x6f,
raimund@3751:         (byte)0x6d, (byte)0x6d, (byte)0x65, (byte)0x6e,
raimund@3751:         (byte)0x74, (byte)0x00, (byte)0xf6, (byte)0xcc,
raimund@3751:         (byte)0x96, (byte)0xbf, (byte)0x00, (byte)0x00,
raimund@3751:         (byte)0x00, (byte)0x0a, (byte)0x49, (byte)0x44,
raimund@3751:         (byte)0x41, (byte)0x54, (byte)0x08, (byte)0xd7,
raimund@3751:         (byte)0x63, (byte)0xf8, (byte)0x0f, (byte)0x00,
raimund@3751:         (byte)0x01, (byte)0x01, (byte)0x01, (byte)0x00,
raimund@3751:         (byte)0x1b, (byte)0xb6, (byte)0xee, (byte)0x56,
raimund@3751:         (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
raimund@3751:         (byte)0x49, (byte)0x45, (byte)0x4e, (byte)0x44,
raimund@3751:         (byte)0xae, (byte)0x42, (byte)0x60, (byte)0x82
raimund@3751:     };
raimund@3751: 
raimund@3751:     private static final Output empty() {
raimund@3751:         return new Output(EMPTY, "image/png");
raimund@3751:     }
raimund@3751: 
raimund@3751:     @Override
raimund@3751:     public Service.Output process(
raimund@3751:         Document      data,
raimund@3751:         GlobalContext globalContext,
raimund@3751:         CallMeta      callMeta
raimund@3751:     ) {
raimund@3751:         log.debug("SQKMChartService.process");
raimund@3751: 
raimund@3751:         SedDBSessionHolder.acquire();
raimund@3751:         try {
raimund@3751:             return doProcess(data, globalContext, callMeta);
raimund@3751:         }
raimund@3751:         finally {
raimund@3751:             SedDBSessionHolder.HOLDER.get().close();
raimund@3751:             SedDBSessionHolder.release();
raimund@3751:         }
raimund@3751:     }
raimund@3751: 
raimund@3751:     protected Service.Output doProcess(
raimund@3751:         Document      input,
raimund@3751:         GlobalContext globalContext,
raimund@3751:         CallMeta      callMeta
raimund@3751:     ) {
raimund@3751:         String    river  = getRiverName(input);
raimund@3751:         Dimension extent = getExtent(input);
raimund@3751:         String    format = getFormat(input);
raimund@3751: 
raimund@3751:         if (river == null) {
raimund@3751:             log.warn("River invalid.");
raimund@3751:             return empty();
raimund@3751:         }
raimund@3751: 
raimund@3751:         BedloadOverview overview = BedloadOverviewFactory.getOverview(river);
raimund@3751: 
raimund@3751:         if (overview == null) {
raimund@3751:             log.warn("No overview found for river '" + river + "'");
raimund@3751:             return empty();
raimund@3751:         }
raimund@3751: 
raimund@3751:         KMIndex<List<Date>> entries = overview.filter(BedloadOverview.ACCEPT);
raimund@3751: 
raimund@3751:         JFreeChart chart = createChart(entries, river, callMeta);
raimund@3751: 
raimund@3751:         return encode(chart, extent, format);
raimund@3751:     }
raimund@3751: 
raimund@3751:     protected static Output encode(
raimund@3751:         JFreeChart chart,
raimund@3751:         Dimension  extent,
raimund@3751:         String     format
raimund@3751:     ) {
raimund@3751:         BufferedImage image = chart.createBufferedImage(
raimund@3751:             extent.width, extent.height,
raimund@3751:             Transparency.BITMASK,
raimund@3751:             null);
raimund@3751: 
raimund@3751:         ByteArrayOutputStream out = new ByteArrayOutputStream();
raimund@3751: 
raimund@3751:         try {
raimund@3751:             ImageIO.write(image, format, out);
raimund@3751:         }
raimund@3751:         catch (IOException ioe) {
raimund@3751:             log.warn("writing image failed", ioe);
raimund@3751:             return empty();
raimund@3751:         }
raimund@3751: 
raimund@3751:         return new Output(out.toByteArray(), "image/" + format);
raimund@3751:     }
raimund@3751: 
raimund@3751:     protected static JFreeChart createChart(
raimund@3751:         KMIndex<List<Date>> entries,
raimund@3751:         String      river,
raimund@3751:         CallMeta    callMeta
raimund@3751:     ) {
raimund@3751: 
raimund@3751:         XYSeriesCollection dataset = new XYSeriesCollection();
raimund@3751:         String key = Resources.format(
raimund@3751:             callMeta, I18N_CHART_LABEL, DEFAULT_CHART_LABEL, river);
raimund@3751: 
raimund@3751:         XYSeries series = new XYSeries(key);
raimund@3751:         for (KMIndex.Entry<List<Date>> e: entries) {
raimund@3751:             double km = e.getKm();
raimund@3751:             List<Date> ds = e.getValue();
raimund@3751:             for (Date d: ds) {
raimund@3751:                 series.add(km, d.getTime());
raimund@3751:             }
raimund@3751:         }
raimund@3751: 
raimund@3751:         dataset.addSeries(series);
raimund@3751:         String title = Resources.format(
raimund@3751:             callMeta, I18N_CHART_TITLE, DEFAULT_CHART_TITLE, river);
raimund@3751: 
raimund@3751:         String kmAxis = Resources.getMsg(
raimund@3751:             callMeta, I18N_KM_AXIS, DEFAULT_KM_AXIS);
raimund@3751: 
raimund@3751:         String dateAxis = Resources.getMsg(
raimund@3751:             callMeta, I18N_DATE_AXIS, DEFAULT_DATE_AXIS);
raimund@3751: 
raimund@3751:         JFreeChart chart = ChartFactory.createXYLineChart(
raimund@3751:             title,
raimund@3751:             kmAxis,
raimund@3751:             dateAxis,
raimund@3751:             null,
raimund@3751:             PlotOrientation.VERTICAL,
raimund@3751:             true,
raimund@3751:             true,
raimund@3751:             false);
raimund@3751: 
raimund@3751:         XYPlot plot = (XYPlot)chart.getPlot();
raimund@3751: 
raimund@3751:         DateAxis dA = new DateAxis();
raimund@3751:         plot.setRangeAxis(dA);
raimund@3751:         plot.setDataset(0, dataset);
raimund@3751: 
raimund@3751:         chart.setBackgroundPaint(Color.white);
raimund@3751:         plot.setBackgroundPaint(Color.white);
raimund@3751:         plot.setDomainGridlinePaint(Color.gray);
raimund@3751:         plot.setRangeGridlinePaint(Color.gray);
raimund@3751:         plot.setDomainGridlinesVisible(true);
raimund@3751:         plot.setRangeGridlinesVisible(true);
raimund@3751:         XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
raimund@3751: 
raimund@3751:         renderer.setSeriesPaint(0, Color.gray);
raimund@3751:         renderer.setSeriesLinesVisible(0, false);
raimund@3751:         renderer.setSeriesShapesVisible(0, true);
raimund@3751:         renderer.setDrawOutlines(true);
raimund@3751:         return chart;
raimund@3751:     }
raimund@3751: 
raimund@3751: 
raimund@3751:     protected static String getRiverName(Document input) {
raimund@3751:         NodeList rivers = input.getElementsByTagName("river");
raimund@3751: 
raimund@3751:         if (rivers.getLength() == 0) {
raimund@3751:             return null;
raimund@3751:         }
raimund@3751: 
raimund@3751:         String river = ((Element)rivers.item(0)).getAttribute("name");
raimund@3751: 
raimund@3751:         return river.length() > 0 ? river : null;
raimund@3751:     }
raimund@3751: 
raimund@3751:     protected static Dimension getExtent(Document input) {
raimund@3751: 
raimund@3751:         int width  = DEFAULT_WIDTH;
raimund@3751:         int height = DEFAULT_HEIGHT;
raimund@3751: 
raimund@3751:         NodeList extents = input.getElementsByTagName("extent");
raimund@3751: 
raimund@3751:         if (extents.getLength() > 0) {
raimund@3751:             Element element = (Element)extents.item(0);
raimund@3751:             String w = element.getAttribute("width");
raimund@3751:             String h = element.getAttribute("height");
raimund@3751: 
raimund@3751:             try {
raimund@3751:                 width = Math.max(1, Integer.parseInt(w));
raimund@3751:             }
raimund@3751:             catch (NumberFormatException nfe) {
raimund@3751:                 log.warn("width '" + w + "' is not a valid.");
raimund@3751:             }
raimund@3751: 
raimund@3751:             try {
raimund@3751:                 height = Math.max(1, Integer.parseInt(h));
raimund@3751:             }
raimund@3751:             catch (NumberFormatException nfe) {
raimund@3751:                 log.warn("height '" + h + "' is not a valid");
raimund@3751:             }
raimund@3751:         }
raimund@3751: 
raimund@3751:         return new Dimension(width, height);
raimund@3751:     }
raimund@3751: 
raimund@3751:     protected static String getFormat(Document input) {
raimund@3751:         String format = DEFAULT_FORMAT;
raimund@3751: 
raimund@3751:         NodeList formats = input.getElementsByTagName("format");
raimund@3751: 
raimund@3751:         if (formats.getLength() > 0) {
raimund@3751:             String type = ((Element)formats.item(0)).getAttribute("type");
raimund@3751:             if (type.length() > 0) {
raimund@3751:                 format = type;
raimund@3751:             }
raimund@3751:         }
raimund@3751: 
raimund@3751:         return format;
raimund@3751:     }
raimund@3751: }