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.util.LinkedList;
11 import club.wpia.gigi.GigiApiException;
12 import club.wpia.gigi.database.GigiPreparedStatement;
13 import club.wpia.gigi.database.GigiResultSet;
15 public abstract class CertificateOwner implements IdCachable, Serializable {
17 private static final long serialVersionUID = -672580485730247314L;
19 private static final ObjectCache<CertificateOwner> myCache = new ObjectCache<>();
23 protected CertificateOwner(int id) {
28 * This constructor has a dummy parameter to allow callers to do checks
29 * before invoking the super constructor.
32 * a parameter that is not used to allow callers to do checks
33 * before super constructor invocation.
35 protected CertificateOwner(Void dummy) {
36 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `certOwners` DEFAULT VALUES")) {
38 id = ps.lastInsertId();
47 public static synchronized CertificateOwner getById(int id) {
48 CertificateOwner cached = myCache.get(id);
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")) {
55 GigiResultSet rsU = psU.executeQuery();
57 return myCache.put(new User(rsU));
59 } catch (GigiApiException e) {
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")) {
65 GigiResultSet rsO = psO.executeQuery();
67 return myCache.put(new Organisation(rsO));
69 } catch (GigiApiException e) {
73 System.err.println("Malformed cert owner: " + id);
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());
81 try (GigiResultSet rs = ps.executeQuery()) {
82 LinkedList<Domain> data = new LinkedList<Domain>();
85 data.add(Domain.getById(rs.getInt(1)));
88 return data.toArray(new Domain[0]);
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());
97 GigiResultSet rs = ps.executeQuery();
98 LinkedList<Certificate> data = new LinkedList<Certificate>();
101 data.add(Certificate.getById(rs.getInt(1)));
104 return data.toArray(new Certificate[0]);
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();
119 public abstract boolean isValidEmail(String email);
121 public void delete() {
122 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `certOwners` SET `deleted`=NOW() WHERE `id`=?")) {
123 ps.setInt(1, getId());
126 myCache.remove(this);
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>();
136 entries.add(res.getString(2) + " (" + res.getString(3) + ")");
138 return entries.toArray(new String[0]);
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();
147 return getById(res.getInt(1));
153 private void writeObject(ObjectOutputStream oos) throws IOException {
154 oos.writeLong(getId());
157 private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
158 id = (int) ois.readLong();
161 protected Object readResolve() throws ObjectStreamException {
163 * Returning the Object by looking up its ID in the cache.
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
170 CertificateOwner co = getById(this.getId());
173 throw new Error("Unknown Certificate Owner");