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