1 package org.cacert.gigi.dbObjects;
3 import java.util.Arrays;
4 import java.util.Collections;
5 import java.util.HashMap;
7 import java.util.Random;
9 import org.cacert.gigi.GigiApiException;
10 import org.cacert.gigi.database.GigiPreparedStatement;
11 import org.cacert.gigi.database.GigiResultSet;
12 import org.cacert.gigi.output.template.SprintfCommand;
14 public class CountryCode {
16 public enum CountryCodeType {
18 "SELECT `id`, `english` as country, `code2` as countrycode FROM `countryIsoCode` ORDER BY code2"), //
20 "SELECT `id`, `english` as country, `code3` as countrycode FROM `countryIsoCode` ORDER BY code3"); //
22 private final String listQuery;
24 private final int len;
26 private CountryCodeType(int len, String listQuery) {
28 this.listQuery = listQuery;
35 protected String getListQuery() {
42 private final String country;
44 private final String countryCode;
46 private final CountryCodeType ctype;
48 private static final CountryCode[] c2s;
50 private static final CountryCode[] c3s;
52 private static final Map<String, CountryCode> byString;
55 c2s = getCountryCodesFromDB(CountryCodeType.CODE_2_CHARS);
56 c3s = getCountryCodesFromDB(CountryCodeType.CODE_3_CHARS);
57 HashMap<String, CountryCode> ccd = new HashMap<>();
58 for (CountryCode c2 : c2s) {
59 ccd.put(c2.getCountryCode(), c2);
61 for (CountryCode c3 : c3s) {
62 ccd.put(c3.getCountryCode(), c3);
64 byString = Collections.unmodifiableMap(ccd);
65 } catch (GigiApiException e) {
70 private CountryCode(int id, String country, String countryCode, CountryCodeType ctype) {
72 this.country = country;
73 this.countryCode = countryCode;
81 public String getCountry() {
85 public String getCountryCode() {
89 public CountryCodeType getCountryCodeType() {
93 public static CountryCode[] getCountryCodes(CountryCodeType clength) {
96 return Arrays.copyOf(c2s, c2s.length);
98 return Arrays.copyOf(c3s, c3s.length);
100 throw new Error("Enum switch was not exhaustive.");
103 private static CountryCode[] getCountryCodesFromDB(CountryCodeType clength) throws GigiApiException {
104 try (GigiPreparedStatement ps = new GigiPreparedStatement(clength.getListQuery(), true)) {
105 GigiResultSet rs = ps.executeQuery();
108 int totalCount = rs.getRow();
112 CountryCode[] finalResult = new CountryCode[totalCount];
114 finalResult[i] = new CountryCode(rs.getInt("id"), rs.getString("country"), rs.getString("countrycode"), clength);
122 public static void checkCountryCode(String countrycode, CountryCodeType cType) throws GigiApiException {
123 getCountryCode(countrycode, cType);
126 public CountryCode convertToCountryCodeType(CountryCodeType ctype) {
127 if (this.ctype.equals(ctype)) {
130 CountryCode[] cclist = getCountryCodes(ctype);
131 for (CountryCode cc : cclist) {
132 if (cc.getId() == this.getId()) {
136 throw new RuntimeException("Internal Error: CountryCode for country not found" + this.getCountry());
139 public static CountryCode getCountryCode(String countrycode, CountryCodeType cType) throws GigiApiException {
140 if (countrycode.length() != cType.getLen()) {
141 throw new GigiApiException(SprintfCommand.createSimple("Country code length does not have the required length of {0} characters", Integer.toString(cType.getLen())));
143 CountryCode i = byString.get(countrycode);
144 if (i == null || i.getCountryCodeType() != cType) {
145 throw new GigiApiException("Country Code was wrong.");
150 public static CountryCode getRandomCountry(CountryCodeType cType) {
151 CountryCode[] cc = CountryCode.getCountryCodes(cType);
152 int rnd = new Random().nextInt(cc.length);