1 package org.cacert.gigi.dbObjects;
3 import java.util.Collections;
4 import java.util.LinkedList;
7 import org.cacert.gigi.GigiApiException;
8 import org.cacert.gigi.database.DatabaseConnection;
9 import org.cacert.gigi.database.GigiPreparedStatement;
10 import org.cacert.gigi.database.GigiResultSet;
11 import org.cacert.gigi.dbObjects.DomainPingConfiguration.PingType;
13 public class Domain implements IdCachable {
15 public class DomainPingExecution {
23 private String result;
25 private DomainPingConfiguration config;
27 public DomainPingExecution(GigiResultSet rs) {
28 state = rs.getString(1);
29 type = rs.getString(2);
30 info = rs.getString(3);
31 result = rs.getString(4);
32 config = DomainPingConfiguration.getById(rs.getInt(5));
35 public String getState() {
39 public String getType() {
43 public String getInfo() {
47 public String getResult() {
51 public DomainPingConfiguration getConfig() {
59 private String suffix;
63 private Domain(int id) {
64 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid, domain FROM `domains` WHERE id=? AND deleted IS NULL");
67 GigiResultSet rs = ps.executeQuery();
69 throw new IllegalArgumentException("Invalid domain id " + id);
72 owner = User.getById(rs.getInt(1));
73 suffix = rs.getString(2);
77 public Domain(User owner, String suffix) throws GigiApiException {
83 private static void checkInsert(String suffix) throws GigiApiException {
84 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `domains` WHERE (domain=RIGHT(?,LENGTH(domain)) OR RIGHT(domain,LENGTH(?))=?) AND deleted IS NULL");
85 ps.setString(1, suffix);
86 ps.setString(2, suffix);
87 ps.setString(3, suffix);
88 GigiResultSet rs = ps.executeQuery();
89 boolean existed = rs.next();
92 throw new GigiApiException("Domain could not be inserted. Domain is already valid.");
96 public void insert() throws GigiApiException {
98 throw new GigiApiException("already inserted.");
100 synchronized (Domain.class) {
102 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `domains` SET memid=?, domain=?");
103 ps.setInt(1, owner.getId());
104 ps.setString(2, suffix);
106 id = ps.lastInsertId();
111 public void delete() throws GigiApiException {
113 throw new GigiApiException("not inserted.");
115 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `domains` SET deleted=CURRENT_TIMESTAMP WHERE id=?");
120 public User getOwner() {
129 public String getSuffix() {
133 private LinkedList<DomainPingConfiguration> configs = null;
135 public List<DomainPingConfiguration> getConfiguredPings() throws GigiApiException {
136 LinkedList<DomainPingConfiguration> configs = this.configs;
137 if (configs == null) {
138 configs = new LinkedList<>();
139 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM pingconfig WHERE domainid=?");
141 GigiResultSet rs = ps.executeQuery();
143 configs.add(DomainPingConfiguration.getById(rs.getInt(1)));
146 this.configs = configs;
149 return Collections.unmodifiableList(configs);
152 public void addPing(PingType type, String config) throws GigiApiException {
153 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO pingconfig SET domainid=?, type=?, info=?");
155 ps.setString(2, type.toString().toLowerCase());
156 ps.setString(3, config);
161 public void verify(String hash) throws GigiApiException {
162 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE domainPinglog SET state='success' WHERE challenge=? AND configId IN (SELECT id FROM pingconfig WHERE domainId=?)");
163 ps.setString(1, hash);
168 public boolean isVerified() {
169 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM domainPinglog INNER JOIN pingconfig ON pingconfig.id=domainPinglog.configId WHERE domainid=? AND state='success'");
171 GigiResultSet rs = ps.executeQuery();
175 public DomainPingExecution[] getPings() throws GigiApiException {
176 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT state, type, info, result, configId FROM domainPinglog INNER JOIN pingconfig ON pingconfig.id=domainPinglog.configid WHERE pingconfig.domainid=? ORDER BY `when` DESC;");
178 GigiResultSet rs = ps.executeQuery();
180 DomainPingExecution[] contents = new DomainPingExecution[rs.getRow()];
182 for (int i = 0; i < contents.length && rs.next(); i++) {
183 contents[i] = new DomainPingExecution(rs);
189 private static ObjectCache<Domain> myCache = new ObjectCache<>();
191 public static synchronized Domain getById(int id) throws IllegalArgumentException {
192 Domain em = myCache.get(id);
194 myCache.put(em = new Domain(id));