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.GroupSelector;
19 import org.cacert.gigi.output.NameInput;
20 import org.cacert.gigi.output.template.Form;
21 import org.cacert.gigi.output.template.Template;
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 private GroupSelector selectedGroup = new GroupSelector("groupToModify", false);
43 public MyDetailsForm(HttpServletRequest hsr, User target) {
48 this.ds = new DateSelector("day", "month", "year", target.getDoB());
50 if (target.getResidenceCountry() == null) {
51 this.cs = new CountrySelector("residenceCountry", true);
53 this.cs = new CountrySelector("residenceCountry", true, target.getResidenceCountry());
58 public boolean submit(HttpServletRequest req) throws GigiApiException {
60 String rn = req.getParameter("removeName");
62 Name n = Name.getById(Integer.parseInt(rn));
63 if (n.getOwner() != target) {
64 throw new GigiApiException("Cannot remove a name that does not belong to this account.");
66 if (n.equals(target.getPreferredName())) {
67 throw new GigiApiException("Cannot remove the account's preferred name.");
72 String dn = req.getParameter("deprecateName");
74 Name n = Name.getById(Integer.parseInt(dn));
75 if (n.getOwner() != target) {
76 throw new GigiApiException("Cannot deprecate a name that does not belong to this account.");
78 if (n.equals(target.getPreferredName())) {
79 throw new GigiApiException("Cannot deprecate the account's preferred name.");
84 String pn = req.getParameter("preferred");
86 Name n = Name.getById(Integer.parseInt(pn));
87 target.setPreferredName(n);
91 String action = req.getParameter("action");
92 if ("addName".equals(action)) {
94 ni.createName(target);
97 if ("updateDoB".equals(action)) {
99 target.setDoB(ds.getDate());
101 if ("updateResidenceCountry".equals(action)) {
103 target.setResidenceCountry(cs.getCountry());
106 if ("addGroup".equals(action) || "removeGroup".equals(action)) {
107 selectedGroup.update(req);
108 Group toMod = selectedGroup.getGroup();
109 if ("addGroup".equals(action)) {
110 target.grantGroup(target, toMod);
112 target.revokeGroup(target, toMod);
117 } catch (NumberFormatException e) {
118 throw new GigiApiException("Invalid value.");
124 protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
125 vars.put("exNames", new ArrayIterable<Name>(target.getNames()) {
127 Name preferred = target.getPreferredName();
130 public void apply(Name t, Language l, Map<String, Object> vars) {
131 if (t.equals(preferred)) {
132 vars.put("preferred", " disabled");
133 vars.put("deprecated", " disabled");
135 if (t.isDeprecated()) {
136 vars.put("deprecated", " disabled");
138 vars.put("deprecated", "");
140 vars.put("preferred", "");
143 vars.put("id", t.getId());
144 vars.put("npoints", Integer.toString(t.getAssurancePoints()));
148 vars.put("name", ni);
149 names.output(out, l, vars);
151 vars.put("residenceCountry", cs);
152 if (target.getReceivedAssurances().length == 0) {
154 templ.output(out, l, vars);
156 vars.put("DoB", target.getDoB());
157 assured.output(out, l, vars);
160 final Set<Group> gr = target.getGroups();
161 vars.put("support-groups", new GroupIterator(gr.iterator(), true));
162 vars.put("groups", new GroupIterator(gr.iterator(), false));
163 vars.put("groupSelector", selectedGroup);
164 roles.output(out, l, vars);