# HG changeset patch # User Torsten Irländer # Date 1370607215 -7200 # Node ID bb22b5063a947ac63d7493eee51a317f16895cc5 # Parent 98fe21a9fbe51070a39168aae550c0d9f13c874e Handle warnings and errors in the response class. diff -r 98fe21a9fbe5 -r bb22b5063a94 src/main/java/de/intevation/lada/rest/Response.java --- 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 errors; + private Map 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 getErrors() { return errors; } - public void setErrors(String errors) { - this.errors = errors; + public void setErrors(Map errors) { + this.errors = this.convertCodes(errors); } - public String getWarnings() { + public Map getWarnings() { return warnings; } - public void setWarnings(String warnings) { - this.warnings = warnings; + public void setWarnings(Map warnings) { + this.warnings = this.convertCodes(warnings); + } + + private HashMap convertCodes(Map codes) { + HashMap converted = new HashMap(); + if (codes == null || codes.isEmpty()) { + return converted; + } + for (Map.Entry entry: codes.entrySet()) { + converted.put(entry.getKey(), Integer.toString(entry.getValue())); + } + return converted; + } + + /* Currently unused but might be helpfull later */ + private String codes2string(Map codes) { + String response = "{"; + if (codes == null || codes.isEmpty()) { + response += "}"; + return response; + } + boolean first = true; + for (Map.Entry entry: codes.entrySet()) { + if (!first) { + response +=","; + } + response += entry.getKey() + ":" + "\"" + entry.getValue() + "\""; + first = false; + } + response += "}"; + return response; } }