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