X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;ds=sidebyside;f=src%2Forg%2Fcacert%2Fgigi%2Foutput%2FNameInput.java;fp=src%2Forg%2Fcacert%2Fgigi%2Foutput%2FNameInput.java;h=5eeba5e99e282404ee6ef881c13f0bd55c610cae;hb=9def69bd08ea69eb27786d5b34f00e154e09e9f3;hp=0000000000000000000000000000000000000000;hpb=8d8e8c319564bab8a34a62fa8a49865cf49225cc;p=gigi.git diff --git a/src/org/cacert/gigi/output/NameInput.java b/src/org/cacert/gigi/output/NameInput.java new file mode 100644 index 00000000..5eeba5e9 --- /dev/null +++ b/src/org/cacert/gigi/output/NameInput.java @@ -0,0 +1,109 @@ +package org.cacert.gigi.output; + +import java.io.PrintWriter; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import org.cacert.gigi.GigiApiException; +import org.cacert.gigi.dbObjects.Name; +import org.cacert.gigi.dbObjects.NamePart; +import org.cacert.gigi.dbObjects.NamePart.NamePartType; +import org.cacert.gigi.dbObjects.User; +import org.cacert.gigi.localisation.Language; +import org.cacert.gigi.output.template.Outputable; +import org.cacert.gigi.output.template.Template; + +public class NameInput implements Outputable { + + private static final Template t = new Template(NameInput.class.getResource("NameInput.templ")); + + private String fname = ""; + + private String lname = ""; + + private String suffix = ""; + + private String name = ""; + + public NameInput() {} + + public void update(HttpServletRequest req) throws GigiApiException { + fname = req.getParameter("fname"); + lname = req.getParameter("lname"); + suffix = req.getParameter("suffix"); + name = req.getParameter("name"); + if (fname == null) { + fname = ""; + } + if (lname == null) { + lname = ""; + } + if (suffix == null) { + suffix = ""; + } + if (name == null) { + name = ""; + } + if (name != null && name.contains(" ")) { + throw new GigiApiException("Single names may only have one part."); + } + + } + + @Override + public void output(PrintWriter out, Language l, Map vars) { + vars.put("fname", fname); + vars.put("lname", lname); + vars.put("suffix", suffix); + vars.put("name", name); + t.output(out, l, vars); + } + + public void createName(User u) throws GigiApiException { + new Name(u, getNameParts()); + } + + public NamePart[] getNameParts() throws GigiApiException { + if (name != null && !name.isEmpty()) { + return new NamePart[] { + new NamePart(NamePartType.SINGLE_NAME, name) + }; + } + String[] fparts = split(fname); + String[] lparts = split(lname); + String[] suff = split(suffix); + if (fparts.length == 0 || fparts[0].equals("") || lparts.length == 0 || lparts[0].equals("")) { + throw new GigiApiException("requires at least one first and one last name"); + } + NamePart[] np = new NamePart[fparts.length + lparts.length + suff.length]; + int p = 0; + for (int i = 0; i < fparts.length; i++) { + np[p++] = new NamePart(NamePartType.FIRST_NAME, fparts[i]); + } + for (int i = 0; i < lparts.length; i++) { + np[p++] = new NamePart(NamePartType.LAST_NAME, lparts[i]); + } + for (int i = 0; i < suff.length; i++) { + np[p++] = new NamePart(NamePartType.SUFFIX, suff[i]); + } + + return np; + } + + private String[] split(String toSplit) { + if (toSplit == null || toSplit.trim().isEmpty()) { + return new String[0]; + } + return toSplit.split(" "); + } + + public String[] getNamePartsPlain() throws GigiApiException { + NamePart[] p = getNameParts(); + String[] s = new String[p.length]; + for (int i = 0; i < s.length; i++) { + s[i] = p[i].getValue(); + } + return s; + } +}