1 package club.wpia.gigi.dbObjects;
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.math.BigInteger;
9 import java.util.LinkedList;
10 import java.util.List;
12 import club.wpia.gigi.GigiApiException;
13 import club.wpia.gigi.database.GigiPreparedStatement;
14 import club.wpia.gigi.database.GigiResultSet;
16 public abstract class CertificateOwner implements IdCachable, Serializable {
18 private static final long serialVersionUID = -672580485730247314L;
20 private static final ObjectCache<CertificateOwner> myCache = new ObjectCache<>();
24 protected CertificateOwner(int id) {
29 * This constructor has a dummy parameter to allow callers to do checks
30 * before invoking the super constructor.
33 * a parameter that is not used to allow callers to do checks
34 * before super constructor invocation.
36 protected CertificateOwner(Void dummy) {
37 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `certOwners` DEFAULT VALUES")) {
39 id = ps.lastInsertId();
48 public static synchronized CertificateOwner getById(int id) {
49 CertificateOwner cached = myCache.get(id);
54 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")) {
56 GigiResultSet rsU = psU.executeQuery();
58 return myCache.put(new User(rsU));
60 } catch (GigiApiException e) {
64 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")) {
66 GigiResultSet rsO = psO.executeQuery();
68 return myCache.put(new Organisation(rsO));
70 } catch (GigiApiException e) {
74 System.err.println("Malformed cert owner: " + id);
78 public Domain[] getDomains() {
79 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id` FROM `domains` WHERE `memid`=? AND `deleted` IS NULL")) {
80 ps.setInt(1, getId());
82 try (GigiResultSet rs = ps.executeQuery()) {
83 LinkedList<Domain> data = new LinkedList<Domain>();
86 data.add(Domain.getById(rs.getInt(1)));
89 return data.toArray(new Domain[0]);
94 public Certificate[] getCertificates(boolean includeRevoked) {
95 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")) {
96 ps.setInt(1, getId());
98 GigiResultSet rs = ps.executeQuery();
99 LinkedList<Certificate> data = new LinkedList<Certificate>();
102 data.add(Certificate.getById(rs.getInt(1)));
105 return data.toArray(new Certificate[0]);
109 public boolean isValidDomain(String domainname) {
110 for (Domain d : getDomains()) {
111 String sfx = d.getSuffix();
112 if (domainname.equals(sfx) || domainname.endsWith("." + sfx)) {
113 return d.isVerified();
120 public abstract boolean isValidEmail(String email);
122 public void delete() {
123 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `certOwners` SET `deleted`=NOW() WHERE `id`=?")) {
124 ps.setInt(1, getId());
127 myCache.remove(this);
130 public String[] getAdminLog() {
131 try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT `when`, type, information FROM `adminLog` WHERE uid=? ORDER BY `when` ASC")) {
132 prep.setInt(1, getId());
133 GigiResultSet res = prep.executeQuery();
134 List<String> entries = new LinkedList<String>();
137 entries.add(res.getString(2) + " (" + res.getString(3) + ")");
139 return entries.toArray(new String[0]);
143 public static CertificateOwner getByEnabledSerial(BigInteger serial) {
144 try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT `memid` FROM `certs` INNER JOIN `logincerts` ON `logincerts`.`id`=`certs`.`id` WHERE serial=? AND `revoked` is NULL")) {
145 prep.setString(1, serial.toString(16));
146 GigiResultSet res = prep.executeQuery();
148 return getById(res.getInt(1));
154 private void writeObject(ObjectOutputStream oos) throws IOException {
155 oos.writeLong(getId());
158 private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
159 id = (int) ois.readLong();
162 protected Object readResolve() throws ObjectStreamException {
164 * Returning the Object by looking up its ID in the cache.
166 * @see http://www.javalobby.org/java/forums/t17491.html
167 * @see http://www.jguru.com/faq/view.jsp?EID=44039
168 * @see http://thecodersbreakfast.net/
169 * ?post/2011/05/12/Serialization-and-magic-methods
171 CertificateOwner co = getById(this.getId());
174 throw new Error("Unknown Certificate Owner");