]> WPIA git - gigi.git/commitdiff
add: class dbObjects/CountryCode to handle country code in organisations
authorINOPIAE <m.maengel@inopiae.de>
Sat, 9 Jul 2016 10:46:05 +0000 (12:46 +0200)
committerFelix Dörre <felix@dogcraft.de>
Sat, 9 Jul 2016 18:58:39 +0000 (20:58 +0200)
Change-Id: I43dbde2566eb7b701a76cd75f5f9f45d3225d8ed

src/org/cacert/gigi/dbObjects/CountryCode.java [new file with mode: 0644]
src/org/cacert/gigi/pages/orga/CreateOrgForm.java
src/org/cacert/gigi/pages/orga/CreateOrgForm.templ
tests/org/cacert/gigi/pages/orga/TestOrgManagement.java

diff --git a/src/org/cacert/gigi/dbObjects/CountryCode.java b/src/org/cacert/gigi/dbObjects/CountryCode.java
new file mode 100644 (file)
index 0000000..bf9375c
--- /dev/null
@@ -0,0 +1,96 @@
+package org.cacert.gigi.dbObjects;
+
+import org.cacert.gigi.GigiApiException;
+import org.cacert.gigi.database.GigiPreparedStatement;
+import org.cacert.gigi.database.GigiResultSet;
+import org.cacert.gigi.output.template.SprintfCommand;
+
+public class CountryCode {
+
+    public enum CountryCodeType {
+        CODE_2_CHARS(2, "SELECT `id`, `english` as country, `code2` as countrycode FROM `countryIsoCode` ORDER BY code2", "SELECT 1 FROM `countryIsoCode` WHERE `code2`=?"),//
+        CODE_3_CHARS(3, "SELECT `id`, `english` as country, `code3` as countrycode FROM `countryIsoCode` ORDER BY code3", "SELECT 1 FROM `countryIsoCode` WHERE `code3`=?");
+
+        private final String listQuery;
+
+        private final String validationQuery;
+
+        private final int len;
+
+        private CountryCodeType(int len, String listQuery, String validationQuery) {
+            this.len = len;
+            this.listQuery = listQuery;
+            this.validationQuery = validationQuery;
+        }
+
+        public int getLen() {
+            return len;
+        }
+
+        public String getListQuery() {
+            return listQuery;
+        }
+
+        public String getValidationQuery() {
+            return validationQuery;
+        }
+    }
+
+    private final int id;
+
+    private final String country;
+
+    private final String countryCode;
+
+    public CountryCode(int id, String country, String countryCode) {
+        this.id = id;
+        this.country = country;
+        this.countryCode = countryCode;
+    }
+
+    public int getId() {
+        return id;
+    }
+
+    public String getCountry() {
+        return country;
+    }
+
+    public String getCountryCode() {
+        return countryCode;
+    }
+
+    public static CountryCode[] getCountryCodes(CountryCodeType clength) throws GigiApiException {
+        try (GigiPreparedStatement ps = new GigiPreparedStatement(clength.getListQuery(), true)) {
+            GigiResultSet rs = ps.executeQuery();
+
+            rs.last();
+            int totalCount = rs.getRow();
+            rs.beforeFirst();
+            int i = 0;
+
+            CountryCode[] finalResult = new CountryCode[totalCount];
+            while (rs.next()) {
+                finalResult[i] = new CountryCode(rs.getInt("id"), rs.getString("country"), rs.getString("countrycode"));
+                i += 1;
+            }
+
+            return finalResult;
+        }
+    }
+
+    public static void checkCountryCode(String countrycode, CountryCodeType cType) throws GigiApiException {
+        if (countrycode.length() != cType.getLen()) {
+            throw new GigiApiException(SprintfCommand.createSimple("Country code length does not have the required length of {0} characters", Integer.toString(cType.getLen())));
+        }
+        try (GigiPreparedStatement ps = new GigiPreparedStatement(cType.getValidationQuery())) {
+            ps.setString(1, countrycode.toUpperCase());
+            GigiResultSet rs = ps.executeQuery();
+
+            if ( !rs.next()) {
+                throw new GigiApiException(SprintfCommand.createSimple("Country code {0} is not available in database", countrycode.toUpperCase()));
+            }
+        }
+
+    }
+}
index 57f39d62c6a1e60e7dd7b9351f537ecea8d7809c..6c30138c62b366767a9afb4d9b8a2499bda9d20a 100644 (file)
@@ -6,10 +6,13 @@ import java.util.Map;
 import javax.servlet.http.HttpServletRequest;
 
 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.output.template.Form;
