]> WPIA git - gigi.git/blob - src/org/cacert/gigi/GigiApiException.java
a61b4b5c68cc431445fa192f0f9d842d0e791ab4
[gigi.git] / src / org / cacert / gigi / GigiApiException.java
1 package org.cacert.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
10 import org.cacert.gigi.localisation.Language;
11 import org.cacert.gigi.output.template.Outputable;
12 import org.cacert.gigi.output.template.TranslateCommand;
13
14 public class GigiApiException extends Exception {
15
16     private static final Language PLAIN_LANGUAGE = Language.getInstance(Locale.ENGLISH);
17
18     private static final long serialVersionUID = -164928670180852588L;
19
20     private SQLException e;
21
22     private LinkedList<Outputable> messages = new LinkedList<>();
23
24     public GigiApiException(SQLException e) {
25         super(e);
26         this.e = e;
27     }
28
29     public GigiApiException(String message) {
30         super(message);
31         messages.add(new TranslateCommand(message));
32     }
33
34     public GigiApiException() {
35
36     }
37
38     public GigiApiException(Outputable out) {
39         messages.add(out);
40     }
41
42     public void mergeInto(GigiApiException e2) {
43         messages.addAll(e2.messages);
44         if (e == null) {
45             e = e2.e;
46         }
47     }
48
49     public boolean isInternalError() {
50         return e != null;
51     }
52
53     public void format(PrintWriter out, Language language) {
54         out.println("<div class='bg-danger error-msgs'>");
55         if (isInternalError()) {
56             e.printStackTrace();
57             out.print("<p>");
58             out.println(language.getTranslation("An internal error occurred."));
59             out.println("</p>");
60         }
61         HashMap<String, Object> map = new HashMap<>();
62         for (Outputable message : messages) {
63             map.clear();
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 }