public class CountryCode {
public enum CountryCodeType {
- CODE_2_CHARS(2, "SELECT `id`, `english` as country, `code2` as countrycode FROM `countryIsoCode` ORDER BY code2", "SELECT 1 FROM `countryIsoCode` WHERE `code2`=?"),//
- CODE_3_CHARS(3, "SELECT `id`, `english` as country, `code3` as countrycode FROM `countryIsoCode` ORDER BY code3", "SELECT 1 FROM `countryIsoCode` WHERE `code3`=?");
+ CODE_2_CHARS(2, //
+ "SELECT `id`, `english` as country, `code2` as countrycode FROM `countryIsoCode` ORDER BY code2",//
+ "SELECT `id`, `english` as country, `code2` as countrycode FROM `countryIsoCode` WHERE `code2`=?",//
+ "SELECT 1 FROM `countryIsoCode` WHERE `code2`=?"),//
+ CODE_3_CHARS(3,//
+ "SELECT `id`, `english` as country, `code3` as countrycode FROM `countryIsoCode` ORDER BY code3", //
+ "SELECT `id`, `english` as country, `code3` as countrycode FROM `countryIsoCode` WHERE `code3`=?",//
+ "SELECT 1 FROM `countryIsoCode` WHERE `code3`=?");
private final String listQuery;
+ private final String getQuery;
+
private final String validationQuery;
private final int len;
- private CountryCodeType(int len, String listQuery, String validationQuery) {
+ private CountryCodeType(int len, String listQuery, String getQuery, String validationQuery) {
this.len = len;
this.listQuery = listQuery;
+ this.getQuery = getQuery;
this.validationQuery = validationQuery;
}
return len;
}
+ public String getGetQuery() {
+ return getQuery;
+ }
+
public String getListQuery() {
return listQuery;
}
}
}
+
+ public static CountryCode getCountryCode(String countrycode, CountryCodeType cType) throws GigiApiException {
+ if (countrycode.length() != cType.getLen()) {
+ throw new GigiApiException(SprintfCommand.createSimple("Country code length does not have the required length of {0} characters", Integer.toString(cType.getLen())));
+ }
+ try (GigiPreparedStatement ps = new GigiPreparedStatement(cType.getGetQuery())) {
+ ps.setString(1, countrycode.toUpperCase());
+ GigiResultSet rs = ps.executeQuery();
+
+ if ( !rs.next()) {
+ throw new GigiApiException(SprintfCommand.createSimple("Country code {0} is not available in database", countrycode.toUpperCase()));
+ }
+ return new CountryCode(rs.getInt("id"), rs.getString("country"), rs.getString("countrycode"));
+ }
+
+ }
}