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) {
27 protected CertificateOwner() {
28 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `certOwners` DEFAULT VALUES")) {
30 id = ps.lastInsertId();
39 public static synchronized CertificateOwner getById(int id) {
40 CertificateOwner cached = myCache.get(id);
45 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")) {
47 GigiResultSet rsU = psU.executeQuery();
49 return myCache.put(new User(rsU));
51 } catch (GigiApiException e) {
55 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")) {
57 GigiResultSet rsO = psO.executeQuery();
59 return myCache.put(new Organisation(rsO));
61 } catch (GigiApiException e) {
65 System.err.println("Malformed cert owner: " + id);
69 public Domain[] getDomains() {
70 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id` FROM `domains` WHERE `memid`=? AND `deleted` IS NULL")) {
71 ps.setInt(1, getId());
73 try (GigiResultSet rs = ps.executeQuery()) {
74 LinkedList<Domain> data = new LinkedList<Domain>();
77 data.add(Domain.getById(rs.getInt(1)));
80 return data.toArray(new Domain[0]);
85 public Certificate[] getCertificates(boolean includeRevoked) {
86 try (GigiPreparedStatement ps = new GigiPreparedStatement(includeRevoked ? "SELECT id FROM certs WHERE memid=?" : "SELECT id FROM certs WHERE memid=? AND revoked IS NULL")) {
87 ps.setInt(1, getId());
89 GigiResultSet rs = ps.executeQuery();
90 LinkedList<Certificate> data = new LinkedList<Certificate>();
93 data.add(Certificate.getById(rs.getInt(1)));
96 return data.toArray(new Certificate[0]);
100 public boolean isValidDomain(String domainname) {
101 for (Domain d : getDomains()) {
102 String sfx = d.getSuffix();
103 if (domainname.equals(sfx) || domainname.endsWith("." + sfx)) {
104 return d.isVerified();
111 public abstract boolean isValidEmail(String email);
113 public void delete() {
114 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `certOwners` SET `deleted`=NOW() WHERE `id`=?")) {
115 ps.setInt(1, getId());
118 myCache.remove(this);
121 public String[] getAdminLog() {
122 try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT `when`, type, information FROM `adminLog` WHERE uid=? ORDER BY `when` ASC")) {
123 prep.setInt(1, getId());
124 GigiResultSet res = prep.executeQuery();
125 List<String> entries = new LinkedList<String>();
128 entries.add(res.getString(2) + " (" + res.getString(3) + ")");
130 return entries.toArray(new String[0]);
134 public static CertificateOwner getByEnabledSerial(String serial) {
135 try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT `memid` FROM `certs` INNER JOIN `logincerts` ON `logincerts`.`id`=`certs`.`id` WHERE serial=? AND `revoked` is NULL")) {
136 prep.setString(1, serial);
137 GigiResultSet res = prep.executeQuery();
139 return getById(res.getInt(1));
145 private void writeObject(ObjectOutputStream oos) throws IOException {
146 oos.writeLong(getId());
149 private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
150 id = (int) ois.readLong();
153 protected Object readResolve() throws ObjectStreamException {
155 * Returning the Object by looking up its ID in the cache.
157 * @see http://www.javalobby.org/java/forums/t17491.html
158 * @see http://www.jguru.com/faq/view.jsp?EID=44039
159 * @see http://thecodersbreakfast.net/
160 * ?post/2011/05/12/Serialization-and-magic-methods
162 CertificateOwner co = getById(this.getId());
165 throw new Error("Unknown Certificate Owner");