]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/dbObjects/DomainPingConfiguration.java
UPD: Factor out PingConfig objects and pingconfig-form.
[gigi.git] / src / org / cacert / gigi / dbObjects / DomainPingConfiguration.java
diff --git a/src/org/cacert/gigi/dbObjects/DomainPingConfiguration.java b/src/org/cacert/gigi/dbObjects/DomainPingConfiguration.java
new file mode 100644 (file)
index 0000000..a031b9e
--- /dev/null
@@ -0,0 +1,68 @@
+package org.cacert.gigi.dbObjects;
+
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import org.cacert.gigi.database.DatabaseConnection;
+
+public class DomainPingConfiguration implements IdCachable {
+
+    public static enum PingType {
+        EMAIL, DNS, HTTP, SSL;
+    }
+
+    int id;
+
+    Domain target;
+
+    PingType type;
+
+    String info;
+
+    private DomainPingConfiguration(int id) throws SQLException {
+        PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id, domainid, type, info FROM pingconfig WHERE id=?");
+        ps.setInt(1, id);
+
+        ResultSet rs = ps.executeQuery();
+        if ( !rs.next()) {
+            throw new IllegalArgumentException("Invalid pingconfig id " + id);
+        }
+        this.id = rs.getInt("id");
+        target = Domain.getById(rs.getInt("domainid"));
+        type = PingType.valueOf(rs.getString("type").toUpperCase());
+        info = rs.getString("info");
+    }
+
+    @Override
+    public int getId() {
+        return id;
+    }
+
+    public Domain getTarget() {
+        return target;
+    }
+
+    public PingType getType() {
+        return type;
+    }
+
+    public String getInfo() {
+        return info;
+    }
+
+    private static ObjectCache<DomainPingConfiguration> cache = new ObjectCache<>();
+
+    public static DomainPingConfiguration getById(int id) {
+        DomainPingConfiguration res = cache.get(id);
+        if (res == null) {
+            try {
+                cache.put(res = new DomainPingConfiguration(id));
+            } catch (SQLException e) {
+                throw new IllegalArgumentException(e);
+            }
+        }
+        return res;
+    }
+
+}