comparison src/main/java/de/intevation/lada/rest/Response.java @ 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 af8eb04a8100
comparison
equal deleted inserted replaced
78:98fe21a9fbe5 79:bb22b5063a94
1 package de.intevation.lada.rest; 1 package de.intevation.lada.rest;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import java.util.logging.Logger;
6
7 import javax.inject.Inject;
2 8
3 /** 9 /**
4 * This class is nice!. 10 * This class is nice!.
5 * 11 *
6 * @author <a href="mailto:torsten@intevation.de">Torsten Irländer</a> 12 * @author <a href="mailto:torsten@intevation.de">Torsten Irländer</a>
7 */ 13 */
8 @SuppressWarnings("serial") 14 @SuppressWarnings("serial")
9 public class Response implements java.io.Serializable { 15 public class Response implements java.io.Serializable {
10 16
17
18 /**
19 * The logger for this class.
20 */
21 @Inject
22 private Logger log;
23
11 private Boolean success; 24 private Boolean success;
12 private String message; 25 private String message;
13 private Object data; 26 private Object data;
14 private String errors; 27 private Map<String, String> errors;
15 private String warnings; 28 private Map<String, String> warnings;
16 29
17 public Response(boolean success, String message, Object data) { 30 public Response(boolean success, int code, Object data) {
18 this.success = success; 31 this.success = success;
19 this.message = message; 32 this.message = Integer.toString(code);
20 this.data = data; 33 this.data = data;
21 } 34 }
22 35
23 public Boolean getSuccess() { 36 public Boolean getSuccess() {
24 return success; 37 return success;
42 55
43 public void setData(Object data) { 56 public void setData(Object data) {
44 this.data = data; 57 this.data = data;
45 } 58 }
46 59
47 public String getErrors() { 60 public Map<String, String> getErrors() {
48 return errors; 61 return errors;
49 } 62 }
50 63
51 public void setErrors(String errors) { 64 public void setErrors(Map<String, Integer> errors) {
52 this.errors = errors; 65 this.errors = this.convertCodes(errors);
53 } 66 }
54 67
55 public String getWarnings() { 68 public Map<String, String> getWarnings() {
56 return warnings; 69 return warnings;
57 } 70 }
58 71
59 public void setWarnings(String warnings) { 72 public void setWarnings(Map<String, Integer> warnings) {
60 this.warnings = warnings; 73 this.warnings = this.convertCodes(warnings);
74 }
75
76 private HashMap<String, String> convertCodes(Map<String, Integer> codes) {
77 HashMap<String, String> converted = new HashMap<String, String>();
78 if (codes == null || codes.isEmpty()) {
79 return converted;
80 }
81 for (Map.Entry<String, Integer> entry: codes.entrySet()) {
82 converted.put(entry.getKey(), Integer.toString(entry.getValue()));
83 }
84 return converted;
85 }
86
87 /* Currently unused but might be helpfull later */
88 private String codes2string(Map<String, Integer> codes) {
89 String response = "{";
90 if (codes == null || codes.isEmpty()) {
91 response += "}";
92 return response;
93 }
94 boolean first = true;
95 for (Map.Entry<String, Integer> entry: codes.entrySet()) {
96 if (!first) {
97 response +=",";
98 }
99 response += entry.getKey() + ":" + "\"" + entry.getValue() + "\"";
100 first = false;
101 }
102 response += "}";
103 return response;
61 } 104 }
62 } 105 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)