]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/wot/AssurePage.java
5fae0f8cd042374e86ff5aabe3a9fd86fc0c90c3
[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
14 import org.cacert.gigi.User;
15 import org.cacert.gigi.database.DatabaseConnection;
16 import org.cacert.gigi.output.DateSelector;
17 import org.cacert.gigi.output.Template;
18 import org.cacert.gigi.pages.LoginPage;
19 import org.cacert.gigi.pages.Page;
20
21 public class AssurePage extends Page {
22         public static final String PATH = "/wot/assure/*";
23         DateSelector ds = new DateSelector("day", "month", "year");
24         Template t;
25
26         public AssurePage() {
27                 super("Assure someone");
28                 t = new Template(new InputStreamReader(
29                                 AssurePage.class.getResourceAsStream("AssureeSearch.templ")));
30         }
31
32         @Override
33         public void doGet(HttpServletRequest req, HttpServletResponse resp)
34                         throws IOException {
35                 PrintWriter out = resp.getWriter();
36
37                 String pi = req.getPathInfo().substring(PATH.length() - 2);
38                 if (pi.length() > 1) {
39                         out.println("I am a Placeholder for the Assurance form # ");
40                         out.println(pi.substring(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                         new AssuranceForm(mid).output(out, getLanguage(req),
49                                         new HashMap<String, Object>());;
50                 } else {
51                         HashMap<String, Object> vars = new HashMap<String, Object>();
52                         vars.put("DoB", ds);
53                         t.output(out, getLanguage(req), vars);
54                 }
55         }
56         @Override
57         public void doPost(HttpServletRequest req, HttpServletResponse resp)
58                         throws IOException {
59                 PrintWriter out = resp.getWriter();
60                 System.out.println("searching for");
61                 try {
62                         PreparedStatement ps = DatabaseConnection.getInstance().prepare(
63                                         "SELECT id FROM users WHERE email=? AND dob=?");
64                         ps.setString(1, req.getParameter("email"));
65                         String day = req.getParameter("year") + "-"
66                                         + req.getParameter("month") + "-" + req.getParameter("day");
67                         ps.setString(2, day);
68                         ResultSet rs = ps.executeQuery();
69                         int id = 0;
70                         if (rs.next()) {
71                                 id = rs.getInt(1);
72                         }
73                         if (rs.next()) {
74                                 out.println("Error, ambigous user. Please contact support@cacert.org");
75                         } else {
76                                 resp.sendRedirect(PATH.substring(0, PATH.length() - 2) + "/"
77                                                 + id);
78                         }
79
80                         rs.close();
81                 } catch (SQLException e) {
82                         e.printStackTrace();
83                 }
84         }
85 }