]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/pages/account/MyDetailsForm.java
add: Allow multiple names, name-schemes, multi-name-assurance, etc.
[gigi.git] / src / org / cacert / gigi / pages / account / MyDetailsForm.java
index d35ba45e84bea8ec27872807196abf4de2be6632..2d1f7ff3c9b80ece5271f3729b8c26503d19c18a 100644 (file)
@@ -9,11 +9,12 @@ import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.dbObjects.Name;
 import org.cacert.gigi.dbObjects.User;
 import org.cacert.gigi.localisation.Language;
 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.DateSelector;
 import org.cacert.gigi.output.DateSelector;
+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.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 {
 
 
 public class MyDetailsForm extends Form {
 
@@ -21,36 +22,65 @@ public class MyDetailsForm extends Form {
 
     private static final Template templ = new Template(MyDetailsForm.class.getResource("MyDetailsForm.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 User target;
 
     private DateSelector ds;
 
+    private NameInput ni;
+
     public MyDetailsForm(HttpServletRequest hsr, User target) {
         super(hsr);
         this.target = target;
     public MyDetailsForm(HttpServletRequest hsr, User target) {
         super(hsr);
         this.target = target;
+        ni = new NameInput();
+
         this.ds = new DateSelector("day", "month", "year", target.getDoB());
     }
 
     @Override
     public boolean submit(PrintWriter out, HttpServletRequest req) {
         try {
         this.ds = new DateSelector("day", "month", "year", target.getDoB());
     }
 
     @Override
     public boolean submit(PrintWriter out, HttpServletRequest req) {
         try {
-            synchronized (target) {
-                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 (newLname.isEmpty()) {
-                        throw new GigiApiException("Last name cannot be empty.");
-                    }
-
-                    target.setName(new Name(newFname, newLname, newMname, newSuffix));
-                    ds.update(req);
-                    target.setDoB(ds.getDate());
-                    target.updateUserData();
-                } else {
-                    throw new GigiApiException("No change after assurance allowed.");
+            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.");
+                }
+                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());
             }
         } catch (GigiApiException e) {
             e.format(out, Page.getLanguage(req));
             }
         } catch (GigiApiException e) {
             e.format(out, Page.getLanguage(req));
@@ -64,12 +94,31 @@ public class MyDetailsForm extends Form {
 
     @Override
     protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
 
     @Override
     protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
-        Name name = target.getName();
-        vars.put("fname", HTMLEncoder.encodeHTML(name.getFname()));
-        vars.put("mname", name.getMname() == null ? "" : HTMLEncoder.encodeHTML(name.getMname()));
-        vars.put("lname", HTMLEncoder.encodeHTML(name.getLname()));
-        vars.put("suffix", name.getSuffix() == null ? "" : HTMLEncoder.encodeHTML(name.getSuffix()));
-        vars.put("details", "");
+        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);
         if (target.getAssurancePoints() == 0) {
             vars.put("DoB", ds);
             templ.output(out, l, vars);
         if (target.getAssurancePoints() == 0) {
             vars.put("DoB", ds);
             templ.output(out, l, vars);