changeset 785:e764cab8c885

Save the relation between selected measurement and the parameter it belongs to (issue210). TODO: Adapt sql statements. gnv-artifacts/trunk@867 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 30 Mar 2010 14:05:22 +0000
parents 7b81d563c8c0
children c907636c0288
files gnv-artifacts/ChangeLog gnv-artifacts/src/main/java/de/intevation/gnv/state/DefaultInputData.java gnv-artifacts/src/main/java/de/intevation/gnv/state/ExtendedInputData.java gnv-artifacts/src/main/java/de/intevation/gnv/state/MeasurementState.java gnv-artifacts/src/main/java/de/intevation/gnv/utils/InputValidator.java
diffstat 5 files changed, 186 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog	Tue Mar 30 09:17:53 2010 +0000
+++ b/gnv-artifacts/ChangeLog	Tue Mar 30 14:05:22 2010 +0000
@@ -1,3 +1,28 @@
+2010-03-30  Ingo Weinzierl <ingo.weinzierl@intevation.de>
+
+	  Issue210 (First work for saving relation between selected measurement and
+	  the parameter it belongs to completed)
+
+	* src/main/java/de/intevation/gnv/state/DefaultInputData.java: Added a
+	  further constructor and changed the visibility of the separator character
+	  used by this class to public.
+
+	* src/main/java/de/intevation/gnv/state/ExtendedInputData.java: This class
+	  enhances DefaultInputData and has a further field storing a string
+	  parameter. This is necessary to save the information about the
+	  relation between selected measurement and the parameter it belongs to.
+
+	* src/main/java/de/intevation/gnv/state/MeasurementState.java: Little
+	  changes in the data of the describe document. The value for the checkboxes
+	  are composed of the measurement id and the parameter id separated by a
+	  semicolon. Furthermore the incoming document of a feed call is parsed in
+	  this class to save the relation between selected measurement and the
+	  parameter it belongs to. ExtendedInputData class is used as storage.
+
+	* src/main/java/de/intevation/gnv/utils/InputValidator.java: Changed the
+	  method signatures of all methods to static for using it without
+	  instantiating an object of this class.
+
 2010-03-30  Ingo Weinzierl <ingo.weinzierl@intevation.de>
 
 	  Issue212 (Improved input validation)
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/DefaultInputData.java	Tue Mar 30 09:17:53 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/DefaultInputData.java	Tue Mar 30 14:05:22 2010 +0000
@@ -18,7 +18,7 @@
 
     private static final Logger logger = Logger.getLogger(DefaultInputData.class);
 
-    private final static String VALUE_SEPARATOR = " , ";
+    public final static String VALUE_SEPARATOR = " , ";
 
     protected String name;
     protected String value;
@@ -39,6 +39,16 @@
         this.object = object;
     }
 
+    public DefaultInputData(
+        String name,
+        String value,
+        Object object)
+    {
+        this.name        = name;
+        this.object      = object;
+        this.value       = value;
+    }
+
     /**
      * @see de.intevation.gnv.state.InputData#getName()
      */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/ExtendedInputData.java	Tue Mar 30 14:05:22 2010 +0000
