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.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;
23 public class ViewOrgPage extends Page {
25 private static final Template orgas = new Template(ViewOrgPage.class.getResource("ViewOrgs.templ"));
27 private static final Template mainTempl = new Template(ViewOrgPage.class.getResource("EditOrg.templ"));
29 public static final String DEFAULT_PATH = "/orga";
31 public ViewOrgPage() {
32 super("View Organisation");
36 public boolean isPermitted(AuthorizationContext ac) {
37 return ac != null && (ac.isInGroup(CreateOrgPage.ORG_ASSURER) || ac.getActor().getOrganisations(true).size() != 0);
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());
50 if ( !u.isInGroup(CreateOrgPage.ORG_ASSURER)) {
51 resp.sendError(403, "Access denied");
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());
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());
66 CreateOrgForm form = Form.getForm(req, CreateOrgForm.class);
67 if (form.submitProtected(resp.getWriter(), req)) {
68 resp.sendRedirect(DEFAULT_PATH + "/" + form.getResult().getId());
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);
87 map.put("orgas", makeOrgDataset(orgList));
89 map.put("orgas", makeOrgDataset(myOrgs.toArray(new Organisation[myOrgs.size()])));
91 orgas.output(out, lang, map);
94 idS = idS.substring(DEFAULT_PATH.length() + 1);
95 int id = Integer.parseInt(idS);
98 o = Organisation.getById(id);
99 } catch (IllegalArgumentException e) {
103 final List<Organisation> myOrgs = u.getOrganisations();
104 final boolean orgAss = u.isInGroup(CreateOrgPage.ORG_ASSURER);
105 if ( !orgAss && !myOrgs.contains(o)) {
109 HashMap<String, Object> vars = new HashMap<>();
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));
116 vars.put("affForm", new AffiliationForm(req, o));
117 vars.put("orgName", o.getName());
119 mainTempl.output(out, lang, vars);
122 private IterableDataset makeOrgDataset(final Organisation[] orgas) {
123 return new IterableDataset() {
128 public boolean next(Language l, Map<String, Object> vars) {
129 if (count >= orgas.length) {
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().getCode());