]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/wot/AssurePage.java
The assurance form outputs errors, but does nothing.
[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.InputStreamReader;
5 import java.io.PrintWriter;
6 import java.sql.PreparedStatement;
7 import java.sql.ResultSet;
8 import java.sql.SQLException;
9 import java.util.HashMap;
10
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13 import javax.servlet.http.HttpSession;
14
15 import org.cacert.gigi.User;
16 import org.cacert.gigi.database.DatabaseConnection;
17 import org.cacert.gigi.output.DateSelector;
18 import org.cacert.gigi.output.Template;
19 import org.cacert.gigi.pages.LoginPage;
20 import org.cacert.gigi.pages.Page;
21
22 public class AssurePage extends Page {
23         public static final String PATH = "/wot/assure";
24         public static final String SESSION = "/wot/assure/FORM";
25         DateSelector ds = new DateSelector("day", "month", "year");
26         Template t;
27
28         public AssurePage() {
29                 super("Assure someone");
30                 t = new Template(new InputStreamReader(
31                                 AssurePage.class.getResourceAsStream("AssureeSearch.templ")));
32         }
33
34         @Override
35         public void doGet(HttpServletRequest req, HttpServletResponse resp)
36                         throws IOException {
37
38                 PrintWriter out = resp.getWriter();
39                 String pi = req.getPathInfo().substring(PATH.length());
40                 if (pi.length() > 1) {
41                         User myself = LoginPage.getUser(req);
42                         int mid = Integer.parseInt(pi.substring(1));
43                         if (mid == myself.getId()) {
44                                 out.println("Cannot assure myself.");
45                                 return;
46                         }
47
48                         HttpSession hs = req.getSession();
49                         AssuranceForm form = (AssuranceForm) hs.getAttribute(SESSION);
50                         if (form == null || form.assuree.getId() != mid) {
51                                 form = new AssuranceForm(mid);
52                                 hs.setAttribute(SESSION, form);
53                         }
54
55                         form.output(out, getLanguage(req), new HashMap<String, Object>());;
56                 } else {
57                         HashMap<String, Object> vars = new HashMap<String, Object>();
58                         vars.put("DoB", ds);
59                         t.output(out, getLanguage(req), vars);
60                 }
61         }
62         @Override
63         public void doPost(HttpServletRequest req, HttpServletResponse resp)
64                         throws IOException {
65                 PrintWriter out = resp.getWriter();
66                 String pi = req.getPathInfo().substring(PATH.length());
67                 if (pi.length() > 1) {
68                         AssuranceForm form = (AssuranceForm) req.getSession().getAttribute(
69                                         SESSION);
70                         if (form == null) {
71                                 out.println("No form found. This is an Error. Fill in the form again.");
72                                 return;
73                         }
74                         form.submit(out, req);
75
76                         return;
77                 }
78
79                 System.out.println("searching for");
80                 try {
81                         PreparedStatement ps = DatabaseConnection.getInstance().prepare(
82                                         "SELECT id FROM users WHERE email=? AND dob=?");
83                         ps.setString(1, req.getParameter("email"));
84                         String day = req.getParameter("year") + "-"
85                                         + req.getParameter("month") + "-" + req.getParameter("day");
86                         ps.setString(2, day);
87                         ResultSet rs = ps.executeQuery();
88                         int id = 0;
89                         if (rs.next()) {
90                                 id = rs.getInt(1);
91                                 if (rs.next()) {
92                                         out.println("Error, ambigous user. Please contact support@cacert.org.");
93                                 } else {
94                                         resp.sendRedirect(PATH + "/" + id);
95                                 }
96                         } else {
97                                 out.print("<div class='formError'>");
98                                 out.println(translate(
99                                                 req,
100                                                 "I'm sorry, there was no email and date of birth matching"
101                                                                 + " what you entered in the system. Please double check"
102                                                                 + " your information."));
103                                 out.print("</div>");
104                         }
105
106                         rs.close();
107                 } catch (SQLException e) {
108                         e.printStackTrace();
109                 }
110         }
111 }