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;
14 public Domain(int id) throws SQLException {
15 PreparedStatement ps = DatabaseConnection.getInstance().prepare(
16 "SELECT memid, domain FROM `domain` WHERE id=? AND deleted IS NULL");
19 ResultSet rs = ps.executeQuery();
21 throw new IllegalArgumentException("Invalid email id " + id);
24 owner = User.getById(rs.getInt(1));
25 suffix = rs.getString(2);
29 public Domain(User owner, String suffix) throws GigiApiException {
35 private static void checkInsert(String suffix) throws GigiApiException {
37 PreparedStatement ps = DatabaseConnection
40 "SELECT 1 FROM `domain` 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(
63 "INSERT INTO `domain` SET memid=?, domain=?");
64 ps.setInt(1, owner.getId());
65 ps.setString(2, suffix);
67 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(
80 "UPDATE `domain` SET deleted=CURRENT_TIMESTAMP WHERE id=?");
83 } catch (SQLException e) {
84 throw new GigiApiException(e);
88 public User getOwner() {
96 public String getSuffix() {
100 public static Domain getById(int id) throws IllegalArgumentException {
103 Domain e = new Domain(id);
105 } catch (SQLException e) {
106 throw new IllegalArgumentException(e);