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