changeset 2038:f73036b991e2

Bugfix: repaired broken named values in exports - the last solution had bad side effects. flys-artifacts/trunk@3515 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 21 Dec 2011 15:44:41 +0000
parents 986820795c3b
children f7e2a84a6d88
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java 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 5 files changed, 121 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Wed Dec 21 11:56:08 2011 +0000
+++ b/flys-artifacts/ChangeLog	Wed Dec 21 15:44:41 2011 +0000
@@ -1,3 +1,26 @@
+2011-12-21  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java: Added a
+	  convinience method to retrieve the value of an data object stored at
+	  FLYSArtifact as Boolean value.
+
+	* src/main/java/de/intevation/flys/utils/FLYSUtils.java: Added a method that
+	  returns the named value of a given double value. This method returns only
+	  the named value, if the WQ mode is "Q at gauge" and if the value fits to a
+	  named value. In addition to this method, there is a new method to retrieve
+	  the selected WQ mode as 'WQ_MODE' enum.
+
+	* src/main/java/de/intevation/flys/exports/AbstractExporter.java:
+	  Removed prepareData() and its call. The data preparation had a bad side
+	  effect: the modifications are "persisted" into cache, which has again bad
+	  side effects.
+
+	* src/main/java/de/intevation/flys/exports/WaterlevelExporter.java: Removed
+	  prepareData(). The label creation for columns in the WST export will now
+	  take place in addWSTColumn(). With help of the master Artifact (I forgot
+	  this Artifact in my last commit) we are able to replace Q values with
+	  their named main value.
+
 2011-12-21	Felix Wolfsteller	<felix.wolfsteller@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/CrossSectionArtifact.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java	Wed Dec 21 11:56:08 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java	Wed Dec 21 15:44:41 2011 +0000
@@ -575,6 +575,26 @@
 
 
     /**
+     * This method returns the value of a StateData object stored in the data
+     * pool of this Artifact is Boolean using Boolean.valueOf().
+     *
+     * @param name The name of the StateData object.
+     *
+     * @return a Boolean representing the value of the data object or null if no
+     * such object is existing.
+     */
+    public Boolean getDataAsBoolean(String name) {
+        String value = getDataAsString(name);
+
+        if (value == null || value.length() == 0) {
+            return null;
+        }
+
+        return Boolean.valueOf(value);
+    }
+
+
+    /**
      * Add StateData containing a given string.
      * @param name Name of the data object.
      * @param value String to store.
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/AbstractExporter.java	Wed Dec 21 11:56:08 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/AbstractExporter.java	Wed Dec 21 15:44:41 2011 +0000
@@ -124,10 +124,7 @@
             return;
         }
 
-        Object data = artifactFacet.getData(context);
-        prepareData(artifactFacet, data);
-
-        addData(data);
+        addData(artifactFacet.getData(context));
     }
 
 
@@ -205,19 +202,6 @@
 
 
     /**
-     * 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	Wed Dec 21 11:56:08 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java	Wed Dec 21 15:44:41 2011 +0000
@@ -14,11 +14,8 @@
 
 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;
@@ -100,40 +97,6 @@
 
 
     /**
-     * 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
@@ -143,13 +106,17 @@
      * @param winfo A WINFO Artifact.
      * @param wqkms A WQKms object that should be prepared.
      */
-    protected void prepareNamedValue(WINFOArtifact winfo, WQKms wqkms) {
+    protected String getColumnTitle(WINFOArtifact winfo, WQKms wqkms) {
         logger.debug("WaterlevelExporter.prepareNamedValue");
 
         String name = wqkms.getName();
 
         logger.debug("Name of WQKms = '" + name + "'");
 
+        if (name.indexOf("W=") >= 0) {
+            return name;
+        }
+
         Matcher m = NUMBERS_PATTERN.matcher(name);
 
         if (m.matches()) {
@@ -158,19 +125,21 @@
             try {
                 double v = Double.valueOf(raw);
 
-                String nmv = FLYSUtils.getNamedMainValue(winfo.getGauge(), v);
+                String nmv = FLYSUtils.getNamedMainValue(winfo, v);
 
                 if (nmv != null && nmv.length() > 0) {
                     nmv = FLYSUtils.stripNamedMainValue(nmv);
                     logger.debug("Set named main value '" + nmv + "'");
 
-                    wqkms.setName(nmv);
+                    return nmv;
                 }
             }
             catch (NumberFormatException nfe) {
                 // do nothing here
             }
         }
+
+        return name;
     }
 
 
@@ -269,8 +238,23 @@
     }
 
 
