changeset 2035:3ebbe497d7f7

#252 Set titles in waterlevel exports to named main values if those are existing for given Q values. flys-artifacts/trunk@3504 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 21 Dec 2011 09:17:23 +0000
parents 7bc9293de4e6
children c03aeb0cf46b
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/exports/AbstractExporter.java flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java flys-artifacts/src/main/java/de/intevation/flys/utils/FLYSUtils.java
diffstat 4 files changed, 137 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Tue Dec 20 15:14:54 2011 +0000
+++ b/flys-artifacts/ChangeLog	Wed Dec 21 09:17:23 2011 +0000
@@ -1,3 +1,20 @@
+2011-12-21  Ingo Weinzierl <ingo@intevation.de>
+
+	flys/issue252 (W-INFO: Wasserspiegellagenberechnung / Mitführung der Jährlichkeiten in der Diagramm-/Ergbnisausgabe)
+
+	* src/main/java/de/intevation/flys/utils/FLYSUtils.java: Added a function
+	  stripNamedMainValue(). The result of this function is a named main value's
+	  base name without declaration of a year.
+
+	* src/main/java/de/intevation/flys/exports/AbstractExporter.java: Added a
+	  method pepareData() that is called in doOut() before the data supported by
+	  the current Facet is added using addData().
+
+	* src/main/java/de/intevation/flys/exports/WaterlevelExporter.java: Override
+	  the prepareData() method to reset the name of WQKms objects. The Qs in a
+	  waterlevel export should be the Q value or the named main value if the
+	  value fits to a named main value.
+
 2011-12-20	Felix Wolfsteller	<felix.wolfsteller@intevation.de>
 
 	* src/main/java/de/intevation/flys/exports/XYChartGenerator.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/AbstractExporter.java	Tue Dec 20 15:14:54 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/AbstractExporter.java	Wed Dec 21 09:17:23 2011 +0000
@@ -124,7 +124,10 @@
             return;
         }
 
-        addData(artifactFacet.getData(context));
+        Object data = artifactFacet.getData(context);
+        prepareData(artifactFacet, data);
+
+        addData(data);
     }
 
 
@@ -202,6 +205,19 @@
 
 
     /**
+     * This method can be used to prepare data items before they are injected
+     * into the exporter using <i>addData()</i>. The method in this
+     * implementation is empty and doesn't do anything.
+     *
+     * @param aaf The wrapper for the Artifact and Facet.
+     * @param data The data object.
+     */
+    protected void prepareData(ArtifactAndFacet aaf, Object data) {
+        // override me in subclasses
+    }
+
+
+    /**
      * This method starts CSV creation. It makes use of writeCSVData() which has
      * to be implemented by concrete subclasses.
      */
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java	Tue Dec 20 15:14:54 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java	Wed Dec 21 09:17:23 2011 +0000
@@ -5,6 +5,8 @@
 import java.text.NumberFormat;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.w3c.dom.Document;
 
@@ -12,12 +14,17 @@
 
 import au.com.bytecode.opencsv.CSVWriter;
 
+import de.intevation.artifacts.Artifact;
 import de.intevation.artifacts.CallContext;
 
+import de.intevation.artifactdatabase.state.ArtifactAndFacet;
+
+import de.intevation.flys.artifacts.WINFOArtifact;
 import de.intevation.flys.artifacts.model.CalculationResult;
 import de.intevation.flys.artifacts.model.WQCKms;
 import de.intevation.flys.artifacts.model.WQKms;
 
+import de.intevation.flys.utils.FLYSUtils;
 import de.intevation.flys.utils.Formatter;
 
 
@@ -42,6 +49,9 @@
     public static final String CSV_Q_HEADER =
         "export.waterlevel.csv.header.q";
 
