]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/CountryCode.java
bd22dbc1df746729d6f4d6a1ff278a13316833b9
[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     private final CountryCodeType ctype;
59
60     private CountryCode(int id, String country, String countryCode, CountryCodeType ctype) {
61         this.id = id;
62         this.country = country;
63         this.countryCode = countryCode;
64         this.ctype = ctype;
65     }
66
67     public int getId() {
68         return id;
69     }
70
71     public String getCountry() {
72         return country;
73     }
74
75     public String getCountryCode() {
76         return countryCode;
77     }
78
79     public CountryCodeType getCountryCodeType() {
80         return ctype;
81     }
82
83     public static CountryCode[] getCountryCodes(CountryCodeType clength) {
84         try (GigiPreparedStatement ps = new GigiPreparedStatement(clength.getListQuery(), true)) {
85             GigiResultSet rs = ps.executeQuery();
86
87             rs.last();
88             int totalCount = rs.getRow();
89             rs.beforeFirst();
90             int i = 0;
91
92             CountryCode[] finalResult = new CountryCode[totalCount];
93             while (rs.next()) {
94                 finalResult[i] = new CountryCode(rs.getInt("id"), rs.getString("country"), rs.getString("countrycode"), clength);
95                 i += 1;
96             }
97
98             return finalResult;
99         }
100     }
101
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())));
105         }
106
107         try (GigiPreparedStatement ps = new GigiPreparedStatement(cType.getValidationQuery())) {
108             ps.setString(1, countrycode.toUpperCase());
109             GigiResultSet rs = ps.executeQuery();
110
111             if ( !rs.next()) {
112                 throw new GigiApiException(SprintfCommand.createSimple("Country code {0} is not available in database", countrycode.toUpperCase()));
113             }
114         }
115     }
116
117     public static CountryCode getCountryCode(String countrycode) throws GigiApiException {
118         return getCountryCode(countrycode, CountryCodeType.CODE_2_CHARS);
119     }
120
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())));
124         }
125         try (GigiPreparedStatement ps = new GigiPreparedStatement(cType.getGetQuery())) {
126             ps.setString(1, countrycode.toUpperCase());
127             GigiResultSet rs = ps.executeQuery();
128
129             if ( !rs.next()) {
130                 throw new GigiApiException(SprintfCommand.createSimple("Country code {0} is not available in database", countrycode.toUpperCase()));
131             }
132             return new CountryCode(rs.getInt("id"), rs.getString("country"), rs.getString("countrycode"), cType);
133         }
134     }
135
136     public CountryCode convertToCountryCodeType(CountryCodeType ctype) {
137         if (this.ctype.equals(ctype)) {
138             return this;
139         }
140
141         CountryCode[] cclist = getCountryCodes(ctype);
142         for (CountryCode cc : cclist) {
143             if (cc.getId() == this.getId()) {
144                 return cc;
145             }
146         }
147
148         throw new RuntimeException("Internal Error: CountryCode for country not found" + this.getCountry());
149     }
150
151 }