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