From: INOPIAE Date: Sat, 9 Jul 2016 10:46:05 +0000 (+0200) Subject: add: class dbObjects/CountryCode to handle country code in organisations X-Git-Url: https://code.wpia.club/?p=gigi.git;a=commitdiff_plain;h=bb73860be30af60fdcc26709066d932f91cb02fc add: class dbObjects/CountryCode to handle country code in organisations Change-Id: I43dbde2566eb7b701a76cd75f5f9f45d3225d8ed --- diff --git a/src/org/cacert/gigi/dbObjects/CountryCode.java b/src/org/cacert/gigi/dbObjects/CountryCode.java new file mode 100644 index 00000000..bf9375ce --- /dev/null +++ b/src/org/cacert/gigi/dbObjects/CountryCode.java @@ -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())); + } + } + + } +} diff --git a/src/org/cacert/gigi/pages/orga/CreateOrgForm.java b/src/org/cacert/gigi/pages/orga/CreateOrgForm.java index 57f39d62..6c30138c 100644 --- a/src/org/cacert/gigi/pages/orga/CreateOrgForm.java +++ b/src/org/cacert/gigi/pages/orga/CreateOrgForm.java @@ -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 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); } diff --git a/src/org/cacert/gigi/pages/orga/CreateOrgForm.templ b/src/org/cacert/gigi/pages/orga/CreateOrgForm.templ index bd0f74de..761bd78b 100644 --- a/src/org/cacert/gigi/pages/orga/CreateOrgForm.templ +++ b/src/org/cacert/gigi/pages/orga/CreateOrgForm.templ @@ -26,7 +26,12 @@ : - + + 'ISO code!'')?> diff --git a/tests/org/cacert/gigi/pages/orga/TestOrgManagement.java b/tests/org/cacert/gigi/pages/orga/TestOrgManagement.java index 6b6d567d..4e606ae7 100644 --- a/tests/org/cacert/gigi/pages/orga/TestOrgManagement.java +++ b/tests/org/cacert/gigi/pages/orga/TestOrgManagement.java @@ -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()));