1 package club.wpia.gigi.pages.orga;
3 import java.io.PrintWriter;
4 import java.util.Iterator;
8 import javax.servlet.http.HttpServletRequest;
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;
21 public class AffiliationForm extends Form {
25 private static final Template t = new Template(AffiliationForm.class.getResource("AffiliationForm.templ"));
27 public AffiliationForm(HttpServletRequest hsr, Organisation o) {
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());
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.");
45 if (byEmail.canVerify()) {
46 o.addAdmin(byEmail, LoginPage.getUser(req), req.getParameter("master") != null);
47 return new RedirectResult(ViewOrgPage.DEFAULT_PATH + "/" + o.getId());
49 throw new GigiApiException("Requested user is not a RA Agent. We need a RA Agent here.");
52 throw new GigiApiException("No action could have been carried out.");
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() {
60 Iterator<Affiliation> iter = admins.iterator();
63 public boolean next(Language l, Map<String, Object> vars) {
64 if ( !iter.hasNext()) {
67 Affiliation aff = iter.next();
68 Name n = aff.getTarget().getPreferredName();
70 vars.put("nameString", n.toString());
71 vars.put("master", aff.isMaster() ? l.getTranslation("Master") : "");
72 vars.put("e-mail", aff.getTarget().getEmail());
76 t.output(out, l, vars);