import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
import org.cacert.gigi.GigiApiException;
import org.cacert.gigi.database.DatabaseConnection;
+import org.cacert.gigi.dbObjects.DomainPingConfiguration.PingType;
public class Domain implements IdCachable {
+ public class DomainPingExecution {
+
+ private String state;
+
+ private String type;
+
+ private String info;
+
+ private String result;
+
+ private DomainPingConfiguration config;
+
+ public DomainPingExecution(ResultSet rs) throws SQLException {
+ state = rs.getString(1);
+ type = rs.getString(2);
+ info = rs.getString(3);
+ result = rs.getString(4);
+ config = DomainPingConfiguration.getById(rs.getInt(5));
+ }
+
+ public String getState() {
+ return state;
+ }
+
+ public String getType() {
+ return type;
+ }
+
+ public String getInfo() {
+ return info;
+ }
+
+ public String getResult() {
+ return result;
+ }
+
+ public DomainPingConfiguration getConfig() {
+ return config;
+ }
+
+ }
+
private User owner;
private String suffix;
private int id;
- public Domain(int id) throws SQLException {
+ private Domain(int id) throws SQLException {
PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid, domain FROM `domains` WHERE id=? AND deleted IS NULL");
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if ( !rs.next()) {
- throw new IllegalArgumentException("Invalid email id " + id);
+ throw new IllegalArgumentException("Invalid domain id " + id);
}
this.id = id;
owner = User.getById(rs.getInt(1));
return owner;
}
+ @Override
public int getId() {
return id;
}
return suffix;
}
- public void addPing(String type, String config) throws GigiApiException {
+ private LinkedList<DomainPingConfiguration> configs = null;
+
+ public List<DomainPingConfiguration> getConfiguredPings() throws GigiApiException {
+ LinkedList<DomainPingConfiguration> configs = this.configs;
+ if (configs == null) {
+ try {
+ configs = new LinkedList<>();
+ PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM pingconfig WHERE domainid=?");
+ ps.setInt(1, id);
+ ResultSet rs = ps.executeQuery();
+ while (rs.next()) {
+ configs.add(DomainPingConfiguration.getById(rs.getInt(1)));
+ }
+ rs.close();
+ this.configs = configs;
+ } catch (SQLException e) {
+ throw new GigiApiException(e);
+ }
+
+ }
+ return Collections.unmodifiableList(configs);
+ }
+
+ public void addPing(PingType type, String config) throws GigiApiException {
try {
PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO pingconfig SET domainid=?, type=?, info=?");
ps.setInt(1, id);
- ps.setString(2, type);
+ ps.setString(2, type.toString().toLowerCase());
ps.setString(3, config);
ps.execute();
+ configs = null;
} catch (SQLException e) {
throw new GigiApiException(e);
}
return false;
}
- public String[][] getPings() throws GigiApiException {
+ public DomainPingExecution[] getPings() throws GigiApiException {
try {
- 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;");
+ PreparedStatement 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;");
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
rs.last();
- String[][] contents = new String[rs.getRow()][];
+ DomainPingExecution[] contents = new DomainPingExecution[rs.getRow()];
rs.beforeFirst();
for (int i = 0; i < contents.length && rs.next(); i++) {
- contents[i] = new String[] {
- rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4)
- };
+ contents[i] = new DomainPingExecution(rs);
}
return contents;
} catch (SQLException e) {
private static ObjectCache<Domain> myCache = new ObjectCache<>();
- public static Domain getById(int id) throws IllegalArgumentException {
+ public static synchronized Domain getById(int id) throws IllegalArgumentException {
Domain em = myCache.get(id);
if (em == null) {
try {
- synchronized (Domain.class) {
- myCache.put(em = new Domain(id));
- }
+ myCache.put(em = new Domain(id));
} catch (SQLException e1) {
throw new IllegalArgumentException(e1);
}