1 /*
2 * This file is part of Indicators.
3 *
4 * Indicators is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * Indicators is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Indicators. If not, see <https://www.gnu.org/licenses/>.
16 */
17 package fr.inrae.agroclim.indicators.exception;
18
19 import java.io.Serializable;
20 import java.util.Collection;
21 import java.util.Locale;
22
23 import org.json.JSONObject;
24
25 import fr.inrae.agroclim.indicators.resources.I18n;
26 import lombok.EqualsAndHashCode;
27 import lombok.Getter;
28 import lombok.RequiredArgsConstructor;
29 import lombok.ToString;
30
31 /**
32 * Error messages for the user.
33 *
34 * Last change $Date$
35 *
36 * @author $Author$
37 * @version $Revision$
38 */
39 @EqualsAndHashCode
40 @RequiredArgsConstructor
41 @ToString
42 public final class ErrorMessage implements Serializable {
43
44 /**
45 * UUID for Serializable.
46 */
47 private static final long serialVersionUID = 6030595237342400004L;
48
49 /**
50 * Get JSON representation of ErrorMessage.
51 *
52 * @param bundleName Path of .property resource.
53 * @param errorType error type
54 * @param arguments Arguments for the message.
55 * @return JSON representation
56 */
57 public static String toJSON(final String bundleName, final ErrorType errorType,
58 final Collection<Serializable> arguments) {
59 return new ErrorMessage(bundleName, errorType, arguments).toJSON();
60 }
61
62 /**
63 * Path of .property resource.
64 */
65 @Getter
66 private final String bundleName;
67
68 /**
69 * Error type.
70 */
71 @Getter
72 private final ErrorType type;
73
74 /**
75 * Arguments for the message.
76 */
77 @Getter
78 private final Collection<Serializable> arguments;
79
80 /**
81 * Get localized message using the Resources for bundleName and default
82 * locale.
83 *
84 * @return localized message
85 */
86 public String getMessage() {
87 return getMessage(Locale.getDefault());
88 }
89
90 /**
91 * Get localized message using a Resources.
92 *
93 * @param resources resources from .properties file.
94 * @return localized message
95 */
96 public String getMessage(final I18n resources) {
97 if (arguments != null) {
98 return resources.format(type.getI18nKey(), arguments.toArray());
99 } else {
100 return resources.get(type.getI18nKey());
101 }
102 }
103
104 /**
105 * Get localized message using the Resources for bundleName and the given locale.
106 *
107 * @param locale locale to localize the message
108 * @return localized message
109 */
110 public String getMessage(final Locale locale) {
111 return getMessage(new I18n(bundleName, locale));
112 }
113
114 /**
115 * Serialize to JSON.
116 *
117 * @return JSON representation
118 */
119 public String toJSON() {
120 final JSONObject json = new JSONObject();
121 json.put("bundleName", bundleName);
122 if (type.getCategory() != null) {
123 final JSONObject cat = new JSONObject();
124 cat.put("code", type.getCategory().getCode());
125 cat.put("name", type.getCategory().getName());
126 json.put("category", cat);
127 }
128 final JSONObject error = new JSONObject();
129 error.put("code", type.getFullCode());
130 error.put("name", type.getName());
131 json.put("error", error);
132 if (arguments != null) {
133 json.put("arguments", arguments.stream().map(Object::toString).toList());
134 }
135 return json.toString(2);
136 }
137 }