diff artifacts/src/main/java/org/dive4elements/river/artifacts/states/MinMaxState.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-artifacts/src/main/java/org/dive4elements/river/artifacts/states/MinMaxState.java@bd047b71ab37
children 4897a58c8746
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/states/MinMaxState.java	Thu Apr 25 15:23:37 2013 +0200
@@ -0,0 +1,173 @@
+package org.dive4elements.river.artifacts.states;
+
+import org.w3c.dom.Element;
+
+import org.apache.log4j.Logger;
+
+import org.dive4elements.artifacts.Artifact;
+import org.dive4elements.artifacts.CallContext;
+
+import org.dive4elements.artifacts.common.ArtifactNamespaceContext;
+import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator;
+
+import org.dive4elements.artifactdatabase.ProtocolUtils;
+
+import org.dive4elements.river.artifacts.FLYSArtifact;
+
+
+/**
+ * State that holds minimun and maximum (for validation).
+ *
+ * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
+ */
+public abstract class MinMaxState extends DefaultState {
+
+    private static final Logger logger = Logger.getLogger(MinMaxState.class);
+
+    @Override
+    protected void appendItems(
+        Artifact       artifact,
+        ElementCreator creator,
+        String         name,
+        CallContext    context,
+        Element        select
+    ) {
+        FLYSArtifact flys = (FLYSArtifact) artifact;
+
+        select.setAttributeNS(
+            ArtifactNamespaceContext.NAMESPACE_URI,
+            "art:type",
+            getType());
+
+        String[] defMinMax = getDefaults(artifact, name);
+
+        Element min = ProtocolUtils.createArtNode(
+            creator,
+            "min",
+            new String[] { "value", "default" },
+            new String[] { String.valueOf(getLower(flys)), defMinMax[0] });
+
+        Element max = ProtocolUtils.createArtNode(
+            creator,
+            "max",
+            new String[] { "value", "default" },
+            new String[] { String.valueOf(getUpper(flys)), defMinMax[1] });
+
+        select.appendChild(min);
+        select.appendChild(max);
+    }
+
+
+    /**
+     * @param cc
+     * @param name
+     * @param value
+     * @param type
+     *
+     * @return
+     */
+    @Override
+    protected String getLabelFor(
+        CallContext cc,
+        String      name,
+        String      value,
+        String      type
+    ) {
+        if (type.indexOf("range") > 0) {
+            String[] minmax = extractRangeAsString(value);
+
+            if (minmax != null) {
+                return minmax[0] + " - " + minmax[1];
+            }
+        }
+
+        return super.getLabelFor(cc, name, value, type);
+    }
+
+
+    /**
+     * Returns a string array with [min,max] from <i>rawValue</i>.
+     * <i>rawValue</i> should be a string like "1999;2001".
+     *
+     * @param rawValue A string with min and max separated by a ';'.
+     *
+     * @return the min and max as string array ([min,max]).
+     */
+    protected String[] extractRangeAsString(String rawValue) {
+        return rawValue.split(";");
+    }
+
+
+    /**
+     * 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 extractRangeAsString(rawValue);
+    }
+
+
+    /**
+     * 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);
+
+    protected abstract String getType();
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org