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