]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/wot/AssuranceForm.java
056a28ab6963714cc5ce0ee5a2222ea10e31991f
[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                 res.put("dob", sdf.format(assuree.getDob()));
40                 templ.output(out, l, res);
41         }
42
43         @Override
44         public boolean submit(PrintWriter out, HttpServletRequest req) {
45                 out.println("<div class='formError'>");
46                 boolean failed = false;
47
48                 if (!"1".equals(req.getParameter("certify"))
49                                 || !"1".equals(req.getParameter("rules"))
50                                 || !"1".equals(req.getParameter("CCAAgreed"))
51                                 || !"1".equals(req.getParameter("assertion"))) {
52                         outputError(out, req, "You failed to check all boxes to validate"
53                                         + " your adherence to the rules and policies of CAcert");
54                         failed = true;
55
56                 }
57                 if (req.getParameter("date") == null
58                                 || req.getParameter("date").equals("")) {
59                         outputError(out, req,
60                                         "You must enter the date when you met the assuree.");
61                         failed = true;
62                 } else {
63                         try {
64                                 Date d = sdf.parse(req.getParameter("date"));
65                                 if (d.getTime() > System.currentTimeMillis()) {
66                                         outputError(out, req,
67                                                         "You must not enter a date in the future.");
68                                         failed = true;
69                                 }
70                         } catch (ParseException e) {
71                                 outputError(out, req,
72                                                 "You must enter the date in this format: YYYY-MM-DD.");
73                                 failed = true;
74                         }
75                 }
76                 // check location, min 3 characters
77                 if (req.getParameter("location") == null
78                                 || req.getParameter("location").equals("")) {
79                         outputError(out, req,
80                                         "You failed to enter a location of your meeting.");
81                         failed = true;
82                 } else if (req.getParameter("location").length() <= 2) {
83                         outputError(out, req,
84                                         "You must enter a location with at least 3 characters eg town and country.");
85                         failed = true;
86                 }
87                 // TODO checkPoints
88                 String points = req.getParameter("points");
89                 if (points == null || "".equals(points)) {
90                         // TODO message
91                         failed = true;
92                 }
93                 out.println("</div>");
94                 if (failed) {
95                         return false;
96                 }
97                 try {
98                         boolean success = Notary.assure(LoginPage.getUser(req), assuree,
99                                         Integer.parseInt(req.getParameter("points")),
100                                         req.getParameter("location"), req.getParameter("date"));
101                         return success;
102                 } catch (SQLException e) {
103                         e.printStackTrace();
104                 }
105
106                 return false;
107         }
108 }