changeset 418:e0fec407a280

ISSUE-53 Formatted number values of CSV exports. flys-artifacts/trunk@1885 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 10 May 2011 16:10:01 +0000
parents e54053bc0e70
children 4de7d9eac10f
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/resources/Resources.java 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/DischargeLongitudinalSectionExporter.java flys-artifacts/src/main/java/de/intevation/flys/exports/DurationCurveExporter.java flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java
diffstat 7 files changed, 173 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Tue May 10 15:28:30 2011 +0000
+++ b/flys-artifacts/ChangeLog	Tue May 10 16:10:01 2011 +0000
@@ -1,3 +1,20 @@
+2011-05-10  Ingo Weinzierl <ingo@intevation.de>
+
+	  ISSUE-53
+
+	* src/main/java/de/intevation/flys/artifacts/resources/Resources.java:
+	  Added a method that returns the preferred locale based on the available
+	  locales of the server and the desired locales of the request (CallMeta).
+
+	* src/main/java/de/intevation/flys/exports/AbstractExporter.java: Added a
+	  method that creates a number formatter with minimum and maximum digits.
+
+	* src/main/java/de/intevation/flys/exports/DischargeLongitudinalSectionExporter.java,
+	  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:
+	  Formatted the number values of the CSV exports.
+
 2011-05-10  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/model/RangeWithValues.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/resources/Resources.java	Tue May 10 15:28:30 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/resources/Resources.java	Tue May 10 16:10:01 2011 +0000
@@ -58,6 +58,16 @@
     }
 
 
+    public static Locale getLocale(CallMeta meta) {
+        if (INSTANCE == null) {
+            INSTANCE = new Resources();
+        }
+
+        Locale[] locales = INSTANCE.getLocales();
+        return meta.getPreferredLocale(locales);
+    }
+
+
     /**
      * This method returns the translated value for <i>key</i> or <i>def</i> if
      * <i>key</i> is not existing in the resource bundle.
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/AbstractExporter.java	Tue May 10 15:28:30 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/AbstractExporter.java	Tue May 10 16:10:01 2011 +0000
@@ -3,6 +3,8 @@
 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;
 
@@ -179,5 +181,16 @@
 
         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 10 15:28:30 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/ComputedDischargeCurveExporter.java	Tue May 10 16:10:01 2011 +0000
@@ -1,6 +1,7 @@
 package de.intevation.flys.exports;
 
 import java.io.OutputStream;
+import java.text.NumberFormat;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -26,6 +27,12 @@
     private static Logger logger =
         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";
 
@@ -59,6 +66,9 @@
 
         writeCSVHeader(writer);
 
+        NumberFormat wf  = getWFormatter();
+        NumberFormat qf  = getQFormatter();
+
         double[] res = new double[3];
 
         for (WQKms wqkms: data) {
@@ -68,8 +78,8 @@
                 res = wqkms.get(i, res);
 
                 writer.writeNext(new String[] {
-                    Double.toString(res[0]),
-                    Double.toString(res[1])
+                    wf.format(res[0]),
+                    qf.format(res[1])
                 });
             }
         }
@@ -84,5 +94,25 @@
             msg(CSV_Q_HEADER, DEFAULT_CSV_Q_HEADER)
         });
     }
+
+
+    /**
+     * Returns the number formatter for W values.
+     *
+     * @return the number formatter for W values.
+     */
+    protected NumberFormat getWFormatter() {
+        return getFormatter(W_MIN_DIGITS, W_MAX_DIGITS);
+    }
+
+
+    /**
+     * Returns the number formatter for Q values.
+     *
+     * @return the number formatter for Q values.
+     */
+    protected NumberFormat getQFormatter() {
+        return getFormatter(Q_MIN_DIGITS, Q_MAX_DIGITS);
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/DischargeLongitudinalSectionExporter.java	Tue May 10 15:28:30 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/DischargeLongitudinalSectionExporter.java	Tue May 10 16:10:01 2011 +0000
@@ -1,5 +1,7 @@
 package de.intevation.flys.exports;
 
+import java.text.NumberFormat;
+
 import org.apache.log4j.Logger;
 
 import au.com.bytecode.opencsv.CSVWriter;
@@ -68,19 +70,23 @@
         int      size   = wqkms.size();
         double[] result = new double[4];
 
+        NumberFormat kmf = getKmFormatter();
+        NumberFormat wf  = getWFormatter();
+        NumberFormat qf  = getQFormatter();
+
         for (int i = 0; i < size; i ++) {
             result = wqkms.get(i, result);
 
             String wc = "";
             if (wqkms instanceof WQCKms) {
-                wc = Double.toString(result[3]);
+                wc = wf.format(result[3]);
             }
 
             writer.writeNext(new String[] {
-                Double.toString(result[2]),
-                Double.toString(result[0]),
+                kmf.format(result[2]),
+                wf.format(result[0]),
                 wc,
-                Double.toString(result[1])
+                qf.format(result[1])
             });
         }
     }
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/DurationCurveExporter.java	Tue May 10 15:28:30 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/DurationCurveExporter.java	Tue May 10 16:10:01 2011 +0000
@@ -1,6 +1,7 @@
 package de.intevation.flys.exports;
 
 import java.io.OutputStream;
