diff artifacts/src/main/java/org/dive4elements/river/artifacts/services/DischargeTablesOverview.java @ 9312:740d65e4aa14

Q [m³/s] one message
author gernotbelger
date Thu, 26 Jul 2018 15:54:20 +0200
parents 5e38e2924c07
children
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/services/DischargeTablesOverview.java	Thu Jul 26 11:16:06 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/services/DischargeTablesOverview.java	Thu Jul 26 15:54:20 2018 +0200
@@ -18,6 +18,16 @@
 import java.util.Locale;
 
 import org.apache.log4j.Logger;
+import org.dive4elements.artifacts.CallMeta;
+import org.dive4elements.artifacts.GlobalContext;
+import org.dive4elements.river.artifacts.model.DischargeTables;
+import org.dive4elements.river.artifacts.model.GaugesFactory;
+import org.dive4elements.river.artifacts.resources.Resources;
+import org.dive4elements.river.backend.SessionHolder;
+import org.dive4elements.river.model.DischargeTable;
+import org.dive4elements.river.model.Gauge;
+import org.dive4elements.river.model.MainValue;
+import org.dive4elements.river.model.TimeInterval;
 import org.jfree.chart.ChartFactory;
 import org.jfree.chart.JFreeChart;
 import org.jfree.chart.plot.Marker;
@@ -29,50 +39,29 @@
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
 
-import org.dive4elements.artifacts.CallMeta;
-import org.dive4elements.artifacts.GlobalContext;
-import org.dive4elements.river.artifacts.model.DischargeTables;
-import org.dive4elements.river.artifacts.model.GaugesFactory;
-import org.dive4elements.river.artifacts.resources.Resources;
-import org.dive4elements.river.backend.SessionHolder;
-import org.dive4elements.river.model.DischargeTable;
-import org.dive4elements.river.model.Gauge;
-import org.dive4elements.river.model.MainValue;
-import org.dive4elements.river.model.TimeInterval;
-
-
 /** Generate Discharge Table chart. */
 public class DischargeTablesOverview extends AbstractChartService {
 
-    private static final Logger log = Logger
-        .getLogger(DischargeTablesOverview.class);
+    private static final Logger log = Logger.getLogger(DischargeTablesOverview.class);
 
     private static final long serialVersionUID = 1L;
 
-    public static final String I18N_CHART_TITLE =
-        "gauge.discharge.service.chart.title";
+    public static final String I18N_CHART_TITLE = "gauge.discharge.service.chart.title";
     public static final String DEFAULT_CHART_TITLE = "Pegel: XXX";
 
-    public static final String I18N_CHART_X_AXIS_TITLE =
-        "gauge.discharge.service.chart.x.title";
+    public static final String I18N_CHART_X_AXIS_TITLE = "common.export.csv.header.q";
     public static final String DEFAULT_X_AXIS_TITLE = "Q [m^3/s]";
 
-    public static final String I18N_CHART_Y_AXIS_TITLE =
-        "gauge.discharge.service.chart.y.title";
+    public static final String I18N_CHART_Y_AXIS_TITLE = "gauge.discharge.service.chart.y.title";
     public static final String DEFAULT_Y_AXIS_TITLE = "W [cm]";
 
-    public static final String I18N_CHART_SERIES_TITLE =
-        "gauge.discharge.service.chart.series.title";
+    public static final String I18N_CHART_SERIES_TITLE = "gauge.discharge.service.chart.series.title";
     public static final String DEFAULT_CHART_SERIES_TITLE = "Abflusskurve";
 
-    public static final String I18N_CHART_SERIES_TITLE_MASTER =
-        "gauge.discharge.service.chart.series.title.master";
-    public static final String DEFAULT_CHART_SERIES_TITLE_MASTER =
-        "Aktuelle Abflusskurve";
+    public static final String I18N_CHART_SERIES_TITLE_MASTER = "gauge.discharge.service.chart.series.title.master";
+    public static final String DEFAULT_CHART_SERIES_TITLE_MASTER = "Aktuelle Abflusskurve";
 
-    public static final DateFormat DATE_FORMAT = DateFormat.getDateInstance(
-        DateFormat.SHORT, Locale.GERMANY);
-
+    public static final DateFormat DATE_FORMAT = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMANY);
 
     @Override
     protected void init() {
@@ -84,10 +73,10 @@
         SessionHolder.release();
     }
 
