]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/dbObjects/Domain.java
upd: cleanup SQL statements to make them statically verifiable.
[gigi.git] / src / org / cacert / gigi / dbObjects / Domain.java
index ba8aacff2dd1a7035a089d8c10dfe6866f512cc3..36b7dc6f651dec5c071f15796ac38c42a3117a56 100644 (file)
@@ -17,24 +17,16 @@ public class Domain implements IdCachable, Verifyable {
 
     private int id;
 
-    private Domain(int id) {
-        try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `memid`, `domain` FROM `domains` WHERE `id`=? AND `deleted` IS NULL")) {
-            ps.setInt(1, id);
-
-            GigiResultSet rs = ps.executeQuery();
-            if ( !rs.next()) {
-                throw new IllegalArgumentException("Invalid domain id " + id);
-            }
-            this.id = id;
-            owner = CertificateOwner.getById(rs.getInt(1));
-            suffix = rs.getString(2);
-        }
+    private Domain(GigiResultSet rs, int id) {
+        this.id = id;
+        owner = CertificateOwner.getById(rs.getInt(1));
+        suffix = rs.getString(2);
     }
 
     public Domain(User actor, CertificateOwner owner, String suffix) throws GigiApiException {
         suffix = suffix.toLowerCase();
         synchronized (Domain.class) {
-            DomainAssessment.checkCertifiableDomain(suffix, actor.isInGroup(Group.CODESIGNING));
+            DomainAssessment.checkCertifiableDomain(suffix, actor.isInGroup(Group.CODESIGNING), true);
             this.owner = owner;
             this.suffix = suffix;
             insert();
@@ -74,9 +66,12 @@ public class Domain implements IdCachable, Verifyable {
         if (id == 0) {
             throw new GigiApiException("not inserted.");
         }
-        try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `domains` SET `deleted`=CURRENT_TIMESTAMP WHERE `id`=?")) {
-            ps.setInt(1, id);
-            ps.execute();
+        synchronized (Domain.class) {
+            myCache.remove(this);
+            try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `domains` SET `deleted`=CURRENT_TIMESTAMP WHERE `id`=?")) {
+                ps.setInt(1, id);
+                ps.execute();
+            }
         }
     }
 
@@ -115,7 +110,7 @@ public class Domain implements IdCachable, Verifyable {
     public void addPing(DomainPingType type, String config) throws GigiApiException {
         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `pingconfig` SET `domainid`=?, `type`=?::`pingType`, `info`=?")) {
             ps.setInt(1, id);
-            ps.setString(2, type.toString().toLowerCase());
+            ps.setEnum(2, type);
             ps.setString(3, config);
             ps.execute();
         }
@@ -163,16 +158,23 @@ public class Domain implements IdCachable, Verifyable {
 
     private static final ObjectCache<Domain> myCache = new ObjectCache<>();
 
-    public static synchronized Domain getById(int id) throws IllegalArgumentException {
+    public static synchronized Domain getById(int id) {
         Domain em = myCache.get(id);
         if (em == null) {
-            myCache.put(em = new Domain(id));
+            try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `memid`, `domain` FROM `domains` WHERE `id`=? AND `deleted` IS NULL")) {
+                ps.setInt(1, id);
+                GigiResultSet rs = ps.executeQuery();
+                if ( !rs.next()) {
+                    return null;
+                }
+                myCache.put(em = new Domain(rs, id));
+            }
         }
         return em;
     }
 
-    public static Domain searchUserIdByDomain(String domain) {
-        try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id` FROM `domains` WHERE `domain` = ?")) {
+    public static Domain searchDomain(String domain) {
+        try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id` FROM `domains` WHERE `domain` = ? AND `deleted` IS NULL")) {
             ps.setString(1, domain);
             GigiResultSet res = ps.executeQuery();
             if (res.next()) {