]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/CountryCode.java
fix: Avoid warning when PoJAM is disabled
[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, "SELECT `id`, `english` as country, `code2` as countrycode FROM `countryIsoCode` ORDER BY code2", "SELECT 1 FROM `countryIsoCode` WHERE `code2`=?"),//
12         CODE_3_CHARS(3, "SELECT `id`, `english` as country, `code3` as countrycode FROM `countryIsoCode` ORDER BY code3", "SELECT 1 FROM `countryIsoCode` WHERE `code3`=?");
13
14         private final String listQuery;
15
16         private final String validationQuery;
17
18         private final int len;
19
20         private CountryCodeType(int len, String listQuery, String validationQuery) {
21             this.len = len;
22             this.listQuery = listQuery;
23             this.validationQuery = validationQuery;
24         }
25
26         public int getLen() {
27             return len;
28         }
29
30         public String getListQuery() {
31             return listQuery;
32         }
33
34         public String getValidationQuery() {
35             return validationQuery;
36         }
37     }
38
39     private final int id;
40
41     private final String country;
42
43     private final String countryCode;
44
45     public CountryCode(int id, String country, String countryCode) {
46         this.id = id;
47         this.country = country;
48         this.countryCode = countryCode;
49     }
50
51     public int getId() {
52         return id;
53     }
54
55     public String getCountry() {
56         return country;
57     }
58
59     public String getCountryCode() {
60         return countryCode;
61     }
62
63     public static CountryCode[] getCountryCodes(CountryCodeType clength) throws GigiApiException {
64         try (GigiPreparedStatement ps = new GigiPreparedStatement(clength.getListQuery(), true)) {
65             GigiResultSet rs = ps.executeQuery();
66
67             rs.last();
68             int totalCount = rs.getRow();
69             rs.beforeFirst();
70             int i = 0;
71
72             CountryCode[] finalResult = new CountryCode[totalCount];
73             while (rs.next()) {
74                 finalResult[i] = new CountryCode(rs.getInt("id"), rs.getString("country"), rs.getString("countrycode"));
75                 i += 1;
76             }
77
78             return finalResult;
79         }
80     }
81
82     public static void checkCountryCode(String countrycode, CountryCodeType cType) throws GigiApiException {
83         if (countrycode.length() != cType.getLen()) {
84             throw new GigiApiException(SprintfCommand.createSimple("Country code length does not have the required length of {0} characters", Integer.toString(cType.getLen())));
85         }
86         try (GigiPreparedStatement ps = new GigiPreparedStatement(cType.getValidationQuery())) {
87             ps.setString(1, countrycode.toUpperCase());
88             GigiResultSet rs = ps.executeQuery();
89
90             if ( !rs.next()) {
91                 throw new GigiApiException(SprintfCommand.createSimple("Country code {0} is not available in database", countrycode.toUpperCase()));
92             }
93         }
94
95     }
96 }