]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/dbObjects/CertificateOwner.java
fix: ResultSet.getDate is often wrong as it fetches day-precision times
[gigi.git] / src / org / cacert / gigi / dbObjects / CertificateOwner.java
index 3ca821f354f64b39efa0eef14537af8205bb1752..a66229fe6bdefa90a8d6ef9df07d1f9341e87d11 100644 (file)
@@ -1,12 +1,20 @@
 package org.cacert.gigi.dbObjects;
 
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectStreamException;
+import java.io.Serializable;
 import java.util.LinkedList;
 import java.util.List;
 
+import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.database.GigiPreparedStatement;
 import org.cacert.gigi.database.GigiResultSet;
 
-public abstract class CertificateOwner implements IdCachable {
+public abstract class CertificateOwner implements IdCachable, Serializable {
+
+    private static final long serialVersionUID = -672580485730247314L;
 
     private static final ObjectCache<CertificateOwner> myCache = new ObjectCache<>();
 
@@ -29,25 +37,33 @@ public abstract class CertificateOwner implements IdCachable {
     }
 
     public static synchronized CertificateOwner getById(int id) {
-        CertificateOwner u = myCache.get(id);
-        if (u == null) {
-            try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT *, `users`.`id` AS uid, `organisations`.`id` AS oid FROM `certOwners` LEFT JOIN `users` ON `users`.`id`=`certOwners`.`id` LEFT JOIN `organisations` ON `organisations`.`id` = `certOwners`.`id` WHERE `certOwners`.`id`=? AND `deleted` is null")) {
-                ps.setInt(1, id);
-                try (GigiResultSet rs = ps.executeQuery()) {
-                    if ( !rs.next()) {
-                        return null;
-                    }
-                    if (rs.getString("uid") != null) {
-                        myCache.put(u = new User(rs));
-                    } else if (rs.getString("oid") != null) {
-                        myCache.put(u = new Organisation(rs));
-                    } else {
-                        System.err.print("Malformed cert owner: " + id);
-                    }
-                }
+        CertificateOwner cached = myCache.get(id);
+        if (cached != null) {
+            return cached;
+        }
+
+        try (GigiPreparedStatement psU = new GigiPreparedStatement("SELECT *, `users`.`id` AS uid FROM `certOwners` INNER JOIN `users` ON `users`.`id`=`certOwners`.`id` WHERE `certOwners`.`id`=? AND `deleted` is null")) {
+            psU.setInt(1, id);
+            GigiResultSet rsU = psU.executeQuery();
+            if (rsU.next()) {
+                return myCache.put(new User(rsU));
             }
+        } catch (GigiApiException e) {
+            throw new Error(e);
         }
-        return u;
+
+        try (GigiPreparedStatement psO = new GigiPreparedStatement("SELECT *, `organisations`.`id` AS oid FROM `certOwners` INNER JOIN `organisations` ON `organisations`.`id`=`certOwners`.`id` WHERE `certOwners`.`id`=? AND `deleted` is null")) {
+            psO.setInt(1, id);
+            GigiResultSet rsO = psO.executeQuery();
+            if (rsO.next()) {
+                return myCache.put(new Organisation(rsO));
+            }
+        } catch (GigiApiException e) {
+            throw new Error(e);
+        }
+
+        System.err.println("Malformed cert owner: " + id);
+        return null;
     }
 
     public Domain[] getDomains() {
@@ -116,8 +132,8 @@ public abstract class CertificateOwner implements IdCachable {
     }
 
     public static CertificateOwner getByEnabledSerial(String serial) {
-        try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT `memid` FROM `certs` WHERE serial=? AND `disablelogin`='f' AND `revoked` is NULL")) {
-            prep.setString(1, serial.toLowerCase());
+        try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT `memid` FROM `certs` INNER JOIN `logincerts` ON `logincerts`.`id`=`certs`.`id` WHERE serial=? AND `revoked` is NULL")) {
+            prep.setString(1, serial);
             GigiResultSet res = prep.executeQuery();
             if (res.next()) {
                 return getById(res.getInt(1));
@@ -125,4 +141,31 @@ public abstract class CertificateOwner implements IdCachable {
             return null;
         }
     }
+
+    private void writeObject(ObjectOutputStream oos) throws IOException {
+        oos.writeLong(getId());
+    }
+
+    private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
+        id = (int) ois.readLong();
+    }
+
+    protected Object readResolve() throws ObjectStreamException {
+        /**
+         * Returning the Object by looking up its ID in the cache.
+         *
+         * @see http://www.javalobby.org/java/forums/t17491.html
+         * @see http://www.jguru.com/faq/view.jsp?EID=44039
+         * @see http://thecodersbreakfast.net/
+         *      ?post/2011/05/12/Serialization-and-magic-methods
+         */
+        CertificateOwner co = getById(this.getId());
+
+        if (null == co) {
+            throw new Error("Unknown Certificate Owner");
+        }
+
+        return co;
+    }
+
 }