From: Felix Dörre Date: Sun, 14 Aug 2016 08:36:25 +0000 (+0200) Subject: upd: make CountryCode a Multiton X-Git-Url: https://code.wpia.club/?p=gigi.git;a=commitdiff_plain;h=5ba19c21224ab3bf365ed803497f940cc995117b upd: make CountryCode a Multiton Change-Id: Icae85456f4b822fe67d3b5b4473de8ced7accfc1 --- diff --git a/src/org/cacert/gigi/dbObjects/CountryCode.java b/src/org/cacert/gigi/dbObjects/CountryCode.java index 002ae06c..5468bcf6 100644 --- a/src/org/cacert/gigi/dbObjects/CountryCode.java +++ b/src/org/cacert/gigi/dbObjects/CountryCode.java @@ -1,5 +1,9 @@ package org.cacert.gigi.dbObjects; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import java.util.Random; import org.cacert.gigi.GigiApiException; @@ -11,44 +15,26 @@ public class CountryCode { public enum CountryCodeType { CODE_2_CHARS(2, // - "SELECT `id`, `english` as country, `code2` as countrycode FROM `countryIsoCode` ORDER BY code2",// - "SELECT `id`, `english` as country, `code2` as countrycode FROM `countryIsoCode` WHERE `code2`=?",// - "SELECT 1 FROM `countryIsoCode` WHERE `code2`=?"),// + "SELECT `id`, `english` as country, `code2` as countrycode FROM `countryIsoCode` ORDER BY code2"), // CODE_3_CHARS(3,// - "SELECT `id`, `english` as country, `code3` as countrycode FROM `countryIsoCode` ORDER BY code3", // - "SELECT `id`, `english` as country, `code3` as countrycode FROM `countryIsoCode` WHERE `code3`=?",// - "SELECT 1 FROM `countryIsoCode` WHERE `code3`=?"); + "SELECT `id`, `english` as country, `code3` as countrycode FROM `countryIsoCode` ORDER BY code3"); // private final String listQuery; - private final String getQuery; - - private final String validationQuery; - private final int len; - private CountryCodeType(int len, String listQuery, String getQuery, String validationQuery) { + private CountryCodeType(int len, String listQuery) { this.len = len; this.listQuery = listQuery; - this.getQuery = getQuery; - this.validationQuery = validationQuery; } public int getLen() { return len; } - public String getGetQuery() { - return getQuery; - } - - public String getListQuery() { + protected String getListQuery() { return listQuery; } - - public String getValidationQuery() { - return validationQuery; - } } private final int id; @@ -59,6 +45,28 @@ public class CountryCode { private final CountryCodeType ctype; + private static final CountryCode[] c2s; + + private static final CountryCode[] c3s; + + private static final Map byString; + static { + try { + c2s = getCountryCodesFromDB(CountryCodeType.CODE_2_CHARS); + c3s = getCountryCodesFromDB(CountryCodeType.CODE_3_CHARS); + HashMap ccd = new HashMap<>(); + for (CountryCode c2 : c2s) { + ccd.put(c2.getCountryCode(), c2); + } + for (CountryCode c3 : c3s) { + ccd.put(c3.getCountryCode(), c3); + } + byString = Collections.unmodifiableMap(ccd); + } catch (GigiApiException e) { + throw new Error(e); + } + } + private CountryCode(int id, String country, String countryCode, CountryCodeType ctype) { this.id = id; this.country = country; @@ -83,6 +91,16 @@ public class CountryCode { } public static CountryCode[] getCountryCodes(CountryCodeType clength) { + switch (clength) { + case CODE_2_CHARS: + return Arrays.copyOf(c2s, c2s.length); + case CODE_3_CHARS: + return Arrays.copyOf(c3s, c3s.length); + } + throw new Error("Enum switch was not exhaustive."); + } + + private static CountryCode[] getCountryCodesFromDB(CountryCodeType clength) throws GigiApiException { try (GigiPreparedStatement ps = new GigiPreparedStatement(clength.getListQuery(), true)) { GigiResultSet rs = ps.executeQuery(); @@ -102,54 +120,33 @@ public class CountryCode { } 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())); - } - } - } - - public static CountryCode getCountryCode(String countrycode) throws GigiApiException { - return getCountryCode(countrycode, CountryCodeType.CODE_2_CHARS); - } - - public static CountryCode getCountryCode(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.getGetQuery())) { - 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())); - } - return new CountryCode(rs.getInt("id"), rs.getString("country"), rs.getString("countrycode"), cType); - } + getCountryCode(countrycode, cType); } public CountryCode convertToCountryCodeType(CountryCodeType ctype) { if (this.ctype.equals(ctype)) { return this; } - CountryCode[] cclist = getCountryCodes(ctype); for (CountryCode cc : cclist) { if (cc.getId() == this.getId()) { return cc; } } - throw new RuntimeException("Internal Error: CountryCode for country not found" + this.getCountry()); } + public static CountryCode getCountryCode(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()))); + } + CountryCode i = byString.get(countrycode); + if (i == null || i.getCountryCodeType() != cType) { + throw new GigiApiException("Country Code was wrong."); + } + return i; + } + public static CountryCode getRandomCountry(CountryCodeType cType) { CountryCode[] cc = CountryCode.getCountryCodes(cType); int rnd = new Random().nextInt(cc.length); diff --git a/src/org/cacert/gigi/pages/orga/CreateOrgForm.java b/src/org/cacert/gigi/pages/orga/CreateOrgForm.java index e54eaecd..5f0b4774 100644 --- a/src/org/cacert/gigi/pages/orga/CreateOrgForm.java +++ b/src/org/cacert/gigi/pages/orga/CreateOrgForm.java @@ -7,6 +7,7 @@ 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; @@ -51,7 +52,7 @@ public class CreateOrgForm extends Form { CountryCode orgState = null; try { - orgState = CountryCode.getCountryCode(t.getState()); + orgState = CountryCode.getCountryCode(t.getState(), CountryCodeType.CODE_2_CHARS); } catch (GigiApiException e) { throw new Error(e); }