]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/dbObjects/DomainPingConfiguration.java
fix: npe in domain overview as domain
[gigi.git] / src / org / cacert / gigi / dbObjects / DomainPingConfiguration.java
index 1146cca0c5c8b0b451c1f75c334cff0f71e87a91..145473fdb9a871c892f8f42ad87caa89a9c4ee5d 100644 (file)
@@ -1,25 +1,30 @@
 package org.cacert.gigi.dbObjects;
 
+import java.util.Arrays;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.cacert.gigi.Gigi;
+import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.database.DatabaseConnection;
 import org.cacert.gigi.database.GigiPreparedStatement;
 import org.cacert.gigi.database.GigiResultSet;
+import org.cacert.gigi.output.template.Scope;
+import org.cacert.gigi.output.template.SprintfCommand;
 
 public class DomainPingConfiguration implements IdCachable {
 
-    public static enum PingType {
-        EMAIL, DNS, HTTP, SSL;
-    }
-
     private int id;
 
     private Domain target;
 
-    private PingType type;
+    private DomainPingType type;
 
     private String info;
 
     private DomainPingConfiguration(int id) {
-        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id, domainid, type, info FROM pingconfig WHERE id=?");
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `id`, `domainid`, `type`, `info` FROM `pingconfig` WHERE `id`=?");
         ps.setInt(1, id);
 
         GigiResultSet rs = ps.executeQuery();
@@ -28,7 +33,7 @@ public class DomainPingConfiguration implements IdCachable {
         }
         this.id = rs.getInt("id");
         target = Domain.getById(rs.getInt("domainid"));
-        type = PingType.valueOf(rs.getString("type").toUpperCase());
+        type = DomainPingType.valueOf(rs.getString("type").toUpperCase());
         info = rs.getString("info");
     }
 
@@ -41,7 +46,7 @@ public class DomainPingConfiguration implements IdCachable {
         return target;
     }
 
-    public PingType getType() {
+    public DomainPingType getType() {
         return type;
     }
 
@@ -59,10 +64,34 @@ public class DomainPingConfiguration implements IdCachable {
         return res;
     }
 
-    public void requestReping() {
-        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE pingconfig set reping='y' WHERE id=?");
+    public Date getLastExecution() {
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `when` AS stamp from `domainPinglog` WHERE `configId`=? ORDER BY `when` DESC LIMIT 1");
+        ps.setInt(1, id);
+        GigiResultSet rs = ps.executeQuery();
+        if (rs.next()) {
+            return new Date(rs.getTimestamp("stamp").getTime());
+        }
+        return new Date(0);
+    }
+
+    public Date getLastSuccess() {
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `when` AS stamp from `domainPinglog` WHERE `configId`=? AND state='success' ORDER BY `when` DESC LIMIT 1");
         ps.setInt(1, id);
-        ps.execute();
+        GigiResultSet rs = ps.executeQuery();
+        if (rs.next()) {
+            return new Date(rs.getTimestamp("stamp").getTime());
+        }
+        return new Date(0);
     }
 
+    public synchronized void requestReping() throws GigiApiException {
+        Date lastExecution = getLastExecution();
+        if (lastExecution.getTime() + 5 * 60 * 1000 < System.currentTimeMillis()) {
+            Gigi.notifyPinger(this);
+            return;
+        }
+        Map<String, Object> data = new HashMap<String, Object>();
+        data.put("data", new Date(lastExecution.getTime() + 5 * 60 * 1000));
+        throw new GigiApiException(new Scope(new SprintfCommand("Reping is only allowed after 5 minutes, yours end at {0}.", Arrays.asList("${data}")), data));
+    }
 }