]> WPIA git - gigi.git/blob - src/org/cacert/gigi/GigiApiException.java
fix: a problem when setting up new test environments
[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 long serialVersionUID = -164928670180852588L;
17
18     private SQLException e;
19
20     private LinkedList<Outputable> messages = new LinkedList<>();
21
22     public GigiApiException(SQLException e) {
23         super(e);
24         this.e = e;
25     }
26
27     public GigiApiException(String message) {
28         super(message);
29         messages.add(new TranslateCommand(message));
30     }
31
32     public GigiApiException() {
33
34     }
35
36     public GigiApiException(Outputable out) {
37         messages.add(out);
38     }
39
40     public void mergeInto(GigiApiException e2) {
41         messages.addAll(e2.messages);
42         if (e == null) {
43             e = e2.e;
44         }
45     }
46
47     public boolean isInternalError() {
48         return e != null;
49     }
50
51     public void format(PrintWriter out, Language language) {
52         out.println("<div class='formError'>");
53         if (isInternalError()) {
54             e.printStackTrace();
55             out.print("<div>");
56             out.println(language.getTranslation("An internal error ouccured."));
57             out.println("</div>");
58         }
59         HashMap<String, Object> map = new HashMap<>();
60         for (Outputable message : messages) {
61             map.clear();
62
63             out.print("<div>");
64             message.output(out, language, map);
65             out.println("</div>");
66         }
67         out.println("</div>");
68
69     }
70
71     public boolean isEmpty() {
72         return e == null && messages.size() == 0;
73     }
74
75     @Override
76     public String getMessage() {
77         if (messages.size() != 0) {
78             StringWriter sw = new StringWriter();
79             PrintWriter pw = new PrintWriter(sw);
80
81             HashMap<String, Object> map = new HashMap<>();
82             for (Outputable message : messages) {
83                 map.clear();
84                 message.output(pw, Language.getInstance(Locale.ENGLISH), map);
85             }
86             pw.flush();
87
88             return sw.toString();
89         }
90         return "";
91     }
92
93 }