+import java.text.NumberFormat;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -26,6 +27,13 @@
     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";
 
@@ -86,12 +94,46 @@
 
         int size = wqday.size();
 
+        NumberFormat wf  = getWFormatter();
+        NumberFormat qf  = getQFormatter();
+        NumberFormat df  = getDFormatter();
+
         for (int i = 0; i < size; i ++) {
             writer.writeNext(new String[] {
-                Double.toString(wqday.getW(i)),
-                Double.toString(wqday.getQ(i)),
-                Double.toString(wqday.getDay(i))
+                wf.format(wqday.getW(i)),
+                qf.format(wqday.getQ(i)),
+                df.format(wqday.getDay(i))
             });
         }
     }
+
+
+    /**
+     * Returns the number formatter for W values.
+     *
+     * @return the number formatter for W values.
+     */
+    protected NumberFormat getWFormatter() {
+        return getFormatter(W_MIN_DIGITS, W_MAX_DIGITS);
+    }
+
+
+    /**
+     * Returns the number formatter for Q values.
+     *
+     * @return the number formatter for Q values.
+     */
+    protected NumberFormat getQFormatter() {
+        return getFormatter(Q_MIN_DIGITS, Q_MAX_DIGITS);
+    }
+
+
+    /**
+     * Returns the number formatter for duration values.
+     *
+     * @return the number formatter for duration values.
+     */
+    protected NumberFormat getDFormatter() {
+        return getFormatter(D_MIN_DIGITS, D_MAX_DIGITS);
+    }
 }
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java	Tue May 10 15:28:30 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java	Tue May 10 16:10:01 2011 +0000
@@ -1,6 +1,7 @@
 package de.intevation.flys.exports;
 
 import java.io.OutputStream;
+import java.text.NumberFormat;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -26,6 +27,14 @@
     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";
 
@@ -102,6 +111,10 @@
     protected void wQKms2CSV(CSVWriter writer, WQKms wqkms) {
         logger.debug("WaterlevelExporter.wQKms2CSV");
 
+        NumberFormat kmf = getKmFormatter();
+        NumberFormat wf  = getWFormatter();
+        NumberFormat qf  = getQFormatter();
+
         int      size   = wqkms.size();
         double[] result = new double[3];
 
@@ -109,11 +122,41 @@
             result = wqkms.get(i, result);
 
             writer.writeNext(new String[] {
-                Double.toString(result[2]),
-                Double.toString(result[0]),
-                Double.toString(result[1])
+                kmf.format(result[2]),
+                wf.format(result[0]),
+                qf.format(result[1])
             });
         }
     }
+
+
+    /**
+     * Returns the number formatter for kilometer values.
+     *
+     * @return the number formatter for kilometer values.
+     */
+    protected NumberFormat getKmFormatter() {
+        return getFormatter(KM_MIN_DIGITS, KM_MAX_DIGITS);
+    }
+
+
+    /**
+     * Returns the number formatter for W values.
+     *
+     * @return the number formatter for W values.
+     */
+    protected NumberFormat getWFormatter() {
+        return getFormatter(W_MIN_DIGITS, W_MAX_DIGITS);
+    }
+
+
+    /**
+     * Returns the number formatter for Q values.
+     *
+     * @return the number formatter for Q values.
+     */
+    protected NumberFormat getQFormatter() {
+        return getFormatter(Q_MIN_DIGITS, Q_MAX_DIGITS);
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org