]> WPIA git - gigi.git/blob - src/org/cacert/gigi/Domain.java
bcc66beb7ea826d31a7fd1ffb86437cba395b474
[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     private User owner;
12
13     private String suffix;
14
15     private 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     public void addPing(String type, String config) throws GigiApiException {
108         try {
109             PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO pingconfig SET domainid=?, type=?, info=?");
110             ps.setInt(1, id);
111             ps.setString(2, type);
112             ps.setString(3, config);
113             ps.execute();
114         } catch (SQLException e) {
115             throw new GigiApiException(e);
116         }
117     }
118
119     public void verify(String hash) throws GigiApiException {
120         try {
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);
123             ps.setInt(2, id);
124             ps.executeUpdate();
125         } catch (SQLException e) {
126             throw new GigiApiException(e);
127         }
128     }
129
130     public boolean isVerified() {
131         try {
132             PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM domainPinglog INNER JOIN pingconfig ON pingconfig.id=domainPinglog.configId WHERE domainid=? AND state='success'");
133             ps.setInt(1, id);
134             ResultSet rs = ps.executeQuery();
135             return rs.next();
136         } catch (SQLException e) {
137             e.printStackTrace();
138         }
139         return false;
140     }
141 }