]> WPIA git - gigi.git/blob - src/club/wpia/gigi/dbObjects/CertificateOwner.java
fix: ensure that Users and Organisations only are inserted completely
[gigi.git] / src / club / wpia / gigi / dbObjects / CertificateOwner.java
1 package club.wpia.gigi.dbObjects;
2
3 import java.io.IOException;
4 import java.io.ObjectInputStream;
5 import java.io.ObjectOutputStream;
6 import java.io.ObjectStreamException;
7 import java.io.Serializable;
8 import java.util.LinkedList;
9 import java.util.List;
10
11 import club.wpia.gigi.GigiApiException;
12 import club.wpia.gigi.database.GigiPreparedStatement;
13 import club.wpia.gigi.database.GigiResultSet;
14
15 public abstract class CertificateOwner implements IdCachable, Serializable {
16
17     private static final long serialVersionUID = -672580485730247314L;
18
19     private static final ObjectCache<CertificateOwner> myCache = new ObjectCache<>();
20
21     private int id;
22
23     protected CertificateOwner(int id) {
24         this.id = id;
25     }
26
27     /**
28      * This constructor has a dummy parameter to allow callers to do checks
29      * before invoking the super constructor.
30      * 
31      * @param dummy
32      *            a parameter that is not used to allow callers to do checks
33      *            before super constructor invocation.
34      */
35     protected CertificateOwner(Void dummy) {
36         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `certOwners` DEFAULT VALUES")) {
37             ps.execute();
38             id = ps.lastInsertId();
39         }
40         myCache.put(this);
41     }
42
43     public int getId() {
44         return id;
45     }
46
47     public static synchronized CertificateOwner getById(int id) {
48         CertificateOwner cached = myCache.get(id);
49         if (cached != null) {
50             return cached;
51         }
52
53         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")) {
54             psU.setInt(1, id);
55             GigiResultSet rsU = psU.executeQuery();
56             if (rsU.next()) {
57                 return myCache.put(new User(rsU));
58             }
59         } catch (GigiApiException e) {
60             throw new Error(e);
61         }
62
63         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")) {
64             psO.setInt(1, id);
65             GigiResultSet rsO = psO.executeQuery();
66             if (rsO.next()) {
67                 return myCache.put(new Organisation(rsO));
68             }
69         } catch (GigiApiException e) {
70             throw new Error(e);
71         }
72
73         System.err.println("Malformed cert owner: " + id);
74         return null;
75     }
76
77     public Domain[] getDomains() {
78         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id` FROM `domains` WHERE `memid`=? AND `deleted` IS NULL")) {
79             ps.setInt(1, getId());
80
81             try (GigiResultSet rs = ps.executeQuery()) {
82                 LinkedList<Domain> data = new LinkedList<Domain>();
83
84                 while (rs.next()) {
85                     data.add(Domain.getById(rs.getInt(1)));
86                 }
87
88                 return data.toArray(new Domain[0]);
89             }
90         }
91     }
92
93     public Certificate[] getCertificates(boolean includeRevoked) {
94         try (GigiPreparedStatement ps = new GigiPreparedStatement(includeRevoked ? "SELECT id FROM certs WHERE memid=? ORDER BY id DESC" : "SELECT id FROM certs WHERE memid=? AND `revoked` IS NULL ORDER BY id DESC")) {
95             ps.setInt(1, getId());
96
97             GigiResultSet rs = ps.executeQuery();
98             LinkedList<Certificate> data = new LinkedList<Certificate>();
99
100             while (rs.next()) {
101                 data.add(Certificate.getById(rs.getInt(1)));
102             }
103
104             return data.toArray(new Certificate[0]);
105         }
106     }
107
108     public boolean isValidDomain(String domainname) {
109         for (Domain d : getDomains()) {
110             String sfx = d.getSuffix();
111             if (domainname.equals(sfx) || domainname.endsWith("." + sfx)) {
112                 return d.isVerified();
113             }
114         }
115
116         return false;
117     }
118
119     public abstract boolean isValidEmail(String email);
120
121     public void delete() {
122         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `certOwners` SET `deleted`=NOW() WHERE `id`=?")) {
123             ps.setInt(1, getId());
124             ps.execute();
125         }
126         myCache.remove(this);
127     }
128
129     public String[] getAdminLog() {
130         try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT `when`, type, information FROM `adminLog` WHERE uid=? ORDER BY `when` ASC")) {
131             prep.setInt(1, getId());
132             GigiResultSet res = prep.executeQuery();
133             List<String> entries = new LinkedList<String>();
134
135             while (res.next()) {
136                 entries.add(res.getString(2) + " (" + res.getString(3) + ")");
137             }
138             return entries.toArray(new String[0]);
139         }
140     }
141
142     public static CertificateOwner getByEnabledSerial(String serial) {
143         try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT `memid` FROM `certs` INNER JOIN `logincerts` ON `logincerts`.`id`=`certs`.`id` WHERE serial=? AND `revoked` is NULL")) {
144             prep.setString(1, serial);
145             GigiResultSet res = prep.executeQuery();
146             if (res.next()) {
147                 return getById(res.getInt(1));
148             }
149             return null;
150         }
151     }
152
153     private void writeObject(ObjectOutputStream oos) throws IOException {
154         oos.writeLong(getId());
155     }
156
157     private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
158         id = (int) ois.readLong();
159     }
160
161     protected Object readResolve() throws ObjectStreamException {
162         /**
163          * Returning the Object by looking up its ID in the cache.
164          *
165          * @see http://www.javalobby.org/java/forums/t17491.html
166          * @see http://www.jguru.com/faq/view.jsp?EID=44039
167          * @see http://thecodersbreakfast.net/
168          *      ?post/2011/05/12/Serialization-and-magic-methods
169          */
170         CertificateOwner co = getById(this.getId());
171
172         if (null == co) {
173             throw new Error("Unknown Certificate Owner");
174         }
175
176         return co;
177     }
178
179 }