]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/dbObjects/CountryCode.java
add: factor out country selection and type-restrict internal api.
[gigi.git] / src / org / cacert / gigi / dbObjects / CountryCode.java
index bf9375ce81afae29c4852b81f95cad1f1192e17b..47abc2776e818bcb5e375986ab626fddf8beb760 100644 (file)
@@ -8,18 +8,27 @@ import org.cacert.gigi.output.template.SprintfCommand;
 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;
         }
 
@@ -27,6 +36,10 @@ public class CountryCode {
             return len;
         }
 
+        public String getGetQuery() {
+            return getQuery;
+        }
+
         public String getListQuery() {
             return listQuery;
         }
@@ -93,4 +106,20 @@ public class CountryCode {
         }
 
     }
+
+    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"));
+        }
+
+    }
 }