]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/Certificate.java
Move Template to another package for moving AST-classes out.
[gigi.git] / src / org / cacert / gigi / Certificate.java
index 9d6d5d8d39f30a1fb36c04aa9fa377454749b5ed..693ef31c14e3b29e08dc54f0b9b271f718c8b9d3 100644 (file)
@@ -17,33 +17,37 @@ import org.cacert.gigi.util.KeyStorage;
 
 public class Certificate {
        private int id;
-       private int serial;
+       private int ownerId;
+       private String serial;
        private String dn;
        private String md;
        private String csrName;
        private String crtName;
        private String csr = null;
 
-       public Certificate(String dn, String md, String csr) {
+       public Certificate(int ownerId, String dn, String md, String csr) {
+               this.ownerId = ownerId;
                this.dn = dn;
                this.md = md;
                this.csr = csr;
        }
 
-       public Certificate(int id) {
+       private Certificate(String serial) {
                try {
                        PreparedStatement ps = DatabaseConnection.getInstance().prepare(
-                               "SELECT subject, md, csr_name, crt_name FROM `emailcerts` WHERE id=?");
-                       ps.setInt(1, id);
+                               "SELECT id,subject, md, csr_name, crt_name,memid FROM `emailcerts` WHERE serial=?");
+                       ps.setString(1, serial);
                        ResultSet rs = ps.executeQuery();
                        if (!rs.next()) {
-                               throw new IllegalArgumentException("Invalid mid " + id);
+                               throw new IllegalArgumentException("Invalid mid " + serial);
                        }
-                       this.id = id;
-                       dn = rs.getString(1);
-                       md = rs.getString(2);
-                       csrName = rs.getString(3);
-                       crtName = rs.getString(4);
+                       this.id = rs.getInt(1);
+                       dn = rs.getString(2);
+                       md = rs.getString(3);
+                       csrName = rs.getString(4);
+                       crtName = rs.getString(5);
+                       ownerId = rs.getInt(6);
+                       this.serial = serial;
                        rs.close();
                } catch (SQLException e) {
                        e.printStackTrace();
@@ -105,7 +109,7 @@ public class Certificate {
                        return CertificateStatus.DRAFT;
                }
                PreparedStatement searcher = DatabaseConnection.getInstance().prepare(
-                       "SELECT crt_name, created, revoked, warning FROM emailcerts WHERE id=?");
+                       "SELECT crt_name, created, revoked, warning, serial FROM emailcerts WHERE id=?");
                searcher.setInt(1, id);
                ResultSet rs = searcher.executeQuery();
                if (!rs.next()) {
@@ -119,7 +123,7 @@ public class Certificate {
                        return CertificateStatus.SIGNING;
                }
                crtName = rs.getString(1);
-               System.out.println(crtName);
+               serial = rs.getString(5);
                if (rs.getTime(2) != null && rs.getTime(3) == null) {
                        return CertificateStatus.ISSUED;
                }
@@ -135,9 +139,10 @@ public class Certificate {
                                throw new IllegalStateException();
                        }
                        PreparedStatement inserter = DatabaseConnection.getInstance().prepare(
-                               "INSERT INTO emailcerts SET md=?, subject=?, coll_found=0, crt_name=''");
+                               "INSERT INTO emailcerts SET md=?, subject=?, coll_found=0, crt_name='', memid=?");
                        inserter.setString(1, md);
                        inserter.setString(2, dn);
+                       inserter.setInt(3, ownerId);
                        inserter.execute();
                        id = DatabaseConnection.lastInsertId(inserter);
                        File csrFile = KeyStorage.locateCsr(id);
@@ -210,7 +215,7 @@ public class Certificate {
                return id;
        }
 
-       public int getSerial() {
+       public String getSerial() {
                return serial;
        }
 
@@ -222,4 +227,18 @@ public class Certificate {
                return md;
        }
 
+       public int getOwnerId() {
+               return ownerId;
+       }
+
+       public static Certificate getBySerial(String serial) {
+               // TODO caching?
+               try {
+                       return new Certificate(serial);
+               } catch (IllegalArgumentException e) {
+
+               }
+               return null;
+       }
+
 }