]> WPIA git - gigi.git/blob - src/club/wpia/gigi/dbObjects/DomainPingConfiguration.java
bdaad75880a5acb1e0c9dd09428a9be7597df19e
[gigi.git] / src / club / wpia / gigi / dbObjects / DomainPingConfiguration.java
1 package club.wpia.gigi.dbObjects;
2
3 import java.util.Date;
4
5 import club.wpia.gigi.Gigi;
6 import club.wpia.gigi.GigiApiException;
7 import club.wpia.gigi.database.GigiPreparedStatement;
8 import club.wpia.gigi.database.GigiResultSet;
9 import club.wpia.gigi.output.template.SprintfCommand;
10
11 public class DomainPingConfiguration implements IdCachable {
12
13     private static final int REPING_MINIMUM_DELAY = 5 * 60 * 1000;
14
15     private int id;
16
17     private Domain target;
18
19     private DomainPingType type;
20
21     private String info;
22
23     private DomainPingConfiguration(int id) {
24         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id`, `domainid`, `type`, `info` FROM `pingconfig` WHERE `id`=?")) {
25             ps.setInt(1, id);
26
27             GigiResultSet rs = ps.executeQuery();
28             if ( !rs.next()) {
29                 throw new IllegalArgumentException("Invalid pingconfig id " + id);
30             }
31             this.id = rs.getInt("id");
32             target = Domain.getById(rs.getInt("domainid"));
33             type = DomainPingType.valueOf(rs.getString("type").toUpperCase());
34             info = rs.getString("info");
35         }
36     }
37
38     @Override
39     public int getId() {
40         return id;
41     }
42
43     public Domain getTarget() {
44         return target;
45     }
46
47     public DomainPingType getType() {
48         return type;
49     }
50
51     public String getInfo() {
52         return info;
53     }
54
55     private static ObjectCache<DomainPingConfiguration> cache = new ObjectCache<>();
56
57     public static synchronized DomainPingConfiguration getById(int id) {
58         DomainPingConfiguration res = cache.get(id);
59         if (res == null) {
60             cache.put(res = new DomainPingConfiguration(id));
61         }
62         return res;
63     }
64
65     public Date getLastExecution() {
66         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `when` AS stamp from `domainPinglog` WHERE `configId`=? ORDER BY `when` DESC LIMIT 1")) {
67             ps.setInt(1, id);
68             GigiResultSet rs = ps.executeQuery();
69             if (rs.next()) {
70                 return new Date(rs.getTimestamp("stamp").getTime());
71             }
72             return new Date(0);
73         }
74     }
75
76     public Date getLastSuccess() {
77         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `when` AS stamp from `domainPinglog` WHERE `configId`=? AND state='success' ORDER BY `when` DESC LIMIT 1")) {
78             ps.setInt(1, id);
79             GigiResultSet rs = ps.executeQuery();
80             if (rs.next()) {
81                 return new Date(rs.getTimestamp("stamp").getTime());
82             }
83             return new Date(0);
84         }
85     }
86
87     public synchronized void requestReping() throws GigiApiException {
88         Date lastExecution = getLastExecution();
89         if (lastExecution.getTime() + REPING_MINIMUM_DELAY < System.currentTimeMillis()) {
90             Gigi.notifyPinger(this);
91             return;
92         }
93         throw new GigiApiException(SprintfCommand.createSimple("Reping is only allowed after {0} minutes, yours end at {1}.", REPING_MINIMUM_DELAY / 60 / 1000, new Date(lastExecution.getTime() + REPING_MINIMUM_DELAY)));
94     }
95 }