import org.cacert.gigi.database.DatabaseConnection;
import org.cacert.gigi.database.GigiPreparedStatement;
import org.cacert.gigi.database.GigiResultSet;
+import org.cacert.gigi.dbObjects.Certificate.CertificateStatus;
public class Organisation extends CertificateOwner {
- private final String name;
+ public class Affiliation {
- private final String state;
+ private final User target;
- private final String province;
+ private final boolean master;
- private final String city;
+ private final String fixedOU;
+
+ public Affiliation(User target, boolean master, String fixedOU) {
+ this.target = target;
+ this.master = master;
+ this.fixedOU = fixedOU;
+ }
+
+ public User getTarget() {
+ return target;
+ }
+
+ public boolean isMaster() {
+ return master;
+ }
+
+ public String getFixedOU() {
+ return fixedOU;
+ }
+
+ public Organisation getOrganisation() {
+ return Organisation.this;
+ }
+ }
+
+ private String name;
+
+ private String state;
+
+ private String province;
+
+ private String city;
public Organisation(String name, String state, String province, String city, User creator) {
this.name = name;
}
protected Organisation(GigiResultSet rs) {
+ super(rs.getInt("id"));
name = rs.getString("name");
state = rs.getString("state");
province = rs.getString("province");
return null;
}
- public void addAdmin(User admin, User actor) {
- GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO org_admin SET orgid=?, memid=?, creator=?");
- ps.setInt(1, getId());
- ps.setInt(2, admin.getId());
- ps.setInt(3, actor.getId());
- ps.execute();
+ public synchronized void addAdmin(User admin, User actor, boolean master) {
+ GigiPreparedStatement ps1 = DatabaseConnection.getInstance().prepare("SELECT 1 FROM org_admin WHERE orgid=? AND memid=? AND deleted is null");
+ ps1.setInt(1, getId());
+ ps1.setInt(2, admin.getId());
+ GigiResultSet result = ps1.executeQuery();
+ if (result.next()) {
+ return;
+ }
+ GigiPreparedStatement ps2 = DatabaseConnection.getInstance().prepare("INSERT INTO org_admin SET orgid=?, memid=?, creator=?, master=?");
+ ps2.setInt(1, getId());
+ ps2.setInt(2, admin.getId());
+ ps2.setInt(3, actor.getId());
+ ps2.setString(4, master ? "y" : "n");
+ ps2.execute();
}
public void removeAdmin(User admin, User actor) {
ps.execute();
}
- public List<User> getAllAdmins() {
- GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid FROM org_admin WHERE orgid=? AND deleted is null");
+ public List<Affiliation> getAllAdmins() {
+ GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid, master FROM org_admin WHERE orgid=? AND deleted is null");
ps.setInt(1, getId());
GigiResultSet rs = ps.executeQuery();
rs.last();
- ArrayList<User> al = new ArrayList<>(rs.getRow());
+ ArrayList<Affiliation> al = new ArrayList<>(rs.getRow());
rs.beforeFirst();
while (rs.next()) {
- al.add(User.getById(rs.getInt(1)));
+ al.add(new Affiliation(User.getById(rs.getInt(1)), rs.getString(2).equals("y"), null));
}
return al;
}
+
+ public static Organisation[] getOrganisations(int offset, int count) {
+ GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT certOwners.id FROM organisations inner join certOwners on certOwners.id=organisations.id where certOwners.deleted is null LIMIT ?,?");
+ ps.setInt(1, offset);
+ ps.setInt(2, count);
+ GigiResultSet res = ps.executeQuery();
+ res.last();
+ Organisation[] resu = new Organisation[res.getRow()];
+ res.beforeFirst();
+ int i = 0;
+ while (res.next()) {
+ resu[i++] = getById(res.getInt(1));
+ }
+ return resu;
+ }
+
+ public void update(String o, String c, String st, String l) {
+ for (Certificate cert : getCertificates()) {
+ if (cert.getStatus() == CertificateStatus.ISSUED) {
+ cert.revoke();
+ }
+ }
+ GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE organisations SET name=?, state=?, province=?, city=?");
+ ps.setString(1, o);
+ ps.setString(2, c);
+ ps.setString(3, st);
+ ps.setString(4, l);
+ ps.execute();
+ name = o;
+ state = c;
+ province = st;
+ city = l;
+ }
}