diff gnv-artifacts/src/main/java/de/intevation/gnv/state/MeasurementState.java @ 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 c4156275c1e1
children a5526908f92f
line wrap: on
line diff
--- 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 :

http://dive4elements.wald.intevation.org