fix: number-format-exception thrown on assurance entry
[gigi.git] / src / org / cacert / gigi / pages / wot / AssurePage.java
1 package org.cacert.gigi.pages.wot;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.HashMap;
6
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9
10 import org.cacert.gigi.GigiApiException;
11 import org.cacert.gigi.database.GigiPreparedStatement;
12 import org.cacert.gigi.database.GigiResultSet;
13 import org.cacert.gigi.dbObjects.User;
14 import org.cacert.gigi.output.DateSelector;
15 import org.cacert.gigi.output.template.Form;
16 import org.cacert.gigi.output.template.Template;
17 import org.cacert.gigi.pages.LoginPage;
18 import org.cacert.gigi.pages.Page;
19 import org.cacert.gigi.util.AuthorizationContext;
20 import org.cacert.gigi.util.Notary;
21
22 public class AssurePage extends Page {
23
24     public static final String PATH = "/wot/assure";
25
26     DateSelector ds = new DateSelector("day", "month", "year");
27
28     Template t;
29
30     public AssurePage() {
31         super("Assure someone");
32         t = new Template(AssuranceForm.class.getResource("AssureeSearch.templ"));
33
34     }
35
36     @Override
37     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
38
39         PrintWriter out = resp.getWriter();
40         HashMap<String, Object> vars = new HashMap<String, Object>();
41         vars.put("DoB", ds);
42         t.output(out, getLanguage(req), vars);
43     }
44
45     @Override
46     public boolean isPermitted(AuthorizationContext ac) {
47         return ac != null && ac.canAssure();
48     }
49
50     @Override
51     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
52         PrintWriter out = resp.getWriter();
53         if (req.getParameter("search") == null) {
54             AssuranceForm form = Form.getForm(req, AssuranceForm.class);
55             try {
56                 if (form.submit(out, req)) {
57                     out.println(translate(req, "Assurance complete."));
58                     return;
59                 }
60             } catch (GigiApiException e) {
61                 e.format(out, Page.getLanguage(req));
62                 try {
63                     Notary.checkAssuranceIsPossible(LoginPage.getUser(req), form.getAssuree());
64                     form.output(out, getLanguage(req), new HashMap<String, Object>());
65                 } catch (GigiApiException e1) {
66                     e1.format(out, Page.getLanguage(req));
67                 }
68             }
69
70             return;
71         }
72
73         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")) {
74             ds.update(req);
75
76             ps.setString(1, req.getParameter("email"));
77             ps.setDate(2, ds.getDate().toSQLDate());
78             GigiResultSet rs = ps.executeQuery();
79             int id = 0;
80             if (rs.next()) {
81                 id = rs.getInt(1);
82                 boolean verified = rs.getBoolean(2);
83                 if (rs.next()) {
84                     out.println("Error, ambigous user. Please contact support@cacert.org.");
85                 } else {
86                     if ( !verified) {
87                         out.println(translate(req, "User is not yet verified. Please try again in 24 hours!"));
88                     } else if (getUser(req).getId() == id) {
89
90                     } else {
91                         User assuree = User.getById(id);
92                         User myself = LoginPage.getUser(req);
93                         try {
94                             Notary.checkAssuranceIsPossible(myself, assuree);
95                             new AssuranceForm(req, assuree).output(out, getLanguage(req), new HashMap<String, Object>());
96                         } catch (GigiApiException e) {
97                             e.format(out, Page.getLanguage(req));
98                         }
99                     }
100                 }
101             } else {
102                 throw new GigiApiException("I'm sorry, there was no email and date of birth matching" //
103                         + " what you entered in the system. Please double check your information.");
104             }
105
106         } catch (GigiApiException e) {
107             e.format(out, getLanguage(req));
108         }
109     }
110 }