]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/wot/AssurePage.java
Make AssuranceForm a session-bound form
[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() - 2);
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() - 2);
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                         }
73                         form.submit(out, req);
74
75                         return;
76                 }
77
78                 System.out.println("searching for");
79                 try {
80                         PreparedStatement ps = DatabaseConnection.getInstance().prepare(
81                                         "SELECT id FROM users WHERE email=? AND dob=?");
82                         ps.setString(1, req.getParameter("email"));
83                         String day = req.getParameter("year") + "-"
84                                         + req.getParameter("month") + "-" + req.getParameter("day");
85                         ps.setString(2, day);
86                         ResultSet rs = ps.executeQuery();
87                         int id = 0;
88                         if (rs.next()) {
89                                 id = rs.getInt(1);
90                         }
91                         if (rs.next()) {
92                                 out.println("Error, ambigous user. Please contact support@cacert.org");
93                         } else {
94                                 resp.sendRedirect(PATH.substring(0, PATH.length() - 2) + "/"
95                                                 + id);
96                         }
97
98                         rs.close();
99                 } catch (SQLException e) {
100                         e.printStackTrace();
101                 }
102         }
103 }