]> WPIA git - gigi.git/commitdiff
upd: narrowing type-safety around Organisation
authorFelix Dörre <felix@dogcraft.de>
Sun, 14 Aug 2016 17:20:41 +0000 (19:20 +0200)
committerFelix Dörre <felix@dogcraft.de>
Tue, 16 Aug 2016 18:23:08 +0000 (20:23 +0200)
Change-Id: I60b86d46a6a1c580e86826dabc0470524258249b

src/org/cacert/gigi/dbObjects/CertificateOwner.java
src/org/cacert/gigi/dbObjects/Organisation.java
src/org/cacert/gigi/output/CountrySelector.java
src/org/cacert/gigi/pages/account/certs/CertificateRequest.java
src/org/cacert/gigi/pages/orga/CreateOrgForm.java
src/org/cacert/gigi/pages/orga/ViewOrgPage.java
tests/org/cacert/gigi/pages/orga/TestOrgManagement.java

index daff8bcb787866d631c83793f3794876c3beccf9..cc96ade7c39a5f3e532986cf3ed68a5378f0f24b 100644 (file)
@@ -8,6 +8,7 @@ import java.io.Serializable;
 import java.util.LinkedList;
 import java.util.List;
 
 import java.util.LinkedList;
 import java.util.List;
 
+import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.database.GigiPreparedStatement;
 import org.cacert.gigi.database.GigiResultSet;
 
 import org.cacert.gigi.database.GigiPreparedStatement;
 import org.cacert.gigi.database.GigiResultSet;
 
@@ -51,6 +52,8 @@ public abstract class CertificateOwner implements IdCachable, Serializable {
                     } else {
                         System.err.print("Malformed cert owner: " + id);
                     }
                     } else {
                         System.err.print("Malformed cert owner: " + id);
                     }
+                } catch (GigiApiException e) {
+                    throw new Error(e);
                 }
             }
         }
                 }
             }
         }
