]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/Country.java
upd: change CountryCode class into a Country class
[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.Random;
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 public class Country {
17
18     public enum CountryCodeType {
19         CODE_2_CHARS(2), //
20         CODE_3_CHARS(3); //
21
22         private final int len;
23
24         private CountryCodeType(int len) {
25             this.len = len;
26         }
27
28         public int getLen() {
29             return len;
30         }
31     }
32
33     private final int id;
34
35     private final String country;
36
37     private final String countryCode2;
38
39     private final String countryCode3;
40
41     private static final List<Country> countries;
42
43     private static final Map<String, Country> byString;
44     static {
45         LinkedList<Country> cs = new LinkedList<>();
46         HashMap<String, Country> ccd = new HashMap<>();
47         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id`, `english` as country, `code2`, `code3` FROM `countryIsoCode`", true)) {
48             GigiResultSet rs = ps.executeQuery();
49             while (rs.next()) {
50                 Country e = new Country(rs);
51                 ccd.put(e.countryCode2, e);
52                 ccd.put(e.countryCode3, e);
53                 cs.add(e);
54             }
55         }
56         countries = Collections.unmodifiableList(new ArrayList<>(cs));
57         byString = Collections.unmodifiableMap(ccd);
58     }
59
60     private Country(GigiResultSet rs) {
61         this.id = rs.getInt("id");
62         this.country = rs.getString("country");
63         this.countryCode2 = rs.getString("code2");
64         this.countryCode3 = rs.getString("code3");
65     }
66
67     public int getId() {
68         return id;
69     }
70
71     public String getName() {
72         return country;
73     }
74
75     public String getCode() {
76         return countryCode2;
77     }
78
79     public String getCode(CountryCodeType type) {
80         switch (type) {
81         case CODE_2_CHARS:
82             return countryCode2;
83         case CODE_3_CHARS:
84             return countryCode3;
85         default:
86             throw new IllegalArgumentException("Enum switch was non-exhaustive");
87         }
88     }
89
90     public static List<Country> getCountries() {
91         return countries;
92     }
93
94     public static void checkCountryCode(String countrycode, CountryCodeType cType) throws GigiApiException {
95         getCountryByCode(countrycode, cType);
96     }
97
98     public static Country getCountryByCode(String countrycode, CountryCodeType cType) throws GigiApiException {
99         if (countrycode.length() != cType.getLen()) {
100             throw new GigiApiException(SprintfCommand.createSimple("Country code length does not have the required length of {0} characters", Integer.toString(cType.getLen())));
101         }
102         Country i = byString.get(countrycode);
103         if (i == null) {
104             throw new GigiApiException("Country Code was wrong.");
105         }
106         return i;
107     }
108
109     public static Country getRandomCountry() {
110         List<Country> cc = Country.getCountries();
111         int rnd = new Random().nextInt(cc.size());
112         return cc.get(rnd);
113     }
114 }