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