changeset 79:bb22b5063a94

Handle warnings and errors in the response class.
author Torsten Irländer <torsten.irlaender@intevation.de>
date Fri, 07 Jun 2013 14:13:35 +0200
parents 98fe21a9fbe5
children 75d42f8063f8
files src/main/java/de/intevation/lada/rest/Response.java
diffstat 1 files changed, 53 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/intevation/lada/rest/Response.java	Fri Jun 07 11:45:46 2013 +0200
+++ b/src/main/java/de/intevation/lada/rest/Response.java	Fri Jun 07 14:13:35 2013 +0200
@@ -1,5 +1,11 @@
 package de.intevation.lada.rest;
 
+import java.util.HashMap;
+import java.util.Map;
+import java.util.logging.Logger;
+
+import javax.inject.Inject;
+
 /**
 * This class is nice!.
 *
@@ -8,15 +14,22 @@
 @SuppressWarnings("serial")
 public class Response implements java.io.Serializable {
 
+
+    /**
+     * The logger for this class.
+     */
+    @Inject
+    private Logger log;
+
     private Boolean success;
     private String message;
     private Object data;
-    private String errors;
-    private String warnings;
+    private Map<String, String> errors;
+    private Map<String, String> warnings;
 
-    public Response(boolean success, String message, Object data) {
+    public Response(boolean success, int code, Object data) {
         this.success = success;
-        this.message = message;
+        this.message = Integer.toString(code);
         this.data = data;
     }
 
@@ -44,19 +57,49 @@
         this.data = data;
     }
 
-    public String getErrors() {
+    public Map<String, String> getErrors() {
         return errors;
     }
 
-    public void setErrors(String errors) {
-        this.errors = errors;
+    public void setErrors(Map<String, Integer> errors) {
+        this.errors = this.convertCodes(errors);
     }
 
-    public String getWarnings() {
+    public Map<String, String> getWarnings() {
         return warnings;
     }
 
-    public void setWarnings(String warnings) {
-        this.warnings = warnings;
+    public void setWarnings(Map<String, Integer> warnings) {
+        this.warnings = this.convertCodes(warnings);
+    }
+
+    private HashMap<String, String> convertCodes(Map<String, Integer> codes) {
+        HashMap<String, String> converted = new HashMap<String, String>();
+        if (codes == null || codes.isEmpty()) {
+            return converted;
+        }
+        for (Map.Entry<String, Integer> entry: codes.entrySet()) {
+            converted.put(entry.getKey(), Integer.toString(entry.getValue()));
+        }
+        return converted;
+    }
+
+    /* Currently unused but might be helpfull later */
+    private String codes2string(Map<String, Integer> codes) {
+        String response = "{";
+        if (codes == null || codes.isEmpty()) {
+            response += "}";
+            return response;
+        }
+        boolean first = true;
+        for (Map.Entry<String, Integer> entry: codes.entrySet()) {
+            if (!first) {
+                response +=",";
+            }
+            response += entry.getKey() + ":" + "\"" + entry.getValue() + "\"";
+            first = false;
+        }
+        response += "}";
+        return response;
     }
 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)