]> WPIA git - gigi.git/blob - src/club/wpia/gigi/output/NameInput.java
Merge "fix: ensure no blanks are entered between name parts and hyphens"
[gigi.git] / src / club / wpia / gigi / output / NameInput.java
1 package club.wpia.gigi.output;
2
3 import java.io.PrintWriter;
4 import java.util.Map;
5
6 import javax.servlet.http.HttpServletRequest;
7
8 import club.wpia.gigi.GigiApiException;
9 import club.wpia.gigi.dbObjects.Name;
10 import club.wpia.gigi.dbObjects.NamePart;
11 import club.wpia.gigi.dbObjects.NamePart.NamePartType;
12 import club.wpia.gigi.dbObjects.User;
13 import club.wpia.gigi.localisation.Language;
14 import club.wpia.gigi.output.template.Outputable;
15 import club.wpia.gigi.output.template.Template;
16
17 public class NameInput implements Outputable {
18
19     private static final Template t = new Template(NameInput.class.getResource("NameInput.templ"));
20
21     private String fname = "";
22
23     private String lname = "";
24
25     private String suffix = "";
26
27     private String name = "";
28
29     private String scheme = "western";
30
31     public NameInput() {}
32
33     public void update(HttpServletRequest req) throws GigiApiException {
34         fname = req.getParameter("fname");
35         lname = req.getParameter("lname");
36         suffix = req.getParameter("suffix");
37         name = req.getParameter("name");
38         scheme = req.getParameter("name-type");
39         if (fname == null) {
40             fname = "";
41         }
42         if (lname == null) {
43             lname = "";
44         }
45         if (suffix == null) {
46             suffix = "";
47         }
48         if (name == null) {
49             name = "";
50         }
51         if ( !"western".equals(scheme) && !"single".equals(scheme)) {
52             throw new GigiApiException("Invalid name type.");
53         }
54         if (name.contains(" ")) {
55             throw new GigiApiException("Single names may only have one part.");
56         }
57
58     }
59
60     @Override
61     public void output(PrintWriter out, Language l, Map<String, Object> vars) {
62         vars.put("fname", fname);
63         vars.put("lname", lname);
64         vars.put("suffix", suffix);
65         vars.put("name", name);
66         vars.put("western", "western".equals(scheme));
67         vars.put("single", "single".equals(scheme));
68         t.output(out, l, vars);
69     }
70
71     public void createName(User u) throws GigiApiException {
72         new Name(u, getNameParts());
73     }
74
75     public NamePart[] getNameParts() throws GigiApiException {
76         if ("single".equals(scheme)) {
77             if (name == null || name.trim().isEmpty()) {
78                 throw new GigiApiException("requires at least one character in the single name");
79             }
80             return new NamePart[] {
81                     new NamePart(NamePartType.SINGLE_NAME, name.trim())
82             };
83         }
84         String[] fparts = split(fname);
85         String[] lparts = split(lname);
86         String[] suff = split(suffix);
87         if (fparts.length == 0 || fparts[0].equals("") || lparts.length == 0 || lparts[0].equals("")) {
88             throw new GigiApiException("requires at least one first and one last name");
89         }
90         NamePart[] np = new NamePart[fparts.length + lparts.length + suff.length];
91         int p = 0;
92         for (int i = 0; i < fparts.length; i++) {
93             np[p++] = new NamePart(NamePartType.FIRST_NAME, fparts[i]);
94         }
95         for (int i = 0; i < lparts.length; i++) {
96             np[p++] = new NamePart(NamePartType.LAST_NAME, lparts[i]);
97         }
98         for (int i = 0; i < suff.length; i++) {
99             np[p++] = new NamePart(NamePartType.SUFFIX, suff[i]);
100         }
101
102         return np;
103     }
104
105     private String[] split(String toSplit) {
106         if (toSplit == null || toSplit.trim().isEmpty()) {
107             return new String[0];
108         }
109         toSplit = toSplit.replaceAll("(?>[\\p{Z}\\s]*)([\u002d\u058a\u05be\u1806\u2010\u2011\u2012\u2013\u2014\u2015\u2e3a\u2e3b\ufe58\ufe63\uff0d])(?>[\\p{Z}\\s]*)", "-");
110         toSplit = toSplit.replaceAll("(?>[\\p{Z}\\s]+)", " ").trim();
111
112         return toSplit.split(" ");
113     }
114
115     public String[] getNamePartsPlain() throws GigiApiException {
116         NamePart[] p = getNameParts();
117         String[] s = new String[p.length];
118         for (int i = 0; i < s.length; i++) {
119             s[i] = p[i].getValue();
120         }
121         return s;
122     }
123 }