diff gnv-artifacts/src/main/java/de/intevation/gnv/state/StateBase.java @ 725:dde7f51dbe1e

Improved exception handling while feeding an artifact. gnv-artifacts/trunk@757 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 10 Mar 2010 13:27:14 +0000
parents 078ba6799bd2
children 18dc6a99f4f4
line wrap: on
line diff
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/StateBase.java	Tue Mar 09 12:54:05 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/StateBase.java	Wed Mar 10 13:27:14 2010 +0000
@@ -29,6 +29,7 @@
 
 import de.intevation.gnv.state.exception.StateException;
 
+import de.intevation.gnv.utils.ArtifactXMLUtilities;
 import de.intevation.gnv.utils.InputValidator;
 
 import java.util.ArrayList;
@@ -39,6 +40,7 @@
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 
 import javax.xml.xpath.XPathConstants;
@@ -79,6 +81,10 @@
     public final static String XPATH_STATIC_UI  = "art:static";
     public final static String XPATH_DYNAMIC_UI = "art:dynamic";
 
+    public static final String EXCEPTION_NO_INPUT      = "no.input.data";
+    public static final String EXCEPTION_INVALID_INPUT =
+        "input.is.not.valid";
+
     /** input value names which should not be rendered from State itself */
     public final static String[] BLACKLIST = {"sourceid", "fisname"};
 
@@ -215,9 +221,17 @@
     }
 
 
-    public void feed(Collection<InputData> inputData, String uuid)
+    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) {
             Iterator<InputData> it = inputData.iterator();
             InputValidator iv = new InputValidator();
@@ -237,26 +251,49 @@
                         tmpItem.setDescription(desc);
                         this.inputData.put(tmpItem.getName(), tmpItem);
                     } else {
-                        String errMsg = "Wrong input for " + tmpItem.getValue()
-                                        + " is not an " + inputValue.getType()
-                                        + " Value.";
-                        log.warn(errMsg);
-                        throw new StateException(errMsg);
+                        String msg = resFactory.getRessource(
+                            locale,
+                            EXCEPTION_INVALID_INPUT,
+                            EXCEPTION_INVALID_INPUT);
+                        log.warn(msg);
+                        return feedFailure(msg);
                     }
 
                 } else {
-                    String errMsg = "No Inputvalue given for Inputdata "
-                                    + tmpItem.getName();
-                    log.warn(errMsg + "Value will be ignored");
+                    String msg = resFactory.getRessource(
+                        locale,
+                        EXCEPTION_INVALID_INPUT,
+                        EXCEPTION_INVALID_INPUT);
+                    log.warn(msg);
+                    return feedFailure(msg);
 
                 }
             }
+
+            return feedSuccess();
         } else {
-            log.warn("No Inputdata given");
+            String msg = resFactory.getRessource(
+                locale,
+                EXCEPTION_NO_INPUT,
+                EXCEPTION_NO_INPUT);
+            log.warn(msg);
+            return feedFailure(msg);
         }
     }
 
 
+    protected Document feedSuccess() {
+        return ArtifactXMLUtilities.createSuccessReport(
+            "Initialize success", XMLUtils.newDocument());
+    }
+
+
+    protected Document feedFailure(String msg) {
+        return ArtifactXMLUtilities.createInputExceptionReport(
+            msg, XMLUtils.newDocument());
+    }
+
+
     private String[] getDescriptionForInputData(InputData data, String uuid) {
         // there is only one element in the list, so take the first
         Object obj = getDescibeData(uuid).get(0);

http://dive4elements.wald.intevation.org