| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.response; |
| 2 | |
| 3 | import com.fasterxml.jackson.annotation.*; |
| 4 | import com.fasterxml.jackson.annotation.JsonInclude.Include; |
| 5 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | import com.fasterxml.jackson.databind.JsonNode; |
| 7 | import com.fasterxml.jackson.databind.node.*; |
| 8 | |
| 9 | import java.util.*; |
| 10 | import java.io.*; |
| 11 | import de.ids_mannheim.korap.response.Message; |
| 12 | import de.ids_mannheim.korap.response.Messages; |
| 13 | import de.ids_mannheim.korap.util.QueryException; |
| 14 | |
| 15 | /** |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 16 | * A unified notification class for KorAP related errors, |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 17 | * warnings and messages. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 18 | * |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 19 | * <p> |
| 20 | * The object contains lists of errors, warnings and messages |
| 21 | * and new warnings, errors or messages are appended to these lists. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 22 | * |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 23 | * <p> |
| 24 | * <blockquote><pre> |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 25 | * Notifications n = new Notifications(); |
| 26 | * n.addWarning(456, "Something went wrong"); |
| 27 | * if (n.hasWarnings()) { |
| 28 | * for (Message msg : n.getWarnings()) |
| 29 | * System.err.out(msg.getCode() + ": " + msg.getMessage()); |
| 30 | * }; |
| 31 | * System.err.println(n.toJsonString()); |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 32 | * </pre></blockquote> |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 33 | * |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 34 | * @author Nils Diewald |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 35 | * @see de.ids_mannheim.korap.response.Messages |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 36 | */ |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 37 | /* |
| 38 | * This will be inherited most of the time as Java does not support roles |
| 39 | * and I have no idea how to do this more elegantly. |
| 40 | */ |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 41 | @JsonInclude(Include.NON_NULL) |
| 42 | @JsonIgnoreProperties(ignoreUnknown = true) |
| 43 | public class Notifications { |
| 44 | |
| 45 | // Create object mapper for JSON generation |
| 46 | ObjectMapper mapper = new ObjectMapper(); |
| 47 | |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 48 | private Messages warnings, errors, messages; |
| 49 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 50 | |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 51 | /** |
| 52 | * Check for warnings. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 53 | * |
| 54 | * @return <tt>true</tt> in case there are warnings, otherwise |
| 55 | * <tt>false</tt> |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 56 | */ |
| 57 | public boolean hasWarnings () { |
| 58 | if (this.warnings == null || this.warnings.size() == 0) |
| 59 | return false; |
| 60 | return true; |
| 61 | }; |
| 62 | |
| 63 | |
| 64 | /** |
| 65 | * Return all warnings. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 66 | * |
| Nils Diewald | c99ed5b | 2015-01-21 22:08:53 +0000 | [diff] [blame] | 67 | * @return {@link Messages} representing all warnings |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 68 | */ |
| Akron | 7d45e6b | 2015-06-26 17:23:42 +0200 | [diff] [blame] | 69 | @JsonIgnore |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 70 | public Messages getWarnings () { |
| 71 | return this.warnings; |
| 72 | }; |
| 73 | |
| 74 | |
| 75 | /** |
| Nils Diewald | c99ed5b | 2015-01-21 22:08:53 +0000 | [diff] [blame] | 76 | * Set warnings by means of a {@link JsonNode}. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 77 | * |
| 78 | * @param msgs |
| 79 | * JSON array of warnings. |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 80 | * @return {@link Notifications} object for chaining. |
| Nils Diewald | c99ed5b | 2015-01-21 22:08:53 +0000 | [diff] [blame] | 81 | */ |
| 82 | public Notifications setWarnings (JsonNode msgs) { |
| 83 | for (JsonNode msg : msgs) |
| 84 | this.addWarning(msg); |
| 85 | return this; |
| 86 | }; |
| 87 | |
| 88 | |
| 89 | |
| 90 | /** |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 91 | * Return a specific warning based on an index. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 92 | * |
| 93 | * @param index |
| 94 | * The index of the warning in the list of warnings. |
| 95 | * @return The message in case it exists, otherwise |
| 96 | * <code>null</code> |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 97 | */ |
| Akron | 7d45e6b | 2015-06-26 17:23:42 +0200 | [diff] [blame] | 98 | @JsonIgnore |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 99 | public Message getWarning (int index) { |
| 100 | if (this.warnings != null) |
| 101 | return this.warnings.get(index); |
| 102 | return (Message) null; |
| 103 | }; |
| 104 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 105 | |
| 106 | /** |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 107 | * Appends a new warning. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 108 | * |
| 109 | * @param code |
| 110 | * Integer code representation of the warning |
| 111 | * @param msg |
| 112 | * String representation of the warning |
| 113 | * @param terms |
| 114 | * Optional strings of additional information |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 115 | * @return Notification object for chaining |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 116 | */ |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 117 | public Notifications addWarning (int code, String msg, String ... terms) { |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 118 | if (this.warnings == null) |
| 119 | this.warnings = new Messages(); |
| 120 | this.warnings.add(code, msg, terms); |
| 121 | return this; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 124 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 125 | /** |
| 126 | * Appends a new warning. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 127 | * |
| 128 | * @param node |
| 129 | * {@link JsonNode} representing a warning message |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 130 | * @return Notification object for chaining |
| 131 | */ |
| 132 | public Notifications addWarning (JsonNode node) { |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 133 | if (this.warnings == null) |
| 134 | this.warnings = new Messages(); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 135 | |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 136 | try { |
| 137 | this.warnings.add(node); |
| 138 | } |
| 139 | catch (QueryException qe) { |
| 140 | this.warnings.add(qe.getErrorCode(), qe.getMessage()); |
| 141 | }; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 142 | |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 143 | return this; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 146 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 147 | /** |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 148 | * Appends new warnings. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 149 | * |
| 150 | * @param msgs |
| 151 | * {@link Messages} representing multiple warnings |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 152 | * @return Notification object for chaining |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 153 | */ |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 154 | public Notifications addWarnings (Messages msgs) { |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 155 | if (this.warnings == null) |
| 156 | this.warnings = msgs; |
| 157 | else |
| 158 | this.warnings.add(msgs); |
| 159 | return this; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | |
| 163 | /** |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 164 | * Return all errors. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 165 | * |
| Nils Diewald | c99ed5b | 2015-01-21 22:08:53 +0000 | [diff] [blame] | 166 | * @return The {@link Messages} object representing all errors |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 167 | */ |
| Akron | 7d45e6b | 2015-06-26 17:23:42 +0200 | [diff] [blame] | 168 | @JsonIgnore |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 169 | public Messages getErrors () { |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 170 | return this.errors; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 171 | }; |
| 172 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 173 | |
| 174 | /** |
| Nils Diewald | c99ed5b | 2015-01-21 22:08:53 +0000 | [diff] [blame] | 175 | * Set errors by means of a {@link JsonNode}. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 176 | * |
| 177 | * @param msgs |
| 178 | * JSON array of errors. |
| Nils Diewald | c99ed5b | 2015-01-21 22:08:53 +0000 | [diff] [blame] | 179 | * @return Notifications object for chaining. |
| 180 | */ |
| 181 | public Notifications setErrors (JsonNode msgs) { |
| 182 | for (JsonNode msg : msgs) |
| 183 | this.addError(msg); |
| 184 | return this; |
| 185 | }; |
| 186 | |
| 187 | |
| 188 | /** |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 189 | * Return a specific error based on an index. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 190 | * |
| 191 | * @param index |
| 192 | * The index of the error in the list of errors. |
| 193 | * @return The message in case it exists, otherwise |
| 194 | * <code>null</code> |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 195 | */ |
| Akron | 7d45e6b | 2015-06-26 17:23:42 +0200 | [diff] [blame] | 196 | @JsonIgnore |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 197 | public Message getError (int index) { |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 198 | if (this.errors != null) |
| 199 | return this.errors.get(index); |
| 200 | return (Message) null; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 201 | }; |
| 202 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 203 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 204 | /** |
| 205 | * Check for errors. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 206 | * |
| 207 | * @return <tt>true</tt> in case there are errors, otherwise |
| 208 | * <tt>false</tt> |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 209 | */ |
| 210 | public boolean hasErrors () { |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 211 | if (this.errors == null || this.errors.size() == 0) |
| 212 | return false; |
| 213 | return true; |
| 214 | }; |
| 215 | |
| 216 | |
| 217 | /** |
| 218 | * Appends a new error. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 219 | * |
| 220 | * @param code |
| 221 | * Integer code representation of the error |
| 222 | * @param msg |
| 223 | * String representation of the error |
| 224 | * @param terms |
| 225 | * Optional strings of additional information |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 226 | * @return Notification object for chaining |
| 227 | */ |
| 228 | public Notifications addError (int code, String msg, String ... terms) { |
| 229 | if (this.errors == null) |
| 230 | this.errors = new Messages(); |
| 231 | this.errors.add(code, msg, terms); |
| 232 | return this; |
| 233 | }; |
| 234 | |
| 235 | |
| 236 | /** |
| 237 | * Appends a new error. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 238 | * |
| 239 | * @param node |
| 240 | * {@link JsonNode} representing an error message |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 241 | * @return Notification object for chaining |
| 242 | */ |
| 243 | public Notifications addError (JsonNode msg) { |
| 244 | if (this.errors == null) |
| 245 | this.errors = new Messages(); |
| 246 | try { |
| 247 | this.errors.add(msg); |
| 248 | } |
| 249 | catch (QueryException qe) { |
| 250 | this.errors.add(qe.getErrorCode(), qe.getMessage()); |
| 251 | }; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 252 | |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 253 | return this; |
| 254 | }; |
| 255 | |
| 256 | |
| 257 | /** |
| 258 | * Appends new errors. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 259 | * |
| 260 | * @param msgs |
| 261 | * {@link Messages} representing multiple errors |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 262 | * @return Notification object for chaining |
| 263 | */ |
| 264 | public Notifications addErrors (Messages msgs) { |
| 265 | if (this.errors == null) |
| 266 | this.errors = msgs; |
| 267 | else |
| 268 | this.errors.add(msgs); |
| 269 | return this; |
| 270 | }; |
| 271 | |
| 272 | |
| 273 | /** |
| 274 | * Return all messages. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 275 | * |
| Nils Diewald | c99ed5b | 2015-01-21 22:08:53 +0000 | [diff] [blame] | 276 | * @return {@link Messages} representing all messages |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 277 | */ |
| Akron | 7d45e6b | 2015-06-26 17:23:42 +0200 | [diff] [blame] | 278 | @JsonIgnore |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 279 | public Messages getMessages () { |
| 280 | return this.messages; |
| 281 | }; |
| 282 | |
| Nils Diewald | c99ed5b | 2015-01-21 22:08:53 +0000 | [diff] [blame] | 283 | |
| 284 | /** |
| 285 | * Set messages by means of a {@link JsonNode}. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 286 | * |
| 287 | * @param msgs |
| 288 | * JSON array of messages. |
| Nils Diewald | c99ed5b | 2015-01-21 22:08:53 +0000 | [diff] [blame] | 289 | * @return Notifications object for chaining. |
| 290 | */ |
| 291 | public Notifications setMessages (JsonNode msgs) { |
| 292 | for (JsonNode msg : msgs) |
| 293 | this.addMessage(msg); |
| 294 | return this; |
| 295 | }; |
| 296 | |
| 297 | |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 298 | /** |
| 299 | * Return a specific message based on an index. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 300 | * |
| 301 | * @param index |
| 302 | * The index of the message in the list of messages. |
| 303 | * @return The message in case it exists, otherwise |
| 304 | * <code>null</code> |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 305 | */ |
| Akron | 7d45e6b | 2015-06-26 17:23:42 +0200 | [diff] [blame] | 306 | @JsonIgnore |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 307 | public Message getMessage (int index) { |
| 308 | if (this.messages != null) |
| 309 | return this.messages.get(index); |
| 310 | return (Message) null; |
| 311 | }; |
| 312 | |
| 313 | |
| 314 | /** |
| 315 | * Check for messages. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 316 | * |
| 317 | * @return <tt>true</tt> in case there are messages, otherwise |
| 318 | * <tt>false</tt> |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 319 | */ |
| 320 | public boolean hasMessages () { |
| 321 | if (this.messages == null || this.messages.size() == 0) |
| 322 | return false; |
| 323 | return true; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 324 | }; |
| 325 | |
| 326 | |
| 327 | /** |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 328 | * Appends a new message. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 329 | * |
| 330 | * @param code |
| 331 | * Integer code representation of the message |
| 332 | * @param msg |
| 333 | * String representation of the message |
| 334 | * @param terms |
| 335 | * Optional strings of additional information |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 336 | * @return Notification object for chaining |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 337 | */ |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 338 | public Notifications addMessage (int code, String msg, String ... terms) { |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 339 | if (this.messages == null) |
| 340 | this.messages = new Messages(); |
| 341 | this.messages.add(code, msg, terms); |
| 342 | return this; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 343 | }; |
| 344 | |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 345 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 346 | /** |
| 347 | * Appends a new message. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 348 | * |
| 349 | * @param node |
| 350 | * {@link JsonNode} representing a message |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 351 | * @return Notification object for chaining |
| 352 | */ |
| 353 | public Notifications addMessage (JsonNode msg) { |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 354 | if (this.messages == null) |
| 355 | this.messages = new Messages(); |
| 356 | try { |
| 357 | this.messages.add(msg); |
| 358 | } |
| 359 | catch (QueryException qe) { |
| 360 | this.messages.add(qe.getErrorCode(), qe.getMessage()); |
| 361 | }; |
| 362 | return this; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 363 | }; |
| 364 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 365 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 366 | /** |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 367 | * Appends new messages. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 368 | * |
| 369 | * @param msgs |
| 370 | * {@link Messages} representing multiple messages |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 371 | * @return Notification object for chaining |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 372 | */ |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 373 | public Notifications addMessages (Messages msgs) { |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 374 | if (this.messages == null) |
| 375 | this.messages = msgs; |
| 376 | else |
| 377 | this.messages.add(msgs); |
| 378 | return this; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 379 | }; |
| 380 | |
| 381 | |
| 382 | /** |
| Nils Diewald | c99ed5b | 2015-01-21 22:08:53 +0000 | [diff] [blame] | 383 | * Copy notifications from another notification object. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 384 | * |
| 385 | * @param notes |
| 386 | * Notification object to copy notifications from. |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 387 | * @return Notification object for chaining |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 388 | */ |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 389 | public Notifications copyNotificationsFrom (Notifications notes) { |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 390 | try { |
| 391 | if (notes.hasErrors()) |
| 392 | this.addErrors((Messages) notes.getErrors().clone()); |
| 393 | if (notes.hasWarnings()) |
| 394 | this.addWarnings((Messages) notes.getWarnings().clone()); |
| 395 | if (notes.hasMessages()) |
| 396 | this.addMessages((Messages) notes.getMessages().clone()); |
| 397 | } |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 398 | catch (CloneNotSupportedException cnse) {}; |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 399 | return this; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 400 | }; |
| 401 | |
| 402 | |
| 403 | /** |
| Nils Diewald | c99ed5b | 2015-01-21 22:08:53 +0000 | [diff] [blame] | 404 | * Copy notifications from a {@link JsonNode} object. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 405 | * |
| 406 | * @param request |
| 407 | * Notifications containing {@link JsonNode}. |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 408 | * @return Notification object for chaining |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 409 | */ |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 410 | public Notifications copyNotificationsFrom (JsonNode request) { |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 411 | |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 412 | // Add warnings from JSON |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 413 | if (request.has("warnings") && request.get("warnings").isArray()) { |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 414 | JsonNode msgs = request.get("warnings"); |
| 415 | for (JsonNode msg : msgs) |
| 416 | this.addWarning(msg); |
| 417 | }; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 418 | |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 419 | // Add messages from JSON |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 420 | if (request.has("messages") && request.get("messages").isArray()) { |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 421 | JsonNode msgs = request.get("messages"); |
| 422 | if (msgs.isArray()) |
| 423 | for (JsonNode msg : msgs) |
| 424 | this.addMessage(msg); |
| 425 | }; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 426 | |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 427 | // Add errors from JSON |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 428 | if (request.has("errors") && request.get("errors").isArray()) { |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 429 | JsonNode msgs = request.get("errors"); |
| 430 | if (msgs.isArray()) |
| 431 | for (JsonNode msg : msgs) |
| 432 | this.addError(msg); |
| 433 | }; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 434 | |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 435 | return this; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 436 | }; |
| 437 | |
| 438 | |
| 439 | /** |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 440 | * Move notifications from a passed {@link Notification} object |
| 441 | * to the invocant. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 442 | * |
| 443 | * @param notes |
| 444 | * Notification object. |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 445 | * @return The invocant object for chaining |
| 446 | */ |
| 447 | public Notifications moveNotificationsFrom (Notifications notes) { |
| 448 | this.copyNotificationsFrom(notes); |
| 449 | notes.clearNotifications(); |
| 450 | return this; |
| 451 | }; |
| 452 | |
| 453 | |
| 454 | /** |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 455 | * Clear all notifications. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 456 | * |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 457 | * @return Notification object for chaining |
| 458 | */ |
| 459 | public Notifications clearNotifications () { |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 460 | if (this.warnings != null) |
| 461 | this.warnings.clear(); |
| 462 | if (this.messages != null) |
| 463 | this.messages.clear(); |
| 464 | if (this.errors != null) |
| 465 | this.errors.clear(); |
| 466 | return this; |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 467 | }; |
| 468 | |
| 469 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 470 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 471 | /** |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 472 | * Serialize Notifications as a {@link JsonNode}. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 473 | * |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 474 | * @return {@link JsonNode} representation of all warnings, |
| 475 | * errors, and messages. |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 476 | */ |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 477 | public JsonNode toJsonNode () { |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 478 | ObjectNode json = mapper.createObjectNode(); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 479 | |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 480 | // Add messages |
| 481 | if (this.hasWarnings()) |
| 482 | json.put("warnings", this.getWarnings().toJsonNode()); |
| 483 | if (this.hasErrors()) |
| 484 | json.put("errors", this.getErrors().toJsonNode()); |
| 485 | if (this.hasMessages()) |
| 486 | json.put("messages", this.getMessages().toJsonNode()); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 487 | |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 488 | return (JsonNode) json; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 489 | }; |
| 490 | |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 491 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 492 | /** |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 493 | * Serialize Notifications as a JSON string. |
| 494 | * <p> |
| 495 | * <blockquote><pre> |
| 496 | * { |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 497 | * "errors": [ |
| 498 | * [123, "You are not allowed to serialize these messages"], |
| 499 | * [124, "Your request was invalid"] |
| 500 | * ], |
| 501 | * "messages" : [ |
| 502 | * [125, "Class is deprecated", "Notifications"] |
| 503 | * ] |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 504 | * } |
| 505 | * </pre></blockquote> |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 506 | * |
| 507 | * @return String representation of all warnings, errors, and |
| 508 | * messages |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 509 | */ |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 510 | public String toJsonString () { |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 511 | String msg = ""; |
| 512 | try { |
| 513 | JsonNode node = this.toJsonNode(); |
| 514 | if (node == null) |
| 515 | return "{}"; |
| 516 | return mapper.writeValueAsString(node); |
| 517 | } |
| 518 | catch (Exception e) { |
| 519 | // Bad in case the message contains quotes! |
| 520 | msg = ", \"" + e.getLocalizedMessage() + "\""; |
| 521 | }; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 522 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 523 | return "{\"errors\" : [" + "[620, " + "\"Unable to generate JSON\"" |
| 524 | + msg + "]" + "]}"; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 525 | }; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 526 | }; |