]> WPIA git - gigi.git/blob - src/org/cacert/gigi/Domain.java
Test+implement: object cache for email and domain.
[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 implements IdCachable {
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                 myCache.put(this);
67             } catch (SQLException e) {
68                 throw new GigiApiException(e);
69             }
70         }
71     }
72
73     public void delete() throws GigiApiException {
74         if (id == 0) {
75             throw new GigiApiException("not inserted.");
76         }
77         try {
78             PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `domains` SET deleted=CURRENT_TIMESTAMP WHERE id=?");
79             ps.setInt(1, id);
80             ps.execute();
81         } catch (SQLException e) {
82             throw new GigiApiException(e);
83         }
84     }
85
86     public User getOwner() {
87         return owner;
88     }
89
90     public int getId() {
91         return id;
92     }
93
94     public String getSuffix() {
95         return suffix;
96     }
97
98     public void addPing(String type, String config) throws GigiApiException {
99         try {
100             PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO pingconfig SET domainid=?, type=?, info=?");
101             ps.setInt(1, id);
102             ps.setString(2, type);
103             ps.setString(3, config);
104             ps.execute();
105         } catch (SQLException e) {
106             throw new GigiApiException(e);
107         }
108     }
109
110     public void verify(String hash) throws GigiApiException {
111         try {
112             PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE domainPinglog SET state='success' WHERE challenge=? AND configId IN (SELECT id FROM pingconfig WHERE domainId=?)");
113             ps.setString(1, hash);
114             ps.setInt(2, id);
115             ps.executeUpdate();
116         } catch (SQLException e) {
117             throw new GigiApiException(e);
118         }
119     }
120
121     public boolean isVerified() {
122         try {
123             PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM domainPinglog INNER JOIN pingconfig ON pingconfig.id=domainPinglog.configId WHERE domainid=? AND state='success'");
124             ps.setInt(1, id);
125             ResultSet rs = ps.executeQuery();
126             return rs.next();
127         } catch (SQLException e) {
128             e.printStackTrace();
129         }
130         return false;
131     }
132
133     public String[][] getPings() throws GigiApiException {
134         try {
135             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;");
136             ps.setInt(1, id);
137             ResultSet rs = ps.executeQuery();
138             rs.last();
139             String[][] contents = new String[rs.getRow()][];
140             rs.beforeFirst();
141             for (int i = 0; i < contents.length && rs.next(); i++) {
142                 contents[i] = new String[] {
143                         rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4)
144                 };
145             }
146             return contents;
147         } catch (SQLException e) {
148             throw new GigiApiException(e);
149         }
150
151     }
152
153     private static ObjectCache<Domain> myCache = new ObjectCache<>();
154
155     public static Domain getById(int id) throws IllegalArgumentException {
156         Domain em = myCache.get(id);
157         if (em == null) {
158             try {
159                 synchronized (Domain.class) {
160                     myCache.put(em = new Domain(id));
161                 }
162             } catch (SQLException e1) {
163                 throw new IllegalArgumentException(e1);
164             }
165         }
166         return em;
167     }
168
169 }