]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/CATS.java
Merge "Update notes about password security"
[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     private static HashMap<String, Integer> names = new HashMap<>();
13
14     public static final String ASSURER_CHALLENGE_NAME = "Assurer's Challenge";
15
16     public static final int ASSURER_CHALLENGE_ID;
17
18     private CATS() {
19
20     }
21
22     static {
23         try (GigiPreparedStatement st = new GigiPreparedStatement("SELECT `id`, `type_text` FROM `cats_type`")) {
24             GigiResultSet res = st.executeQuery();
25             while (res.next()) {
26                 names.put(res.getString(2), res.getInt(1));
27             }
28         }
29         ASSURER_CHALLENGE_ID = getID(ASSURER_CHALLENGE_NAME);
30     }
31
32     public static synchronized int getID(String name) {
33         Integer i = names.get(name);
34         if (i == null) {
35             try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `cats_type` SET `type_text`=?")) {
36                 ps.setString(1, name);
37                 ps.execute();
38                 i = ps.lastInsertId();
39             }
40             names.put(name, i);
41         }
42         return i;
43     }
44
45     public static void enterResult(User user, String testType, Date passDate, String language, String version) {
46         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `cats_passed` SET `user_id`=?, `variant_id`=?, `pass_date`=?, `language`=?, `version`=?")) {
47             ps.setInt(1, user.getId());
48             ps.setInt(2, getID(testType));
49             ps.setTimestamp(3, new Timestamp(passDate.getTime()));
50             ps.setString(4, language);
51             ps.setString(5, version);
52             ps.execute();
53         }
54     }
55 }