]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/account/UserTrainings.java
add: show support and user the account history and the trainings.
[gigi.git] / src / org / cacert / gigi / pages / account / UserTrainings.java
1 package org.cacert.gigi.pages.account;
2
3 import java.io.IOException;
4 import java.util.HashMap;
5
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
8
9 import org.cacert.gigi.dbObjects.User;
10 import org.cacert.gigi.output.template.OutputableArrayIterable;
11 import org.cacert.gigi.pages.Page;
12 import org.cacert.gigi.util.AuthorizationContext;
13
14 public class UserTrainings extends Page {
15
16     public static final String SUPPORT_PATH = "/support/user/*/trainings";
17
18     public static final String PATH = "/account/trainings";
19
20     private static final int intStart = SUPPORT_PATH.indexOf('*');
21
22     private boolean support;
23
24     public UserTrainings(boolean support) {
25         super(support ? "Support User Trainings" : "Trainings");
26         this.support = support;
27     }
28
29     @Override
30     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
31         User u;
32         if (support) {
33             String info = req.getPathInfo();
34             int id = Integer.parseInt(info.substring(intStart, info.length() - SUPPORT_PATH.length() + intStart + 1));
35             u = User.getById(id);
36             if (u == null) {
37                 resp.sendError(404);
38                 return;
39             }
40         } else {
41             u = getUser(req);
42         }
43         HashMap<String, Object> vars = new HashMap<>();
44         vars.put("entries", new OutputableArrayIterable(u.getTrainings(), "entry"));
45         getDefaultTemplate().output(resp.getWriter(), getLanguage(req), vars);
46     }
47
48     @Override
49     public boolean isPermitted(AuthorizationContext ac) {
50         return ac != null && ( !support || ac.canSupport());
51     }
52 }