1 package org.cacert.gigi;
3 import java.sql.PreparedStatement;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
6 import org.cacert.gigi.database.DatabaseConnection;
12 private String suffix;
16 public Domain(int id) throws SQLException {
17 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid, domain FROM `domains` WHERE id=? AND deleted IS NULL");
20 ResultSet rs = ps.executeQuery();
22 throw new IllegalArgumentException("Invalid email id " + id);
25 owner = User.getById(rs.getInt(1));
26 suffix = rs.getString(2);
30 public Domain(User owner, String suffix) throws GigiApiException {
36 private static void checkInsert(String suffix) throws GigiApiException {
38 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `domains` WHERE (domain=RIGHT(?,LENGTH(domain)) OR RIGHT(domain,LENGTH(?))=?) AND deleted IS NULL");
39 ps.setString(1, suffix);
40 ps.setString(2, suffix);
41 ps.setString(3, suffix);
42 ResultSet rs = ps.executeQuery();
43 boolean existed = rs.next();
46 throw new GigiApiException("Domain could not be inserted. Domain is already valid.");
48 } catch (SQLException e) {
49 throw new GigiApiException(e);
53 public void insert() throws GigiApiException {
55 throw new GigiApiException("already inserted.");
57 synchronized (Domain.class) {
60 PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `domains` SET memid=?, domain=?");
61 ps.setInt(1, owner.getId());
62 ps.setString(2, suffix);
64 id = DatabaseConnection.lastInsertId(ps);
65 } catch (SQLException e) {
66 throw new GigiApiException(e);
71 public void delete() throws GigiApiException {
73 throw new GigiApiException("not inserted.");
76 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `domains` SET deleted=CURRENT_TIMESTAMP WHERE id=?");
79 } catch (SQLException e) {
80 throw new GigiApiException(e);
84 public User getOwner() {
92 public String getSuffix() {
96 public static Domain getById(int id) throws IllegalArgumentException {
99 Domain e = new Domain(id);
101 } catch (SQLException e) {
102 throw new IllegalArgumentException(e);
106 public void addPing(String type, String config) throws GigiApiException {
108 PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO pingconfig SET domainid=?, type=?, info=?");
110 ps.setString(2, type);
111 ps.setString(3, config);
113 } catch (SQLException e) {
114 throw new GigiApiException(e);
118 public void verify(String hash) throws GigiApiException {
120 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE domainPinglog SET state='success' WHERE challenge=? AND configId IN (SELECT id FROM pingconfig WHERE domainId=?)");
121 ps.setString(1, hash);
124 } catch (SQLException e) {
125 throw new GigiApiException(e);
129 public boolean isVerified() {
131 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM domainPinglog INNER JOIN pingconfig ON pingconfig.id=domainPinglog.configId WHERE domainid=? AND state='success'");
133 ResultSet rs = ps.executeQuery();
135 } catch (SQLException e) {
141 public String[][] getPings() throws GigiApiException {
143 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;");
145 ResultSet rs = ps.executeQuery();
147 String[][] contents = new String[rs.getRow()][];
149 for (int i = 0; i < contents.length && rs.next(); i++) {
150 contents[i] = new String[] {
151 rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4)
155 } catch (SQLException e) {
156 throw new GigiApiException(e);