changeset 445:a7947972fdeb

Added a new class that supports formatters for different types of data. flys-artifacts/trunk@1937 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 17 May 2011 14:22:27 +0000
parents 932a5e3c7fa1
children c0bec245f608
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/exports/AbstractExporter.java flys-artifacts/src/main/java/de/intevation/flys/exports/ComputedDischargeCurveExporter.java flys-artifacts/src/main/java/de/intevation/flys/exports/DurationCurveExporter.java flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java flys-artifacts/src/main/java/de/intevation/flys/utils/Formatter.java
diffstat 6 files changed, 214 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Tue May 17 11:53:02 2011 +0000
+++ b/flys-artifacts/ChangeLog	Tue May 17 14:22:27 2011 +0000
@@ -1,3 +1,16 @@
+2011-05-17  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/utils/Formatter.java: New. This class
+	  supports functions to retrieve formatters for specific types of data
+	  used in FLYS.
+
+	* src/main/java/de/intevation/flys/exports/WaterlevelExporter.java,
+	  src/main/java/de/intevation/flys/exports/DurationCurveExporter.java,
+	  src/main/java/de/intevation/flys/exports/ComputedDischargeCurveExporter.java,
+	  src/main/java/de/intevation/flys/exports/AbstractExporter.java:
+	  Removed the formatter declaration - the whole formatter stuff is done in
+	  Formatter now.
+
 2011-05-17  Ingo Weinzierl <ingo@intevation.de>
 
 	  ISSUE-72
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/AbstractExporter.java	Tue May 17 11:53:02 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/AbstractExporter.java	Tue May 17 14:22:27 2011 +0000
@@ -3,8 +3,6 @@
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
-import java.text.NumberFormat;
-import java.util.Locale;
 
 import org.w3c.dom.Document;
 
@@ -15,6 +13,9 @@
 import de.intevation.artifacts.Artifact;
 import de.intevation.artifacts.CallContext;
 
+import de.intevation.artifacts.common.ArtifactNamespaceContext;
+import de.intevation.artifacts.common.utils.XMLUtils;
+
 import de.intevation.flys.artifacts.resources.Resources;
 
 
@@ -39,6 +40,9 @@
     /** The default separator for the CSV export.*/
     public static final char DEFAULT_CSV_SEPARATOR = ',';
 
+    /** XPath that points to the desired export facet.*/
+    public static final String XPATH_FACET = "/art:action/@art:type";
+
 
     /** The document of the incoming out() request.*/
     protected Document request;
