]> WPIA git - gigi.git/blob - src/club/wpia/gigi/pages/wot/VerifyPage.java
upd: get default variables into outputables for error messages
[gigi.git] / src / club / wpia / gigi / pages / wot / VerifyPage.java
1 package club.wpia.gigi.pages.wot;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.Map;
6
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9
10 import club.wpia.gigi.GigiApiException;
11 import club.wpia.gigi.database.GigiPreparedStatement;
12 import club.wpia.gigi.database.GigiResultSet;
13 import club.wpia.gigi.dbObjects.User;
14 import club.wpia.gigi.output.DateSelector;
15 import club.wpia.gigi.output.template.Form;
16 import club.wpia.gigi.output.template.Template;
17 import club.wpia.gigi.pages.Page;
18 import club.wpia.gigi.util.AuthorizationContext;
19
20 public class VerifyPage extends Page {
21
22     public static final String PATH = "/wot/verify";
23
24     DateSelector ds = new DateSelector("day", "month", "year");
25
26     private static final Template t = new Template(VerificationForm.class.getResource("ApplicantSearch.templ"));
27
28     public VerifyPage() {
29         super("Verify someone");
30
31     }
32
33     @Override
34     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
35
36         PrintWriter out = resp.getWriter();
37         Map<String, Object> vars = getDefaultVars(req);
38         vars.put("DoB", ds);
39         t.output(out, getLanguage(req), vars);
40     }
41
42     @Override
43     public boolean isPermitted(AuthorizationContext ac) {
44         return ac != null && ac.canVerify();
45     }
46
47     @Override
48     public boolean beforePost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
49         if (req.getParameter("search") == null) {
50             VerificationForm form = Form.getForm(req, VerificationForm.class);
51             return form.submitExceptionProtected(req, resp);
52         }
53         return super.beforePost(req, resp);
54     }
55
56     @Override
57     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
58         PrintWriter out = resp.getWriter();
59         if (req.getParameter("search") == null) {
60             if (Form.printFormErrors(req, out)) {
61                 VerificationForm form = Form.getForm(req, VerificationForm.class);
62                 form.output(out, getLanguage(req), getDefaultVars(req));
63             }
64             return;
65         }
66
67         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `users`.`id`, `verified` FROM `users` INNER JOIN `certOwners` ON `certOwners`.`id`=`users`.`id` WHERE `email`=? AND `dob`=? AND `deleted` IS NULL")) {
68             ds.update(req);
69
70             ps.setString(1, req.getParameter("email"));
71             ps.setDate(2, ds.getDate().toSQLDate());
72             GigiResultSet rs = ps.executeQuery();
73             int id = 0;
74             if (rs.next()) {
75                 id = rs.getInt(1);
76                 boolean verified = rs.getBoolean(2);
77                 if (rs.next()) {
78                     out.println("Error, ambigous user. Please contact support.");
79                 } else {
80                     if ( !verified) {
81                         out.println(translate(req, "User is not yet verified. Please try again in 24 hours!"));
82                     } else {
83                         User applicant = User.getById(id);
84                         try {
85                             new VerificationForm(req, applicant).output(out, getLanguage(req), getDefaultVars(req));
86                         } catch (GigiApiException e) {
87                             e.format(out, Page.getLanguage(req), getDefaultVars(req));
88                         }
89                     }
90                 }
91             } else {
92                 throw new GigiApiException("I'm sorry, there was no email and date of birth matching" //
93                         + " what you entered in the system. Please double check your information.");
94             }
95
96         } catch (GigiApiException e) {
97             e.format(out, getLanguage(req), getDefaultVars(req));
98         }
99     }
100 }