+    public static final Pattern NUMBERS_PATTERN =
+        Pattern.compile("\\D*(\\d++.\\d*)\\D*");
+
     public static final String DEFAULT_CSV_KM_HEADER = "Fluss-Km";
     public static final String DEFAULT_CSV_W_HEADER  = "W [NN + m]";
     public static final String DEFAULT_CSV_Q_HEADER  = "Q [m\u00b3/s]";
@@ -89,6 +99,81 @@
     }
 
 
+    /**
+     * This method is used to determine, if the name of the WQKms object
+     * <i>data</i> matches a named value. In such cases, the name of <i>data</i>
+     * is reset to this named value.
+     *
+     * @param aaf The wrapper for the Artifact and Facet.
+     * @param data Needs to be a CalculationResult that contains a WQKms[]
+     * objects, otherwise nothing is done.
+     */
+    @Override
+    protected void prepareData(ArtifactAndFacet aaf, Object data) {
+        logger.debug("WaterlevelExporter.prepareData");
+
+        Artifact artifact = aaf.getArtifact();
+
+        if (!(artifact instanceof WINFOArtifact)) {
+            logger.debug("Artifact is no WINFOArtifact. Cannot prepare data.");
+            return;
+        }
+
+        if (data instanceof CalculationResult) {
+            data = ((CalculationResult) data).getData();
+
+            if (data instanceof WQKms[]) {
+                WQKms[] wqkms = (WQKms[]) data;
+
+                for (WQKms item: wqkms) {
+                    prepareNamedValue((WINFOArtifact) artifact, item);
+                }
+            }
+        }
+    }
+
+
+    /**
+     * This method is used to prepare the column titles of waterlevel exports.
+     * Titles in this export include the Q value. If a Q value matches a named
+     * main value (as HQ100 or MNQ) this named main value should be used as
+     * title. This method resets the name of the <i>wqkms</i> object if such
+     * named main value fits to the chosen Q.
+     *
+     * @param winfo A WINFO Artifact.
+     * @param wqkms A WQKms object that should be prepared.
+     */
+    protected void prepareNamedValue(WINFOArtifact winfo, WQKms wqkms) {
+        logger.debug("WaterlevelExporter.prepareNamedValue");
+
+        String name = wqkms.getName();
+
+        logger.debug("Name of WQKms = '" + name + "'");
+
+        Matcher m = NUMBERS_PATTERN.matcher(name);
+
+        if (m.matches()) {
+            String raw = m.group(1);
+
+            try {
+                double v = Double.valueOf(raw);
+
+                String nmv = FLYSUtils.getNamedMainValue(winfo.getGauge(), v);
+
+                if (nmv != null && nmv.length() > 0) {
+                    nmv = FLYSUtils.stripNamedMainValue(nmv);
+                    logger.debug("Set named main value '" + nmv + "'");
+
+                    wqkms.setName(nmv);
+                }
+            }
+            catch (NumberFormatException nfe) {
+                // do nothing here
+            }
+        }
+    }
+
+
     @Override
     protected void writeCSVData(CSVWriter writer) {
         logger.info("WaterlevelExporter.writeData");
--- a/flys-artifacts/src/main/java/de/intevation/flys/utils/FLYSUtils.java	Tue Dec 20 15:14:54 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/FLYSUtils.java	Wed Dec 21 09:17:23 2011 +0000
@@ -370,6 +370,24 @@
 
 
     /**
+     *
+     * @param nmv A string that represents a named main value.
+     *
+     * @throws NullPointerException if nmv is null.
+     */
+    public static String stripNamedMainValue(String nmv) {
+        int startIndex = nmv.indexOf("(");
+        int endIndex   = nmv.indexOf(")");
+
+        if (startIndex > 0 && endIndex > 0 && startIndex < endIndex) {
+            return nmv.substring(0, startIndex);
+        }
+
+        return nmv;
+    }
+
+
+    /**
      * Returns the URL of user mapfile for the owner of Artifact
      * <i>artifactId</i>.
      *

http://dive4elements.wald.intevation.org