]> WPIA git - gigi.git/blob - src/club/wpia/gigi/pages/admin/support/FindUserByEmailPage.java
upd: rename package name and all references to it
[gigi.git] / src / club / wpia / gigi / pages / admin / support / FindUserByEmailPage.java
1 package club.wpia.gigi.pages.admin.support;
2
3 import java.io.IOException;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9
10 import club.wpia.gigi.dbObjects.EmailAddress;
11 import club.wpia.gigi.localisation.Language;
12 import club.wpia.gigi.output.template.Form;
13 import club.wpia.gigi.output.template.IterableDataset;
14 import club.wpia.gigi.output.template.Template;
15 import club.wpia.gigi.pages.Page;
16 import club.wpia.gigi.util.AuthorizationContext;
17
18 public class FindUserByEmailPage extends Page {
19
20     public static final String PATH = "/support/find/email";
21
22     private static final Template USERTABLE = new Template(FindUserByDomainPage.class.getResource("FindUserByEmailUsertable.templ"));
23
24     public FindUserByEmailPage() {
25         super("Find Email");
26     }
27
28     @Override
29     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
30         new FindUserByEmailForm(req).output(resp.getWriter(), Page.getLanguage(req), new HashMap<String, Object>());
31     }
32
33     @Override
34     public boolean beforePost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
35         return Form.getForm(req, FindUserByEmailForm.class).submitExceptionProtected(req, resp);
36     }
37
38     @Override
39     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
40         if (Form.printFormErrors(req, resp.getWriter())) {
41             Form.getForm(req, FindUserByEmailForm.class).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
42         } else {
43             final EmailAddress[] emails = ((FindUserByEmailForm.FindEmailResult) req.getAttribute(Form.SUBMIT_RESULT)).getEmails();
44             if (emails.length == 1) {
45                 resp.sendRedirect(SupportUserDetailsPage.PATH + emails[0].getOwner().getId() + "/");
46             } else {
47                 HashMap<String, Object> vars = new HashMap<String, Object>();
48                 vars.put("usertable", new IterableDataset() {
49
50                     int i = 0;
51
52                     @Override
53                     public boolean next(Language l, Map<String, Object> vars) {
54                         if (i == emails.length) {
55                             return false;
56                         }
57                         vars.put("usrid", emails[i].getOwner().getId());
58                         vars.put("usermail", emails[i].getAddress());
59                         i++;
60                         return true;
61                     }
62                 });
63                 USERTABLE.output(resp.getWriter(), getLanguage(req), vars);
64             }
65         }
66     }
67
68     @Override
69     public boolean isPermitted(AuthorizationContext ac) {
70         return ac != null && ac.canSupport();
71     }
72
73 }