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