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;
23 import club.wpia.gigi.util.ServerConstants;
25 public class MyDetailsForm extends Form {
27 private static final Template verified = new Template(MyDetails.class.getResource("MyDetailsFormVerified.templ"));
29 private static final Template templ = new Template(MyDetailsForm.class.getResource("MyDetailsForm.templ"));
31 private static final Template names = new Template(MyDetailsForm.class.getResource("NamesForm.templ"));
33 private static final Template roles = new Template(MyDetailsForm.class.getResource("MyDetailsRoles.templ"));
35 private static final Template contracts = new Template(MyDetailsForm.class.getResource("MyDetailsContracts.templ"));
39 private DateSelector ds;
43 private CountrySelector cs;
45 private GroupSelector selectedGroup = new GroupSelector("groupToModify", false);
47 public MyDetailsForm(HttpServletRequest hsr, User target) {
52 this.ds = new DateSelector("day", "month", "year", target.getDoB());
54 if (target.getResidenceCountry() == null) {
55 this.cs = new CountrySelector("residenceCountry", true);
57 this.cs = new CountrySelector("residenceCountry", true, target.getResidenceCountry());
62 public SubmissionResult submit(HttpServletRequest req) throws GigiApiException {
64 String rn = req.getParameter("removeName");
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.");
70 if (n.equals(target.getPreferredName())) {
71 throw new GigiApiException("Cannot remove the account's preferred name.");
74 return new RedirectResult(MyDetails.PATH);
76 String dn = req.getParameter("deprecateName");
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.");
82 if (n.equals(target.getPreferredName())) {
83 throw new GigiApiException("Cannot deprecate the account's preferred name.");
86 return new RedirectResult(MyDetails.PATH);
88 String pn = req.getParameter("preferred");
90 Name n = Name.getById(Integer.parseInt(pn));
91 target.setPreferredName(n);
92 return new RedirectResult(MyDetails.PATH);
95 String action = req.getParameter("action");
96 if ("addName".equals(action)) {
98 ni.createName(target);
99 return new RedirectResult(MyDetails.PATH);
100 } else if ("updateDoB".equals(action)) {
102 target.setDoB(ds.getDate());
103 return new RedirectResult(MyDetails.PATH);
104 } else if ("updateResidenceCountry".equals(action)) {
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);
114 target.revokeGroup(target, toMod);
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);
127 return new RedirectResult(MyDetails.PATH);
129 throw new GigiApiException("Invalid action.");
132 } catch (NumberFormatException e) {
133 throw new GigiApiException("Invalid value.");
138 protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
139 vars.put("exNames", new ArrayIterable<Name>(target.getNames()) {
141 Name preferred = target.getPreferredName();
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");
149 if (t.isDeprecated()) {
150 vars.put("deprecated", " disabled");
152 vars.put("deprecated", "");
154 vars.put("preferred", "");
157 vars.put("id", t.getId());
158 vars.put("npoints", Integer.toString(t.getVerificationPoints()));
162 vars.put("name", ni);
163 names.output(out, l, vars);
165 vars.put("residenceCountry", cs);
166 if (target.getReceivedVerifications().length == 0) {
168 templ.output(out, l, vars);
170 vars.put("DoB", target.getDoB());
171 verified.output(out, l, vars);
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);
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);