]> WPIA git - gigi.git/blob - src/club/wpia/gigi/pages/orga/MyOrganisationsForm.java
fix: move switch to organisation context to separate page
[gigi.git] / src / club / wpia / gigi / pages / orga / MyOrganisationsForm.java
1 package club.wpia.gigi.pages.orga;
2
3 import java.io.PrintWriter;
4 import java.util.Enumeration;
5 import java.util.Iterator;
6 import java.util.List;
7 import java.util.Map;
8
9 import javax.servlet.http.HttpServletRequest;
10
11 import club.wpia.gigi.Gigi;
12 import club.wpia.gigi.GigiApiException;
13 import club.wpia.gigi.dbObjects.Organisation;
14 import club.wpia.gigi.localisation.Language;
15 import club.wpia.gigi.output.template.Form;
16 import club.wpia.gigi.output.template.IterableDataset;
17 import club.wpia.gigi.output.template.Template;
18 import club.wpia.gigi.pages.LoginPage;
19 import club.wpia.gigi.util.AuthorizationContext;
20
21 public class MyOrganisationsForm extends Form {
22
23     private AuthorizationContext target;
24
25     public MyOrganisationsForm(HttpServletRequest hsr) {
26         super(hsr);
27         target = LoginPage.getAuthorizationContext(hsr);
28     }
29
30     private static final Template template = new Template(MyOrganisationsForm.class.getResource("MyOrganisationsForm.templ"));
31
32     @Override
33     public SubmissionResult submit(HttpServletRequest req) throws GigiApiException {
34         if (req.getParameter("org-leave") != null) {
35             req.getSession().setAttribute(Gigi.AUTH_CONTEXT, new AuthorizationContext(target.getActor(), target.getActor()));
36             return new RedirectResult(SwitchOrganisation.PATH);
37         }
38         Enumeration<String> i = req.getParameterNames();
39         int orgId = -1;
40         while (i.hasMoreElements()) {
41             String s = i.nextElement();
42             if (s.startsWith("org:")) {
43                 int id = Integer.parseInt(s.substring(4));
44                 if (orgId == -1) {
45                     orgId = id;
46                 } else {
47                     throw new GigiApiException("Error: invalid parameter.");
48                 }
49             }
50         }
51         for (Organisation org : target.getActor().getOrganisations()) {
52             if (org.getId() == orgId) {
53
54                 req.getSession().setAttribute(Gigi.AUTH_CONTEXT, new AuthorizationContext(org, target.getActor()));
55                 return new RedirectResult(SwitchOrganisation.PATH);
56             }
57         }
58         throw new PermamentFormException(new GigiApiException("Context switch failed."));
59     }
60
61     @Override
62     protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
63         final List<Organisation> o = target.getActor().getOrganisations();
64         if (target.getTarget() != target.getActor()) {
65             vars.put("personal", target.getTarget() != target.getActor());
66         }
67         vars.put("orgas", new IterableDataset() {
68
69             Iterator<Organisation> it = o.iterator();
70
71             @Override
72             public boolean next(Language l, Map<String, Object> vars) {
73                 if ( !it.hasNext()) {
74                     return false;
75                 }
76                 Organisation o = it.next();
77                 vars.put("orgName", o.getName());
78                 vars.put("orgID", o.getId());
79                 return true;
80             }
81         });
82         template.output(out, l, vars);
83
84     }
85
86 }