]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/CertificateProfile.java
ADD: A step towards a more friendly SQL API.
[gigi.git] / src / org / cacert / gigi / dbObjects / CertificateProfile.java
1 package org.cacert.gigi.dbObjects;
2
3 import java.util.HashMap;
4
5 import org.cacert.gigi.database.DatabaseConnection;
6 import org.cacert.gigi.database.GigiPreparedStatement;
7 import org.cacert.gigi.database.GigiResultSet;
8
9 public class CertificateProfile {
10
11     private final int id;
12
13     private final String keyName;
14
15     private final String visibleName;
16
17     private static HashMap<String, CertificateProfile> byName = new HashMap<>();
18
19     private static HashMap<Integer, CertificateProfile> byId = new HashMap<>();
20
21     private CertificateProfile(int id, String keyName, String visibleName) {
22         this.id = id;
23         this.keyName = keyName;
24         this.visibleName = visibleName;
25     }
26
27     public int getId() {
28         return id;
29     }
30
31     public String getKeyName() {
32         return keyName;
33     }
34
35     public String getVisibleName() {
36         return visibleName;
37     }
38
39     static {
40         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id, keyname, name FROM `profiles`");
41         GigiResultSet rs = ps.executeQuery();
42         while (rs.next()) {
43             CertificateProfile cp = new CertificateProfile(rs.getInt("id"), rs.getString("keyName"), rs.getString("name"));
44             byId.put(cp.getId(), cp);
45             byName.put(cp.getKeyName(), cp);
46         }
47
48     }
49
50     public static CertificateProfile getById(int id) {
51         return byId.get(id);
52     }
53
54     public static CertificateProfile getByName(String name) {
55         return byName.get(name);
56     }
57
58 }