]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/CATS.java
add: split API and add CATS import API
[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.DatabaseConnection;
8 import org.cacert.gigi.database.GigiPreparedStatement;
9 import org.cacert.gigi.database.GigiResultSet;
10
11 public class CATS {
12
13     private static HashMap<String, Integer> names = new HashMap<>();
14
15     public static final String ASSURER_CHALLANGE_NAME = "Assurer's Challange";
16
17     public static final int ASSURER_CHALLANGE_ID;
18
19     private CATS() {
20
21     }
22
23     static {
24         GigiResultSet res = DatabaseConnection.getInstance().prepare("SELECT `id`, `type_text` FROM `cats_type`").executeQuery();
25         while (res.next()) {
26             names.put(res.getString(2), res.getInt(1));
27         }
28         ASSURER_CHALLANGE_ID = getID(ASSURER_CHALLANGE_NAME);
29     }
30
31     public static synchronized int getID(String name) {
32         Integer i = names.get(name);
33         if (i == null) {
34             GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `cats_type` SET `type_text`=?");
35             ps.setString(1, name);
36             ps.execute();
37             i = ps.lastInsertId();
38             names.put(name, i);
39         }
40         return i;
41     }
42
43     public static void enterResult(User user, String testType, Date passDate) {
44         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `cats_passed` SET `user_id`=?, `variant_id`=?, `pass_date`=?");
45         ps.setInt(1, user.getId());
46         ps.setInt(2, getID(testType));
47         ps.setTimestamp(3, new Timestamp(passDate.getTime()));
48         ps.execute();
49     }
50 }