]> WPIA git - gigi.git/blob - src/club/wpia/gigi/pages/account/MyDetailsForm.java
upd: terminology in code
[gigi.git] / src / club / wpia / gigi / pages / account / MyDetailsForm.java
1 package club.wpia.gigi.pages.account;
2
3 import java.io.PrintWriter;
4 import java.util.Map;
5 import java.util.Set;
6
7 import javax.servlet.http.HttpServletRequest;
8
9 import club.wpia.gigi.GigiApiException;
10 import club.wpia.gigi.dbObjects.Group;
11 import club.wpia.gigi.dbObjects.Name;
12 import club.wpia.gigi.dbObjects.User;
13 import club.wpia.gigi.localisation.Language;
14 import club.wpia.gigi.output.ArrayIterable;
15 import club.wpia.gigi.output.CountrySelector;
16 import club.wpia.gigi.output.DateSelector;
17 import club.wpia.gigi.output.GroupIterator;
18 import club.wpia.gigi.output.GroupSelector;
19 import club.wpia.gigi.output.NameInput;
20 import club.wpia.gigi.output.template.Form;
21 import club.wpia.gigi.output.template.Template;
22
23 public class MyDetailsForm extends Form {
24
25     private static final Template verified = new Template(MyDetails.class.getResource("MyDetailsFormVerified.templ"));
26
27     private static final Template templ = new Template(MyDetailsForm.class.getResource("MyDetailsForm.templ"));
28
29     private static final Template names = new Template(MyDetailsForm.class.getResource("NamesForm.templ"));
30
31     private static final Template roles = new Template(MyDetailsForm.class.getResource("MyDetailsRoles.templ"));
32
33     private User target;
34
35     private DateSelector ds;
36
37     private NameInput ni;
38
39     private CountrySelector cs;
40
41     private GroupSelector selectedGroup = new GroupSelector("groupToModify", false);
42
43     public MyDetailsForm(HttpServletRequest hsr, User target) {
44         super(hsr);
45         this.target = target;
46         ni = new NameInput();
47
48         this.ds = new DateSelector("day", "month", "year", target.getDoB());
49
50         if (target.getResidenceCountry() == null) {
51             this.cs = new CountrySelector("residenceCountry", true);
52         } else {
53             this.cs = new CountrySelector("residenceCountry", true, target.getResidenceCountry());
54         }
55     }
56
57     @Override
58     public SubmissionResult submit(HttpServletRequest req) throws GigiApiException {
59         try {
60             String rn = req.getParameter("removeName");
61             if (rn != null) {
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.");
65                 }
66                 if (n.equals(target.getPreferredName())) {
67                     throw new GigiApiException("Cannot remove the account's preferred name.");
68                 }
69                 n.remove();
70                 return new RedirectResult(MyDetails.PATH);
71             }
72             String dn = req.getParameter("deprecateName");
73             if (dn != null) {
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.");
77                 }
78                 if (n.equals(target.getPreferredName())) {
79                     throw new GigiApiException("Cannot deprecate the account's preferred name.");
80                 }
81                 n.deprecate();
82                 return new RedirectResult(MyDetails.PATH);
83             }
84             String pn = req.getParameter("preferred");
85             if (pn != null) {
86                 Name n = Name.getById(Integer.parseInt(pn));
87                 target.setPreferredName(n);
88                 return new RedirectResult(MyDetails.PATH);
89             }
90
91             String action = req.getParameter("action");
92             if ("addName".equals(action)) {
93                 ni.update(req);
94                 ni.createName(target);
95                 return new RedirectResult(MyDetails.PATH);
96             } else if ("updateDoB".equals(action)) {
97                 ds.update(req);
98                 target.setDoB(ds.getDate());
99                 return new RedirectResult(MyDetails.PATH);
100             } else if ("updateResidenceCountry".equals(action)) {
101                 cs.update(req);
102                 target.setResidenceCountry(cs.getCountry());
103                 return new RedirectResult(MyDetails.PATH);
104             } else if ("addGroup".equals(action) || "removeGroup".equals(action)) {
105                 selectedGroup.update(req);
106                 Group toMod = selectedGroup.getGroup();
107                 if ("addGroup".equals(action)) {
108                     target.grantGroup(target, toMod);
109                 } else {
110                     target.revokeGroup(target, toMod);
111                 }
112                 return new RedirectResult(MyDetails.PATH);
113             } else {
114                 throw new GigiApiException("Invalid action.");
115             }
116
117         } catch (NumberFormatException e) {
118             throw new GigiApiException("Invalid value.");
119         }
120     }
121
122     @Override
123     protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
124         vars.put("exNames", new ArrayIterable<Name>(target.getNames()) {
125
126             Name preferred = target.getPreferredName();
127
128             @Override
129             public void apply(Name t, Language l, Map<String, Object> vars) {
130                 if (t.equals(preferred)) {
131                     vars.put("preferred", " disabled");
132                     vars.put("deprecated", " disabled");
133                 } else {
134                     if (t.isDeprecated()) {
135                         vars.put("deprecated", " disabled");
136                     } else {
137                         vars.put("deprecated", "");
138                     }
139                     vars.put("preferred", "");
140                 }
141                 vars.put("name", t);
142                 vars.put("id", t.getId());
143                 vars.put("npoints", Integer.toString(t.getVerificationPoints()));
144             }
145
146         });
147         vars.put("name", ni);
148         names.output(out, l, vars);
149
150         vars.put("residenceCountry", cs);
151         if (target.getReceivedVerifications().length == 0) {
152             vars.put("DoB", ds);
153             templ.output(out, l, vars);
154         } else {
155             vars.put("DoB", target.getDoB());
156             verified.output(out, l, vars);
157         }
158
159         final Set<Group> gr = target.getGroups();
160         vars.put("support-groups", new GroupIterator(gr.iterator(), true));
161         vars.put("groups", new GroupIterator(gr.iterator(), false));
162         vars.put("groupSelector", selectedGroup);
163         roles.output(out, l, vars);
164     }
165
166 }