]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/account/mail/MailManagementForm.java
9be2bf528132044ed4132b331a39631b5fa3a554
[gigi.git] / src / org / cacert / gigi / pages / account / mail / MailManagementForm.java
1 package org.cacert.gigi.pages.account.mail;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.Map;
6 import java.util.Map.Entry;
7
8 import javax.servlet.http.HttpServletRequest;
9
10 import org.cacert.gigi.GigiApiException;
11 import org.cacert.gigi.dbObjects.EmailAddress;
12 import org.cacert.gigi.dbObjects.User;
13 import org.cacert.gigi.localisation.Language;
14 import org.cacert.gigi.output.template.Form;
15 import org.cacert.gigi.output.template.IterableDataset;
16 import org.cacert.gigi.output.template.Template;
17 import org.cacert.gigi.pages.Page;
18
19 public class MailManagementForm extends Form {
20
21     private static Template t;
22
23     private User target;
24     static {
25         t = new Template(MailAddForm.class.getResource("MailManagementForm.templ"));
26     }
27
28     public MailManagementForm(HttpServletRequest hsr, User target) {
29         super(hsr);
30         this.target = target;
31     }
32
33     @Override
34     public boolean submit(PrintWriter out, HttpServletRequest req) {
35         Map<String, String[]> map = req.getParameterMap();
36         try {
37             for (Entry<String, String[]> e : map.entrySet()) {
38                 String k = e.getKey();
39                 String[] p = k.split(":", 2);
40                 if (p[0].equals("default")) {
41                     target.updateDefaultEmail(EmailAddress.getById(Integer.parseInt(p[1])));
42                 }
43                 if (p[0].equals("delete")) {
44                     target.deleteEmail(EmailAddress.getById(Integer.parseInt(p[1])));
45                 }
46                 if (p[0].equals("reping")) {
47                     EmailAddress.getById(Integer.parseInt(p[1])).requestReping(Page.getLanguage(req));
48                 }
49             }
50         } catch (GigiApiException e) {
51             e.format(out, Page.getLanguage(req));
52             return false;
53         } catch (IOException e1) {
54             new GigiApiException("Error while doing reping.").format(out, Page.getLanguage(req));
55             return false;
56         }
57         return true;
58     }
59
60     @Override
61     protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
62         final EmailAddress[] emails = target.getEmails();
63         IterableDataset ds = new IterableDataset() {
64
65             private int point = 0;
66
67             @Override
68             public boolean next(Language l, Map<String, Object> vars) {
69                 if (point >= emails.length) {
70                     return false;
71                 }
72                 EmailAddress emailAddress = emails[point];
73                 int mailID = emailAddress.getId();
74                 vars.put("id", mailID);
75                 if (emailAddress.getAddress().equals(target.getEmail())) {
76                     vars.put("default", " disabled");
77                 } else {
78                     vars.put("default", "");
79                 }
80                 if (emailAddress.isVerified()) {
81                     vars.put("verification", l.getTranslation("Verified"));
82                 } else {
83                     vars.put("verification", l.getTranslation("Unverified"));
84                 }
85                 vars.put("last_verification", emailAddress.getLastPing(true));
86                 if (target.getEmail().equals(emailAddress.getAddress())) {
87                     vars.put("delete", "N/A");
88                 } else {
89                     vars.put("delete", "<input type=\"checkbox\" name=\"delid[]\" value=\"" + mailID + "\"/>");
90                 }
91                 vars.put("address", emailAddress.getAddress());
92                 point++;
93                 return true;
94             }
95
96         };
97         vars.put("emails", ds);
98         t.output(out, l, vars);
99     }
100 }