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