]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/Country.java
fix: ResultSet.getDate is often wrong as it fetches day-precision times
[gigi.git] / src / org / cacert / gigi / dbObjects / Country.java
1 package org.cacert.gigi.dbObjects;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.LinkedList;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.RandomAccess;
10
11 import org.cacert.gigi.GigiApiException;
12 import org.cacert.gigi.database.GigiPreparedStatement;
13 import org.cacert.gigi.database.GigiResultSet;
14 import org.cacert.gigi.output.template.SprintfCommand;
15
16 /**
17  * Represents a country containing its ISO 3166-1-Code and its English name.
18  */
19 public class Country {
20
21     public enum CountryCodeType {
22         CODE_2_CHARS(2), //
23         CODE_3_CHARS(3); //
24
25         private final int len;
26
27         private CountryCodeType(int len) {
28             this.len = len;
29         }
30
31         public int getLen() {
32             return len;
33         }
34     }
35
36     /**
37      * Id of the database entry.
38      */
39     private final int id;
40
41     /**
42      * English name of the country.
43      */
44     private final String country;
45
46     /**
47      * ISO 3166-1 alpha-2 code of the country.
48      */
49     private final String countryCode2;
50
51     /**
52      * ISO 3166-1 alpha-3 code of the country.
53      */
54     private final String countryCode3;
55
56     /**
57      * A unmodifiable {@link RandomAccess}-List of all Countries.
58      */
59     private static final List<Country> countries;
60
61     /**
62      * An unmodifiable index of all 2- and 3-letter country codes.
63      */
64     private static final Map<String, Country> byString;
65
66     static {
67         LinkedList<Country> cs = new LinkedList<>();
68         HashMap<String, Country> ccd = new HashMap<>();
69         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id`, `english` as country, `code2`, `code3` FROM `countryIsoCode`", true)) {
70             GigiResultSet rs = ps.executeQuery();
71             while (rs.next()) {
72                 Country e = new Country(rs);
73                 ccd.put(e.countryCode2, e);
74                 ccd.put(e.countryCode3, e);
75                 cs.add(e);
76             }
77         }
78         countries = Collections.unmodifiableList(new ArrayList<>(cs));
79         byString = Collections.unmodifiableMap(ccd);
80     }
81
82     private Country(GigiResultSet rs) {
83         this.id = rs.getInt("id");
84         this.country = rs.getString("country");
85         this.countryCode2 = rs.getString("code2");
86         this.countryCode3 = rs.getString("code3");
87     }
88
89     public int getId() {
90         return id;
91     }
92
93     public String getName() {
94         return country;
95     }
96
97     /**
98      * Returns the default (ISO 3166-1 alpha-2) country code of this country.
99      * 
100      * @return the country code
101      */
102     public String getCode() {
103         return countryCode2;
104     }
105
106     /**
107      * Gets the specified type of country code for this country.
108      * 
109      * @param type
110      *            the type of the code
111      * @return the corresponding code
112      */
113     public String getCode(CountryCodeType type) {
114         switch (type) {
115         case CODE_2_CHARS:
116             return countryCode2;
117         case CODE_3_CHARS:
118             return countryCode3;
119         default:
120             throw new IllegalArgumentException("Enum switch was non-exhaustive");
121         }
122     }
123
124     /**
125      * Gets an unmodifiable, {@link RandomAccess}-List of all countries.
126      * 
127      * @return the list.
128      */
129     public static List<Country> getCountries() {
130         return countries;
131     }
132
133     /**
134      * Checks a country code for its validity and conformance to the given type.
135      * 
136      * @param countrycode
137      *            the code to check
138      * @param cType
139      *            the type it should have
140      * @throws GigiApiException
141      *             if the code was wrong
142      */
143     public static void checkCountryCode(String countrycode, CountryCodeType cType) throws GigiApiException {
144         getCountryByCode(countrycode, cType);
145     }
146
147     /**
148      * Fetches the {@link Country} object for the given country code.
149      * 
150      * @param countrycode
151      *            the code to fetch the county for
152      * @param cType
153      *            the type of the code
154      * @return the specified country
155      * @throws GigiApiException
156      *             if the code was wrong.
157      */
158     public static Country getCountryByCode(String countrycode, CountryCodeType cType) throws GigiApiException {
159         if (countrycode.length() != cType.getLen()) {
160             throw new GigiApiException(SprintfCommand.createSimple("Country code length does not have the required length of {0} characters", Integer.toString(cType.getLen())));
161         }
162         Country i = byString.get(countrycode);
163         if (i == null) {
164             throw new GigiApiException("Country Code was wrong.");
165         }
166         return i;
167     }
168
169 }