--- /dev/null
+package org.cacert.gigi.dbObjects;
+
+import org.cacert.gigi.GigiApiException;
+import org.cacert.gigi.database.GigiPreparedStatement;
+import org.cacert.gigi.database.GigiResultSet;
+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`=?");
+
+ private final String listQuery;
+
+ private final String validationQuery;
+
+ private final int len;
+
+ private CountryCodeType(int len, String listQuery, String validationQuery) {
+ this.len = len;
+ this.listQuery = listQuery;
+ this.validationQuery = validationQuery;
+ }
+
+ public int getLen() {
+ return len;
+ }
+
+ public String getListQuery() {
+ return listQuery;
+ }
+
+ public String getValidationQuery() {
+ return validationQuery;
+ }
+ }
+
+ private final int id;
+
+ private final String country;
+
+ private final String countryCode;
+
+ public CountryCode(int id, String country, String countryCode) {
+ this.id = id;
+ this.country = country;
+ this.countryCode = countryCode;
+ }
+
+ public int getId() {
+ return id;
+ }
+
+ public String getCountry() {
+ return country;
+ }
+
+ public String getCountryCode() {
+ return countryCode;
+ }
+
+ public static CountryCode[] getCountryCodes(CountryCodeType clength) throws GigiApiException {
+ try (GigiPreparedStatement ps = new GigiPreparedStatement(clength.getListQuery(), true)) {
+ GigiResultSet rs = ps.executeQuery();
+
+ rs.last();
+ int totalCount = rs.getRow();
+ rs.beforeFirst();
+ int i = 0;
+
+ CountryCode[] finalResult = new CountryCode[totalCount];
+ while (rs.next()) {
+ finalResult[i] = new CountryCode(rs.getInt("id"), rs.getString("country"), rs.getString("countrycode"));
+ i += 1;
+ }
+
+ return finalResult;
+ }
+ }
+
+ public static void checkCountryCode(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.getValidationQuery())) {
+ 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()));
+ }
+ }
+
+ }
+}
import javax.servlet.http.HttpServletRequest;
import org.cacert.gigi.GigiApiException;
+import org.cacert.gigi.dbObjects.CountryCode;
+import org.cacert.gigi.dbObjects.CountryCode.CountryCodeType;
import org.cacert.gigi.dbObjects.Organisation;
import org.cacert.gigi.email.EmailProvider;
import org.cacert.gigi.localisation.Language;
import org.cacert.gigi.output.template.Form;
+import org.cacert.gigi.output.template.IterableDataset;
import org.cacert.gigi.output.template.SprintfCommand;
import org.cacert.gigi.output.template.Template;
import org.cacert.gigi.pages.LoginPage;
private boolean isEdit = false;
+ private CountryCode[] countryCode;
+
public CreateOrgForm(HttpServletRequest hsr) {
super(hsr);
+ try {
+ countryCode = CountryCode.getCountryCodes(CountryCodeType.CODE_2_CHARS);
+ } catch (GigiApiException e) {
+ throw new Error(e); // should not happen
+ }
}
public CreateOrgForm(HttpServletRequest hsr, Organisation t) {
- super(hsr);
+ this(hsr);
isEdit = true;
result = t;
o = t.getName();
private void checkCertData(HttpServletRequest req) throws GigiApiException {
o = extractParam(req, "O");
- c = extractParam(req, "C");
+ c = extractParam(req, "C").toUpperCase();
st = extractParam(req, "ST");
l = extractParam(req, "L");
if (o.length() > 64 || o.length() < 1) {
throw new GigiApiException(SprintfCommand.createSimple("{0} not given or longer than {1} characters", "Organisation name", 64));
}
- if (c.length() != 2) {
- throw new GigiApiException(SprintfCommand.createSimple("{0} not given or not exactly {1} characters long", "Country code", 2));
- }
+
+ CountryCode.checkCountryCode(c, CountryCodeType.CODE_2_CHARS);
if (st.length() > 128 || st.length() < 1) {
throw new GigiApiException(SprintfCommand.createSimple("{0} not given or longer than {1} characters", "State/county", 128));
vars.put("email", email);
vars.put("optionalName", optionalName);
vars.put("postalAddress", postalAddress);
+ vars.put("countryCode", new IterableDataset() {
+
+ int i = 0;
+
+ @Override
+ public boolean next(Language l, Map<String, Object> vars) {
+ if (i >= countryCode.length) {
+ return false;
+ }
+ CountryCode t = countryCode[i++];
+ vars.put("id", t.getId());
+ vars.put("cc", t.getCountryCode());
+ vars.put("display", t.getCountry());
+ if (t.getCountryCode().equals(c)) {
+ vars.put("selected", "selected");
+ } else {
+ vars.put("selected", "");
+ }
+ return true;
+ }
+ });
+ // vars.put("countryCode", countryCode);
if (isEdit) {
vars.put("edit", true);
}
</tr>
<tr>
<td><?=_Country?>:</td>
- <td><input class="form-control" type="text" name="C" value="<?=$C?>" maxlength="2" size="5">
+ <td>
+ <select name="C">
+ <? foreach($countryCode) { ?>
+ <option value="<?=$cc?>" <?=$selected?> ><?=$cc?> - <?=$display?></option>
+ <? } ?>
+ </select>
<?=_(2 letter !'<a href="http://www.iso.org/iso/home/standards/country_codes/iso-3166-1_decoding_table.htm">'ISO code!'</a>')?>
</td>
</tr>
assertNotNull(upCertData(o1, o1.getName(), "D", o1.getProvince(), o1.getCity()));
assertNull(upCertData(o1, o1.getName(), "DE", o1.getProvince(), o1.getCity()));
assertNotNull(upCertData(o1, o1.getName(), "DES", o1.getProvince(), o1.getCity()));
+ // country code does not exist
+ assertNotNull(upCertData(o1, o1.getName(), "DD", o1.getProvince(), o1.getCity()));
+ // 3-letter country code should not be accepted
+ assertNotNull(upCertData(o1, o1.getName(), "DEU", o1.getProvince(), o1.getCity()));
// test contact mail
assertNull(upOptData(o1, o1.getContactEmail()));