]> WPIA git - gigi.git/blob - 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
1 package org.cacert.gigi.dbObjects;
2
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;
7
8 public class CountryCode {
9
10     public enum CountryCodeType {
11         CODE_2_CHARS(2, //
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`=?"),//
15         CODE_3_CHARS(3,//
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`=?");
19
20         private final String listQuery;
21
22         private final String getQuery;
23
24         private final String validationQuery;
25
26         private final int len;
27
28         private CountryCodeType(int len, String listQuery, String getQuery, String validationQuery) {
29             this.len = len;
30             this.listQuery = listQuery;
31             this.getQuery = getQuery;
32             this.validationQuery = validationQuery;
33         }
34
35         public int getLen() {
36             return len;
37         }
38
39         public String getGetQuery() {
40             return getQuery;
41         }
42
43         public String getListQuery() {
44             return listQuery;
45         }
46
47         public String getValidationQuery() {
48             return validationQuery;
49         }
50     }
51
52     private final int id;
53
54     private final String country;
55
56     private final String countryCode;
57
58     public CountryCode(int id, String country, String countryCode) {
59         this.id = id;
60         this.country = country;
61         this.countryCode = countryCode;
62     }
63
64     public int getId() {
65         return id;
66     }
67
68     public String getCountry() {
69         return country;
70     }
71
72     public String getCountryCode() {
73         return countryCode;
74     }
75
76     public static CountryCode[] getCountryCodes(CountryCodeType clength) throws GigiApiException {
77         try (GigiPreparedStatement ps = new GigiPreparedStatement(clength.getListQuery(), true)) {
78             GigiResultSet rs = ps.executeQuery();
79
80             rs.last();
81             int totalCount = rs.getRow();
82             rs.beforeFirst();
83             int i = 0;
84
85             CountryCode[] finalResult = new CountryCode[totalCount];
86             while (rs.next()) {
87                 finalResult[i] = new CountryCode(rs.getInt("id"), rs.getString("country"), rs.getString("countrycode"));
88                 i += 1;
89             }
90
91             return finalResult;
92         }
93     }
94
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())));
98         }
99         try (GigiPreparedStatement ps = new GigiPreparedStatement(cType.getValidationQuery())) {
100             ps.setString(1, countrycode.toUpperCase());
101             GigiResultSet rs = ps.executeQuery();
102
103             if ( !rs.next()) {
104                 throw new GigiApiException(SprintfCommand.createSimple("Country code {0} is not available in database", countrycode.toUpperCase()));
105             }
106         }
107
108     }
109
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())));
113         }
114         try (GigiPreparedStatement ps = new GigiPreparedStatement(cType.getGetQuery())) {
115             ps.setString(1, countrycode.toUpperCase());
116             GigiResultSet rs = ps.executeQuery();
117
118             if ( !rs.next()) {
119                 throw new GigiApiException(SprintfCommand.createSimple("Country code {0} is not available in database", countrycode.toUpperCase()));
120             }
121             return new CountryCode(rs.getInt("id"), rs.getString("country"), rs.getString("countrycode"));
122         }
123
124     }
125 }