]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/wot/AssuranceForm.java
fix: assurance form, keep location and date + testCase
[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.template.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 String location = "";
29
30     private String date = "";
31
32     private static final Template templ;
33     static {
34         templ = new Template(AssuranceForm.class.getResource("AssuranceForm.templ"));
35     }
36
37     public AssuranceForm(HttpServletRequest hsr, User assuree) {
38         super(hsr);
39         this.assuree = assuree;
40         assureeName = this.assuree.getName();
41         dob = this.assuree.getDoB();
42     }
43
44     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
45
46     SimpleDateFormat sdf2 = new SimpleDateFormat("dd. MMM yyyy");
47
48     @Override
49     public void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
50         HashMap<String, Object> res = new HashMap<String, Object>();
51         res.putAll(vars);
52         res.put("nameExplicit", assuree.getName());
53         res.put("name", assuree.getName().toString());
54         res.put("maxpoints", assuree.getMaxAssurePoints());
55         res.put("dob", sdf.format(assuree.getDoB()));
56         res.put("dobFmt2", sdf2.format(assuree.getDoB()));
57         res.put("location", location);
58         res.put("date", date);
59         templ.output(out, l, res);
60     }
61
62     @Override
63     public boolean submit(PrintWriter out, HttpServletRequest req) {
64         location = req.getParameter("location");
65         date = req.getParameter("date");
66         if (date == null || location == null) {
67             outputError(out, req, "You need to enter location and date!");
68         }
69
70         if ( !"1".equals(req.getParameter("certify")) || !"1".equals(req.getParameter("rules")) || !"1".equals(req.getParameter("CCAAgreed")) || !"1".equals(req.getParameter("assertion"))) {
71             outputError(out, req, "You failed to check all boxes to validate" + " your adherence to the rules and policies of CAcert");
72
73         }
74         int pointsI = 0;
75         String points = req.getParameter("points");
76         if (points == null || "".equals(points)) {
77             outputError(out, req, "For an assurance, you need to enter points.");
78         } else {
79             try {
80                 pointsI = Integer.parseInt(points);
81             } catch (NumberFormatException e) {
82                 outputError(out, req, "The points entered were not a number.");
83             }
84         }
85
86         if (isFailed(out)) {
87             return false;
88         }
89         try {
90             Notary.assure(Page.getUser(req), assuree, assureeName, dob, pointsI, location, req.getParameter("date"));
91             return true;
92         } catch (GigiApiException e) {
93             e.format(out, Page.getLanguage(req));
94         }
95
96         return false;
97     }
98
99     public User getAssuree() {
100         return assuree;
101     }
102
103 }