]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/dbObjects/Organisation.java
upd: narrowing type-safety around Organisation
[gigi.git] / src / org / cacert / gigi / dbObjects / Organisation.java
index 101c973ef3fa3fdebea65d402b32b5a85292b40d..526bf303c710f1785f70f044b4ae9d947ba3cefc 100644 (file)
@@ -1,5 +1,8 @@
 package org.cacert.gigi.dbObjects;
 
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -7,10 +10,13 @@ import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.database.GigiPreparedStatement;
 import org.cacert.gigi.database.GigiResultSet;
 import org.cacert.gigi.dbObjects.Certificate.CertificateStatus;
+import org.cacert.gigi.dbObjects.CountryCode.CountryCodeType;
 import org.cacert.gigi.dbObjects.wrappers.DataContainer;
 
 public class Organisation extends CertificateOwner {
 
+    private static final long serialVersionUID = -2386342985586320843L;
+
     @DataContainer
     public static class Affiliation {
 
@@ -48,7 +54,7 @@ public class Organisation extends CertificateOwner {
 
     private String name;
 
-    private String state;
+    private CountryCode state;
 
     private String province;
 
@@ -60,9 +66,12 @@ public class Organisation extends CertificateOwner {
 
     private String postalAddress;
 
-    public Organisation(String name, String state, String province, String city, String email, String optionalName, String postalAddress, User creator) throws GigiApiException {
+    public Organisation(String name, CountryCode state, String province, String city, String email, String optionalName, String postalAddress, User creator) throws GigiApiException {
         if ( !creator.isInGroup(Group.ORGASSURER)) {
-            throw new GigiApiException("Only org-assurers may create organisations.");
+            throw new GigiApiException("Only Organisation RA Agents may create organisations.");
+        }
+        if (state == null || state.getCountryCodeType() != CountryCodeType.CODE_2_CHARS) {
+            throw new GigiApiException("Got country code of illegal type.");
         }
         this.name = name;
         this.state = state;
@@ -75,7 +84,7 @@ public class Organisation extends CertificateOwner {
         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO organisations SET id=?, name=?, state=?, province=?, city=?, contactEmail=?, optional_name=?, postal_address=?, creator=?")) {
             ps.setInt(1, id);
             ps.setString(2, name);
-            ps.setString(3, state);
+            ps.setString(3, state.getCountryCode());
             ps.setString(4, province);
             ps.setString(5, city);
             ps.setString(6, email);
@@ -88,10 +97,10 @@ public class Organisation extends CertificateOwner {
         }
     }
 
-    protected Organisation(GigiResultSet rs) {
+    protected Organisation(GigiResultSet rs) throws GigiApiException {
         super(rs.getInt("id"));
         name = rs.getString("name");
-        state = rs.getString("state");
+        state = CountryCode.getCountryCode(rs.getString("state"), CountryCodeType.CODE_2_CHARS);
         province = rs.getString("province");
         city = rs.getString("city");
         email = rs.getString("contactEmail");
@@ -103,7 +112,7 @@ public class Organisation extends CertificateOwner {
         return name;
     }
 
-    public String getState() {
+    public CountryCode getState() {
         return state;
     }
 
@@ -137,10 +146,10 @@ public class Organisation extends CertificateOwner {
 
     public synchronized void addAdmin(User admin, User actor, boolean master) throws GigiApiException {
         if ( !admin.canAssure()) {
-            throw new GigiApiException("Cannot add non-assurer.");
+            throw new GigiApiException("Cannot add person who is not RA Agent.");
         }
         if ( !actor.isInGroup(Group.ORGASSURER) && !isMaster(actor)) {
-            throw new GigiApiException("Only org assurer or master-admin may add admins to an organisation.");
+            throw new GigiApiException("Only Organisation RA Agents or Organisation Administrators may add admins to an organisation.");
         }
         try (GigiPreparedStatement ps1 = new GigiPreparedStatement("SELECT 1 FROM `org_admin` WHERE `orgid`=? AND `memid`=? AND `deleted` IS NULL")) {
             ps1.setInt(1, getId());
@@ -161,7 +170,7 @@ public class Organisation extends CertificateOwner {
 
     public void removeAdmin(User admin, User actor) throws GigiApiException {
         if ( !actor.isInGroup(Group.ORGASSURER) && !isMaster(actor)) {
-            throw new GigiApiException("Only org assurer or master-admin may delete admins from an organisation.");
+            throw new GigiApiException("Only Organisation RA Agents or Organisation Administrators may delete admins from an organisation.");
         }
         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE org_admin SET deleter=?, deleted=NOW() WHERE orgid=? AND memid=?")) {
             ps.setInt(1, actor.getId());
@@ -201,7 +210,10 @@ public class Organisation extends CertificateOwner {
         }
     }
 
-    public void updateCertData(String o, String c, String st, String l) {
+    public void updateCertData(String o, CountryCode c, String st, String l) throws GigiApiException {
+        if (c == null || c.getCountryCodeType() != CountryCodeType.CODE_2_CHARS) {
+            throw new GigiApiException("Got country code of illegal type.");
+        }
         for (Certificate cert : getCertificates(false)) {
             if (cert.getStatus() == CertificateStatus.ISSUED) {
                 cert.revoke();
@@ -209,7 +221,7 @@ public class Organisation extends CertificateOwner {
         }
         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `organisations` SET `name`=?, `state`=?, `province`=?, `city`=? WHERE `id`=?")) {
             ps.setString(1, o);
-            ps.setString(2, c);
+            ps.setString(2, c.getCountryCode());
             ps.setString(3, st);
             ps.setString(4, l);
             ps.setInt(5, getId());
@@ -253,4 +265,9 @@ public class Organisation extends CertificateOwner {
     public boolean isSelfOrganisation() {
         return SELF_ORG_NAME.equals(getName());
     }
+
+    private void writeObject(ObjectOutputStream oos) throws IOException {}
+
+    private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {}
+
 }