]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/wot/AssuranceForm.java
upd: enforce pattern of making templates static and final.
[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.HashMap;
6 import java.util.Iterator;
7 import java.util.LinkedList;
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.Assurance.AssuranceType;
14 import org.cacert.gigi.dbObjects.Name;
15 import org.cacert.gigi.dbObjects.User;
16 import org.cacert.gigi.localisation.Language;
17 import org.cacert.gigi.output.template.Form;
18 import org.cacert.gigi.output.template.IterableDataset;
19 import org.cacert.gigi.output.template.Template;
20 import org.cacert.gigi.pages.Page;
21 import org.cacert.gigi.pages.PasswordResetPage;
22 import org.cacert.gigi.util.DayDate;
23 import org.cacert.gigi.util.Notary;
24
25 public class AssuranceForm extends Form {
26
27     private User assuree;
28
29     private Name assureeName;
30
31     private DayDate dob;
32
33     private String location = "";
34
35     private String date = "";
36
37     private String aword;
38
39     private User assurer;
40
41     private AssuranceType type = AssuranceType.FACE_TO_FACE;
42
43     private static final Template templ = new Template(AssuranceForm.class.getResource("AssuranceForm.templ"));
44
45     public AssuranceForm(HttpServletRequest hsr, User assuree) {
46         super(hsr);
47         assurer = Page.getUser(hsr);
48         this.assuree = assuree;
49         assureeName = this.assuree.getName();
50         dob = this.assuree.getDoB();
51     }
52
53     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
54
55     SimpleDateFormat sdf2 = new SimpleDateFormat("dd. MMM yyyy");
56
57     @Override
58     public void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
59         HashMap<String, Object> res = new HashMap<String, Object>();
60         res.putAll(vars);
61         res.put("nameExplicit", assuree.getName());
62         res.put("name", assuree.getName().toString());
63         res.put("maxpoints", assurer.getMaxAssurePoints());
64         res.put("dob", sdf.format(assuree.getDoB().toDate()));
65         res.put("dobFmt2", sdf2.format(assuree.getDoB().toDate()));
66         res.put("location", location);
67         res.put("date", date);
68         res.put("aword", aword);
69         final LinkedList<AssuranceType> ats = new LinkedList<>();
70         for (AssuranceType at : AssuranceType.values()) {
71             try {
72                 Notary.may(assurer, assuree, at);
73                 ats.add(at);
74             } catch (GigiApiException e) {
75             }
76         }
77         res.put("ats", new IterableDataset() {
78
79             Iterator<AssuranceType> t = ats.iterator();
80
81             @Override
82             public boolean next(Language l, Map<String, Object> vars) {
83                 if ( !t.hasNext()) {
84                     return false;
85                 }
86                 AssuranceType t1 = t.next();
87                 vars.put("type", t1.getDescription());
88                 vars.put("id", t1.toString());
89                 vars.put("sel", t1 == type ? " selected" : "");
90                 return true;
91             }
92         });
93         templ.output(out, l, res);
94     }
95
96     @Override
97     public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
98         location = req.getParameter("location");
99         date = req.getParameter("date");
100         GigiApiException gae = new GigiApiException();
101         if (date == null || location == null) {
102             gae.mergeInto(new GigiApiException("You need to enter location and date!"));
103         }
104
105         if ( !"1".equals(req.getParameter("certify")) || !"1".equals(req.getParameter("rules")) || !"1".equals(req.getParameter("assertion"))) {
106             gae.mergeInto(new GigiApiException("You failed to check all boxes to validate" + " your adherence to the rules and policies of SomeCA"));
107         }
108         if ("1".equals(req.getParameter("passwordReset"))) {
109             aword = req.getParameter("passwordResetValue");
110             if ("".equals(aword)) {
111                 aword = null;
112             }
113         } else {
114             aword = null;
115         }
116         String val = req.getParameter("assuranceType");
117         if (val != null) {
118             try {
119                 type = AssuranceType.valueOf(val);
120             } catch (IllegalArgumentException e) {
121                 gae.mergeInto(new GigiApiException("Assurance Type wrong."));
122             }
123         }
124
125         int pointsI = 0;
126         String points = req.getParameter("points");
127         if (points == null || "".equals(points)) {
128             gae.mergeInto(new GigiApiException("For an assurance, you need to enter points."));
129         } else {
130             try {
131                 pointsI = Integer.parseInt(points);
132             } catch (NumberFormatException e) {
133                 gae.mergeInto(new GigiApiException("The points entered were not a number."));
134             }
135         }
136
137         if ( !gae.isEmpty()) {
138             throw gae;
139         }
140         Notary.assure(assurer, assuree, assureeName, dob, pointsI, location, req.getParameter("date"), type);
141         if (aword != null && !aword.equals("")) {
142             Language l = Language.getInstance(assuree.getPreferredLocale());
143             String method = l.getTranslation("A password reset was triggered. If you did a password reset by assurance, please enter your secret password using this form:");
144             String subject = l.getTranslation("Password reset by assurance");
145             PasswordResetPage.initPasswordResetProcess(out, assuree, req, aword, l, method, subject);
146         }
147         return true;
148     }
149
150     public User getAssuree() {
151         return assuree;
152     }
153
154 }