]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/orga/AffiliationForm.java
ad988895b351d5c26e375813257ba881cbf5639c
[gigi.git] / src / org / cacert / gigi / pages / orga / AffiliationForm.java
1 package org.cacert.gigi.pages.orga;
2
3 import java.io.PrintWriter;
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.Map;
7
8 import javax.servlet.http.HttpServletRequest;
9
10 import org.cacert.gigi.GigiApiException;
11 import org.cacert.gigi.dbObjects.Organisation;
12 import org.cacert.gigi.dbObjects.Organisation.Affiliation;
13 import org.cacert.gigi.dbObjects.User;
14 import org.cacert.gigi.localisation.Language;
15 import org.cacert.gigi.output.template.Form;
16 import org.cacert.gigi.output.template.IterableDataset;
17 import org.cacert.gigi.output.template.Template;
18 import org.cacert.gigi.pages.LoginPage;
19 import org.cacert.gigi.pages.Page;
20
21 public class AffiliationForm extends Form {
22
23     Organisation o;
24
25     private static final Template t = new Template(AffiliationForm.class.getResource("AffiliationForm.templ"));
26
27     public AffiliationForm(HttpServletRequest hsr, Organisation o) {
28         super(hsr);
29         this.o = o;
30     }
31
32     @Override
33     public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
34         if (req.getParameter("del") != null) {
35             User toRemove = User.getByEmail(req.getParameter("del"));
36             if (toRemove != null) {
37                 o.removeAdmin(toRemove, LoginPage.getUser(req));
38                 return true;
39             }
40         } else if (req.getParameter("do_affiliate") != null) {
41             User byEmail = User.getByEmail(req.getParameter("email"));
42             if (byEmail != null && byEmail.canAssure()) {
43                 o.addAdmin(byEmail, LoginPage.getUser(req), req.getParameter("master") != null);
44                 return true;
45             }
46         }
47         out.println(Page.getLanguage(req).getTranslation("No action could have been carried out."));
48         return false;
49     }
50
51     @Override
52     protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
53         final List<Affiliation> admins = o.getAllAdmins();
54         vars.put("admins", new IterableDataset() {
55
56             Iterator<Affiliation> iter = admins.iterator();
57
58             @Override
59             public boolean next(Language l, Map<String, Object> vars) {
60                 if ( !iter.hasNext()) {
61                     return false;
62                 }
63                 Affiliation aff = iter.next();
64                 vars.put("name", aff.getTarget().getName());
65                 vars.put("master", aff.isMaster() ? l.getTranslation("master") : "");
66                 vars.put("e-mail", aff.getTarget().getEmail());
67                 return true;
68             }
69         });
70         t.output(out, l, vars);
71     }
72
73     public Organisation getOrganisation() {
74         return o;
75     }
76 }