]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/CountryCode.java
add: random countries for tests
[gigi.git] / src / org / cacert / gigi / dbObjects / CountryCode.java
1 package org.cacert.gigi.dbObjects;
2
3 import java.util.Random;
4
5 import org.cacert.gigi.GigiApiException;
6 import org.cacert.gigi.database.GigiPreparedStatement;
7 import org.cacert.gigi.database.GigiResultSet;
8 import org.cacert.gigi.output.template.SprintfCommand;
9
10 public class CountryCode {
11
12     public enum CountryCodeType {
13         CODE_2_CHARS(2, //
14                 "SELECT `id`, `english` as country, `code2` as countrycode FROM `countryIsoCode` ORDER BY code2",//
15                 "SELECT `id`, `english` as country, `code2` as countrycode FROM `countryIsoCode` WHERE `code2`=?",//
16                 "SELECT 1 FROM `countryIsoCode` WHERE `code2`=?"),//
17         CODE_3_CHARS(3,//
18                 "SELECT `id`, `english` as country, `code3` as countrycode FROM `countryIsoCode` ORDER BY code3", //
19                 "SELECT `id`, `english` as country, `code3` as countrycode FROM `countryIsoCode` WHERE `code3`=?",//
20                 "SELECT 1 FROM `countryIsoCode` WHERE `code3`=?");
21
22         private final String listQuery;
23
24         private final String getQuery;
25
26         private final String validationQuery;
27
28         private final int len;
29
30         private CountryCodeType(int len, String listQuery, String getQuery, String validationQuery) {
31             this.len = len;
32             this.listQuery = listQuery;
33             this.getQuery = getQuery;
34             this.validationQuery = validationQuery;
35         }
36
37         public int getLen() {
38             return len;
39         }
40
41         public String getGetQuery() {
42             return getQuery;
43         }
44
45         public String getListQuery() {
46             return listQuery;
47         }
48
49         public String getValidationQuery() {
50             return validationQuery;
51         }
52     }
53
54     private final int id;
55
56     private final String country;
57
58     private final String countryCode;
59
60     private final CountryCodeType ctype;
61
62     private CountryCode(int id, String country, String countryCode, CountryCodeType ctype) {
63         this.id = id;
64         this.country = country;
65         this.countryCode = countryCode;
66         this.ctype = ctype;
67     }
68
69     public int getId() {
70         return id;
71     }
72
73     public String getCountry() {
74         return country;
75     }
76
77     public String getCountryCode() {
78         return countryCode;
79     }
80
81     public CountryCodeType getCountryCodeType() {
82         return ctype;
83     }
84
85     public static CountryCode[] getCountryCodes(CountryCodeType clength) {
86         try (GigiPreparedStatement ps = new GigiPreparedStatement(clength.getListQuery(), true)) {
87             GigiResultSet rs = ps.executeQuery();
88
89             rs.last();
90             int totalCount = rs.getRow();
91             rs.beforeFirst();
92             int i = 0;
93
94             CountryCode[] finalResult = new CountryCode[totalCount];
95             while (rs.next()) {
96                 finalResult[i] = new CountryCode(rs.getInt("id"), rs.getString("country"), rs.getString("countrycode"), clength);
97                 i += 1;
98             }
99
100             return finalResult;
101         }
102     }
103
104     public static void checkCountryCode(String countrycode, CountryCodeType cType) throws GigiApiException {
105         if (countrycode.length() != cType.getLen()) {
106             throw new GigiApiException(SprintfCommand.createSimple("Country code length does not have the required length of {0} characters", Integer.toString(cType.getLen())));
107         }
108
109         try (GigiPreparedStatement ps = new GigiPreparedStatement(cType.getValidationQuery())) {
110             ps.setString(1, countrycode.toUpperCase());
111             GigiResultSet rs = ps.executeQuery();
112
113             if ( !rs.next()) {
114                 throw new GigiApiException(SprintfCommand.createSimple("Country code {0} is not available in database", countrycode.toUpperCase()));
115             }
116         }
117     }
118
119     public static CountryCode getCountryCode(String countrycode) throws GigiApiException {
120         return getCountryCode(countrycode, CountryCodeType.CODE_2_CHARS);
121     }
122
123     public static CountryCode getCountryCode(String countrycode, CountryCodeType cType) throws GigiApiException {
124         if (countrycode.length() != cType.getLen()) {
125             throw new GigiApiException(SprintfCommand.createSimple("Country code length does not have the required length of {0} characters", Integer.toString(cType.getLen())));
126         }
127         try (GigiPreparedStatement ps = new GigiPreparedStatement(cType.getGetQuery())) {
128             ps.setString(1, countrycode.toUpperCase());
129             GigiResultSet rs = ps.executeQuery();
130
131             if ( !rs.next()) {
132                 throw new GigiApiException(SprintfCommand.createSimple("Country code {0} is not available in database", countrycode.toUpperCase()));
133             }
134             return new CountryCode(rs.getInt("id"), rs.getString("country"), rs.getString("countrycode"), cType);
135         }
136     }
137
138     public CountryCode convertToCountryCodeType(CountryCodeType ctype) {
139         if (this.ctype.equals(ctype)) {
140             return this;
141         }
142
143         CountryCode[] cclist = getCountryCodes(ctype);
144         for (CountryCode cc : cclist) {
145             if (cc.getId() == this.getId()) {
146                 return cc;
147             }
148         }
149
150         throw new RuntimeException("Internal Error: CountryCode for country not found" + this.getCountry());
151     }
152
153     public static CountryCode getRandomCountry(CountryCodeType cType) {
154         CountryCode[] cc = CountryCode.getCountryCodes(cType);
155         int rnd = new Random().nextInt(cc.length);
156         return cc[rnd];
157     }
158
159 }