]> WPIA git - gigi.git/blob - src/club/wpia/gigi/GigiApiException.java
chg: ensure actor, target and support ticket are non-null
[gigi.git] / src / club / wpia / gigi / GigiApiException.java
1 package club.wpia.gigi;
2
3 import java.io.PrintWriter;
4 import java.io.StringWriter;
5 import java.sql.SQLException;
6 import java.util.HashMap;
7 import java.util.LinkedList;
8 import java.util.Locale;
9 import java.util.Map;
10
11 import club.wpia.gigi.localisation.Language;
12 import club.wpia.gigi.output.template.Outputable;
13 import club.wpia.gigi.output.template.TranslateCommand;
14
15 public class GigiApiException extends Exception {
16
17     private static final Language PLAIN_LANGUAGE = Language.getInstance(Locale.ENGLISH);
18
19     private static final long serialVersionUID = -164928670180852588L;
20
21     private SQLException e;
22
23     private LinkedList<Outputable> messages = new LinkedList<>();
24
25     public GigiApiException(SQLException e) {
26         super(e);
27         this.e = e;
28     }
29
30     public GigiApiException(String message) {
31         super(message);
32         messages.add(new TranslateCommand(message));
33     }
34
35     public GigiApiException() {
36
37     }
38
39     public GigiApiException(Outputable out) {
40         messages.add(out);
41     }
42
43     public void mergeInto(GigiApiException e2) {
44         messages.addAll(e2.messages);
45         if (e == null) {
46             e = e2.e;
47         }
48     }
49
50     public boolean isInternalError() {
51         return e != null;
52     }
53
54     public void format(PrintWriter out, Language language, Map<String, Object> vars) {
55         out.println("<div class='alert alert-danger error-msgs'>");
56         if (isInternalError()) {
57             e.printStackTrace();
58             out.print("<p>");
59             out.println(language.getTranslation("An internal error occurred."));
60             out.println("</p>");
61         }
62         for (Outputable message : messages) {
63             HashMap<String, Object> map = new HashMap<>(vars);
64
65             out.print("<p>");
66             message.output(out, language, map);
67             out.println("</p>");
68         }
69         out.println("</div>");
70
71     }
72
73     public void formatPlain(PrintWriter out) {
74         if (isInternalError()) {
75             out.println(PLAIN_LANGUAGE.getTranslation("An internal error occurred."));
76         }
77         HashMap<String, Object> map = new HashMap<>();
78         for (Outputable message : messages) {
79             if (message instanceof TranslateCommand) {
80                 String m = ((TranslateCommand) message).getRaw();
81                 // Skip HTML Entities
82                 out.println(PLAIN_LANGUAGE.getTranslation(m));
83             } else {
84                 map.clear();
85                 message.output(out, PLAIN_LANGUAGE, map);
86                 out.println();
87             }
88         }
89     }
90
91     public boolean isEmpty() {
92         return e == null && messages.size() == 0;
93     }
94
95     @Override
96     public String getMessage() {
97         if (messages.size() != 0) {
98             StringWriter sw = new StringWriter();
99             PrintWriter pw = new PrintWriter(sw);
100
101             HashMap<String, Object> map = new HashMap<>();
102             for (Outputable message : messages) {
103                 map.clear();
104                 message.output(pw, PLAIN_LANGUAGE, map);
105             }
106             pw.flush();
107
108             return sw.toString();
109         }
110         return "";
111     }
112
113 }