1 package org.cacert.gigi.pages.orga;
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.HashMap;
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
12 import org.cacert.gigi.GigiApiException;
13 import org.cacert.gigi.dbObjects.Organisation;
14 import org.cacert.gigi.dbObjects.User;
15 import org.cacert.gigi.localisation.Language;
16 import org.cacert.gigi.output.template.Form;
17 import org.cacert.gigi.output.template.IterableDataset;
18 import org.cacert.gigi.output.template.Template;
19 import org.cacert.gigi.pages.LoginPage;
20 import org.cacert.gigi.pages.Page;
21 import org.cacert.gigi.pages.account.domain.DomainManagementForm;
22 import org.cacert.gigi.util.AuthorizationContext;
24 public class ViewOrgPage extends Page {
26 private static final Template orgas = new Template(ViewOrgPage.class.getResource("ViewOrgs.templ"));
28 private static final Template mainTempl = new Template(ViewOrgPage.class.getResource("EditOrg.templ"));
30 public static final String DEFAULT_PATH = "/orga";
32 public ViewOrgPage() {
33 super("View Organisation");
37 public boolean isPermitted(AuthorizationContext ac) {
38 return ac != null && (ac.isInGroup(CreateOrgPage.ORG_ASSURER) || ac.getActor().getOrganisations(true).size() != 0);
42 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
44 User u = LoginPage.getUser(req);
45 if (req.getParameter("do_affiliate") != null || req.getParameter("del") != null) {
46 AffiliationForm form = Form.getForm(req, AffiliationForm.class);
47 if (form.submit(resp.getWriter(), req)) {
48 resp.sendRedirect(DEFAULT_PATH + "/" + form.getOrganisation().getId());
52 if ( !u.isInGroup(CreateOrgPage.ORG_ASSURER)) {
53 resp.sendError(403, "Access denied");
57 if (req.getParameter("addDomain") != null) {
58 OrgDomainAddForm form = Form.getForm(req, OrgDomainAddForm.class);
59 if (form.submit(resp.getWriter(), req)) {
60 resp.sendRedirect(DEFAULT_PATH + "/" + form.getOrganisation().getId());
62 } else if (req.getParameter("delete") != null) {
63 DomainManagementForm form = Form.getForm(req, DomainManagementForm.class);
64 if (form.submit(resp.getWriter(), req)) {
65 resp.sendRedirect(DEFAULT_PATH + "/" + form.getTarget().getId());
68 CreateOrgForm form = Form.getForm(req, CreateOrgForm.class);
69 if (form.submit(resp.getWriter(), req)) {
70 resp.sendRedirect(DEFAULT_PATH + "/" + form.getResult().getId());
75 } catch (GigiApiException e) {
76 e.format(resp.getWriter(), getLanguage(req));
81 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
82 User u = LoginPage.getUser(req);
83 String idS = req.getPathInfo();
84 Language lang = getLanguage(req);
85 PrintWriter out = resp.getWriter();
86 if (idS.length() < DEFAULT_PATH.length() + 2) {
87 final Organisation[] orgList = Organisation.getOrganisations(0, 30);
88 HashMap<String, Object> map = new HashMap<>();
89 final List<Organisation> myOrgs = u.getOrganisations(true);
90 final boolean orgAss = u.isInGroup(CreateOrgPage.ORG_ASSURER);
92 map.put("orgas", makeOrgDataset(orgList));
94 map.put("orgas", makeOrgDataset(myOrgs.toArray(new Organisation[myOrgs.size()])));
96 orgas.output(out, lang, map);
99 idS = idS.substring(DEFAULT_PATH.length() + 1);
100 int id = Integer.parseInt(idS);
103 o = Organisation.getById(id);
104 } catch (IllegalArgumentException e) {
108 final List<Organisation> myOrgs = u.getOrganisations();
109 final boolean orgAss = u.isInGroup(CreateOrgPage.ORG_ASSURER);
110 if ( !orgAss && !myOrgs.contains(o)) {
114 HashMap<String, Object> vars = new HashMap<>();
116 vars.put("editForm", new CreateOrgForm(req, o));
117 vars.put("affForm", new AffiliationForm(req, o));
118 vars.put("mgmDom", new DomainManagementForm(req, o, true));
119 vars.put("addDom", new OrgDomainAddForm(req, o));
121 vars.put("affForm", new AffiliationForm(req, o));
122 vars.put("orgName", o.getName());
124 mainTempl.output(out, lang, vars);
127 private IterableDataset makeOrgDataset(final Organisation[] orgas) {
128 return new IterableDataset() {
133 public boolean next(Language l, Map<String, Object> vars) {
134 if (count >= orgas.length) {
137 Organisation org = orgas[count++];
138 vars.put("id", Integer.toString(org.getId()));
139 vars.put("name", org.getName());
140 vars.put("country", org.getState());