1 package org.cacert.gigi.dbObjects;
3 import java.sql.PreparedStatement;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
7 import org.cacert.gigi.GigiApiException;
8 import org.cacert.gigi.database.DatabaseConnection;
10 public class Domain implements IdCachable {
14 private String suffix;
18 private Domain(int id) throws SQLException {
19 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid, domain FROM `domains` WHERE id=? AND deleted IS NULL");
22 ResultSet rs = ps.executeQuery();
24 throw new IllegalArgumentException("Invalid email id " + id);
27 owner = User.getById(rs.getInt(1));
28 suffix = rs.getString(2);
32 public Domain(User owner, String suffix) throws GigiApiException {
38 private static void checkInsert(String suffix) throws GigiApiException {
40 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `domains` WHERE (domain=RIGHT(?,LENGTH(domain)) OR RIGHT(domain,LENGTH(?))=?) AND deleted IS NULL");
41 ps.setString(1, suffix);
42 ps.setString(2, suffix);
43 ps.setString(3, suffix);
44 ResultSet rs = ps.executeQuery();
45 boolean existed = rs.next();
48 throw new GigiApiException("Domain could not be inserted. Domain is already valid.");
50 } catch (SQLException e) {
51 throw new GigiApiException(e);
55 public void insert() throws GigiApiException {
57 throw new GigiApiException("already inserted.");
59 synchronized (Domain.class) {
62 PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `domains` SET memid=?, domain=?");
63 ps.setInt(1, owner.getId());
64 ps.setString(2, suffix);
66 id = DatabaseConnection.lastInsertId(ps);
68 } catch (SQLException e) {
69 throw new GigiApiException(e);
74 public void delete() throws GigiApiException {
76 throw new GigiApiException("not inserted.");
79 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `domains` SET deleted=CURRENT_TIMESTAMP WHERE id=?");
82 } catch (SQLException e) {
83 throw new GigiApiException(e);
87 public User getOwner() {
95 public String getSuffix() {
99 public void addPing(String type, String config) throws GigiApiException {
101 PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO pingconfig SET domainid=?, type=?, info=?");
103 ps.setString(2, type);
104 ps.setString(3, config);
106 } catch (SQLException e) {
107 throw new GigiApiException(e);
111 public void verify(String hash) throws GigiApiException {
113 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE domainPinglog SET state='success' WHERE challenge=? AND configId IN (SELECT id FROM pingconfig WHERE domainId=?)");
114 ps.setString(1, hash);
117 } catch (SQLException e) {
118 throw new GigiApiException(e);
122 public boolean isVerified() {
124 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM domainPinglog INNER JOIN pingconfig ON pingconfig.id=domainPinglog.configId WHERE domainid=? AND state='success'");
126 ResultSet rs = ps.executeQuery();
128 } catch (SQLException e) {
134 public String[][] getPings() throws GigiApiException {
136 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT state, type, info, result FROM domainPinglog INNER JOIN pingconfig ON pingconfig.id=domainPinglog.configid WHERE pingconfig.domainid=? ORDER BY `when` DESC;");
138 ResultSet rs = ps.executeQuery();
140 String[][] contents = new String[rs.getRow()][];
142 for (int i = 0; i < contents.length && rs.next(); i++) {
143 contents[i] = new String[] {
144 rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4)
148 } catch (SQLException e) {
149 throw new GigiApiException(e);
154 private static ObjectCache<Domain> myCache = new ObjectCache<>();
156 public static Domain getById(int id) throws IllegalArgumentException {
157 Domain em = myCache.get(id);
160 synchronized (Domain.class) {
161 myCache.put(em = new Domain(id));
163 } catch (SQLException e1) {
164 throw new IllegalArgumentException(e1);