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