1 package org.cacert.gigi.pages.account;
3 import java.io.PrintWriter;
7 import javax.servlet.http.HttpServletRequest;
9 import org.cacert.gigi.GigiApiException;
10 import org.cacert.gigi.dbObjects.Group;
11 import org.cacert.gigi.dbObjects.Name;
12 import org.cacert.gigi.dbObjects.User;
13 import org.cacert.gigi.localisation.Language;
14 import org.cacert.gigi.output.ArrayIterable;
15 import org.cacert.gigi.output.CountrySelector;
16 import org.cacert.gigi.output.DateSelector;
17 import org.cacert.gigi.output.GroupIterator;
18 import org.cacert.gigi.output.NameInput;
19 import org.cacert.gigi.output.template.Form;
20 import org.cacert.gigi.output.template.Template;
21 import org.cacert.gigi.pages.Page;
23 public class MyDetailsForm extends Form {
25 private static final Template assured = new Template(MyDetails.class.getResource("MyDetailsFormAssured.templ"));
27 private static final Template templ = new Template(MyDetailsForm.class.getResource("MyDetailsForm.templ"));
29 private static final Template names = new Template(MyDetailsForm.class.getResource("NamesForm.templ"));
31 private static final Template roles = new Template(MyDetailsForm.class.getResource("MyDetailsRoles.templ"));
35 private DateSelector ds;
39 private CountrySelector cs;
41 public MyDetailsForm(HttpServletRequest hsr, User target) {
46 this.ds = new DateSelector("day", "month", "year", target.getDoB());
48 if (target.getResidenceCountry() == null) {
49 this.cs = new CountrySelector("residenceCountry", true);
51 this.cs = new CountrySelector("residenceCountry", true, target.getResidenceCountry());
56 public boolean submit(PrintWriter out, HttpServletRequest req) {
58 String rn = req.getParameter("removeName");
60 Name n = Name.getById(Integer.parseInt(rn));
61 if (n.getOwner() != target) {
62 throw new GigiApiException("Cannot remove a name that does not belong to this account.");
64 if (n.equals(target.getPreferredName())) {
65 throw new GigiApiException("Cannot remove the account's preferred name.");
70 String dn = req.getParameter("deprecateName");
72 Name n = Name.getById(Integer.parseInt(dn));
73 if (n.getOwner() != target) {
74 throw new GigiApiException("Cannot deprecate a name that does not belong to this account.");
76 if (n.equals(target.getPreferredName())) {
77 throw new GigiApiException("Cannot deprecate the account's preferred name.");
82 String pn = req.getParameter("preferred");
84 Name n = Name.getById(Integer.parseInt(pn));
85 target.setPreferredName(n);
89 String action = req.getParameter("action");
90 if ("addName".equals(action)) {
92 ni.createName(target);
95 if ("updateDoB".equals(action)) {
97 target.setDoB(ds.getDate());
99 if ("updateResidenceCountry".equals(action)) {
101 target.setResidenceCountry(cs.getCountry());
103 } catch (GigiApiException e) {
104 e.format(out, Page.getLanguage(req));
106 } catch (NumberFormatException e) {
107 new GigiApiException("Invalid value.").format(out, Page.getLanguage(req));
114 protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
115 vars.put("exNames", new ArrayIterable<Name>(target.getNames()) {
117 Name preferred = target.getPreferredName();
120 public void apply(Name t, Language l, Map<String, Object> vars) {
121 if (t.equals(preferred)) {
122 vars.put("preferred", " disabled");
123 vars.put("deprecated", " disabled");
125 if (t.isDeprecated()) {
126 vars.put("deprecated", " disabled");
128 vars.put("deprecated", "");
130 vars.put("preferred", "");
133 vars.put("id", t.getId());
134 vars.put("npoints", Integer.toString(t.getAssurancePoints()));
138 vars.put("name", ni);
139 final Set<Group> gr = target.getGroups();
140 names.output(out, l, vars);
142 vars.put("support-groups", new GroupIterator(gr.iterator(), true));
143 vars.put("groups", new GroupIterator(gr.iterator(), false));
144 roles.output(out, l, vars);
146 vars.put("residenceCountry", cs);
147 if (target.getReceivedAssurances().length == 0) {
149 templ.output(out, l, vars);
151 vars.put("DoB", target.getDoB());
152 assured.output(out, l, vars);