-    protected JFreeChart createChart(Document data,
-        GlobalContext globalContext, CallMeta callMeta) {
+    @Override
+    protected JFreeChart createChart(final Document data, final GlobalContext globalContext, final CallMeta callMeta) {
 
-        Gauge gauge = extractGauge(data);
+        final Gauge gauge = extractGauge(data);
 
         if (gauge == null) {
             log.warn("Could not determine Gauge from request!");
@@ -95,39 +84,34 @@
         }
 
         log.info("create discharge chart for gauge '" + gauge.getName() + "'");
-        TimeInterval timerange = extractTimeInterval(data);
+        final TimeInterval timerange = extractTimeInterval(data);
 
-        List<DischargeTable> dts = getDischargeTables(gauge, timerange);
-        XYSeriesCollection dataset = new XYSeriesCollection();
+        final List<DischargeTable> dts = getDischargeTables(gauge, timerange);
+        final XYSeriesCollection dataset = new XYSeriesCollection();
 
-        for (DischargeTable dt : dts) {
+        for (final DischargeTable dt : dts) {
             try {
-                XYSeries series = createSeries(callMeta, dt);
+                final XYSeries series = createSeries(callMeta, dt);
                 if (series != null) {
                     dataset.addSeries(series);
                 }
             }
-            catch (IllegalArgumentException iae) {
-                log.warn("unable to create discharge curve: "
-                    + iae.getMessage());
+            catch (final IllegalArgumentException iae) {
+                log.warn("unable to create discharge curve: " + iae.getMessage());
             }
         }
 
-        String title = Resources.format(callMeta, I18N_CHART_TITLE,
-            DEFAULT_CHART_TITLE, gauge.getName());
+        final String title = Resources.format(callMeta, I18N_CHART_TITLE, DEFAULT_CHART_TITLE, gauge.getName());
 
-        String xAxis = Resources.getMsg(callMeta, I18N_CHART_X_AXIS_TITLE,
-            DEFAULT_X_AXIS_TITLE);
+        final String xAxis = Resources.getMsg(callMeta, I18N_CHART_X_AXIS_TITLE, DEFAULT_X_AXIS_TITLE);
 
-        String yAxis = Resources.format(callMeta, I18N_CHART_Y_AXIS_TITLE,
-            DEFAULT_Y_AXIS_TITLE);
+        final String yAxis = Resources.format(callMeta, I18N_CHART_Y_AXIS_TITLE, DEFAULT_Y_AXIS_TITLE);
 
-        JFreeChart chart = ChartFactory.createXYLineChart(title, xAxis, yAxis,
-            null, PlotOrientation.VERTICAL, true, true, false);
+        final JFreeChart chart = ChartFactory.createXYLineChart(title, xAxis, yAxis, null, PlotOrientation.VERTICAL, true, true, false);
 
         chart.setBackgroundPaint(Color.white);
 
-        XYPlot plot = (XYPlot) chart.getPlot();
+        final XYPlot plot = (XYPlot) chart.getPlot();
         plot.setDataset(0, dataset);
         plot.setBackgroundPaint(Color.white);
         plot.setDomainGridlinePaint(Color.gray);
@@ -135,22 +119,18 @@
         plot.setDomainGridlinesVisible(true);
         plot.setRangeGridlinesVisible(true);
 
-        applyMainValueMarkers(
-            plot,
-            gauge,
-            callMeta);
+        applyMainValueMarkers(plot, gauge, callMeta);
 
         return chart;
     }
 
-    protected XYSeries createSeries(CallMeta callMeta, DischargeTable dt)
-        throws IllegalArgumentException {
+    protected XYSeries createSeries(final CallMeta callMeta, final DischargeTable dt) throws IllegalArgumentException {
 
         double[][] xy = null;
 
         xy = DischargeTables.loadDischargeTableValues(dt);
 
-        XYSeries series = new XYSeries(createSeriesTitle(callMeta, dt), false);
+        final XYSeries series = new XYSeries(createSeriesTitle(callMeta, dt), false);
         for (int i = 0, n = xy[0].length; i < n; i++) {
             series.add(xy[0][i], xy[1][i]);
         }
@@ -158,74 +138,56 @@
         return series;
     }
 
-
     /** Add domain markers to plot that indicate mainvalues. */
-    protected static void applyMainValueMarkers(
-        XYPlot   plot,
-        Gauge    gauge,
-        CallMeta meta
-    ) {
-        String river = gauge.getRiver().getName();
-        double km    = gauge.getStation().doubleValue();
+    protected static void applyMainValueMarkers(final XYPlot plot, final Gauge gauge, final CallMeta meta) {
+        final String river = gauge.getRiver().getName();
+        final double km = gauge.getStation().doubleValue();
 
         // Get Gauge s mainvalues.
-        List<MainValue> mainValues = gauge.getMainValues();
-        for (MainValue mainValue : mainValues) {
+        final List<MainValue> mainValues = gauge.getMainValues();
+        for (final MainValue mainValue : mainValues) {
             if (mainValue.getMainValue().getType().getName().equals("Q")) {
                 // Its a Q main value.
-                Marker m = FixingsKMChartService.createQSectorMarker(
-                    mainValue.getValue().doubleValue(),
-                    mainValue.getMainValue().getName());
+                final Marker m = FixingsKMChartService.createQSectorMarker(mainValue.getValue().doubleValue(), mainValue.getMainValue().getName());
                 plot.addDomainMarker(m);
-            }
-            else if (
-                mainValue.getMainValue().getType().getName().equals("W")
-            ) {
+            } else if (mainValue.getMainValue().getType().getName().equals("W")) {
                 // Its a W main value.
-                Marker m = FixingsKMChartService.createQSectorMarker(
-                    mainValue.getValue().doubleValue(),
-                    mainValue.getMainValue().getName());
+                final Marker m = FixingsKMChartService.createQSectorMarker(mainValue.getValue().doubleValue(), mainValue.getMainValue().getName());
                 plot.addRangeMarker(m);
             }
         }
     }
 
-    protected String createSeriesTitle(CallMeta callMeta, DischargeTable dt)
-        throws IllegalArgumentException {
-        TimeInterval timeInterval = dt.getTimeInterval();
+    protected String createSeriesTitle(final CallMeta callMeta, final DischargeTable dt) throws IllegalArgumentException {
+        final TimeInterval timeInterval = dt.getTimeInterval();
 
         if (timeInterval == null) {
             return Resources.format(callMeta, DEFAULT_CHART_SERIES_TITLE);
         }
 
-        Date start = timeInterval.getStartTime();
-        Date end = timeInterval.getStopTime();
+        final Date start = timeInterval.getStartTime();
+        final Date end = timeInterval.getStopTime();
 
         if (start != null && end != null) {
-            return Resources.format(callMeta, I18N_CHART_SERIES_TITLE,
-                DEFAULT_CHART_SERIES_TITLE, start, end);
-        }
-        else if (start != null) {
-            return Resources.format(callMeta, I18N_CHART_SERIES_TITLE_MASTER,
-                DEFAULT_CHART_SERIES_TITLE, start);
-        }
-        else {
-            throw new IllegalArgumentException(
-                "Missing start date of DischargeTable " + dt.getId());
+            return Resources.format(callMeta, I18N_CHART_SERIES_TITLE, DEFAULT_CHART_SERIES_TITLE, start, end);
+        } else if (start != null) {
+            return Resources.format(callMeta, I18N_CHART_SERIES_TITLE_MASTER, DEFAULT_CHART_SERIES_TITLE, start);
+        } else {
+            throw new IllegalArgumentException("Missing start date of DischargeTable " + dt.getId());
         }
     }
 
-    protected Gauge extractGauge(Document data) {
-        NodeList gauges = data.getElementsByTagName("gauge");
+    protected Gauge extractGauge(final Document data) {
+        final NodeList gauges = data.getElementsByTagName("gauge");
 
         if (gauges.getLength() > 0) {
-            String name = ((Element) gauges.item(0)).getAttribute("name");
+            final String name = ((Element) gauges.item(0)).getAttribute("name");
 
             try {
-                long officialNumber = Long.valueOf(name);
+                final long officialNumber = Long.valueOf(name);
                 return Gauge.getGaugeByOfficialNumber(officialNumber);
             }
-            catch (NumberFormatException nfe) {
+            catch (final NumberFormatException nfe) {
                 // it seems, that the client uses the name of the gauge instead
                 // of its official number
             }
@@ -238,23 +200,23 @@
         return null;
     }
 
-    protected TimeInterval extractTimeInterval(Document data) {
-        NodeList timeranges = data.getElementsByTagName("timerange");
+    protected TimeInterval extractTimeInterval(final Document data) {
+        final NodeList timeranges = data.getElementsByTagName("timerange");
 
         if (timeranges != null && timeranges.getLength() > 0) {
-            Element timerange = (Element) timeranges.item(0);
+            final Element timerange = (Element) timeranges.item(0);
 
-            String lower = timerange.getAttribute("lower");
-            String upper = timerange.getAttribute("upper");
+            final String lower = timerange.getAttribute("lower");
+            final String upper = timerange.getAttribute("upper");
 
             if (lower != null && upper != null) {
                 try {
-                    Date d1 = DATE_FORMAT.parse(lower);
-                    Date d2 = DATE_FORMAT.parse(upper);
+                    final Date d1 = DATE_FORMAT.parse(lower);
+                    final Date d2 = DATE_FORMAT.parse(upper);
 
                     return new TimeInterval(d1, d2);
                 }
-                catch (ParseException pe) {
+                catch (final ParseException pe) {
                     log.warn("Wrong time format: " + pe.getMessage());
                 }
             }
@@ -263,36 +225,33 @@
         return null;
     }
 
-    protected List<DischargeTable> getDischargeTables(Gauge gauge,
-        TimeInterval timerange) {
-        List<DischargeTable> all = gauge.getDischargeTables();
+    protected List<DischargeTable> getDischargeTables(final Gauge gauge, final TimeInterval timerange) {
+        final List<DischargeTable> all = gauge.getDischargeTables();
         Collections.sort(all);
 
         if (timerange == null) {
             return all;
         }
 
-        List<DischargeTable> dts = new ArrayList<DischargeTable>(all.size());
-        long startDate = timerange.getStartTime().getTime();
-        long stopDate = timerange.getStopTime().getTime();
+        final List<DischargeTable> dts = new ArrayList<>(all.size());
+        final long startDate = timerange.getStartTime().getTime();
+        final long stopDate = timerange.getStopTime().getTime();
 
-        for (DischargeTable dt : all) {
-            TimeInterval tmp = dt.getTimeInterval();
+        for (final DischargeTable dt : all) {
+            final TimeInterval tmp = dt.getTimeInterval();
             if (tmp == null) {
                 // this should never happen because all discharge tables should
                 // have a time interval set!
                 continue;
             }
 
-            Date start = tmp.getStartTime();
-            Date stop = tmp.getStartTime();
+            final Date start = tmp.getStartTime();
+            final Date stop = tmp.getStartTime();
 
             if (start.getTime() > startDate && start.getTime() < stopDate) {
                 dts.add(dt);
                 continue;
-            }
-            else if (stop != null && stop.getTime() < stopDate
-                && stop.getTime() > startDate) {
+            } else if (stop != null && stop.getTime() < stopDate && stop.getTime() > startDate) {
                 dts.add(dt);
                 continue;
             }

http://dive4elements.wald.intevation.org