changeset 1178:1b432c260e97

Enabled the FloodMapState to fetch waterlevel data from external Artifact. flys-artifacts/trunk@2764 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 15 Sep 2011 15:36:05 +0000
parents 70b8ac64404b
children ebb48bef3529
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/FloodMapState.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WaterlevelSelectState.java
diffstat 3 files changed, 165 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu Sep 15 15:10:04 2011 +0000
+++ b/flys-artifacts/ChangeLog	Thu Sep 15 15:36:05 2011 +0000
@@ -1,3 +1,13 @@
+2011-09-15  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/states/WaterlevelSelectState.java:
+	  Validate incoming data string and strip brackets.
+
+	* src/main/java/de/intevation/flys/artifacts/states/FloodMapState.java:
+	  Fetch waterlevel data from external Artifact if existing. If no external
+	  Artifact is specified that provides waterlevel data, we gonna try to
+	  fetch it from the current Artifact.
+
 2011-09-15  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/states/WaterlevelPairSelectState.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/FloodMapState.java	Thu Sep 15 15:10:04 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/FloodMapState.java	Thu Sep 15 15:36:05 2011 +0000
@@ -65,7 +65,7 @@
         System.getProperty("flys.uesk.keep.artifactsdir", "false");
 
 
-    public static final String WSP_ARTIFACT_UUID = "uesk.wsp.artifact";
+    public static final String WSP_ARTIFACT = "wsp";
 
     public static final String WINFO_WSP_STATE_ID = "state.winfo.waterlevel";
 
@@ -690,6 +690,39 @@
     }
 
 
+    protected WQKms getWQKms(FLYSArtifact flys, CallContext cc) {
+        String   wspString = flys.getDataAsString(WSP_ARTIFACT);
+        String[] parts     = wspString.split(";");
+
+        String otherArtifact = parts[0];
+
+        int idx = -1;
+        try {
+            idx = Integer.parseInt(parts[2]);
+        }
+        catch (NumberFormatException nfe) { /* do nothing */ }
+
+        FLYSArtifact src = otherArtifact != null
+            ? FLYSUtils.getArtifact(otherArtifact, cc)
+            : flys;
+
+        logger.debug("Use waterlevel provided by Artifact: " + src.identifier());
+
+        CalculationResult rawData = (CalculationResult) src.compute(
+            cc,
+            null,
+            WINFO_WSP_STATE_ID,
+            ComputeType.ADVANCE,
+            false);
+
+        WQKms[] wqkms = (WQKms[]) rawData.getData();
+
+        return wqkms == null || idx == -1 || idx >= wqkms.length
+            ? null
+            : wqkms[idx];
+    }
+
+
     protected void setWsp(
         FLYSArtifact artifact,
         CallContext  context,
@@ -698,44 +731,31 @@
     {
         logger.debug("FloodMapState.setWsp");
 
-        String otherArtifact = artifact.getDataAsString(WSP_ARTIFACT_UUID);
-        FLYSArtifact     src = otherArtifact != null
-            ? FLYSUtils.getArtifact(otherArtifact, context)
-            : artifact;
+        WQKms data = getWQKms(artifact, context);
 
-        CalculationResult rawData = (CalculationResult) src.compute(
-            context,
-            null,
-            WINFO_WSP_STATE_ID,
-            ComputeType.ADVANCE,
-            false);
-
-        if (rawData == null) {
+        if (data == null) {
             logger.warn("No WST data found!");
             return;
         }
 
-        WQKms[]    data = (WQKms[]) rawData.getData();
-        WstWriter writer = new WstWriter(data.length);
+        WstWriter writer = new WstWriter(1);
 
         // TODO REMOVE job.setWspTag(...) This is only used until the user is
         // able to select the WSP column himself!
         boolean writeWspTag = true;
 
         double[] buf = new double[4];
-        for (WQKms wqkms: data) {
-            logger.debug("Add WST column: " + wqkms.getName());
-            writer.addColumn(wqkms.getName());
+        logger.debug("Add WST column: " + data.getName());
+        writer.addColumn(data.getName());
 
-            if (writeWspTag) {
-                job.setWspTag(wqkms.getName());
-                writeWspTag = false;
-            }
+        if (writeWspTag) {
+            job.setWspTag(data.getName());
+            writeWspTag = false;
+        }
 
-            for (int i = 0, num = wqkms.size(); i < num; i++) {
-                wqkms.get(i, buf);
-                writer.add(buf);
-            }
+        for (int i = 0, num = data.size(); i < num; i++) {
+            data.get(i, buf);
+            writer.add(buf);
         }
 
         FileOutputStream fout = null;
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WaterlevelSelectState.java	Thu Sep 15 15:10:04 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WaterlevelSelectState.java	Thu Sep 15 15:36:05 2011 +0000
@@ -1,13 +1,122 @@
 package de.intevation.flys.artifacts.states;
 
+import org.apache.log4j.Logger;
+
+import de.intevation.artifacts.Artifact;
+import de.intevation.artifacts.CallContext;
+
+import de.intevation.artifactdatabase.data.DefaultStateData;
+import de.intevation.artifactdatabase.data.StateData;
+
+import de.intevation.flys.artifacts.FLYSArtifact;
+import de.intevation.flys.utils.FLYSUtils;
+
+
 /**
  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
  */
 public class WaterlevelSelectState extends DefaultState {
 
+    private static final Logger logger =
+        Logger.getLogger(WaterlevelSelectState.class);
+
+    public static final String SPLIT_CHAR = ";";
+
+
     @Override
     protected String getUIProvider() {
         return "wsp_datacage_panel";
     }
+
+
+    @Override
+    public StateData transform(
+        FLYSArtifact flys,
+        CallContext  cc,
+        String       name,
+        String       val
+    ) {
+        if (!isValueValid(val)) {
+            logger.error("The given input string is not valid: '" + val + "'");
+            return null;
+        }
+
+        return new DefaultStateData(name, null, null, strip(val));
+    }
+
+
+    @Override
+    public boolean validate(Artifact artifact)
+    throws IllegalArgumentException
+    {
+        FLYSArtifact flys = (FLYSArtifact) artifact;
+
+        StateData data = flys.getData("wsp");
+
+        if (data == null) {
+            throw new IllegalArgumentException("WSP is empty");
+        }
+
+        return true;
+    }
+
+
+    public static String strip(String value) {
+        int start = value.indexOf("[");
+        int end   = value.indexOf("]");
+
+        if (start < 0 || end < 0) {
+            return value;
+        }
+
+        value = value.substring(start+1, end);
+
+        return value;
+    }
+
+
+    /**
+     * Validates the given String. A valid string for this state requires the
+     * format: "UUID;FACETNAME;FACETINDEX".
+     *
+     * @param value The string value requires validation.
+     *
+     * @return true, if the string applies the specified format, otherwise
+     * false.
+     */
+    public static boolean isValueValid(String value) {
+        logger.debug("Validate string: '" + value + "'");
+
+        value = strip(value);
+
+        logger.debug("Validate substring: '" + value + "'");
+
+        if (value == null || value.length() == 0) {
+            return false;
+        }
+
+        String[] parts = value.split(SPLIT_CHAR);
+
+        if (parts == null || parts.length < 3) {
+            return false;
+        }
+
+        if (parts[0] == null || parts[0].length() == 0) {
+            return false;
+        }
+
+        if (parts[1] == null || parts[1].length() == 0) {
+            return false;
+        }
+
+        try {
+            Integer.parseInt(parts[2]);
+        }
+        catch (NumberFormatException nfe) {
+            logger.error("Index is not a valid integer!", nfe);
+        }
+
+        return true;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org