@@ -0,0 +1,30 @@
+package de.intevation.gnv.state;
+
+/**
+ * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
+ */
+public class ExtendedInputData extends DefaultInputData {
+
+    public static final String SEPARATOR = ";";
+
+    protected String parameterid;
+
+    public ExtendedInputData(
+        String name,
+        String value,
+        Object object,
+        String parameterid)
+    {
+        super(name, value, object);
+        this.parameterid = parameterid;
+    }
+
+    public String getParameterID() {
+        return parameterid;
+    }
+
+    public void setParameter(String parameterid) {
+        this.parameterid = parameterid;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/MeasurementState.java	Tue Mar 30 09:17:53 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/MeasurementState.java	Tue Mar 30 14:05:22 2010 +0000
@@ -2,6 +2,7 @@
 
 import de.intevation.artifactdatabase.XMLUtils;
 
+import de.intevation.artifacts.CallContext;
 import de.intevation.artifacts.CallMeta;
 
 import de.intevation.gnv.artifacts.ressource.RessourceFactory;
@@ -14,11 +15,17 @@
 import de.intevation.gnv.state.describedata.NamedArrayList;
 import de.intevation.gnv.state.describedata.NamedCollection;
 
+import de.intevation.gnv.state.exception.StateException;
+
+import de.intevation.gnv.utils.InputValidator;
+
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Locale;
 
 import org.apache.log4j.Logger;
 
@@ -36,6 +43,8 @@
 
     public static final String SQL_KEY_PARAMETERID = "PARAMETERID";
 
+    public static final String SEPARATOR = ";";
+
 
     private class ParameterMatrix {
         private final Logger logger =
@@ -95,9 +104,8 @@
         }
 
         private void initParameters(String[] parameter) {
-            int count = parameter.length;
-            for (int i = 0; i < count; i++) {
-                parameters.add(parameter[i]);
+            for (String param: parameter) {
+                parameters.add(param);
             }
         }
 
@@ -220,7 +228,8 @@
 
         for (int i = 0; i < parameters; i++) {
             Element select = creator.create("select");
-            creator.addAttr(select, "label", inputParam.getDescription(matrix.getParameter(i)));
+            String param   = matrix.getParameter(i);
+            creator.addAttr(select, "label", inputParam.getDescription(param));
             creator.addAttr(select, "ref", name);
 
             for (int j = 0; j < measurements; j++) {
@@ -241,8 +250,9 @@
                     creator.addAttr(item, "disabled", "false");
                 }
 
+                String tmpValue = matrix.getMeasurement(j) + ";" + param;
                 label.setTextContent(matrix.getMDescription(j));
-                value.setTextContent(matrix.getMeasurement(j));
+                value.setTextContent(tmpValue);
 
                 item.appendChild(label);
                 item.appendChild(value);
@@ -254,5 +264,103 @@
 
         dynamicNode.appendChild(matrixNode);
     }
+
+
+    @Override
+    public Document feed(
+        CallContext           context,
+        Collection<InputData> input,
+        String                uuid)
+    throws StateException
+    {
+        RessourceFactory resFactory = RessourceFactory.getInstance();
+        Locale[] serverLocales      = resFactory.getLocales();
+        Locale locale               = context.getMeta().getPreferredLocale(
+            serverLocales);
+
+        if (input == null) {
+            String msg = resFactory.getRessource(
+                locale,
+                EXCEPTION_NO_INPUT,
+                EXCEPTION_NO_INPUT);
+            logger.warn(msg);
+            return feedFailure(msg);
+        }
+
+        for(InputData item: input) {
+            String   name         = item.getName();
+            InputValue inputValue = inputValues.get(name);
+
+            String[] tupel = extractValuesAndParams(item.getValue());
+            String   type  = inputValue.getType();
+
+            if (inputValue == null) {
+                String msg = resFactory.getRessource(
+                    locale,
+                    EXCEPTION_INVALID_INPUT,
+                    EXCEPTION_INVALID_INPUT);
+                logger.warn(msg);
+                return feedFailure(msg);
+            }
+
+            if (!InputValidator.isInputValid(tupel[0], type)) {
+                String msg = resFactory.getRessource(
+                    locale,
+                    EXCEPTION_INVALID_INPUT,
+                    EXCEPTION_INVALID_INPUT);
+                logger.warn(msg);
+                return feedFailure(msg);
+            }
+
+            if (inputData == null) {
+                inputData = new HashMap<String, InputData>();
+            }
+
+            ExtendedInputData extended = new ExtendedInputData(
+                name,
+                tupel[0],
+                item.getObject(),
+                tupel[1]);
+
+            if (name.equals(dataName)) {
+                String[] desc = getDescriptionForInputData(extended, uuid);
+                extended.setDescription(desc);
+            }
+
+            inputData.put(name, extended);
+        }
+
+        return feedSuccess();
+    }
+
+
+    protected String[] extractValuesAndParams(String tmp) {
+        String[] array = tmp.split(DefaultInputData.VALUE_SEPARATOR);
+
+        String[] extracted = new String[2];
+        for (String item: array) {
+            String[] tupel = item.split(ExtendedInputData.SEPARATOR);
+
+            if (extracted[0] == null) {
+                extracted[0] = tupel[0];
+            }
+            else {
+                extracted[0] +=
+                    DefaultInputData.VALUE_SEPARATOR + tupel[0];
+            }
+
+            if (extracted[1] == null) {
+                extracted[1] = tupel[1];
+            }
+            else {
+                extracted[1] += DefaultInputData.VALUE_SEPARATOR + tupel[1];
+            }
+        }
+
+        logger.debug("VALUES RESULT: " + extracted[0]);
+        logger.debug("PARAMS RESULT: " + extracted[1]);
+
+        return extracted;
+    }
 }
-// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/utils/InputValidator.java	Tue Mar 30 09:17:53 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/utils/InputValidator.java	Tue Mar 30 14:05:22 2010 +0000
@@ -19,6 +19,7 @@
 
 /**
  * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
+ * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
  *
  */
 public class InputValidator {
@@ -37,7 +38,7 @@
         super();
     }
 
-    public boolean isInputValid(String minInput, String maxInput, String type) {
+    public static boolean isInputValid(String minInput, String maxInput, String type) {
         log.debug("InputValidator.isInputValid " + minInput + " " + maxInput + " " +type);
         boolean returnValue = false;
         if ("Date".equalsIgnoreCase(type)) {
@@ -62,7 +63,7 @@
         return returnValue;
     }
 
-    public boolean isInputValid(String input, String type) {
+    public static boolean isInputValid(String input, String type) {
         log.debug("InputValidator.isInputValid " + input + " " + type);
         boolean returnValue = false;
         String[] values = input.split(",");
@@ -95,7 +96,7 @@
                                                                 // REGEXP
             } else if ("Coordinate".equalsIgnoreCase(type)) {
                     try {
-                        valid = this.getPointValue(values[i]) != null;
+                        valid = getPointValue(values[i]) != null;
                     } catch (ValidationException e) {
                         log.debug(e.getMessage());
                         valid = false;
@@ -115,7 +116,7 @@
     }
 
 
-    public Point getPointValue(String value) throws ValidationException{
+    public static Point getPointValue(String value) throws ValidationException{
         log.debug("InputValidator.getPointValue " + value);
 
         if (value.toLowerCase().startsWith("point")){
@@ -164,7 +165,7 @@
     }
 
 
-    public boolean isDateValid(Date tmp, Date lo, Date up) {
+    public static boolean isDateValid(Date tmp, Date lo, Date up) {
         // we need to transform the given dates into seconds, because
         // they differ in milliseconds -> that's why we cannot use the
         // Date.compareTo(Date) method.

http://dive4elements.wald.intevation.org