upd: terminology in code
[gigi.git] / src / club / wpia / gigi / pages / orga / ViewOrgPage.java
1 package club.wpia.gigi.pages.orga;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11
12 import club.wpia.gigi.dbObjects.Organisation;
13 import club.wpia.gigi.dbObjects.User;
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.output.template.Form.CSRFException;
19 import club.wpia.gigi.pages.LoginPage;
20 import club.wpia.gigi.pages.ManagedMultiFormPage;
21 import club.wpia.gigi.pages.account.domain.DomainManagementForm;
22 import club.wpia.gigi.util.AuthorizationContext;
23
24 public class ViewOrgPage extends ManagedMultiFormPage {
25
26     private static final Template orgas = new Template(ViewOrgPage.class.getResource("ViewOrgs.templ"));
27
28     private static final Template mainTempl = new Template(ViewOrgPage.class.getResource("EditOrg.templ"));
29
30     public static final String DEFAULT_PATH = "/orga";
31
32     public ViewOrgPage() {
33         super("View Organisation");
34     }
35
36     @Override
37     public boolean isPermitted(AuthorizationContext ac) {
38         return ac != null && (ac.isInGroup(CreateOrgPage.ORG_AGENT) || ac.getActor().getOrganisations(true).size() != 0);
39     }
40
41     @Override
42     public Form getForm(HttpServletRequest req) throws CSRFException {
43         if (req.getParameter("do_affiliate") != null || req.getParameter("del") != null) {
44             return Form.getForm(req, AffiliationForm.class);
45         } else {
46             if ( !getUser(req).isInGroup(CreateOrgPage.ORG_AGENT)) {
47                 return null;
48             }
49
50             if (req.getParameter("addDomain") != null) {
51                 return Form.getForm(req, OrgDomainAddForm.class);
52             } else if (req.getParameter("delete") != null) {
53                 return Form.getForm(req, DomainManagementForm.class);
54             } else {
55                 return Form.getForm(req, CreateOrgForm.class);
56             }
57         }
58     }
59
60     @Override
61     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
62         User u = LoginPage.getUser(req);
63         String idS = req.getPathInfo();
64         Language lang = getLanguage(req);
65         PrintWriter out = resp.getWriter();
66         if (idS.length() < DEFAULT_PATH.length() + 2) {
67             final Organisation[] orgList = Organisation.getOrganisations(0, 30);
68             HashMap<String, Object> map = new HashMap<>();
69             final List<Organisation> myOrgs = u.getOrganisations(true);
70             final boolean orgAss = u.isInGroup(CreateOrgPage.ORG_AGENT);
71             if (orgAss) {
72                 map.put("orgas", makeOrgDataset(orgList));
73             } else {
74                 map.put("orgas", makeOrgDataset(myOrgs.toArray(new Organisation[myOrgs.size()])));
75             }
76             orgas.output(out, lang, map);
77             return;
78         }
79         idS = idS.substring(DEFAULT_PATH.length() + 1);
80         int id = Integer.parseInt(idS);
81         Organisation o;
82         try {
83             o = Organisation.getById(id);
84         } catch (IllegalArgumentException e) {
85             resp.sendError(404);
86             return;
87         }
88         final List<Organisation> myOrgs = u.getOrganisations();
89         final boolean orgAss = u.isInGroup(CreateOrgPage.ORG_AGENT);
90         if ( !orgAss && !myOrgs.contains(o)) {
91             resp.sendError(404);
92             return;
93         }
94         HashMap<String, Object> vars = new HashMap<>();
95         if (orgAss) {
96             vars.put("editForm", new CreateOrgForm(req, o));
97             vars.put("affForm", new AffiliationForm(req, o));
98             vars.put("mgmDom", new DomainManagementForm(req, o, true));
99             vars.put("addDom", new OrgDomainAddForm(req, o));
100         } else {
101             vars.put("affForm", new AffiliationForm(req, o));
102             vars.put("orgName", o.getName());
103         }
104         mainTempl.output(out, lang, vars);
105     }
106
107     private IterableDataset makeOrgDataset(final Organisation[] orgas) {
108         return new IterableDataset() {
109
110             int count = 0;
111
112             @Override
113             public boolean next(Language l, Map<String, Object> vars) {
114                 if (count >= orgas.length) {
115                     return false;
116                 }
117                 Organisation org = orgas[count++];
118                 vars.put("id", Integer.toString(org.getId()));
119                 vars.put("name", org.getName());
120                 vars.put("country", org.getCountry().getCode());
121                 return true;
122             }
123         };
124     }
125 }