]> WPIA git - gigi.git/blob - src/org/cacert/gigi/Domain.java
Adding domain Object (+ testcase)
[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 {
10         User owner;
11         String suffix;
12         int id;
13
14         public Domain(int id) throws SQLException {
15                 PreparedStatement ps = DatabaseConnection.getInstance().prepare(
16                         "SELECT memid, domain FROM `domain` WHERE id=? AND deleted IS NULL");
17                 ps.setInt(1, id);
18
19                 ResultSet rs = ps.executeQuery();
20                 if (!rs.next()) {
21                         throw new IllegalArgumentException("Invalid email id " + id);
22                 }
23                 this.id = id;
24                 owner = User.getById(rs.getInt(1));
25                 suffix = rs.getString(2);
26                 rs.close();
27         }
28
29         public Domain(User owner, String suffix) throws GigiApiException {
30                 this.owner = owner;
31                 this.suffix = suffix;
32
33         }
34
35         private static void checkInsert(String suffix) throws GigiApiException {
36                 try {
37                         PreparedStatement ps = DatabaseConnection
38                                 .getInstance()
39                                 .prepare(
40                                         "SELECT 1 FROM `domain` 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(
63                                         "INSERT INTO `domain` SET memid=?, domain=?");
64                                 ps.setInt(1, owner.getId());
65                                 ps.setString(2, suffix);
66                                 ps.execute();
67                                 id = DatabaseConnection.lastInsertId(ps);
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(
80                                 "UPDATE `domain` SET deleted=CURRENT_TIMESTAMP WHERE id=?");
81                         ps.setInt(1, id);
82                         ps.execute();
83                 } catch (SQLException e) {
84                         throw new GigiApiException(e);
85                 }
86         }
87
88         public User getOwner() {
89                 return owner;
90         }
91
92         public int getId() {
93                 return id;
94         }
95
96         public String getSuffix() {
97                 return suffix;
98         }
99
100         public static Domain getById(int id) throws IllegalArgumentException {
101                 // TODO cache
102                 try {
103                         Domain e = new Domain(id);
104                         return e;
105                 } catch (SQLException e) {
106                         throw new IllegalArgumentException(e);
107                 }
108         }
109
110 }