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