]> WPIA git - gigi.git/blob - src/org/cacert/gigi/Domain.java
21fe668edda1d638beb90287e769758c8b1f860e
[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 import org.cacert.gigi.database.DatabaseConnection;
7
8 public class Domain {
9
10     private User owner;
11
12     private String suffix;
13
14     private int id;
15
16     public Domain(int id) throws SQLException {
17         PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid, domain FROM `domains` WHERE id=? AND deleted IS NULL");
18         ps.setInt(1, id);
19
20         ResultSet rs = ps.executeQuery();
21         if ( !rs.next()) {
22             throw new IllegalArgumentException("Invalid email id " + id);
23         }
24         this.id = id;
25         owner = User.getById(rs.getInt(1));
26         suffix = rs.getString(2);
27         rs.close();
28     }
29
30     public Domain(User owner, String suffix) throws GigiApiException {
31         this.owner = owner;
32         this.suffix = suffix;
33
34     }
35
36     private static void checkInsert(String suffix) throws GigiApiException {
37         try {
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();
44             rs.close();
45             if (existed) {
46                 throw new GigiApiException("Domain could not be inserted. Domain is already valid.");
47             }
48         } catch (SQLException e) {
49             throw new GigiApiException(e);
50         }
51     }
52
53     public void insert() throws GigiApiException {
54         if (id != 0) {
55             throw new GigiApiException("already inserted.");
56         }
57         synchronized (Domain.class) {
58             checkInsert(suffix);
59             try {
60                 PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `domains` SET memid=?, domain=?");
61                 ps.setInt(1, owner.getId());
62                 ps.setString(2, suffix);
63                 ps.execute();
64                 id = DatabaseConnection.lastInsertId(ps);
65             } catch (SQLException e) {
66                 throw new GigiApiException(e);
67             }
68         }
69     }
70
71     public void delete() throws GigiApiException {
72         if (id == 0) {
73             throw new GigiApiException("not inserted.");
74         }
75         try {
76             PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `domains` SET deleted=CURRENT_TIMESTAMP WHERE id=?");
77             ps.setInt(1, id);
78             ps.execute();
79         } catch (SQLException e) {
80             throw new GigiApiException(e);
81         }
82     }
83
84     public User getOwner() {
85         return owner;
86     }
87
88     public int getId() {
89         return id;
90     }
91
92     public String getSuffix() {
93         return suffix;
94     }
95
96     public static Domain getById(int id) throws IllegalArgumentException {
97         // TODO cache
98         try {
99             Domain e = new Domain(id);
100             return e;
101         } catch (SQLException e) {
102             throw new IllegalArgumentException(e);
103         }
104     }
105
106     public void addPing(String type, String config) throws GigiApiException {
107         try {
108             PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO pingconfig SET domainid=?, type=?, info=?");
109             ps.setInt(1, id);
110             ps.setString(2, type);
111             ps.setString(3, config);
112             ps.execute();
113         } catch (SQLException e) {
114             throw new GigiApiException(e);
115         }
116     }
117
118     public void verify(String hash) throws GigiApiException {
119         try {
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);
122             ps.setInt(2, id);
123             ps.executeUpdate();
124         } catch (SQLException e) {
125             throw new GigiApiException(e);
126         }
127     }
128
129     public boolean isVerified() {
130         try {
131             PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM domainPinglog INNER JOIN pingconfig ON pingconfig.id=domainPinglog.configId WHERE domainid=? AND state='success'");
132             ps.setInt(1, id);
133             ResultSet rs = ps.executeQuery();
134             return rs.next();
135         } catch (SQLException e) {
136             e.printStackTrace();
137         }
138         return false;
139     }
140
141     public String[][] getPings() throws GigiApiException {
142         try {
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;");
144             ps.setInt(1, id);
145             ResultSet rs = ps.executeQuery();
146             rs.last();
147             String[][] contents = new String[rs.getRow()][];
148             rs.beforeFirst();
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)
152                 };
153             }
154             return contents;
155         } catch (SQLException e) {
156             throw new GigiApiException(e);
157         }
158
159     }
160 }