changeset 3728:d03e65378b9f

Removed obsolete code. Removed NPE as regular flow control. flys-artifacts/trunk@5401 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sat, 08 Sep 2012 13:36:06 +0000
parents b81f328da582
children e727e3ebdf85
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/MainValuesFactory.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MainValuesService.java
diffstat 3 files changed, 41 insertions(+), 69 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Sat Sep 08 12:58:58 2012 +0000
+++ b/flys-artifacts/ChangeLog	Sat Sep 08 13:36:06 2012 +0000
@@ -1,3 +1,12 @@
+2012-09-08	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/model/MainValuesFactory.java;
+	   Removed because code is found in backend already.
+
+	* src/main/java/de/intevation/flys/artifacts/services/MainValuesService.java:
+	  Simplified. Please _DO_NOT_ use NullPointerExceptions for regular flow control!
+	  This hides unintended errors.
+
 2012-09-08	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/exports/fixings/FixChartGenerator.java,
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/MainValuesFactory.java	Sat Sep 08 12:58:58 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,33 +0,0 @@
-package de.intevation.flys.artifacts.model;
-
-import java.util.List;
-
-import org.apache.log4j.Logger;
-
-import org.hibernate.Session;
-import org.hibernate.Query;
-
-import de.intevation.flys.backend.SessionHolder;
-
-import de.intevation.flys.model.Gauge;
-import de.intevation.flys.model.MainValue;
-
-
-/**
- * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
- */
-public class MainValuesFactory {
-
-    private static Logger logger = Logger.getLogger(MainValuesFactory.class);
-
-    public static List<MainValue> getMainValues(Gauge gauge) {
-        Session session = SessionHolder.HOLDER.get();
-
-        Query query = session.createQuery(
-            "from MainValue where gauge=:gauge");
-        query.setParameter("gauge", gauge);
-
-        return query.list();
-    }
-}
-// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MainValuesService.java	Sat Sep 08 12:58:58 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MainValuesService.java	Sat Sep 08 13:36:06 2012 +0000
@@ -21,7 +21,6 @@
 import de.intevation.flys.model.Range;
 import de.intevation.flys.model.River;
 
-import de.intevation.flys.artifacts.model.MainValuesFactory;
 import de.intevation.flys.artifacts.model.RiverFactory;
 
 
@@ -52,6 +51,11 @@
     public MainValuesService() {
     }
 
+    private static final Document error(String msg) {
+        logger.debug(msg);
+        return XMLUtils.newDocument();
+    }
+
 
     @Override
     public Document doProcess(
@@ -61,23 +65,21 @@
     ) {
         logger.debug("MainValuesService.process");
 
-        try {
-            River river     = getRequestedRiver(data);
-            double[] minmax = getRequestedStartEnd(data, river);
-            Gauge gauge     = river.determineGauge(minmax[0], minmax[1]);
-
-            logger.debug("Found gauge: " + gauge.getName());
+        River river = getRequestedRiver(data);
+        if (river == null) {
+            return error("no river found.");
+        }
 
-            List<MainValue> mainValues = getMainValues(river, gauge);
+        double[] minmax = getRequestedStartEnd(data, river);
+        Gauge gauge = river.determineGauge(minmax[0], minmax[1]);
 
-            return buildDocument(river, gauge, mainValues, context);
+        if (gauge == null) {
+            return error("no gauge found.");
         }
-        catch (NullPointerException npe) {
-            logger.error("Could not process the request.");
-            logger.error(npe, npe);
 
-            return XMLUtils.newDocument();
-        }
+        List<MainValue> mainValues = getMainValues(river, gauge);
+
+        return buildDocument(river, gauge, mainValues, context);
     }
 
 
@@ -98,17 +100,9 @@
         String riverStr = XMLUtils.xpathString(
             data, XPATH_RIVER, ArtifactNamespaceContext.INSTANCE);
 
-        if (riverStr == null || riverStr.trim().length() == 0) {
-            throw new NullPointerException("No river found in the request.");
-        }
-
-        River river = RiverFactory.getRiver(riverStr);
-
-        if (river == null) {
-            throw new NullPointerException("No such river found: " + riverStr);
-        }
-
-        return river;
+         return riverStr != null && (riverStr = riverStr.trim()).length() > 0
+            ? RiverFactory.getRiver(riverStr)
+            : null;
     }
 
 
@@ -132,18 +126,23 @@
         String endStr = XMLUtils.xpathString(
             data, XPATH_END, ArtifactNamespaceContext.INSTANCE);
 
+        if (startStr == null || endStr == null) {
+            return river.determineMinMaxDistance();
+        }
+
         try {
             double start = Double.parseDouble(startStr);
             double end   = Double.parseDouble(endStr);
 
-            logger.debug("Found start: " + start);
-            logger.debug("Found end: " + end);
+            if (logger.isDebugEnabled()) {
+                logger.debug("Found start: " + start);
+                logger.debug("Found end: " + end);
+            }
 
             return new double[] { start, end };
         }
         catch (NumberFormatException nfe) {
             logger.warn(nfe, nfe);
-
             return river.determineMinMaxDistance();
         }
     }
@@ -159,23 +158,20 @@
      * @return a document that includes the main values of the specified river
      * at the specified gauge.
      */
-    protected List<MainValue> getMainValues(River river, Gauge gauge)
-    throws    NullPointerException
-    {
+    protected List<MainValue> getMainValues(River river, Gauge gauge) {
+
         if (logger.isDebugEnabled()) {
             logger.debug("MainValuesService.buildMainValues");
             logger.debug("River: " + river.getName());
             logger.debug("Gauge: " + gauge.getName());
         }
 
-        List<MainValue> mainValues = MainValuesFactory.getMainValues(gauge);
+        List<MainValue> mainValues = gauge.fetchMainValues();
 
-        if (mainValues == null || mainValues.isEmpty()) {
-            throw new NullPointerException("No main values found.");
+        if (logger.isDebugEnabled()) {
+            logger.debug(mainValues.size() + " main values found.");
         }
 
-        logger.debug(mainValues.size() + " main values found.");
-
         return mainValues;
     }
 

http://dive4elements.wald.intevation.org