1 package org.cacert.gigi.dbObjects;
3 import org.cacert.gigi.GigiApiException;
4 import org.cacert.gigi.database.GigiPreparedStatement;
5 import org.cacert.gigi.database.GigiResultSet;
6 import org.cacert.gigi.output.template.SprintfCommand;
8 public class CountryCode {
10 public enum CountryCodeType {
12 "SELECT `id`, `english` as country, `code2` as countrycode FROM `countryIsoCode` ORDER BY code2",//
13 "SELECT `id`, `english` as country, `code2` as countrycode FROM `countryIsoCode` WHERE `code2`=?",//
14 "SELECT 1 FROM `countryIsoCode` WHERE `code2`=?"),//
16 "SELECT `id`, `english` as country, `code3` as countrycode FROM `countryIsoCode` ORDER BY code3", //
17 "SELECT `id`, `english` as country, `code3` as countrycode FROM `countryIsoCode` WHERE `code3`=?",//
18 "SELECT 1 FROM `countryIsoCode` WHERE `code3`=?");
20 private final String listQuery;
22 private final String getQuery;
24 private final String validationQuery;
26 private final int len;
28 private CountryCodeType(int len, String listQuery, String getQuery, String validationQuery) {
30 this.listQuery = listQuery;
31 this.getQuery = getQuery;
32 this.validationQuery = validationQuery;
39 public String getGetQuery() {
43 public String getListQuery() {
47 public String getValidationQuery() {
48 return validationQuery;
54 private final String country;
56 private final String countryCode;
58 public CountryCode(int id, String country, String countryCode) {
60 this.country = country;
61 this.countryCode = countryCode;
68 public String getCountry() {
72 public String getCountryCode() {
76 public static CountryCode[] getCountryCodes(CountryCodeType clength) throws GigiApiException {
77 try (GigiPreparedStatement ps = new GigiPreparedStatement(clength.getListQuery(), true)) {
78 GigiResultSet rs = ps.executeQuery();
81 int totalCount = rs.getRow();
85 CountryCode[] finalResult = new CountryCode[totalCount];
87 finalResult[i] = new CountryCode(rs.getInt("id"), rs.getString("country"), rs.getString("countrycode"));
95 public static void checkCountryCode(String countrycode, CountryCodeType cType) throws GigiApiException {
96 if (countrycode.length() != cType.getLen()) {
97 throw new GigiApiException(SprintfCommand.createSimple("Country code length does not have the required length of {0} characters", Integer.toString(cType.getLen())));
99 try (GigiPreparedStatement ps = new GigiPreparedStatement(cType.getValidationQuery())) {
100 ps.setString(1, countrycode.toUpperCase());
101 GigiResultSet rs = ps.executeQuery();
104 throw new GigiApiException(SprintfCommand.createSimple("Country code {0} is not available in database", countrycode.toUpperCase()));
110 public static CountryCode getCountryCode(String countrycode, CountryCodeType cType) throws GigiApiException {
111 if (countrycode.length() != cType.getLen()) {
112 throw new GigiApiException(SprintfCommand.createSimple("Country code length does not have the required length of {0} characters", Integer.toString(cType.getLen())));
114 try (GigiPreparedStatement ps = new GigiPreparedStatement(cType.getGetQuery())) {
115 ps.setString(1, countrycode.toUpperCase());
116 GigiResultSet rs = ps.executeQuery();
119 throw new GigiApiException(SprintfCommand.createSimple("Country code {0} is not available in database", countrycode.toUpperCase()));
121 return new CountryCode(rs.getInt("id"), rs.getString("country"), rs.getString("countrycode"));