]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/CountryCode.java
upd: narrowing type-safety around Organisation
[gigi.git] / src / org / cacert / gigi / dbObjects / CountryCode.java
1 package org.cacert.gigi.dbObjects;
2
3 import java.util.Arrays;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.Map;
7 import java.util.Random;
8
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;
13
14 public class CountryCode {
15
16     public enum CountryCodeType {
17         CODE_2_CHARS(2, //
18                 "SELECT `id`, `english` as country, `code2` as countrycode FROM `countryIsoCode` ORDER BY code2"), //
19         CODE_3_CHARS(3,//
20                 "SELECT `id`, `english` as country, `code3` as countrycode FROM `countryIsoCode` ORDER BY code3"); //
21
22         private final String listQuery;
23
24         private final int len;
25
26         private CountryCodeType(int len, String listQuery) {
27             this.len = len;
28             this.listQuery = listQuery;
29         }
30
31         public int getLen() {
32             return len;
33         }
34
35         protected String getListQuery() {
36             return listQuery;
37         }
38     }
39
40     private final int id;
41
42     private final String country;
43
44     private final String countryCode;
45
46     private final CountryCodeType ctype;
47
48     private static final CountryCode[] c2s;
49
50     private static final CountryCode[] c3s;
51
52     private static final Map<String, CountryCode> byString;
53     static {
54         try {
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);
60             }
61             for (CountryCode c3 : c3s) {
62                 ccd.put(c3.getCountryCode(), c3);
63             }
64             byString = Collections.unmodifiableMap(ccd);
65         } catch (GigiApiException e) {
66             throw new Error(e);
67         }
68     }
69
70     private CountryCode(int id, String country, String countryCode, CountryCodeType ctype) {
71         this.id = id;
72         this.country = country;
73         this.countryCode = countryCode;
74         this.ctype = ctype;
75     }
76
77     public int getId() {
78         return id;
79     }
80
81     public String getCountry() {
82         return country;
83     }
84
85     public String getCountryCode() {
86         return countryCode;
87     }
88
89     public CountryCodeType getCountryCodeType() {
90         return ctype;
91     }
92
93     public static CountryCode[] getCountryCodes(CountryCodeType clength) {
94         switch (clength) {
95         case CODE_2_CHARS:
96             return Arrays.copyOf(c2s, c2s.length);
97         case CODE_3_CHARS:
98             return Arrays.copyOf(c3s, c3s.length);
99         }
100         throw new Error("Enum switch was not exhaustive.");
101     }
102
103     private static CountryCode[] getCountryCodesFromDB(CountryCodeType clength) throws GigiApiException {
104         try (GigiPreparedStatement ps = new GigiPreparedStatement(clength.getListQuery(), true)) {
105             GigiResultSet rs = ps.executeQuery();
106
107             rs.last();
108             int totalCount = rs.getRow();
109             rs.beforeFirst();
110             int i = 0;
111
112             CountryCode[] finalResult = new CountryCode[totalCount];
113             while (rs.next()) {
114                 finalResult[i] = new CountryCode(rs.getInt("id"), rs.getString("country"), rs.getString("countrycode"), clength);
115                 i += 1;
116             }
117
118             return finalResult;
119         }
120     }
121
122     public static void checkCountryCode(String countrycode, CountryCodeType cType) throws GigiApiException {
123         getCountryCode(countrycode, cType);
124     }
125
126     public CountryCode convertToCountryCodeType(CountryCodeType ctype) {
127         if (this.ctype.equals(ctype)) {
128             return this;
129         }
130         CountryCode[] cclist = getCountryCodes(ctype);
131         for (CountryCode cc : cclist) {
132             if (cc.getId() == this.getId()) {
133                 return cc;
134             }
135         }
136         throw new RuntimeException("Internal Error: CountryCode for country not found" + this.getCountry());
137     }
138
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())));
142         }
143         CountryCode i = byString.get(countrycode);
144         if (i == null || i.getCountryCodeType() != cType) {
145             throw new GigiApiException("Country Code was wrong.");
146         }
147         return i;
148     }
149
150     public static CountryCode getRandomCountry(CountryCodeType cType) {
151         CountryCode[] cc = CountryCode.getCountryCodes(cType);
152         int rnd = new Random().nextInt(cc.length);
153         return cc[rnd];
154     }
155
156 }