]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/wot/AssuranceForm.java
Merge branch 'libs/jetty/upstream' into libs/jetty/local
[gigi.git] / src / org / cacert / gigi / pages / wot / AssuranceForm.java
1 package org.cacert.gigi.pages.wot;
2
3 import java.io.InputStreamReader;
4 import java.io.PrintWriter;
5 import java.sql.SQLException;
6 import java.text.ParseException;
7 import java.text.SimpleDateFormat;
8 import java.util.Date;
9 import java.util.HashMap;
10 import java.util.Map;
11
12 import javax.servlet.http.HttpServletRequest;
13
14 import org.cacert.gigi.Language;
15 import org.cacert.gigi.User;
16 import org.cacert.gigi.output.Form;
17 import org.cacert.gigi.output.Template;
18 import org.cacert.gigi.pages.LoginPage;
19 import org.cacert.gigi.util.Notary;
20
21 public class AssuranceForm extends Form {
22         User assuree;
23         static final Template templ;
24         static {
25                 templ = new Template(new InputStreamReader(
26                                 AssuranceForm.class.getResourceAsStream("AssuranceForm.templ")));
27         }
28
29         public AssuranceForm(int assuree) {
30                 this.assuree = new User(assuree);
31         }
32         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
33
34         @Override
35         public void output(PrintWriter out, Language l, Map<String, Object> vars) {
36                 HashMap<String, Object> res = new HashMap<String, Object>();
37                 res.putAll(vars);
38                 res.put("name", assuree.getName());
39                 try {
40                         res.put("maxpoints", assuree.getMaxAssurePoints());
41                 } catch (SQLException e) {
42                         e.printStackTrace();
43                 }
44                 res.put("dob", sdf.format(assuree.getDob()));
45                 templ.output(out, l, res);
46         }
47
48         @Override
49         public boolean submit(PrintWriter out, HttpServletRequest req) {
50                 out.println("<div class='formError'>");
51                 boolean failed = false;
52
53                 if (!"1".equals(req.getParameter("certify"))
54                                 || !"1".equals(req.getParameter("rules"))
55                                 || !"1".equals(req.getParameter("CCAAgreed"))
56                                 || !"1".equals(req.getParameter("assertion"))) {
57                         outputError(out, req, "You failed to check all boxes to validate"
58                                         + " your adherence to the rules and policies of CAcert");
59                         failed = true;
60
61                 }
62                 if (req.getParameter("date") == null
63                                 || req.getParameter("date").equals("")) {
64                         outputError(out, req,
65                                         "You must enter the date when you met the assuree.");
66                         failed = true;
67                 } else {
68                         try {
69                                 Date d = sdf.parse(req.getParameter("date"));
70                                 if (d.getTime() > System.currentTimeMillis()) {
71                                         outputError(out, req,
72                                                         "You must not enter a date in the future.");
73                                         failed = true;
74                                 }
75                         } catch (ParseException e) {
76                                 outputError(out, req,
77                                                 "You must enter the date in this format: YYYY-MM-DD.");
78                                 failed = true;
79                         }
80                 }
81                 // check location, min 3 characters
82                 if (req.getParameter("location") == null
83                                 || req.getParameter("location").equals("")) {
84                         outputError(out, req,
85                                         "You failed to enter a location of your meeting.");
86                         failed = true;
87                 } else if (req.getParameter("location").length() <= 2) {
88                         outputError(out, req,
89                                         "You must enter a location with at least 3 characters eg town and country.");
90                         failed = true;
91                 }
92                 // TODO checkPoints
93                 String points = req.getParameter("points");
94                 if (points == null || "".equals(points)) {
95                         // TODO message
96                         failed = true;
97                 }
98                 if (failed) {
99                         out.println("</div>");
100                         return false;
101                 }
102                 try {
103                         boolean success = Notary.assure(LoginPage.getUser(req), assuree,
104                                         Integer.parseInt(req.getParameter("points")),
105                                         req.getParameter("location"), req.getParameter("date"));
106                         if (!success) {
107                                 outputError(out, req,
108                                                 "Assurance failed. Maybe user data changed.");
109                         }
110                         out.println("</div>");
111                         return success;
112                 } catch (SQLException e) {
113                         e.printStackTrace();
114                 }
115
116                 out.println("</div>");
117                 return false;
118         }
119 }