1 package club.wpia.gigi.dbObjects;
3 import java.io.IOException;
4 import java.io.ObjectInputStream;
5 import java.io.ObjectOutputStream;
6 import java.util.ArrayList;
9 import club.wpia.gigi.GigiApiException;
10 import club.wpia.gigi.database.GigiPreparedStatement;
11 import club.wpia.gigi.database.GigiResultSet;
12 import club.wpia.gigi.dbObjects.Certificate.CertificateStatus;
13 import club.wpia.gigi.dbObjects.Country.CountryCodeType;
14 import club.wpia.gigi.dbObjects.wrappers.DataContainer;
16 public class Organisation extends CertificateOwner {
18 private static final long serialVersionUID = -2386342985586320843L;
21 public static class Affiliation {
23 private final User target;
25 private final boolean master;
27 private final String fixedOU;
29 private Organisation o;
31 public Affiliation(Organisation o, User target, boolean master, String fixedOU) {
35 this.fixedOU = fixedOU;
38 public User getTarget() {
42 public boolean isMaster() {
46 public String getFixedOU() {
50 public Organisation getOrganisation() {
57 private Country country;
59 private String province;
65 private String optionalName;
67 private String postalAddress;
69 public Organisation(String name, Country country, String province, String city, String email, String optionalName, String postalAddress, User creator) throws GigiApiException {
70 if ( !creator.isInGroup(Group.ORG_AGENT)) {
71 throw new GigiApiException("Only Organisation RA Agents may create organisations.");
73 if (country == null) {
74 throw new GigiApiException("Got country code of illegal type.");
77 this.country = country;
78 this.province = province;
81 this.optionalName = optionalName;
82 this.postalAddress = postalAddress;
84 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO organisations SET id=?, name=?, country=?, province=?, city=?, contactEmail=?, optional_name=?, postal_address=?, creator=?")) {
86 ps.setString(2, name);
87 ps.setString(3, country.getCode());
88 ps.setString(4, province);
89 ps.setString(5, city);
90 ps.setString(6, email);
91 ps.setString(7, optionalName);
92 ps.setString(8, postalAddress);
93 ps.setInt(9, creator.getId());
94 synchronized (Organisation.class) {
100 protected Organisation(GigiResultSet rs) throws GigiApiException {
101 super(rs.getInt("id"));
102 name = rs.getString("name");
103 country = Country.getCountryByCode(rs.getString("country"), CountryCodeType.CODE_2_CHARS);
104 province = rs.getString("province");
105 city = rs.getString("city");
106 email = rs.getString("contactEmail");
107 optionalName = rs.getString("optional_name");
108 postalAddress = rs.getString("postal_address");
111 public String getName() {
115 public Country getCountry() {
119 public String getProvince() {
123 public String getCity() {
127 public String getContactEmail() {
131 public String getOptionalName() {
135 public String getPostalAddress() {
136 return postalAddress;
139 public static synchronized Organisation getById(int id) {
140 CertificateOwner co = CertificateOwner.getById(id);
141 if (co instanceof Organisation) {
142 return (Organisation) co;
144 throw new IllegalArgumentException("Organisation not found.");
147 public synchronized void addAdmin(User admin, User actor, boolean master) throws GigiApiException {
148 if (actor == admin) {
149 throw new GigiApiException("You may not add yourself as Organisation Admin. Ask another Organisation Agent to do so.");
151 if ( !admin.canVerify()) {
152 throw new GigiApiException("Cannot add person who is not RA Agent.");
154 if ( !actor.isInGroup(Group.ORG_AGENT) && !isMaster(actor)) {
155 throw new GigiApiException("Only Organisation RA Agents or Organisation Administrators may add admins to an organisation.");
157 try (GigiPreparedStatement ps1 = new GigiPreparedStatement("SELECT 1 FROM `org_admin` WHERE `orgid`=? AND `memid`=? AND `deleted` IS NULL")) {
158 ps1.setInt(1, getId());
159 ps1.setInt(2, admin.getId());
160 GigiResultSet result = ps1.executeQuery();
165 try (GigiPreparedStatement ps2 = new GigiPreparedStatement("INSERT INTO `org_admin` SET `orgid`=?, `memid`=?, `creator`=?, `master`=?::`yesno`")) {
166 ps2.setInt(1, getId());
167 ps2.setInt(2, admin.getId());
168 ps2.setInt(3, actor.getId());
169 ps2.setString(4, master ? "y" : "n");
174 public void removeAdmin(User admin, User actor) throws GigiApiException {
175 if ( !actor.isInGroup(Group.ORG_AGENT) && !isMaster(actor)) {
176 throw new GigiApiException("Only Organisation RA Agents or Organisation Administrators may delete admins from an organisation.");
178 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE org_admin SET deleter=?, deleted=NOW() WHERE orgid=? AND memid=?")) {
179 ps.setInt(1, actor.getId());
180 ps.setInt(2, getId());
181 ps.setInt(3, admin.getId());
186 public List<Affiliation> getAllAdmins() {
187 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `memid`, `master` FROM `org_admin` WHERE `orgid`=? AND `deleted` IS NULL", true)) {
188 ps.setInt(1, getId());
189 GigiResultSet rs = ps.executeQuery();
191 ArrayList<Affiliation> al = new ArrayList<>(rs.getRow());
194 al.add(new Affiliation(this, User.getById(rs.getInt(1)), rs.getString(2).equals("y"), null));
200 public static Organisation[] getOrganisations(int offset, int count) {
201 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `certOwners`.`id` FROM `organisations` INNER JOIN `certOwners` ON `certOwners`.`id`=`organisations`.`id` WHERE `certOwners`.`deleted` IS NULL OFFSET ? LIMIT ?", true)) {
202 ps.setInt(1, offset);
204 GigiResultSet res = ps.executeQuery();
206 Organisation[] resu = new Organisation[res.getRow()];
210 resu[i++] = getById(res.getInt(1));
216 public void updateCertData(String o, Country c, String st, String l) throws GigiApiException {
218 throw new GigiApiException("Got country code of illegal type.");
220 for (Certificate cert : getCertificates(false)) {
221 if (cert.getStatus() == CertificateStatus.ISSUED) {
225 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `organisations` SET `name`=?, `country`=?, `province`=?, `city`=? WHERE `id`=?")) {
227 ps.setString(2, c.getCode());
230 ps.setInt(5, getId());
239 public void updateOrgData(String mail, String o_name, String p_address) {
240 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `organisations` SET `contactEmail`=?, `optional_name`=?, `postal_address`=? WHERE `id`=?")) {
241 ps.setString(1, mail);
242 ps.setString(2, o_name);
243 ps.setString(3, p_address);
244 ps.setInt(4, getId());
248 optionalName = o_name;
249 postalAddress = p_address;
252 public boolean isMaster(User u) {
253 for (Affiliation i : getAllAdmins()) {
254 if (i.isMaster() && i.getTarget() == u) {
262 public boolean isValidEmail(String email) {
263 return isValidDomain(email.split("@", 2)[1]);
266 public static final String SELF_ORG_NAME = "SomeCA";
268 public boolean isSelfOrganisation() {
269 return SELF_ORG_NAME.equals(getName());
272 private void writeObject(ObjectOutputStream oos) throws IOException {}
274 private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {}