1 package org.cacert.gigi.dbObjects;
3 import java.util.Random;
5 import org.cacert.gigi.GigiApiException;
6 import org.cacert.gigi.database.GigiPreparedStatement;
7 import org.cacert.gigi.database.GigiResultSet;
8 import org.cacert.gigi.output.template.SprintfCommand;
10 public class CountryCode {
12 public enum CountryCodeType {
14 "SELECT `id`, `english` as country, `code2` as countrycode FROM `countryIsoCode` ORDER BY code2",//
15 "SELECT `id`, `english` as country, `code2` as countrycode FROM `countryIsoCode` WHERE `code2`=?",//
16 "SELECT 1 FROM `countryIsoCode` WHERE `code2`=?"),//
18 "SELECT `id`, `english` as country, `code3` as countrycode FROM `countryIsoCode` ORDER BY code3", //
19 "SELECT `id`, `english` as country, `code3` as countrycode FROM `countryIsoCode` WHERE `code3`=?",//
20 "SELECT 1 FROM `countryIsoCode` WHERE `code3`=?");
22 private final String listQuery;
24 private final String getQuery;
26 private final String validationQuery;
28 private final int len;
30 private CountryCodeType(int len, String listQuery, String getQuery, String validationQuery) {
32 this.listQuery = listQuery;
33 this.getQuery = getQuery;
34 this.validationQuery = validationQuery;
41 public String getGetQuery() {
45 public String getListQuery() {
49 public String getValidationQuery() {
50 return validationQuery;
56 private final String country;
58 private final String countryCode;
60 private final CountryCodeType ctype;
62 private CountryCode(int id, String country, String countryCode, CountryCodeType ctype) {
64 this.country = country;
65 this.countryCode = countryCode;
73 public String getCountry() {
77 public String getCountryCode() {
81 public CountryCodeType getCountryCodeType() {
85 public static CountryCode[] getCountryCodes(CountryCodeType clength) {
86 try (GigiPreparedStatement ps = new GigiPreparedStatement(clength.getListQuery(), true)) {
87 GigiResultSet rs = ps.executeQuery();
90 int totalCount = rs.getRow();
94 CountryCode[] finalResult = new CountryCode[totalCount];
96 finalResult[i] = new CountryCode(rs.getInt("id"), rs.getString("country"), rs.getString("countrycode"), clength);
104 public static void checkCountryCode(String countrycode, CountryCodeType cType) throws GigiApiException {
105 if (countrycode.length() != cType.getLen()) {
106 throw new GigiApiException(SprintfCommand.createSimple("Country code length does not have the required length of {0} characters", Integer.toString(cType.getLen())));
109 try (GigiPreparedStatement ps = new GigiPreparedStatement(cType.getValidationQuery())) {
110 ps.setString(1, countrycode.toUpperCase());
111 GigiResultSet rs = ps.executeQuery();
114 throw new GigiApiException(SprintfCommand.createSimple("Country code {0} is not available in database", countrycode.toUpperCase()));
119 public static CountryCode getCountryCode(String countrycode) throws GigiApiException {
120 return getCountryCode(countrycode, CountryCodeType.CODE_2_CHARS);
123 public static CountryCode getCountryCode(String countrycode, CountryCodeType cType) throws GigiApiException {
124 if (countrycode.length() != cType.getLen()) {
125 throw new GigiApiException(SprintfCommand.createSimple("Country code length does not have the required length of {0} characters", Integer.toString(cType.getLen())));
127 try (GigiPreparedStatement ps = new GigiPreparedStatement(cType.getGetQuery())) {
128 ps.setString(1, countrycode.toUpperCase());
129 GigiResultSet rs = ps.executeQuery();
132 throw new GigiApiException(SprintfCommand.createSimple("Country code {0} is not available in database", countrycode.toUpperCase()));
134 return new CountryCode(rs.getInt("id"), rs.getString("country"), rs.getString("countrycode"), cType);
138 public CountryCode convertToCountryCodeType(CountryCodeType ctype) {
139 if (this.ctype.equals(ctype)) {
143 CountryCode[] cclist = getCountryCodes(ctype);
144 for (CountryCode cc : cclist) {
145 if (cc.getId() == this.getId()) {
150 throw new RuntimeException("Internal Error: CountryCode for country not found" + this.getCountry());
153 public static CountryCode getRandomCountry(CountryCodeType cType) {
154 CountryCode[] cc = CountryCode.getCountryCodes(cType);
155 int rnd = new Random().nextInt(cc.length);