1 package club.wpia.gigi.pages.account;
3 import java.io.PrintWriter;
7 import javax.servlet.http.HttpServletRequest;
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;
24 public class MyDetailsForm extends Form {
26 private static final Template verified = new Template(MyDetails.class.getResource("MyDetailsFormVerified.templ"));
28 private static final Template templ = new Template(MyDetailsForm.class.getResource("MyDetailsForm.templ"));
30 private static final Template names = new Template(MyDetailsForm.class.getResource("NamesForm.templ"));
32 private static final Template roles = new Template(MyDetailsForm.class.getResource("MyDetailsRoles.templ"));
34 private static final Template contracts = new Template(MyDetailsForm.class.getResource("MyDetailsContracts.templ"));
38 private DateSelector ds;
42 private CountrySelector cs;
44 private GroupSelector selectedGroup = new GroupSelector("groupToModify", false);
46 public MyDetailsForm(HttpServletRequest hsr, User target) {
51 this.ds = new DateSelector("day", "month", "year", target.getDoB());
53 if (target.getResidenceCountry() == null) {
54 this.cs = new CountrySelector("residenceCountry", true);
56 this.cs = new CountrySelector("residenceCountry", true, target.getResidenceCountry());
61 public SubmissionResult submit(HttpServletRequest req) throws GigiApiException {
63 String rn = req.getParameter("removeName");
65 Name n = Name.getById(Integer.parseInt(rn));
66 if (n.getOwner() != target) {
67 throw new GigiApiException("Cannot remove a name that does not belong to this account.");
69 if (n.equals(target.getPreferredName())) {
70 throw new GigiApiException("Cannot remove the account's preferred name.");
73 return new RedirectResult(MyDetails.PATH);
75 String dn = req.getParameter("deprecateName");
77 Name n = Name.getById(Integer.parseInt(dn));
78 if (n.getOwner() != target) {
79 throw new GigiApiException("Cannot deprecate a name that does not belong to this account.");
81 if (n.equals(target.getPreferredName())) {
82 throw new GigiApiException("Cannot deprecate the account's preferred name.");
85 return new RedirectResult(MyDetails.PATH);
87 String pn = req.getParameter("preferred");
89 Name n = Name.getById(Integer.parseInt(pn));
90 target.setPreferredName(n);
91 return new RedirectResult(MyDetails.PATH);
94 String action = req.getParameter("action");
95 if ("addName".equals(action)) {
97 ni.createName(target);
98 return new RedirectResult(MyDetails.PATH);
99 } else if ("updateDoB".equals(action)) {
101 target.setDoB(ds.getDate());
102 return new RedirectResult(MyDetails.PATH);
103 } else if ("updateResidenceCountry".equals(action)) {
105 target.setResidenceCountry(cs.getCountry());
106 return new RedirectResult(MyDetails.PATH);
107 } else 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);
113 target.revokeGroup(target, toMod);
115 return new RedirectResult(MyDetails.PATH);
116 } else if ("viewContract".equals(action)) {
117 return new RedirectResult(MyContracts.PATH);
118 } else if ("signContract".equals(action)) {
119 new Contract(target, Contract.ContractType.RA_AGENT_CONTRACT);
120 return new RedirectResult(MyDetails.PATH);
121 } else if ("revokeContract".equals(action)) {
122 Contract c = Contract.getRAAgentContractByUser(target);
126 return new RedirectResult(MyDetails.PATH);
128 throw new GigiApiException("Invalid action.");
131 } catch (NumberFormatException e) {
132 throw new GigiApiException("Invalid value.");
137 protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
138 vars.put("exNames", new ArrayIterable<Name>(target.getNames()) {
140 Name preferred = target.getPreferredName();
143 public void apply(Name t, Language l, Map<String, Object> vars) {
144 if (t.equals(preferred)) {
145 vars.put("preferred", " disabled");
146 vars.put("deprecated", " disabled");
148 if (t.isDeprecated()) {
149 vars.put("deprecated", " disabled");
151 vars.put("deprecated", "");
153 vars.put("preferred", "");
156 vars.put("id", t.getId());
157 vars.put("npoints", Integer.toString(t.getVerificationPoints()));
161 vars.put("name", ni);
162 names.output(out, l, vars);
164 vars.put("residenceCountry", cs);
165 if (target.getReceivedVerifications().length == 0) {
167 templ.output(out, l, vars);
169 vars.put("DoB", target.getDoB());
170 verified.output(out, l, vars);
173 final Set<Group> gr = target.getGroups();
174 vars.put("support-groups", new GroupList(gr, true));
175 vars.put("groups", new GroupList(gr, false));
176 vars.put("groupSelector", selectedGroup);
177 roles.output(out, l, vars);
179 boolean hasSignedContract = Contract.hasSignedContract(target, Contract.ContractType.RA_AGENT_CONTRACT);
180 vars.put("contractsign", hasSignedContract ? "disabled" : "");
181 vars.put("contractrevoke", hasSignedContract ? "" : "disabled");
182 contracts.output(out, l, vars);