diff gnv-artifacts/src/main/java/de/intevation/gnv/state/SingleInputState.java @ 1119:7c4f81f74c47

merged gnv-artifacts
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:00 +0200
parents f953c9a559d8
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/SingleInputState.java	Fri Sep 28 12:14:00 2012 +0200
@@ -0,0 +1,129 @@
+/*
+ * Copyright (c) 2010 by Intevation GmbH
+ *
+ * This program is free software under the LGPL (>=v2.1)
+ * Read the file LGPL.txt coming with the software for details
+ * or visit http://www.gnu.org/licenses/ if it does not exist.
+ */
+
+package de.intevation.gnv.state;
+
+import de.intevation.artifacts.CallContext;
+
+import de.intevation.gnv.artifacts.ressource.RessourceFactory;
+import de.intevation.gnv.geobackend.base.Result;
+import de.intevation.gnv.state.describedata.DefaultSingleValueDescribeData;
+import de.intevation.gnv.state.exception.StateException;
+import de.intevation.gnv.utils.InputValidator;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+
+import org.apache.log4j.Logger;
+
+import org.w3c.dom.Document;
+
+/**
+ * This state handles single user input. The user is allowed to select just one
+ * value.
+ *
+ * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
+ *
+ */
+public class SingleInputState extends StateBase {
+
+    /**
+     * the logger, used to log exceptions and additonaly information
+     */
+    private static Logger log = Logger.getLogger(SingleInputState.class);
+
+    private static final long serialVersionUID = -6169497306324917318L;
+
+    /**
+     * Constructor
+     */
+    public SingleInputState() {
+    }
+
+
+    @Override
+    protected List<Object> purifyResult(Collection<Result> result, String uuid) {
+        log.debug("SingleInputState.purifyResult");
+        List<Object> describeData = new ArrayList<Object>();
+
+        String value = null;
+        if (result != null && result.size() == 1) {
+            Result tmpItem = result.iterator().next();
+            value = tmpItem.getObject("MAX").toString();
+        } else {
+            value = "";
+        }
+
+        describeData.add(new DefaultSingleValueDescribeData(
+            this.dataName, value, getID()));
+
+        return describeData;
+    }
+
+    /**
+     * This feed method needs a collection of two InputData objects. These
+     * objects' values need to be a datetime string which is turned into a Date
+     * object. Afterwards, the given dates are validated. Min and max date need
+     * to be in range of the min and max date retrieved by
+     * {@link #getDescibeData(java.lang.String)}.
+     */
+    @Override
+    public Document feed(
+        CallContext           context,
+        Collection<InputData> inputData,
+        String                uuid)
+    throws StateException {
+        RessourceFactory resFactory = RessourceFactory.getInstance();
+        Locale[] serverLocales      = resFactory.getLocales();
+        Locale locale               = context.getMeta().getPreferredLocale(
+            serverLocales);
+
+        if (inputData == null) {
+            String msg = "No input data given.";
+            log.warn(msg);
+            return feedFailure(msg);
+        }
+
+        Iterator<InputData> it = inputData.iterator();
+        InputData tmpItem      = it.next();
+        InputValue inputValue  = inputValues.get(tmpItem.getName());
+
+        if (inputValue == null) {
+            String msg = resFactory.getRessource(
+                locale,
+                EXCEPTION_INVALID_INPUT,
+                EXCEPTION_INVALID_INPUT);
+            log.warn(msg);
+            return feedFailure(msg);
+        }
+
+        boolean valid = InputValidator.isInputValid(
+            tmpItem.getValue(), inputValue.getType());
+
+        if (valid) {
+            String[] desc = getDescriptionForInputData(tmpItem, context, uuid);
+            tmpItem.setDescription(desc);
+
+            this.inputData.put(tmpItem.getName(), tmpItem);
+            return feedSuccess();
+        }
+
+        else {
+            String msg = resFactory.getRessource(
+                locale,
+                EXCEPTION_INVALID_INPUT,
+                EXCEPTION_INVALID_INPUT);
+            log.warn(msg);
+            return feedFailure(msg);
+        }
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org