@@ -141,21 +145,44 @@
     protected boolean isFacetValid(String facet) {
         logger.debug("AbstractExporter.isFacetValid");
 
-        if (facet == null || facet.length() == 0) {
+        String thisFacet = getFacet();
+
+        if (thisFacet == null || thisFacet.length() == 0) {
             return false;
         }
-        else if (this.facet == null || this.facet.length() == 0) {
-            logger.debug("Set the facet of this export: " + facet);
-            this.facet = facet;
-
-            return true;
+        else if (facet == null || facet.length() == 0) {
+            return false;
         }
         else {
-            return this.facet.equals(facet);
+            return thisFacet.equals(facet);
         }
     }
 
 
+    /**
+     * Returns the name of the desired facet.
+     *
+     * @return the name of the desired facet.
+     */
+    protected String getFacet() {
+        if (facet == null) {
+            facet = getFacetFromRequest();
+        }
+
+        return facet;
+    }
+
+
+    /**
+     * Extracts the name of the requested facet from request document.
+     *
+     * @return the name of the requested facet.
+     */
+    protected String getFacetFromRequest() {
+        return XMLUtils.xpathString(
+            request, XPATH_FACET, ArtifactNamespaceContext.INSTANCE);
+    }
+
 
     protected String msg(String key, String def) {
         return Resources.getMsg(context.getMeta(), key, def);
@@ -181,16 +208,5 @@
 
         writer.close();
     }
-
-
-    protected NumberFormat getFormatter(int min, int max) {
-        Locale       locale = Resources.getLocale(context.getMeta());
-        NumberFormat nf     = NumberFormat.getInstance(locale);
-
-        nf.setMaximumFractionDigits(max);
-        nf.setMinimumFractionDigits(min);
-
-        return nf;
-    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/ComputedDischargeCurveExporter.java	Tue May 17 11:53:02 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/ComputedDischargeCurveExporter.java	Tue May 17 14:22:27 2011 +0000
@@ -16,6 +16,7 @@
 
 import de.intevation.flys.artifacts.WINFOArtifact;
 import de.intevation.flys.artifacts.model.WQKms;
+import de.intevation.flys.utils.Formatter;
 
 
 /**
@@ -28,11 +29,6 @@
         Logger.getLogger(ComputedDischargeCurveExporter.class);
 
 
-    public static final int W_MIN_DIGITS  = 2;
-    public static final int W_MAX_DIGITS  = 2;
-    public static final int Q_MIN_DIGITS  = 0;
-    public static final int Q_MAX_DIGITS  = 0;
-
     public static final String CSV_W_HEADER =
         "export.computed.discharge.curve.csv.header.w";
 
@@ -102,7 +98,7 @@
      * @return the number formatter for W values.
      */
     protected NumberFormat getWFormatter() {
-        return getFormatter(W_MIN_DIGITS, W_MAX_DIGITS);
+        return Formatter.getComputedDischargeW(context);
     }
 
 
@@ -112,7 +108,7 @@
      * @return the number formatter for Q values.
      */
     protected NumberFormat getQFormatter() {
-        return getFormatter(Q_MIN_DIGITS, Q_MAX_DIGITS);
+        return Formatter.getComputedDischargeQ(context);
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/DurationCurveExporter.java	Tue May 17 11:53:02 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/DurationCurveExporter.java	Tue May 17 14:22:27 2011 +0000
@@ -16,6 +16,7 @@
 
 import de.intevation.flys.artifacts.WINFOArtifact;
 import de.intevation.flys.artifacts.model.WQDay;
+import de.intevation.flys.utils.Formatter;
 
 
 /**
@@ -27,13 +28,6 @@
     private static Logger logger = Logger.getLogger(WaterlevelExporter.class);
 
 
-    public static final int W_MIN_DIGITS = 0;
-    public static final int W_MAX_DIGITS = 2;
-    public static final int Q_MIN_DIGITS = 0;
-    public static final int Q_MAX_DIGITS = 1;
-    public static final int D_MIN_DIGITS = 0;
-    public static final int D_MAX_DIGITS = 0;
-
     public static final String CSV_DURATION_HEADER =
         "export.duration.curve.csv.header.duration";
 
@@ -114,7 +108,7 @@
      * @return the number formatter for W values.
      */
     protected NumberFormat getWFormatter() {
-        return getFormatter(W_MIN_DIGITS, W_MAX_DIGITS);
+        return Formatter.getDurationW(context);
     }
 
 
@@ -124,7 +118,7 @@
      * @return the number formatter for Q values.
      */
     protected NumberFormat getQFormatter() {
-        return getFormatter(Q_MIN_DIGITS, Q_MAX_DIGITS);
+        return Formatter.getDurationQ(context);
     }
 
 
@@ -134,6 +128,6 @@
      * @return the number formatter for duration values.
      */
     protected NumberFormat getDFormatter() {
-        return getFormatter(D_MIN_DIGITS, D_MAX_DIGITS);
+        return Formatter.getDurationD(context);
     }
 }
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java	Tue May 17 11:53:02 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java	Tue May 17 14:22:27 2011 +0000
@@ -16,6 +16,7 @@
 
 import de.intevation.flys.artifacts.WINFOArtifact;
 import de.intevation.flys.artifacts.model.WQKms;
+import de.intevation.flys.utils.Formatter;
 
 
 /**
@@ -27,14 +28,6 @@
     private static Logger logger = Logger.getLogger(WaterlevelExporter.class);
 
 
-    public static final int KM_MIN_DIGITS = 3;
-    public static final int KM_MAX_DIGITS = 3;
-    public static final int W_MIN_DIGITS  = 0;
-    public static final int W_MAX_DIGITS  = 2;
-    public static final int Q_MIN_DIGITS  = 0;
-    public static final int Q_MAX_DIGITS  = 2;
-
-
     public static final String CSV_KM_HEADER =
         "export.waterlevel.csv.header.km";
 
@@ -136,7 +129,7 @@
      * @return the number formatter for kilometer values.
      */
     protected NumberFormat getKmFormatter() {
-        return getFormatter(KM_MIN_DIGITS, KM_MAX_DIGITS);
+        return Formatter.getWaterlevelKM(context);
     }
 
 
@@ -146,7 +139,7 @@
      * @return the number formatter for W values.
      */
     protected NumberFormat getWFormatter() {
-        return getFormatter(W_MIN_DIGITS, W_MAX_DIGITS);
+        return Formatter.getWaterlevelW(context);
     }
 
 
@@ -156,7 +149,7 @@
      * @return the number formatter for Q values.
      */
     protected NumberFormat getQFormatter() {
-        return getFormatter(Q_MIN_DIGITS, Q_MAX_DIGITS);
+        return Formatter.getWaterlevelQ(context);
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/Formatter.java	Tue May 17 14:22:27 2011 +0000
@@ -0,0 +1,154 @@
+package de.intevation.flys.utils;
+
+import java.text.NumberFormat;
+import java.util.Locale;
+
+import de.intevation.artifacts.CallContext;
+
+import de.intevation.flys.artifacts.resources.Resources;
+
+
+public final class Formatter {
+
+    // WATERLEVEL FORMATTER CONSTANTS
+    public static final int WATERLEVEL_KM_MIN_DIGITS = 3;
+    public static final int WATERLEVEL_KM_MAX_DIGITS = 3;
+    public static final int WATERLEVEL_W_MIN_DIGITS  = 0;
+    public static final int WATERLEVEL_W_MAX_DIGITS  = 2;
+    public static final int WATERLEVEL_Q_MIN_DIGITS  = 0;
+    public static final int WATERLEVEL_Q_MAX_DIGITS  = 2;
+
+
+    // COMPUTED DISCHARGE CURVE FORMATTER CONSTANTS
+    public static final int COMPUTED_DISCHARGE_W_MIN_DIGITS  = 2;
+    public static final int COMPUTED_DISCHARGE_W_MAX_DIGITS  = 2;
+    public static final int COMPUTED_DISCHARGE_Q_MIN_DIGITS  = 0;
+    public static final int COMPUTED_DISCHARGE_Q_MAX_DIGITS  = 0;
+
+
+    // DURATION CURVE FORMATTER CONSTANTS
+    public static final int DURATION_W_MIN_DIGITS = 0;
+    public static final int DURATION_W_MAX_DIGITS = 2;
+    public static final int DURATION_Q_MIN_DIGITS = 0;
+    public static final int DURATION_Q_MAX_DIGITS = 1;
+    public static final int DURATION_D_MIN_DIGITS = 0;
+    public static final int DURATION_D_MAX_DIGITS = 0;
+
+
+    public static NumberFormat getFormatter(CallContext c, int min, int max){
+        Locale       locale = Resources.getLocale(c.getMeta());
+        NumberFormat nf     = NumberFormat.getInstance(locale);
+
+        nf.setMaximumFractionDigits(max);
+        nf.setMinimumFractionDigits(min);
+
+        return nf;
+    }
+
+
+    /**
+     * Returns the number formatter for kilometer values in waterlevel exports.
+     *
+     * @return the number formatter for kilometer values.
+     */
+    public static NumberFormat getWaterlevelKM(CallContext context) {
+        return getFormatter(
+            context,
+            WATERLEVEL_KM_MIN_DIGITS,
+            WATERLEVEL_KM_MAX_DIGITS);
+    }
+
+
+    /**
+     * Returns the number formatter for W values in waterlevel exports.
+     *
+     * @return the number formatter for W values.
+     */
+    public static NumberFormat getWaterlevelW(CallContext context) {
+        return getFormatter(
+            context,
+            WATERLEVEL_W_MIN_DIGITS,
+            WATERLEVEL_W_MAX_DIGITS);
+    }
+
+
+    /**
+     * Returns the number formatter for Q values in waterlevel exports.
+     *
+     * @return the number formatter for Q values.
+     */
+    public static NumberFormat getWaterlevelQ(CallContext context) {
+        return getFormatter(
+            context,
+            WATERLEVEL_Q_MIN_DIGITS,
+            WATERLEVEL_Q_MAX_DIGITS);
+    }
+
+
+    /**
+     * Returns the number formatter for W values in exports of computed
+     * discharge curves.
+     *
+     * @return the number formatter for W values.
+     */
+    public static NumberFormat getComputedDischargeW(CallContext context) {
+        return getFormatter(
+            context,
+            COMPUTED_DISCHARGE_W_MIN_DIGITS,
+            COMPUTED_DISCHARGE_W_MAX_DIGITS);
+    }
+
+
+    /**
+     * Returns the number formatter for Q values in exports of computed
+     * discharge curves.
+     *
+     * @return the number formatter for Q values.
+     */
+    public static NumberFormat getComputedDischargeQ(CallContext context) {
+        return getFormatter(
+            context,
+            COMPUTED_DISCHARGE_Q_MIN_DIGITS,
+            COMPUTED_DISCHARGE_Q_MAX_DIGITS);
+    }
+
+
+    /**
+     * Returns the number formatter for W values in duration curve exports.
+     *
+     * @return the number formatter for W values.
+     */
+    public static NumberFormat getDurationW(CallContext context) {
+        return getFormatter(
+            context,
+            DURATION_W_MIN_DIGITS,
+            DURATION_W_MAX_DIGITS);
+    }
+
+
+    /**
+     * Returns the number formatter for Q values in duration curve exports.
+     *
+     * @return the number formatter for W values.
+     */
+    public static NumberFormat getDurationQ(CallContext context) {
+        return getFormatter(
+            context,
+            DURATION_Q_MIN_DIGITS,
+            DURATION_Q_MAX_DIGITS);
+    }
+
+
+    /**
+     * Returns the number formatter for D values in duration curve exports.
+     *
+     * @return the number formatter for W values.
+     */
+    public static NumberFormat getDurationD(CallContext context) {
+        return getFormatter(
+            context,
+            DURATION_D_MIN_DIGITS,
+            DURATION_D_MAX_DIGITS);
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org