+    /**
+     * This method is used to register a new column at <i>writer</i>. The name /
+     * title of the column depends on the Q or W value of <i>wqkms</i>. If a Q
+     * was selected and the Q fits to a named main value, the title is set to
+     * the named main value. Otherwise, the name returned by
+     * <i>WQKms.getName()</i> is set.
+     *
+     * @param writer The WstWriter.
+     * @param wqkms The new WST column.
+     */
     protected void addWSTColumn(WstWriter writer, WQKms wqkms) {
-        writer.addColumn(wqkms.getName());
+        if (master instanceof WINFOArtifact) {
+            writer.addColumn(getColumnTitle((WINFOArtifact) master, wqkms));
+        }
+        else {
+            writer.addColumn(wqkms.getName());
+        }
     }
 
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/utils/FLYSUtils.java	Wed Dec 21 11:56:08 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/FLYSUtils.java	Wed Dec 21 15:44:41 2011 +0000
@@ -39,6 +39,12 @@
 
     public static enum KM_MODE { RANGE, LOCATIONS, NONE };
 
+    /**
+     * An enum that represents the 5 possible WQ modes in FLYS. The 5 values are
+     * <i>QFREE</i> <i>QGAUGE</i> <i>WGAUGE</i> <i>WFREE</i> and <i>NONE</i>.
+     */
+    public static enum WQ_MODE { QFREE, QGAUGE, WFREE, WGAUGE, NONE };
+
     public static final String XPATH_RIVER_PROJECTION =
         "/artifact-database/floodmap/river[@name=$name]/srid/@value";
 
@@ -133,6 +139,31 @@
     }
 
 
+    /**
+     * This method returns an WQ_MODE enum which is based on the parameters
+     * stored in <i>flys</i> Artifact. If there is no <i>wq_mode</i> parameter
+     * existing, WQ_MODE.NONE is returned.
+     *
+     * @param flys The FLYSArtifact that stores wq mode relevant parameters.
+     *
+     * @return an enum WQ_MODE.
+     */
+    public static WQ_MODE getWQMode(FLYSArtifact flys) {
+        String  mode = flys.getDataAsString("wq_mode");
+        boolean free = flys.getDataAsBoolean("wq_free");
+
+        if (mode != null && mode.equals("Q")) {
+            return free ? WQ_MODE.QFREE : WQ_MODE.QGAUGE;
+        }
+        else if (mode != null && mode.equals("W")) {
+            return free ? WQ_MODE.WFREE : WQ_MODE.WGAUGE;
+        }
+        else {
+            return WQ_MODE.NONE;
+        }
+    }
+
+
     public static KM_MODE getKmRangeMode(FLYSArtifact flys) {
         String mode = flys.getDataAsString("ld_mode");
 
@@ -353,6 +384,27 @@
     }
 
 
+    /**
+     * Returns the named main value if a Q was selected and if this Q fits to a
+     * named main value. Otherwise, this function returns null.
+     *
+     * @param winfo The WINFO Artifact.
+     * @param value The Q (or W) value.
+     *
+     * @return a named main value or null.
+     */
+    public static String getNamedMainValue(WINFOArtifact winfo, double value) {
+        WQ_MODE wqmode = getWQMode(winfo);
+
+        if (wqmode != WQ_MODE.QGAUGE) {
+            return null;
+        }
+        else {
+            return getNamedMainValue(winfo.getGauge(), value);
+        }
+    }
+
+
     public static String getNamedMainValue(Gauge gauge, double value) {
         List<MainValue> mainValues = gauge.getMainValues();
         logger.debug("Search named main value for: " + value);

http://dive4elements.wald.intevation.org