+import org.cacert.gigi.output.template.IterableDataset;
 import org.cacert.gigi.output.template.SprintfCommand;
 import org.cacert.gigi.output.template.Template;
 import org.cacert.gigi.pages.LoginPage;
@@ -36,12 +39,19 @@ public class CreateOrgForm extends Form {
 
     private boolean isEdit = false;
 
+    private CountryCode[] countryCode;
+
     public CreateOrgForm(HttpServletRequest hsr) {
         super(hsr);
+        try {
+            countryCode = CountryCode.getCountryCodes(CountryCodeType.CODE_2_CHARS);
+        } catch (GigiApiException e) {
+            throw new Error(e); // should not happen
+        }
     }
 
     public CreateOrgForm(HttpServletRequest hsr, Organisation t) {
-        super(hsr);
+        this(hsr);
         isEdit = true;
         result = t;
         o = t.getName();
@@ -90,16 +100,15 @@ public class CreateOrgForm extends Form {
 
     private void checkCertData(HttpServletRequest req) throws GigiApiException {
         o = extractParam(req, "O");
-        c = extractParam(req, "C");
+        c = extractParam(req, "C").toUpperCase();
         st = extractParam(req, "ST");
         l = extractParam(req, "L");
 
         if (o.length() > 64 || o.length() < 1) {
             throw new GigiApiException(SprintfCommand.createSimple("{0} not given or longer than {1} characters", "Organisation name", 64));
         }
-        if (c.length() != 2) {
-            throw new GigiApiException(SprintfCommand.createSimple("{0} not given or not exactly {1} characters long", "Country code", 2));
-        }
+
+        CountryCode.checkCountryCode(c, CountryCodeType.CODE_2_CHARS);
 
         if (st.length() > 128 || st.length() < 1) {
             throw new GigiApiException(SprintfCommand.createSimple("{0} not given or longer than {1} characters", "State/county", 128));
@@ -131,6 +140,28 @@ public class CreateOrgForm extends Form {
         vars.put("email", email);
         vars.put("optionalName", optionalName);
         vars.put("postalAddress", postalAddress);
+        vars.put("countryCode", new IterableDataset() {
+
+            int i = 0;
+
+            @Override
+            public boolean next(Language l, Map<String, Object> vars) {
+                if (i >= countryCode.length) {
+                    return false;
+                }
+                CountryCode t = countryCode[i++];
+                vars.put("id", t.getId());
+                vars.put("cc", t.getCountryCode());
+                vars.put("display", t.getCountry());
+                if (t.getCountryCode().equals(c)) {
+                    vars.put("selected", "selected");
+                } else {
+                    vars.put("selected", "");
+                }
+                return true;
+            }
+        });
+        // vars.put("countryCode", countryCode);
         if (isEdit) {
             vars.put("edit", true);
         }
index bd0f74dece7d19f20f7656d85a4f1ff9fb91df86..761bd78b0655fa0e6f4e05019b1110fef65727f3 100644 (file)
   </tr>
   <tr>
     <td><?=_Country?>:</td>
-    <td><input class="form-control" type="text" name="C" value="<?=$C?>" maxlength="2" size="5">
+    <td>
+      <select name="C">
+    <? foreach($countryCode) { ?>
+        <option value="<?=$cc?>" <?=$selected?> ><?=$cc?> - <?=$display?></option>
+    <? } ?>
+      </select>
       <?=_(2 letter !'<a href="http://www.iso.org/iso/home/standards/country_codes/iso-3166-1_decoding_table.htm">'ISO code!'</a>')?>
     </td>
   </tr>
index 6b6d567daba9a1bc065a904aead3107e178499c8..4e606ae7abdd79fdaeab6ce32b5c761e101ce3d8 100644 (file)
@@ -202,6 +202,10 @@ public class TestOrgManagement extends OrgTest {
         assertNotNull(upCertData(o1, o1.getName(), "D", o1.getProvince(), o1.getCity()));
         assertNull(upCertData(o1, o1.getName(), "DE", o1.getProvince(), o1.getCity()));
         assertNotNull(upCertData(o1, o1.getName(), "DES", o1.getProvince(), o1.getCity()));
+        // country code does not exist
+        assertNotNull(upCertData(o1, o1.getName(), "DD", o1.getProvince(), o1.getCity()));
+        // 3-letter country code should not be accepted
+        assertNotNull(upCertData(o1, o1.getName(), "DEU", o1.getProvince(), o1.getCity()));
 
         // test contact mail
         assertNull(upOptData(o1, o1.getContactEmail()));