]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/Organisation.java
FIX: affiliation form with associated test cases.
[gigi.git] / src / org / cacert / gigi / dbObjects / Organisation.java
1 package org.cacert.gigi.dbObjects;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.cacert.gigi.database.DatabaseConnection;
7 import org.cacert.gigi.database.GigiPreparedStatement;
8 import org.cacert.gigi.database.GigiResultSet;
9 import org.cacert.gigi.dbObjects.Certificate.CertificateStatus;
10
11 public class Organisation extends CertificateOwner {
12
13     public class Affiliation {
14
15         private final User target;
16
17         private final boolean master;
18
19         private final String fixedOU;
20
21         public Affiliation(User target, boolean master, String fixedOU) {
22             this.target = target;
23             this.master = master;
24             this.fixedOU = fixedOU;
25         }
26
27         public User getTarget() {
28             return target;
29         }
30
31         public boolean isMaster() {
32             return master;
33         }
34
35         public String getFixedOU() {
36             return fixedOU;
37         }
38
39         public Organisation getOrganisation() {
40             return Organisation.this;
41         }
42     }
43
44     private String name;
45
46     private String state;
47
48     private String province;
49
50     private String city;
51
52     private String email;
53
54     public Organisation(String name, String state, String province, String city, String email, User creator) {
55         this.name = name;
56         this.state = state;
57         this.province = province;
58         this.city = city;
59         this.email = email;
60         int id = super.insert();
61         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO organisations SET id=?, name=?, state=?, province=?, city=?, contactEmail=?, creator=?");
62         ps.setInt(1, id);
63         ps.setString(2, name);
64         ps.setString(3, state);
65         ps.setString(4, province);
66         ps.setString(5, city);
67         ps.setString(6, email);
68         ps.setInt(7, creator.getId());
69         synchronized (Organisation.class) {
70             ps.execute();
71         }
72
73     }
74
75     protected Organisation(GigiResultSet rs) {
76         super(rs.getInt("id"));
77         name = rs.getString("name");
78         state = rs.getString("state");
79         province = rs.getString("province");
80         city = rs.getString("city");
81         email = rs.getString("contactEmail");
82     }
83
84     public String getName() {
85         return name;
86     }
87
88     public String getState() {
89         return state;
90     }
91
92     public String getProvince() {
93         return province;
94     }
95
96     public String getCity() {
97         return city;
98     }
99
100     public String getContactEmail() {
101         return email;
102     }
103
104     public static synchronized Organisation getById(int id) {
105         CertificateOwner co = CertificateOwner.getById(id);
106         if (co instanceof Organisation) {
107             return (Organisation) co;
108         }
109         return null;
110     }
111
112     public synchronized void addAdmin(User admin, User actor, boolean master) {
113         GigiPreparedStatement ps1 = DatabaseConnection.getInstance().prepare("SELECT 1 FROM org_admin WHERE orgid=? AND memid=? AND deleted is null");
114         ps1.setInt(1, getId());
115         ps1.setInt(2, admin.getId());
116         GigiResultSet result = ps1.executeQuery();
117         if (result.next()) {
118             return;
119         }
120         GigiPreparedStatement ps2 = DatabaseConnection.getInstance().prepare("INSERT INTO org_admin SET orgid=?, memid=?, creator=?, master=?");
121         ps2.setInt(1, getId());
122         ps2.setInt(2, admin.getId());
123         ps2.setInt(3, actor.getId());
124         ps2.setString(4, master ? "y" : "n");
125         ps2.execute();
126     }
127
128     public void removeAdmin(User admin, User actor) {
129         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE org_admin SET deleter=?, deleted=NOW() WHERE orgid=? AND memid=?");
130         ps.setInt(1, actor.getId());
131         ps.setInt(2, getId());
132         ps.setInt(3, admin.getId());
133         ps.execute();
134     }
135
136     public List<Affiliation> getAllAdmins() {
137         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid, master FROM org_admin WHERE orgid=? AND deleted is null");
138         ps.setInt(1, getId());
139         GigiResultSet rs = ps.executeQuery();
140         rs.last();
141         ArrayList<Affiliation> al = new ArrayList<>(rs.getRow());
142         rs.beforeFirst();
143         while (rs.next()) {
144             al.add(new Affiliation(User.getById(rs.getInt(1)), rs.getString(2).equals("y"), null));
145         }
146         return al;
147     }
148
149     public static Organisation[] getOrganisations(int offset, int count) {
150         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT certOwners.id FROM organisations inner join certOwners on certOwners.id=organisations.id where certOwners.deleted is null LIMIT ?,?");
151         ps.setInt(1, offset);
152         ps.setInt(2, count);
153         GigiResultSet res = ps.executeQuery();
154         res.last();
155         Organisation[] resu = new Organisation[res.getRow()];
156         res.beforeFirst();
157         int i = 0;
158         while (res.next()) {
159             resu[i++] = getById(res.getInt(1));
160         }
161         return resu;
162     }
163
164     public void update(String o, String c, String st, String l, String mail) {
165         for (Certificate cert : getCertificates(false)) {
166             if (cert.getStatus() == CertificateStatus.ISSUED) {
167                 cert.revoke();
168             }
169         }
170         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE organisations SET name=?, state=?, province=?, city=?, contactEmail=?");
171         ps.setString(1, o);
172         ps.setString(2, c);
173         ps.setString(3, st);
174         ps.setString(4, l);
175         ps.setString(5, mail);
176         ps.execute();
177         email = mail;
178         name = o;
179         state = c;
180         province = st;
181         city = l;
182     }
183 }