1 package club.wpia.gigi.pages.admin;
3 import java.io.IOException;
4 import java.util.Arrays;
5 import java.util.HashMap;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
11 import club.wpia.gigi.dbObjects.Group;
12 import club.wpia.gigi.dbObjects.User;
13 import club.wpia.gigi.localisation.Language;
14 import club.wpia.gigi.output.template.Form;
15 import club.wpia.gigi.output.template.IterableDataset;
16 import club.wpia.gigi.output.template.SprintfCommand;
17 import club.wpia.gigi.pages.Page;
18 import club.wpia.gigi.pages.error.PageNotFound;
19 import club.wpia.gigi.util.AuthorizationContext;
21 public class TTPAdminPage extends Page {
23 public static final String PATH = "/admin/ttp";
25 public static final Group TTP_APPLICANT = Group.TTP_APPLICANT;
27 public TTPAdminPage() {
32 public boolean beforePost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
33 return Form.getForm(req, TTPAdminForm.class).submitExceptionProtected(req, resp);
37 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
38 if (Form.printFormErrors(req, resp.getWriter())) {
39 Form.getForm(req, TTPAdminForm.class).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
43 private static final int PAGE_LEN = 30;
46 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
47 String path = req.getPathInfo();
48 if (path != null && path.length() > PATH.length() + 1) {
49 int id = Integer.parseInt(path.substring(1 + PATH.length()));
50 User u = User.getById(id);
51 if (u == null || !u.isInGroup(TTP_APPLICANT)) {
52 SprintfCommand command = new SprintfCommand("The TTP-request is not available anymore. You might want to go {0}back{1}.", Arrays.asList("!'<a href=\"" + PATH + "\">", "!'</a>"));
53 req.setAttribute(PageNotFound.MESSAGE_ATTRIBUTE, command);
57 new TTPAdminForm(req, u).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
61 String offsetS = req.getParameter("offset");
62 if (offsetS != null) {
63 offset = Integer.parseInt(offsetS);
66 final User[] users = TTP_APPLICANT.getMembers(offset, PAGE_LEN + 1);
67 HashMap<String, Object> vars = new HashMap<>();
68 vars.put("users", new IterableDataset() {
73 public boolean next(Language l, Map<String, Object> vars) {
74 if (i >= Math.min(PAGE_LEN, users.length)) {
77 vars.put("id", Integer.toString(users[i].getId()));
78 vars.put("name", users[i].getPreferredName().toString());
79 vars.put("email", users[i].getEmail());
85 if (users.length == PAGE_LEN + 1) {
86 vars.put("next", Integer.toString(offset + 30));
88 getDefaultTemplate().output(resp.getWriter(), getLanguage(req), vars);
92 public boolean isPermitted(AuthorizationContext ac) {
93 return ac != null && ac.isInGroup(Group.TTP_AGENT);