]> WPIA git - gigi.git/blob - src/club/wpia/gigi/pages/orga/AffiliationForm.java
fix: adjust output to show delete button correct
[gigi.git] / src / club / wpia / gigi / pages / orga / AffiliationForm.java
1 package club.wpia.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 club.wpia.gigi.GigiApiException;
11 import club.wpia.gigi.dbObjects.Name;
12 import club.wpia.gigi.dbObjects.Organisation;
13 import club.wpia.gigi.dbObjects.Organisation.Affiliation;
14 import club.wpia.gigi.dbObjects.User;
15 import club.wpia.gigi.localisation.Language;
16 import club.wpia.gigi.output.template.Form;
17 import club.wpia.gigi.output.template.IterableDataset;
18 import club.wpia.gigi.output.template.Template;
19 import club.wpia.gigi.pages.LoginPage;
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 SubmissionResult submit(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 new RedirectResult(ViewOrgPage.DEFAULT_PATH + "/" + o.getId());
39             }
40         } else if (req.getParameter("do_affiliate") != null) {
41             User byEmail = User.getByEmail(req.getParameter("email"));
42             if (byEmail == null) {
43                 throw new GigiApiException("To add an admin, the email address needs to be known to the system.");
44             }
45             if (byEmail.canVerify()) {
46                 o.addAdmin(byEmail, LoginPage.getUser(req), req.getParameter("master") != null);
47                 return new RedirectResult(ViewOrgPage.DEFAULT_PATH + "/" + o.getId());
48             } else {
49                 throw new GigiApiException("Requested user is not a RA Agent. We need a RA Agent here.");
50             }
51         }
52         throw new GigiApiException("No action could have been carried out.");
53     }
54
55     @Override
56     protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
57         final List<Affiliation> admins = o.getAllAdmins();
58         vars.put("admins", new IterableDataset() {
59
60             Iterator<Affiliation> iter = admins.iterator();
61
62             @Override
63             public boolean next(Language l, Map<String, Object> vars) {
64                 if ( !iter.hasNext()) {
65                     return false;
66                 }
67                 Affiliation aff = iter.next();
68                 Name n = aff.getTarget().getPreferredName();
69                 vars.put("name", n);
70                 vars.put("nameString", n.toString());
71                 vars.put("master", aff.isMaster() ? l.getTranslation("Master") : "");
72                 vars.put("e-mail", aff.getTarget().getEmail());
73                 return true;
74             }
75         });
76         t.output(out, l, vars);
77     }
78 }