]> WPIA git - gigi.git/blob - src/org/cacert/gigi/Domain.java
0adf6a4c2d31683f3b3da15a4299d7c0f48ff299
[gigi.git] / src / org / cacert / gigi / Domain.java
1 package org.cacert.gigi;
2
3 import java.sql.PreparedStatement;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
6
7 import org.cacert.gigi.database.DatabaseConnection;
8
9 public class Domain {
10
11     User owner;
12
13     String suffix;
14
15     int id;
16
17     public Domain(int id) throws SQLException {
18         PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid, domain FROM `domains` WHERE id=? AND deleted IS NULL");
19         ps.setInt(1, id);
20
21         ResultSet rs = ps.executeQuery();
22         if ( !rs.next()) {
23             throw new IllegalArgumentException("Invalid email id " + id);
24         }
25         this.id = id;
26         owner = User.getById(rs.getInt(1));
27         suffix = rs.getString(2);
28         rs.close();
29     }
30
31     public Domain(User owner, String suffix) throws GigiApiException {
32         this.owner = owner;
33         this.suffix = suffix;
34
35     }
36
37     private static void checkInsert(String suffix) throws GigiApiException {
38         try {
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();
45             rs.close();
46             if (existed) {
47                 throw new GigiApiException("Domain could not be inserted. Domain is already valid.");
48             }
49         } catch (SQLException e) {
50             throw new GigiApiException(e);
51         }
52     }
53
54     public void insert() throws GigiApiException {
55         if (id != 0) {
56             throw new GigiApiException("already inserted.");
57         }
58         synchronized (Domain.class) {
59             checkInsert(suffix);
60             try {
61                 PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `domains` SET memid=?, domain=?");
62                 ps.setInt(1, owner.getId());
63                 ps.setString(2, suffix);
64                 ps.execute();
65                 id = DatabaseConnection.lastInsertId(ps);
66             } catch (SQLException e) {
67                 throw new GigiApiException(e);
68             }
69         }
70     }
71
72     public void delete() throws GigiApiException {
73         if (id == 0) {
74             throw new GigiApiException("not inserted.");
75         }
76         try {
77             PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `domains` SET deleted=CURRENT_TIMESTAMP WHERE id=?");
78             ps.setInt(1, id);
79             ps.execute();
80         } catch (SQLException e) {
81             throw new GigiApiException(e);
82         }
83     }
84
85     public User getOwner() {
86         return owner;
87     }
88
89     public int getId() {
90         return id;
91     }
92
93     public String getSuffix() {
94         return suffix;
95     }
96
97     public static Domain getById(int id) throws IllegalArgumentException {
98         // TODO cache
99         try {
100             Domain e = new Domain(id);
101             return e;
102         } catch (SQLException e) {
103             throw new IllegalArgumentException(e);
104         }
105     }
106
107 }