]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/CATS.java
a353e1684e7ff9bd81c63bf1cd3f5966b8bc7ca1
[gigi.git] / src / org / cacert / gigi / dbObjects / CATS.java
1 package org.cacert.gigi.dbObjects;
2
3 import java.sql.Timestamp;
4 import java.util.Date;
5 import java.util.HashMap;
6
7 import org.cacert.gigi.database.GigiPreparedStatement;
8 import org.cacert.gigi.database.GigiResultSet;
9
10 public class CATS {
11
12     public enum CATSType {
13         ASSURER_CHALLENGE("Agent Qualifying Challenge"),
14
15         ORG_AGENT_CHALLENGE("Organisation Agent Qualifying Challenge"),
16
17         TTP_AGENT_CHALLENGE("TTP Agent Qualifying Challenge"),
18
19         TTP_TOPUP_AGENT_CHALLENGE_NAME("TTP TOPUP Agent Qualifying Challenge"),
20
21         CODE_SIGNING_CHALLENGE_NAME("Code Signing Challenge"),
22
23         ORG_ADMIN_DP_CHALLENGE_NAME("Organisation Administrator Data Protection Challenge"),
24
25         SUPPORT_DP_CHALLENGE_NAME("Support Data Protection Challenge");
26
27         private final String displayName;
28
29         private final int id;
30
31         private CATSType(String displayName) {
32             this.displayName = displayName;
33             id = getID(displayName);
34         }
35
36         public String getDisplayName() {
37             return displayName;
38         }
39
40         public int getId() {
41             return id;
42         }
43     }
44
45     /**
46      * The maximal number of months a passed test is considered "recent".
47      */
48     public static final int TEST_MONTHS = 12;
49
50     private static HashMap<String, Integer> names = new HashMap<>();
51
52     private CATS() {
53
54     }
55
56     static {
57         try (GigiPreparedStatement st = new GigiPreparedStatement("SELECT `id`, `type_text` FROM `cats_type`")) {
58             GigiResultSet res = st.executeQuery();
59             while (res.next()) {
60                 names.put(res.getString(2), res.getInt(1));
61             }
62         }
63     }
64
65     public static synchronized int getID(String name) {
66         Integer i = names.get(name);
67         if (i == null) {
68             try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `cats_type` SET `type_text`=?")) {
69                 ps.setString(1, name);
70                 ps.execute();
71                 i = ps.lastInsertId();
72             }
73             names.put(name, i);
74         }
75         return i;
76     }
77
78     public static void enterResult(User user, CATSType testType, Date passDate, String language, String version) {
79         enterResult(user, testType.id, passDate, language, version);
80     }
81
82     public static void enterResult(User user, String testType, Date passDate, String language, String version) {
83         enterResult(user, getID(testType), passDate, language, version);
84     }
85
86     private static void enterResult(User user, int testTypeId, Date passDate, String language, String version) {
87
88         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `cats_passed` SET `user_id`=?, `variant_id`=?, `pass_date`=?, `language`=?, `version`=?")) {
89             ps.setInt(1, user.getId());
90             ps.setInt(2, testTypeId);
91             ps.setTimestamp(3, new Timestamp(passDate.getTime()));
92             ps.setString(4, language);
93             ps.setString(5, version);
94             ps.execute();
95         }
96     }
97
98     public static boolean isInCatsLimit(int uID, int testID) {
99         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `cats_passed` WHERE `user_id` = ? AND `variant_id` = ? AND`pass_date` > (now() - interval '1 months' * ?)")) {
100             ps.setInt(1, uID);
101             ps.setInt(2, testID);
102             ps.setInt(3, TEST_MONTHS);
103
104             GigiResultSet rs = ps.executeQuery();
105             return rs.next();
106         }
107     }
108 }