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