package org.cacert.gigi.pages.account;
-import java.io.InputStreamReader;
import java.io.PrintWriter;
-import java.sql.Date;
-import java.sql.SQLException;
-import java.util.Calendar;
import java.util.Map;
-import java.util.TimeZone;
import javax.servlet.http.HttpServletRequest;
import org.cacert.gigi.GigiApiException;
-import org.cacert.gigi.User;
+import org.cacert.gigi.dbObjects.Name;
+import org.cacert.gigi.dbObjects.User;
import org.cacert.gigi.localisation.Language;
+import org.cacert.gigi.output.ArrayIterable;
+import org.cacert.gigi.output.CountrySelector;
import org.cacert.gigi.output.DateSelector;
-import org.cacert.gigi.output.Form;
+import org.cacert.gigi.output.NameInput;
+import org.cacert.gigi.output.template.Form;
import org.cacert.gigi.output.template.Template;
import org.cacert.gigi.pages.Page;
-import org.cacert.gigi.util.HTMLEncoder;
public class MyDetailsForm extends Form {
- private static Template templ;
- static {
- templ = new Template(new InputStreamReader(MyDetailsForm.class.getResourceAsStream("MyDetailsForm.templ")));
- }
+ private static final Template assured = new Template(MyDetails.class.getResource("MyDetailsFormAssured.templ"));
+
+ private static final Template templ = new Template(MyDetailsForm.class.getResource("MyDetailsForm.templ"));
+
+ private static final Template names = new Template(MyDetailsForm.class.getResource("NamesForm.templ"));
private User target;
+ private DateSelector ds;
+
+ private NameInput ni;
+
+ private CountrySelector cs;
+
public MyDetailsForm(HttpServletRequest hsr, User target) {
super(hsr);
this.target = target;
+ ni = new NameInput();
+
+ this.ds = new DateSelector("day", "month", "year", target.getDoB());
+
+ if (target.getResidenceCountry() == null) {
+ this.cs = new CountrySelector("residenceCountry", true);
+ } else {
+ this.cs = new CountrySelector("residenceCountry", true, target.getResidenceCountry());
+ }
}
@Override
public boolean submit(PrintWriter out, HttpServletRequest req) {
try {
- if (target.getAssurancePoints() == 0) {
- String newFname = req.getParameter("fname").trim();
- String newLname = req.getParameter("lname").trim();
- String newMname = req.getParameter("mname").trim();
- String newSuffix = req.getParameter("suffix").trim();
- if ((newFname.isEmpty() && !target.getFname().isEmpty()) || (newLname.isEmpty() && !target.getLname().isEmpty()) || (newMname.isEmpty() && !target.getMname().isEmpty()) || (newSuffix.isEmpty() && !target.getSuffix().isEmpty())) {
- throw new GigiApiException("Names couldn't be removed.");
+ String rn = req.getParameter("removeName");
+ if (rn != null) {
+ Name n = Name.getById(Integer.parseInt(rn));
+ if (n.getOwner() != target) {
+ throw new GigiApiException("Cannot remove a name that does not belong to this account.");
+ }
+ if (n.equals(target.getPreferredName())) {
+ throw new GigiApiException("Cannot remove the account's preferred name.");
}
- target.setFname(newFname);
- target.setLname(newLname);
- target.setMname(newMname);
- target.setSuffix(newSuffix);
- int newYear = Integer.parseInt(req.getParameter("year"));
- int newMonth = Integer.parseInt(req.getParameter("month"));
- int newDay = Integer.parseInt(req.getParameter("day"));
- Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
- cal.set(Calendar.YEAR, newYear);
- cal.set(Calendar.MONTH, newMonth);
- cal.set(Calendar.DAY_OF_MONTH, newDay);
- target.setDob(new Date(cal.getTimeInMillis()));
- target.updateUserData();
- } else {
- throw new GigiApiException("No change after assurance allowed.");
+ n.remove();
+ return true;
+ }
+ String dn = req.getParameter("deprecateName");
+ if (dn != null) {
+ Name n = Name.getById(Integer.parseInt(dn));
+ if (n.getOwner() != target) {
+ throw new GigiApiException("Cannot deprecate a name that does not belong to this account.");
+ }
+ if (n.equals(target.getPreferredName())) {
+ throw new GigiApiException("Cannot deprecate the account's preferred name.");
+ }
+ n.deprecate();
+ return true;
+ }
+ String pn = req.getParameter("preferred");
+ if (pn != null) {
+ Name n = Name.getById(Integer.parseInt(pn));
+ target.setPreferredName(n);
+ return true;
+ }
+
+ String action = req.getParameter("action");
+ if ("addName".equals(action)) {
+ ni.update(req);
+ ni.createName(target);
+ return true;
+ }
+ if ("updateDoB".equals(action)) {
+ ds.update(req);
+ target.setDoB(ds.getDate());
+ }
+ if ("updateResidenceCountry".equals(action)) {
+ cs.update(req);
+ target.setResidenceCountry(cs.getCountry());
}
- } catch (SQLException e) {
- new GigiApiException(e).format(out, Page.getLanguage(req));
- return false;
} catch (GigiApiException e) {
e.format(out, Page.getLanguage(req));
return false;
@Override
protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
- vars.put("fname", HTMLEncoder.encodeHTML(target.getFname()));
- vars.put("mname", target.getMname() == null ? "" : HTMLEncoder.encodeHTML(target.getMname()));
- vars.put("lname", HTMLEncoder.encodeHTML(target.getLname()));
- vars.put("suffix", target.getSuffix() == null ? "" : HTMLEncoder.encodeHTML(target.getSuffix()));
- DateSelector ds = new DateSelector("day", "month", "year", target.getDob());
- vars.put("DoB", ds);
- vars.put("details", "");
- templ.output(out, l, vars);
+ vars.put("exNames", new ArrayIterable<Name>(target.getNames()) {
+
+ Name preferred = target.getPreferredName();
+
+ @Override
+ public void apply(Name t, Language l, Map<String, Object> vars) {
+ if (t.equals(preferred)) {
+ vars.put("preferred", " disabled");
+ vars.put("deprecated", " disabled");
+ } else {
+ if (t.isDeprecated()) {
+ vars.put("deprecated", " disabled");
+ } else {
+ vars.put("deprecated", "");
+ }
+ vars.put("preferred", "");
+ }
+ vars.put("name", t);
+ vars.put("id", t.getId());
+ vars.put("npoints", Integer.toString(t.getAssurancePoints()));
+ }
+
+ });
+ vars.put("name", ni);
+ names.output(out, l, vars);
+ vars.put("residenceCountry", cs);
+ if (target.getReceivedAssurances().length == 0) {
+ vars.put("DoB", ds);
+ templ.output(out, l, vars);
+ } else {
+ vars.put("DoB", target.getDoB());
+ assured.output(out, l, vars);
+ }
}
}