]> WPIA git - gigi.git/blob - src/club/wpia/gigi/pages/account/MyDetailsForm.java
add: handling of RA Agent Contract
[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
24 public class MyDetailsForm extends Form {
25
26     private static final Template verified = new Template(MyDetails.class.getResource("MyDetailsFormVerified.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 static final Template contracts = new Template(MyDetailsForm.class.getResource("MyDetailsContracts.templ"));
35
36     private User target;
37
38     private DateSelector ds;
39
40     private NameInput ni;
41
42     private CountrySelector cs;
43
44     private GroupSelector selectedGroup = new GroupSelector("groupToModify", false);
45
46     public MyDetailsForm(HttpServletRequest hsr, User target) {
47         super(hsr);
48         this.target = target;
49         ni = new NameInput();
50
51         this.ds = new DateSelector("day", "month", "year", target.getDoB());
52
53         if (target.getResidenceCountry() == null) {
54             this.cs = new CountrySelector("residenceCountry", true);
55         } else {
56             this.cs = new CountrySelector("residenceCountry", true, target.getResidenceCountry());
57         }
58     }
59
60     @Override
61     public SubmissionResult submit(HttpServletRequest req) throws GigiApiException {
62         try {
63             String rn = req.getParameter("removeName");
64             if (rn != null) {
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.");
68                 }
69                 if (n.equals(target.getPreferredName())) {
70                     throw new GigiApiException("Cannot remove the account's preferred name.");
71                 }
72                 n.remove();
73                 return new RedirectResult(MyDetails.PATH);
74             }
75             String dn = req.getParameter("deprecateName");
76             if (dn != null) {
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.");
80                 }
81                 if (n.equals(target.getPreferredName())) {
82                     throw new GigiApiException("Cannot deprecate the account's preferred name.");
83                 }
84                 n.deprecate();
85                 return new RedirectResult(MyDetails.PATH);
86             }
87             String pn = req.getParameter("preferred");
88             if (pn != null) {
89                 Name n = Name.getById(Integer.parseInt(pn));
90                 target.setPreferredName(n);
91                 return new RedirectResult(MyDetails.PATH);
92             }
93
94             String action = req.getParameter("action");
95             if ("addName".equals(action)) {
96                 ni.update(req);
97                 ni.createName(target);
98                 return new RedirectResult(MyDetails.PATH);
99             } else if ("updateDoB".equals(action)) {
100                 ds.update(req);
101                 target.setDoB(ds.getDate());
102                 return new RedirectResult(MyDetails.PATH);
103             } else if ("updateResidenceCountry".equals(action)) {
104                 cs.update(req);
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);
112                 } else {
113                     target.revokeGroup(target, toMod);
114                 }
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);
123                 if (c != null) {
124                     c.revokeContract();
125                 }
126                 return new RedirectResult(MyDetails.PATH);
127             } else {
128                 throw new GigiApiException("Invalid action.");
129             }
130
131         } catch (NumberFormatException e) {
132             throw new GigiApiException("Invalid value.");
133         }
134     }
135
136     @Override
137     protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
138         vars.put("exNames", new ArrayIterable<Name>(target.getNames()) {
139
140             Name preferred = target.getPreferredName();
141
142             @Override
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");
147                 } else {
148                     if (t.isDeprecated()) {
149                         vars.put("deprecated", " disabled");
150                     } else {
151                         vars.put("deprecated", "");
152                     }
153                     vars.put("preferred", "");
154                 }
155                 vars.put("name", t);
156                 vars.put("id", t.getId());
157                 vars.put("npoints", Integer.toString(t.getVerificationPoints()));
158             }
159
160         });
161         vars.put("name", ni);
162         names.output(out, l, vars);
163
164         vars.put("residenceCountry", cs);
165         if (target.getReceivedVerifications().length == 0) {
166             vars.put("DoB", ds);
167             templ.output(out, l, vars);
168         } else {
169             vars.put("DoB", target.getDoB());
170             verified.output(out, l, vars);
171         }
172
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);
178
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);
183     }
184
185 }