changeset 2202:d1660809daab

Added default values for min and max items prepared by MinMaxState. flys-artifacts/trunk@3821 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 30 Jan 2012 10:08:25 +0000
parents 4e3bfa2d9584
children 1db66b88868f
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/MinMaxState.java
diffstat 2 files changed, 81 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Mon Jan 30 09:57:59 2012 +0000
+++ b/flys-artifacts/ChangeLog	Mon Jan 30 10:08:25 2012 +0000
@@ -1,3 +1,8 @@
+2012-01-30  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/states/MinMaxState.java:
+	  Write default values for min and max items into the DESCRIBE document.
+
 2012-01-28	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/model/WW.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/MinMaxState.java	Mon Jan 30 09:57:59 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/MinMaxState.java	Mon Jan 30 10:08:25 2012 +0000
@@ -2,6 +2,8 @@
 
 import org.w3c.dom.Element;
 
+import org.apache.log4j.Logger;
+
 import de.intevation.artifacts.Artifact;
 import de.intevation.artifacts.CallContext;
 
@@ -18,6 +20,8 @@
  */
 public abstract class MinMaxState extends DefaultState {
 
+    private static final Logger logger = Logger.getLogger(MinMaxState.class);
+
     @Override
     protected void appendItems(
         Artifact       artifact,
@@ -33,23 +37,91 @@
             "art:type",
             getType());
 
+        String[] defMinMax = getDefaults(artifact, name);
+
         Element min = ProtocolUtils.createArtNode(
             creator,
             "min",
-            new String[] { "value" },
-            new String[] { String.valueOf(getLower(flys)) });
+            new String[] { "value", "default" },
+            new String[] { String.valueOf(getLower(flys)), defMinMax[0] });
 
         Element max = ProtocolUtils.createArtNode(
             creator,
             "max",
-            new String[] { "value" },
-            new String[] { String.valueOf(getUpper(flys)) });
+            new String[] { "value", "default" },
+            new String[] { String.valueOf(getUpper(flys)), defMinMax[1] });
 
         select.appendChild(min);
         select.appendChild(max);
     }
 
 
+    /**
+     * This method returns the default values for min and max. If the static
+     * field DefaultState.USE_DEFAULTS is set, the minimum and maximum inserted
+     * by the user is returned as string. Otherwise, the absolute minimum and
+     * maximum are returned.
+     *
+     * @param artifact The FLYSArtifact.
+     * @param name The name of the parameter.
+     *
+     * @return a string array [min,max] that contains the minimum and maximum
+     * values for the parameter <i>name</i>.
+     */
+    protected String[] getDefaults(Artifact artifact, String name) {
+        if (DefaultState.USE_DEFAULTS) {
+            String[] tmp = getMinMaxByParameter(artifact, name);
+
+            return tmp != null ? tmp : getMinMaxDefaults(artifact, name);
+        }
+        else {
+            return getMinMaxDefaults(artifact, name);
+        }
+    }
+
+
+    /**
+     * Returns a string array with minimum and maximum inserted by the user as
+     * [min,max].
+     *
+     * @param artifact The FLYSArtifact that stores the parameter.
+     * @param name The name of the parameter for raw min/max value string.
+     *
+     * @return a string array [min,max].
+     */
+    protected String[] getMinMaxByParameter(Artifact artifact, String name) {
+        FLYSArtifact flys     = (FLYSArtifact) artifact;
+        String       rawValue = flys.getDataAsString(name);
+
+        if (rawValue == null) {
+            logger.debug("No value for '" + rawValue + "' existing.");
+            return null;
+        }
+
+        logger.debug("Raw value for '" + name + "' = " + rawValue);
+
+        return rawValue.split(";");
+    }
+
+
+    /**
+     * Returns a string array with absolute minimum and maximum as [min,max].
+     *
+     * @param artifact The FLYSArtifact (not used in this implementation).
+     * @param name The parameter name (not used in this implementation).
+     *
+     * @return a string array [min,max].
+     */
+    protected String[] getMinMaxDefaults(Artifact artifact, String name) {
+        FLYSArtifact flys = (FLYSArtifact) artifact;
+
+        Object lower = getLower(flys);
+        Object upper = getUpper(flys);
+
+        return new String[] { String.valueOf(lower), String.valueOf(upper) };
+    }
+
+
     protected abstract Object getLower(FLYSArtifact flys);
 
     protected abstract Object getUpper(FLYSArtifact flys);

http://dive4elements.wald.intevation.org