UPD: show the "verified"-status primitive-like for domains.
[gigi.git] / src / org / cacert / gigi / pages / account / DomainManagementForm.java
1 package org.cacert.gigi.pages.account;
2
3 import java.io.PrintWriter;
4 import java.util.Map;
5
6 import javax.servlet.http.HttpServletRequest;
7
8 import org.cacert.gigi.Domain;
9 import org.cacert.gigi.GigiApiException;
10 import org.cacert.gigi.User;
11 import org.cacert.gigi.localisation.Language;
12 import org.cacert.gigi.output.Form;
13 import org.cacert.gigi.output.template.IterableDataset;
14 import org.cacert.gigi.output.template.Template;
15 import org.cacert.gigi.pages.Page;
16
17 public class DomainManagementForm extends Form {
18
19     private static final Template t = new Template(DomainManagementForm.class.getResource("DomainManagementForm.templ"));
20
21     private User target;
22
23     public DomainManagementForm(HttpServletRequest hsr, User target) {
24         super(hsr);
25         this.target = target;
26     }
27
28     @Override
29     public boolean submit(PrintWriter out, HttpServletRequest req) {
30         try {
31             String[] dels = req.getParameterValues("delid[]");
32             Domain[] usDomains = target.getDomains();
33             for (int i = 0; i < dels.length; i++) {
34                 int delId = Integer.parseInt(dels[i]);
35                 for (int j = 0; j < usDomains.length; j++) {
36                     if (usDomains[j].getId() == delId) {
37                         usDomains[j].delete();
38                         break;
39                     }
40                 }
41             }
42         } catch (GigiApiException e) {
43             e.format(out, Page.getLanguage(req));
44             return false;
45         }
46         return true;
47     }
48
49     @Override
50     protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
51         final Domain[] doms = (Domain[]) vars.get("doms");
52         IterableDataset dts = new IterableDataset() {
53
54             private int point = 0;
55
56             @Override
57             public boolean next(Language l, Map<String, Object> vars) {
58                 if (point >= doms.length) {
59                     return false;
60                 }
61                 Domain domain = doms[point];
62                 vars.put("id", domain.getId());
63                 vars.put("domain", domain.getSuffix());
64                 vars.put("status", l.getTranslation(domain.isVerified() ? "verified" : "not verified"));
65                 point++;
66                 return true;
67             }
68         };
69         vars.put("domains", dts);
70         t.output(out, l, vars);
71     }
72
73 }