1 package org.cacert.gigi.dbObjects;
3 import java.util.ArrayList;
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;
11 public class Organisation extends CertificateOwner {
13 public class Affiliation {
15 private final User target;
17 private final boolean master;
19 private final String fixedOU;
21 public Affiliation(User target, boolean master, String fixedOU) {
24 this.fixedOU = fixedOU;
27 public User getTarget() {
31 public boolean isMaster() {
35 public String getFixedOU() {
39 public Organisation getOrganisation() {
40 return Organisation.this;
48 private String province;
54 public Organisation(String name, String state, String province, String city, String email, User creator) {
57 this.province = province;
60 int id = super.insert();
61 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO organisations SET id=?, name=?, state=?, province=?, city=?, contactEmail=?, creator=?");
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) {
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");
84 public String getName() {
88 public String getState() {
92 public String getProvince() {
96 public String getCity() {
100 public String getContactEmail() {
104 public static synchronized Organisation getById(int id) {
105 CertificateOwner co = CertificateOwner.getById(id);
106 if (co instanceof Organisation) {
107 return (Organisation) co;
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();
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");
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());
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();
141 ArrayList<Affiliation> al = new ArrayList<>(rs.getRow());
144 al.add(new Affiliation(User.getById(rs.getInt(1)), rs.getString(2).equals("y"), null));
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);
153 GigiResultSet res = ps.executeQuery();
155 Organisation[] resu = new Organisation[res.getRow()];
159 resu[i++] = getById(res.getInt(1));
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) {
170 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE organisations SET name=?, state=?, province=?, city=?, contactEmail=?");
175 ps.setString(5, mail);