]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/wot/AssuranceForm.java
ADD: A step towards a more friendly SQL API.
[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.text.SimpleDateFormat;
5 import java.util.Date;
6 import java.util.HashMap;
7 import java.util.Map;
8
9 import javax.servlet.http.HttpServletRequest;
10
11 import org.cacert.gigi.GigiApiException;
12 import org.cacert.gigi.dbObjects.Name;
13 import org.cacert.gigi.dbObjects.User;
14 import org.cacert.gigi.localisation.Language;
15 import org.cacert.gigi.output.Form;
16 import org.cacert.gigi.output.template.Template;
17 import org.cacert.gigi.pages.Page;
18 import org.cacert.gigi.util.Notary;
19
20 public class AssuranceForm extends Form {
21
22     private User assuree;
23
24     private Name assureeName;
25
26     private Date dob;
27
28     private static final Template templ;
29     static {
30         templ = new Template(AssuranceForm.class.getResource("AssuranceForm.templ"));
31     }
32
33     public AssuranceForm(HttpServletRequest hsr, User assuree) {
34         super(hsr);
35         this.assuree = assuree;
36         assureeName = this.assuree.getName();
37         dob = this.assuree.getDob();
38     }
39
40     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
41
42     SimpleDateFormat sdf2 = new SimpleDateFormat("dd. MMM yyyy");
43
44     @Override
45     public void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
46         HashMap<String, Object> res = new HashMap<String, Object>();
47         res.putAll(vars);
48         res.put("nameExplicit", assuree.getName());
49         res.put("name", assuree.getName().toString());
50         res.put("maxpoints", assuree.getMaxAssurePoints());
51         res.put("dob", sdf.format(assuree.getDob()));
52         res.put("dobFmt2", sdf2.format(assuree.getDob()));
53         templ.output(out, l, res);
54     }
55
56     @Override
57     public boolean submit(PrintWriter out, HttpServletRequest req) {
58         if ( !"1".equals(req.getParameter("certify")) || !"1".equals(req.getParameter("rules")) || !"1".equals(req.getParameter("CCAAgreed")) || !"1".equals(req.getParameter("assertion"))) {
59             outputError(out, req, "You failed to check all boxes to validate" + " your adherence to the rules and policies of CAcert");
60
61         }
62         int pointsI = 0;
63         String points = req.getParameter("points");
64         if (points == null || "".equals(points)) {
65             outputError(out, req, "For an assurance, you need to enter points.");
66         } else {
67             try {
68                 pointsI = Integer.parseInt(points);
69             } catch (NumberFormatException e) {
70                 outputError(out, req, "The points entered were not a number.");
71             }
72         }
73
74         if (isFailed(out)) {
75             return false;
76         }
77         try {
78             Notary.assure(Page.getUser(req), assuree, assureeName, dob, pointsI, req.getParameter("location"), req.getParameter("date"));
79             return true;
80         } catch (GigiApiException e) {
81             e.format(out, Page.getLanguage(req));
82         }
83
84         return false;
85     }
86
87     public User getAssuree() {
88         return assuree;
89     }
90
91 }