1 package org.cacert.gigi;
3 import java.sql.PreparedStatement;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
7 import org.cacert.gigi.database.DatabaseConnection;
13 private String suffix;
17 public Domain(int id) throws SQLException {
18 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid, domain FROM `domains` WHERE id=? AND deleted IS NULL");
21 ResultSet rs = ps.executeQuery();
23 throw new IllegalArgumentException("Invalid email id " + id);
26 owner = User.getById(rs.getInt(1));
27 suffix = rs.getString(2);
31 public Domain(User owner, String suffix) throws GigiApiException {
37 private static void checkInsert(String suffix) throws GigiApiException {
39 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `domains` WHERE (domain=RIGHT(?,LENGTH(domain)) OR RIGHT(domain,LENGTH(?))=?) AND deleted IS NULL");
40 ps.setString(1, suffix);
41 ps.setString(2, suffix);
42 ps.setString(3, suffix);
43 ResultSet rs = ps.executeQuery();
44 boolean existed = rs.next();
47 throw new GigiApiException("Domain could not be inserted. Domain is already valid.");
49 } catch (SQLException e) {
50 throw new GigiApiException(e);
54 public void insert() throws GigiApiException {
56 throw new GigiApiException("already inserted.");
58 synchronized (Domain.class) {
61 PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `domains` SET memid=?, domain=?");
62 ps.setInt(1, owner.getId());
63 ps.setString(2, suffix);
65 id = DatabaseConnection.lastInsertId(ps);
66 } catch (SQLException e) {
67 throw new GigiApiException(e);
72 public void delete() throws GigiApiException {
74 throw new GigiApiException("not inserted.");
77 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `domains` SET deleted=CURRENT_TIMESTAMP WHERE id=?");
80 } catch (SQLException e) {
81 throw new GigiApiException(e);
85 public User getOwner() {
93 public String getSuffix() {
97 public static Domain getById(int id) throws IllegalArgumentException {
100 Domain e = new Domain(id);
102 } catch (SQLException e) {
103 throw new IllegalArgumentException(e);
107 public void addPing(String type, String config) throws GigiApiException {
109 PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO pingconfig SET domainid=?, type=?, info=?");
111 ps.setString(2, type);
112 ps.setString(3, config);
114 } catch (SQLException e) {
115 throw new GigiApiException(e);
119 public void verify(String hash) throws GigiApiException {
121 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE domainPinglog SET state='success' WHERE challenge=? AND configId IN (SELECT id FROM pingconfig WHERE domainId=?)");
122 ps.setString(1, hash);
125 } catch (SQLException e) {
126 throw new GigiApiException(e);
130 public boolean isVerified() {
132 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM domainPinglog INNER JOIN pingconfig ON pingconfig.id=domainPinglog.configId WHERE domainid=? AND state='success'");
134 ResultSet rs = ps.executeQuery();
136 } catch (SQLException e) {