index bbb3214c84ad2408b690cdf9b41d2eab43c83863..526bf303c710f1785f70f044b4ae9d947ba3cefc 100644 (file)
@@ -10,6 +10,7 @@ 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.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 {
 import org.cacert.gigi.dbObjects.wrappers.DataContainer;
 
 public class Organisation extends CertificateOwner {
@@ -53,7 +54,7 @@ public class Organisation extends CertificateOwner {
 
     private String name;
 
 
     private String name;
 
-    private String state;
+    private CountryCode state;
 
     private String province;
 
 
     private String province;
 
@@ -69,8 +70,11 @@ public class Organisation extends CertificateOwner {
         if ( !creator.isInGroup(Group.ORGASSURER)) {
             throw new GigiApiException("Only Organisation RA Agents may create organisations.");
         }
         if ( !creator.isInGroup(Group.ORGASSURER)) {
             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.name = name;
-        this.state = state.getCountryCode();
+        this.state = state;
         this.province = province;
         this.city = city;
         this.email = email;
         this.province = province;
         this.city = city;
         this.email = email;
@@ -93,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");
         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");
         province = rs.getString("province");
         city = rs.getString("city");
         email = rs.getString("contactEmail");
@@ -108,7 +112,7 @@ public class Organisation extends CertificateOwner {
         return name;
     }
 
         return name;
     }
 
-    public String getState() {
+    public CountryCode getState() {
         return state;
     }
 
         return state;
     }
 
@@ -206,7 +210,10 @@ public class Organisation extends CertificateOwner {
         }
     }
 
         }
     }
 
-    public void updateCertData(String o, CountryCode 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();
         for (Certificate cert : getCertificates(false)) {
             if (cert.getStatus() == CertificateStatus.ISSUED) {
                 cert.revoke();
@@ -221,7 +228,7 @@ public class Organisation extends CertificateOwner {
             ps.executeUpdate();
         }
         name = o;
             ps.executeUpdate();
         }
         name = o;
-        state = c.getCountryCode();
+        state = c;
         province = st;
         city = l;
     }
         province = st;
         city = l;
     }
index 95e373bacf266cafcd2bc0748452de80bd1dcfaf..5bd4b63243641504d0552a4bf8b2b5d1d8615d13 100644 (file)
@@ -32,6 +32,10 @@ public class CountrySelector implements Outputable {
     public CountrySelector(String name, boolean optional, CountryCode state) {
         this(name, optional);
         selected = state == null ? null : state.convertToCountryCodeType(CountryCodeType.CODE_2_CHARS);
     public CountrySelector(String name, boolean optional, CountryCode state) {
         this(name, optional);
         selected = state == null ? null : state.convertToCountryCodeType(CountryCodeType.CODE_2_CHARS);
+        if (state.getCountryCodeType() != CountryCodeType.CODE_2_CHARS) {
+            throw new IllegalArgumentException("Got country code of illegal type.");
+        }
+        selected = state;
     }
 
     public void update(HttpServletRequest r) throws GigiApiException {
     }
 
     public void update(HttpServletRequest r) throws GigiApiException {
index bae43d589e6fae2dab514b3b925872e6289856cf..41c7e84e3b8c90546ab16edaf61e4885672e10ae 100644 (file)
@@ -423,7 +423,7 @@ public class CertificateRequest {
         if (ctx.getTarget() instanceof Organisation) {
             Organisation org = (Organisation) ctx.getTarget();
             subject.put("O", org.getName());
         if (ctx.getTarget() instanceof Organisation) {
             Organisation org = (Organisation) ctx.getTarget();
             subject.put("O", org.getName());
-            subject.put("C", org.getState());
+            subject.put("C", org.getState().getCountryCode());
             subject.put("ST", org.getProvince());
             subject.put("L", org.getCity());
             if (ou != null) {
             subject.put("ST", org.getProvince());
             subject.put("L", org.getCity());
             if (ou != null) {
index 5f0b47746d5cada6835fcc62bbbc4a4931334fbf..8a9c52db2805309d0cefb60c4c407b9863f5876e 100644 (file)
@@ -7,7 +7,6 @@ import javax.servlet.http.HttpServletRequest;
 
 import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.dbObjects.CountryCode;
 
 import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.dbObjects.CountryCode;
-import org.cacert.gigi.dbObjects.CountryCode.CountryCodeType;
 import org.cacert.gigi.dbObjects.Organisation;
 import org.cacert.gigi.email.EmailProvider;
 import org.cacert.gigi.localisation.Language;
 import org.cacert.gigi.dbObjects.Organisation;
 import org.cacert.gigi.email.EmailProvider;
 import org.cacert.gigi.localisation.Language;
@@ -50,13 +49,7 @@ public class CreateOrgForm extends Form {
         result = t;
         o = t.getName();
 
         result = t;
         o = t.getName();
 
-        CountryCode orgState = null;
-        try {
-            orgState = CountryCode.getCountryCode(t.getState(), CountryCodeType.CODE_2_CHARS);
-        } catch (GigiApiException e) {
-            throw new Error(e);
-        }
-        cs = new CountrySelector("C", false, orgState);
+        cs = new CountrySelector("C", false, t.getState());
 
         st = t.getProvince();
         l = t.getCity();
 
         st = t.getProvince();
         l = t.getCity();
index 98f7635362b21c24fe92bd32304b7a74bbe8ef35..d5839013222d6a70d543970bc12a77d7c414c141 100644 (file)
@@ -132,7 +132,7 @@ public class ViewOrgPage extends Page {
                 Organisation org = orgas[count++];
                 vars.put("id", Integer.toString(org.getId()));
                 vars.put("name", org.getName());
                 Organisation org = orgas[count++];
                 vars.put("id", Integer.toString(org.getId()));
                 vars.put("name", org.getName());
-                vars.put("country", org.getState());
+                vars.put("country", org.getState().getCountryCode());
                 return true;
             }
         };
                 return true;
             }
         };
index c8f3d11972dcab9fd2ea6041bb6d9931fdb738a6..dc922907cf956a1c334792dbeadd8bc2ab9c2975 100644 (file)
@@ -149,7 +149,7 @@ public class TestOrgManagement extends OrgTest {
         Organisation o1 = createUniqueOrg();
         o1.updateCertData("name", CountryCode.getCountryCode("DE", CountryCodeType.CODE_2_CHARS), DIFFICULT_CHARS, "Köln");
         assertEquals("name", o1.getName());
         Organisation o1 = createUniqueOrg();
         o1.updateCertData("name", CountryCode.getCountryCode("DE", CountryCodeType.CODE_2_CHARS), DIFFICULT_CHARS, "Köln");
         assertEquals("name", o1.getName());
-        assertEquals("DE", o1.getState());
+        assertEquals("DE", o1.getState().getCountryCode());
         assertEquals(DIFFICULT_CHARS, o1.getProvince());
         assertEquals("Köln", o1.getCity());
         o1.delete();
         assertEquals(DIFFICULT_CHARS, o1.getProvince());
         assertEquals("Köln", o1.getCity());
         o1.delete();
@@ -179,25 +179,25 @@ public class TestOrgManagement extends OrgTest {
         String s128 = str128;
         String s129 = str128 + "a";
 
         String s128 = str128;
         String s129 = str128 + "a";
 
-        assertNull(upCertData(o1, o1.getName(), o1.getState(), o1.getProvince(), o1.getCity()));
+        assertNull(upCertData(o1, o1.getName(), null, o1.getProvince(), o1.getCity()));
 
         // test organisation name
 
         // test organisation name
-        assertNotNull(upCertData(o1, "", o1.getState(), o1.getProvince(), o1.getCity()));
-        assertNull(upCertData(o1, "A", o1.getState(), o1.getProvince(), o1.getCity()));
-        assertNull(upCertData(o1, s64, o1.getState(), o1.getProvince(), o1.getCity()));
-        assertNotNull(upCertData(o1, s65, o1.getState(), o1.getProvince(), o1.getCity()));
+        assertNotNull(upCertData(o1, "", null, o1.getProvince(), o1.getCity()));
+        assertNull(upCertData(o1, "A", null, o1.getProvince(), o1.getCity()));
+        assertNull(upCertData(o1, s64, null, o1.getProvince(), o1.getCity()));
+        assertNotNull(upCertData(o1, s65, null, o1.getProvince(), o1.getCity()));
 
         // test state
 
         // test state
-        assertNotNull(upCertData(o1, o1.getName(), o1.getState(), se, o1.getCity()));
-        assertNull(upCertData(o1, o1.getName(), o1.getState(), "A", o1.getCity()));
-        assertNull(upCertData(o1, o1.getName(), o1.getState(), s128, o1.getCity()));
-        assertNotNull(upCertData(o1, o1.getName(), o1.getState(), s129, o1.getCity()));
+        assertNotNull(upCertData(o1, o1.getName(), null, se, o1.getCity()));
+        assertNull(upCertData(o1, o1.getName(), null, "A", o1.getCity()));
+        assertNull(upCertData(o1, o1.getName(), null, s128, o1.getCity()));
+        assertNotNull(upCertData(o1, o1.getName(), null, s129, o1.getCity()));
 
         // test town
 
         // test town
-        assertNotNull(upCertData(o1, o1.getName(), o1.getState(), o1.getProvince(), se));
-        assertNull(upCertData(o1, o1.getName(), o1.getState(), o1.getProvince(), "A"));
-        assertNull(upCertData(o1, o1.getName(), o1.getState(), o1.getProvince(), s128));
-        assertNotNull(upCertData(o1, o1.getName(), o1.getState(), o1.getProvince(), s129));
+        assertNotNull(upCertData(o1, o1.getName(), null, o1.getProvince(), se));
+        assertNull(upCertData(o1, o1.getName(), null, o1.getProvince(), "A"));
+        assertNull(upCertData(o1, o1.getName(), null, o1.getProvince(), s128));
+        assertNotNull(upCertData(o1, o1.getName(), null, o1.getProvince(), s129));
 
         // test country
         assertNotNull(upCertData(o1, o1.getName(), "", o1.getProvince(), o1.getCity()));
 
         // test country
         assertNotNull(upCertData(o1, o1.getName(), "", o1.getProvince(), o1.getCity()));
@@ -236,7 +236,8 @@ public class TestOrgManagement extends OrgTest {
      * @param o
      *            the new name
      * @param c
      * @param o
      *            the new name
      * @param c
-     *            the new country
+     *            the new country or <code>null</code> to keep the current
+     *            country.
      * @param province
      *            the new "province/state"
      * @param ct
      * @param province
      *            the new "province/state"
      * @param ct
@@ -244,6 +245,9 @@ public class TestOrgManagement extends OrgTest {
      * @return an error message or <code>null</code>
      */
     private String upCertData(Organisation o1, String o, String c, String province, String ct) throws IOException, MalformedURLException, UnsupportedEncodingException {
      * @return an error message or <code>null</code>
      */
     private String upCertData(Organisation o1, String o, String c, String province, String ct) throws IOException, MalformedURLException, UnsupportedEncodingException {
+        if (c == null) {
+            c = o1.getState().getCountryCode();
+        }
         return executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId(), "action=updateCertificateData&O=" + o + "&C=" + c + "&ST=" + province + "&L=" + ct, 0);
     }
 
         return executeBasicWebInteraction(cookie, ViewOrgPage.DEFAULT_PATH + "/" + o1.getId(), "action=updateCertificateData&O=" + o + "&C=" + c + "&ST=" + province + "&L=" + ct, 0);
     }