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 private final CountryCodeType ctype;
60 private CountryCode(int id, String country, String countryCode, CountryCodeType ctype) {
62 this.country = country;
63 this.countryCode = countryCode;
71 public String getCountry() {
75 public String getCountryCode() {
79 public CountryCodeType getCountryCodeType() {
83 public static CountryCode[] getCountryCodes(CountryCodeType clength) {
84 try (GigiPreparedStatement ps = new GigiPreparedStatement(clength.getListQuery(), true)) {
85 GigiResultSet rs = ps.executeQuery();
88 int totalCount = rs.getRow();
92 CountryCode[] finalResult = new CountryCode[totalCount];
94 finalResult[i] = new CountryCode(rs.getInt("id"), rs.getString("country"), rs.getString("countrycode"), clength);
102 public static void checkCountryCode(String countrycode, CountryCodeType cType) throws GigiApiException {
103 if (countrycode.length() != cType.getLen()) {
104 throw new GigiApiException(SprintfCommand.createSimple("Country code length does not have the required length of {0} characters", Integer.toString(cType.getLen())));
107 try (GigiPreparedStatement ps = new GigiPreparedStatement(cType.getValidationQuery())) {
108 ps.setString(1, countrycode.toUpperCase());
109 GigiResultSet rs = ps.executeQuery();
112 throw new GigiApiException(SprintfCommand.createSimple("Country code {0} is not available in database", countrycode.toUpperCase()));
117 public static CountryCode getCountryCode(String countrycode) throws GigiApiException {
118 return getCountryCode(countrycode, CountryCodeType.CODE_2_CHARS);
121 public static CountryCode getCountryCode(String countrycode, CountryCodeType cType) throws GigiApiException {
122 if (countrycode.length() != cType.getLen()) {
123 throw new GigiApiException(SprintfCommand.createSimple("Country code length does not have the required length of {0} characters", Integer.toString(cType.getLen())));
125 try (GigiPreparedStatement ps = new GigiPreparedStatement(cType.getGetQuery())) {
126 ps.setString(1, countrycode.toUpperCase());
127 GigiResultSet rs = ps.executeQuery();
130 throw new GigiApiException(SprintfCommand.createSimple("Country code {0} is not available in database", countrycode.toUpperCase()));
132 return new CountryCode(rs.getInt("id"), rs.getString("country"), rs.getString("countrycode"), cType);
136 public CountryCode convertToCountryCodeType(CountryCodeType ctype) {
137 if (this.ctype.equals(ctype)) {
141 CountryCode[] cclist = getCountryCodes(ctype);
142 for (CountryCode cc : cclist) {
143 if (cc.getId() == this.getId()) {
148 throw new RuntimeException("Internal Error: CountryCode for country not found" + this.getCountry());