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;
52 public Organisation(String name, String state, String province, String city, User creator) {
55 this.province = province;
57 int id = super.insert();
58 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO organisations SET id=?, name=?, state=?, province=?, city=?, creator=?");
60 ps.setString(2, name);
61 ps.setString(3, state);
62 ps.setString(4, province);
63 ps.setString(5, city);
64 ps.setInt(6, creator.getId());
65 synchronized (Organisation.class) {
71 protected Organisation(GigiResultSet rs) {
72 super(rs.getInt("id"));
73 name = rs.getString("name");
74 state = rs.getString("state");
75 province = rs.getString("province");
76 city = rs.getString("city");
79 public String getName() {
83 public String getState() {
87 public String getProvince() {
91 public String getCity() {
95 public static synchronized Organisation getById(int id) {
96 CertificateOwner co = CertificateOwner.getById(id);
97 if (co instanceof Organisation) {
98 return (Organisation) co;
103 public synchronized void addAdmin(User admin, User actor, boolean master) {
104 GigiPreparedStatement ps1 = DatabaseConnection.getInstance().prepare("SELECT 1 FROM org_admin WHERE orgid=? AND memid=? AND deleted is null");
105 ps1.setInt(1, getId());
106 ps1.setInt(2, admin.getId());
107 GigiResultSet result = ps1.executeQuery();
111 GigiPreparedStatement ps2 = DatabaseConnection.getInstance().prepare("INSERT INTO org_admin SET orgid=?, memid=?, creator=?, master=?");
112 ps2.setInt(1, getId());
113 ps2.setInt(2, admin.getId());
114 ps2.setInt(3, actor.getId());
115 ps2.setString(4, master ? "y" : "n");
119 public void removeAdmin(User admin, User actor) {
120 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE org_admin SET deleter=?, deleted=NOW() WHERE orgid=? AND memid=?");
121 ps.setInt(1, actor.getId());
122 ps.setInt(2, getId());
123 ps.setInt(3, admin.getId());
127 public List<Affiliation> getAllAdmins() {
128 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid, master FROM org_admin WHERE orgid=? AND deleted is null");
129 ps.setInt(1, getId());
130 GigiResultSet rs = ps.executeQuery();
132 ArrayList<Affiliation> al = new ArrayList<>(rs.getRow());
135 al.add(new Affiliation(User.getById(rs.getInt(1)), rs.getString(2).equals("y"), null));
140 public static Organisation[] getOrganisations(int offset, int count) {
141 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT certOwners.id FROM organisations inner join certOwners on certOwners.id=organisations.id where certOwners.deleted is null LIMIT ?,?");
142 ps.setInt(1, offset);
144 GigiResultSet res = ps.executeQuery();
146 Organisation[] resu = new Organisation[res.getRow()];
150 resu[i++] = getById(res.getInt(1));
155 public void update(String o, String c, String st, String l) {
156 for (Certificate cert : getCertificates()) {
157 if (cert.getStatus() == CertificateStatus.ISSUED) {
161 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE organisations SET name=?, state=?, province=?, city=?");