]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/DomainPingConfiguration.java
ADD: A step towards a more friendly SQL API.
[gigi.git] / src / org / cacert / gigi / dbObjects / DomainPingConfiguration.java
1 package org.cacert.gigi.dbObjects;
2
3 import org.cacert.gigi.database.DatabaseConnection;
4 import org.cacert.gigi.database.GigiPreparedStatement;
5 import org.cacert.gigi.database.GigiResultSet;
6
7 public class DomainPingConfiguration implements IdCachable {
8
9     public static enum PingType {
10         EMAIL, DNS, HTTP, SSL;
11     }
12
13     private int id;
14
15     private Domain target;
16
17     private PingType type;
18
19     private String info;
20
21     private DomainPingConfiguration(int id) {
22         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id, domainid, type, info FROM pingconfig WHERE id=?");
23         ps.setInt(1, id);
24
25         GigiResultSet rs = ps.executeQuery();
26         if ( !rs.next()) {
27             throw new IllegalArgumentException("Invalid pingconfig id " + id);
28         }
29         this.id = rs.getInt("id");
30         target = Domain.getById(rs.getInt("domainid"));
31         type = PingType.valueOf(rs.getString("type").toUpperCase());
32         info = rs.getString("info");
33     }
34
35     @Override
36     public int getId() {
37         return id;
38     }
39
40     public Domain getTarget() {
41         return target;
42     }
43
44     public PingType getType() {
45         return type;
46     }
47
48     public String getInfo() {
49         return info;
50     }
51
52     private static ObjectCache<DomainPingConfiguration> cache = new ObjectCache<>();
53
54     public static synchronized DomainPingConfiguration getById(int id) {
55         DomainPingConfiguration res = cache.get(id);
56         if (res == null) {
57             cache.put(res = new DomainPingConfiguration(id));
58         }
59         return res;
60     }
61
62     public void requestReping() {
63         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE pingconfig set reping='y' WHERE id=?");
64         ps.setInt(1, id);
65         ps.execute();
66     }
67
68 }