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`=?");
+ CODE_2_CHARS(2, //
+ "SELECT `id`, `english` as country, `code2` as countrycode FROM `countryIsoCode` ORDER BY code2",//
+ "SELECT `id`, `english` as country, `code2` as countrycode FROM `countryIsoCode` WHERE `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 `id`, `english` as country, `code3` as countrycode FROM `countryIsoCode` WHERE `code3`=?",//
+ "SELECT 1 FROM `countryIsoCode` WHERE `code3`=?");
private final String listQuery;
+ private final String getQuery;
+
private final String validationQuery;
private final int len;
- private CountryCodeType(int len, String listQuery, String validationQuery) {
+ private CountryCodeType(int len, String listQuery, String getQuery, String validationQuery) {
this.len = len;
this.listQuery = listQuery;
+ this.getQuery = getQuery;
this.validationQuery = validationQuery;
}
return len;
}
+ public String getGetQuery() {
+ return getQuery;
+ }
+
public String getListQuery() {
return listQuery;
}
}
}
+
+ public static CountryCode getCountryCode(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.getGetQuery())) {
+ 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()));
+ }
+ return new CountryCode(rs.getInt("id"), rs.getString("country"), rs.getString("countrycode"));
+ }
+
+ }
}
private String postalAddress;
- public Organisation(String name, String state, String province, String city, String email, String optionalName, String postalAddress, User creator) throws GigiApiException {
+ public Organisation(String name, CountryCode state, String province, String city, String email, String optionalName, String postalAddress, User creator) throws GigiApiException {
if ( !creator.isInGroup(Group.ORGASSURER)) {
throw new GigiApiException("Only Organisation RA Agents may create organisations.");
}
this.name = name;
- this.state = state;
+ this.state = state.getCountryCode();
this.province = province;
this.city = city;
this.email = email;
try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO organisations SET id=?, name=?, state=?, province=?, city=?, contactEmail=?, optional_name=?, postal_address=?, creator=?")) {
ps.setInt(1, id);
ps.setString(2, name);
- ps.setString(3, state);
+ ps.setString(3, state.getCountryCode());
ps.setString(4, province);
ps.setString(5, city);
ps.setString(6, email);
}
}
- public void updateCertData(String o, String c, String st, String l) {
+ public void updateCertData(String o, CountryCode c, String st, String l) {
for (Certificate cert : getCertificates(false)) {
if (cert.getStatus() == CertificateStatus.ISSUED) {
cert.revoke();
}
try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `organisations` SET `name`=?, `state`=?, `province`=?, `city`=? WHERE `id`=?")) {
ps.setString(1, o);
- ps.setString(2, c);
+ ps.setString(2, c.getCountryCode());
ps.setString(3, st);
ps.setString(4, l);
ps.setInt(5, getId());
ps.executeUpdate();
}
name = o;
- state = c;
+ state = c.getCountryCode();
province = st;
city = l;
}
--- /dev/null
+package org.cacert.gigi.output;
+
+import java.io.PrintWriter;
+import java.util.Map;
+
+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.localisation.Language;
+import org.cacert.gigi.output.template.Outputable;
+import org.cacert.gigi.output.template.Template;
+
+public class CountrySelector implements Outputable {
+
+ private static final Template t = new Template(CountrySelector.class.getResource("CountrySelector.templ"));
+
+ private CountryCode[] all = CountryCode.getCountryCodes(CountryCodeType.CODE_2_CHARS);
+
+ private String name;
+
+ private CountryCode selected;
+
+ private boolean optional;
+
+ public CountrySelector(String name, boolean optional) throws GigiApiException {
+ this.name = name;
+ this.optional = optional;
+ }
+
+ public CountrySelector(String name, boolean optional, String state) throws GigiApiException {
+ this(name, optional);
+ selected = CountryCode.getCountryCode(state, CountryCodeType.CODE_2_CHARS);
+
+ }
+
+ public void update(HttpServletRequest r) throws GigiApiException {
+ String vS = r.getParameter(name);
+ selected = null;
+ if (vS == null || vS.equals("invalid")) {
+ if (optional) {
+ return;
+ } else {
+ throw new GigiApiException("Country code required.");
+ }
+ }
+ selected = CountryCode.getCountryCode(vS, CountryCodeType.CODE_2_CHARS);
+
+ }
+
+ @Override
+ public void output(PrintWriter out, Language l, Map<String, Object> vars) {
+ vars.put("countryCode", new ArrayIterable<CountryCode>(all) {
+
+ @Override
+ public void apply(CountryCode t, Language l, Map<String, Object> vars) {
+ vars.put("cc", t.getCountryCode());
+ vars.put("display", t.getCountry());
+ if (selected != null && t.getCountryCode().equals(selected.getCountryCode())) {
+ vars.put("selected", "selected");
+ } else {
+ vars.put("selected", "");
+ }
+ }
+ });
+ vars.put("optional", optional);
+ vars.put("name", name);
+ t.output(out, l, vars);
+ }
+
+ public CountryCode getCountry() {
+ return selected;
+ }
+
+}
--- /dev/null
+<select name='<?=$name?>'>
+<option value='invalid'><? if($optional) { ?><?=_not specified?><? } else { ?><?=_please select...?><? } ?></option>
+<? foreach($countryCode) { ?>
+ <option value="<?=$cc?>" <?=$selected?> ><?=$cc?> - <?=$display?></option>
+<? } ?>
+</select>
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.CountrySelector;
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 String o = "";
- private String c = "";
-
private String st = "";
private String l = "";
private boolean isEdit = false;
- private CountryCode[] countryCode;
+ private CountrySelector cs;
public CreateOrgForm(HttpServletRequest hsr) {
super(hsr);
try {
- countryCode = CountryCode.getCountryCodes(CountryCodeType.CODE_2_CHARS);
+ cs = new CountrySelector("C", false);
} catch (GigiApiException e) {
throw new Error(e); // should not happen
}
isEdit = true;
result = t;
o = t.getName();
- c = t.getState();
+ try {
+ cs = new CountrySelector("C", false, t.getState());
+ } catch (GigiApiException e) {
+ throw new Error(e);
+ }
st = t.getProvince();
l = t.getCity();
email = t.getContactEmail();
if (action.equals("new")) {
checkCertData(req);
checkOrganisationData(req);
- Organisation ne = new Organisation(o, c, st, l, email, optionalName, postalAddress, LoginPage.getUser(req));
+ Organisation ne = new Organisation(o, cs.getCountry(), st, l, email, optionalName, postalAddress, LoginPage.getUser(req));
result = ne;
return true;
} else if (action.equals("updateOrganisationData")) {
return true;
} else if (action.equals("updateCertificateData")) {
checkCertData(req);
- result.updateCertData(o, c, st, l);
+ result.updateCertData(o, cs.getCountry(), st, l);
return true;
}
private void checkCertData(HttpServletRequest req) throws GigiApiException {
o = extractParam(req, "O");
- c = extractParam(req, "C").toUpperCase();
st = extractParam(req, "ST");
l = extractParam(req, "L");
throw new GigiApiException(SprintfCommand.createSimple("{0} not given or longer than {1} characters", "Organisation name", 64));
}
- CountryCode.checkCountryCode(c, CountryCodeType.CODE_2_CHARS);
+ cs.update(req);
if (st.length() > 128 || st.length() < 1) {
throw new GigiApiException(SprintfCommand.createSimple("{0} not given or longer than {1} characters", "State/county", 128));
@Override
protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
vars.put("O", o);
- vars.put("C", c);
+ vars.put("C", cs);
vars.put("ST", st);
vars.put("L", this.l);
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);
+ vars.put("countryCode", cs);
if (isEdit) {
vars.put("edit", true);
}
<tr>
<td><?=_Country?>:</td>
<td>
- <select name="C">
- <? foreach($countryCode) { ?>
- <option value="<?=$cc?>" <?=$selected?> ><?=$cc?> - <?=$display?></option>
- <? } ?>
- </select>
+ <?=$C?>
<?=_(2 letter !'<a href="http://www.iso.org/iso/home/standards/country_codes/iso-3166-1_decoding_table.htm">'ISO code!'</a>')?>
</td>
</tr>
import java.io.IOException;
+import org.cacert.gigi.dbObjects.CountryCode;
+import org.cacert.gigi.dbObjects.CountryCode.CountryCodeType;
import org.cacert.gigi.dbObjects.Group;
import org.cacert.gigi.dbObjects.Organisation;
import org.cacert.gigi.dbObjects.User;
u3.grantGroup(u1, Group.ORGASSURER);
User u4 = User.getById(createAssuranceUser("fn", "ln", createUniqueName() + "@email.org", TEST_PASSWORD));
u4.grantGroup(u1, Group.ORGASSURER);
- Organisation o1 = new Organisation("name", "ST", "prov", "city", "email", "optional name", "postal address", u1);
+ Organisation o1 = new Organisation("name", CountryCode.getCountryCode("DE", CountryCodeType.CODE_2_CHARS), "prov", "city", "email", "optional name", "postal address", u1);
assertEquals(0, o1.getAllAdmins().size());
o1.addAdmin(u2, u1, false);
assertEquals(1, o1.getAllAdmins().size());
import org.cacert.gigi.dbObjects.Certificate;
import org.cacert.gigi.dbObjects.Certificate.CSRType;
import org.cacert.gigi.dbObjects.Certificate.CertificateStatus;
+import org.cacert.gigi.dbObjects.CountryCode.CountryCodeType;
import org.cacert.gigi.dbObjects.CertificateProfile;
+import org.cacert.gigi.dbObjects.CountryCode;
import org.cacert.gigi.dbObjects.Digest;
import org.cacert.gigi.dbObjects.Domain;
import org.cacert.gigi.dbObjects.Group;
makeAssurer(id);
u.grantGroup(u, Group.ORGASSURER);
- Organisation o1 = new Organisation("name", "st", "pr", "st", "test@mail", "", "", u);
+ Organisation o1 = new Organisation("name", CountryCode.getCountryCode("DE", CountryCodeType.CODE_2_CHARS), "pr", "st", "test@mail", "", "", u);
o1.addAdmin(u, u, false);
String testdom = createUniqueName() + "-example.com";
Domain d2 = new Domain(u, o1, testdom);
import java.util.List;
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.dbObjects.Organisation.Affiliation;
import org.cacert.gigi.dbObjects.User;
@Test
public void testUpdateOrgCertData() throws IOException, GigiApiException {
Organisation o1 = createUniqueOrg();
- o1.updateCertData("name", "DE", DIFFICULT_CHARS, "Köln");
+ o1.updateCertData("name", CountryCode.getCountryCode("DE", CountryCodeType.CODE_2_CHARS), DIFFICULT_CHARS, "Köln");
assertEquals("name", o1.getName());
assertEquals("DE", o1.getState());
assertEquals(DIFFICULT_CHARS, o1.getProvince());
c.add(Calendar.MONTH, -Notary.LIMIT_MAX_MONTHS_VERIFICATION + 1);
return sdf.format(new Date(c.getTimeInMillis()));
}
-
}
import java.io.IOException;
import org.cacert.gigi.GigiApiException;
+import org.cacert.gigi.dbObjects.CountryCode;
+import org.cacert.gigi.dbObjects.CountryCode.CountryCodeType;
import org.cacert.gigi.dbObjects.Group;
import org.cacert.gigi.dbObjects.Organisation;
}
public Organisation createUniqueOrg() throws GigiApiException {
- Organisation o1 = new Organisation(createUniqueName(), "st", "pr", "city", "test@example.com", "", "", u);
+ Organisation o1 = new Organisation(createUniqueName(), CountryCode.getCountryCode("DE", CountryCodeType.CODE_2_CHARS), "pr", "city", "test@example.com", "", "", u);
return o1;
}
}
import org.cacert.gigi.dbObjects.Certificate.CSRType;
import org.cacert.gigi.dbObjects.Certificate.SANType;
import org.cacert.gigi.dbObjects.CertificateProfile;
+import org.cacert.gigi.dbObjects.CountryCode;
+import org.cacert.gigi.dbObjects.CountryCode.CountryCodeType;
import org.cacert.gigi.dbObjects.Digest;
import org.cacert.gigi.dbObjects.Group;
import org.cacert.gigi.dbObjects.Organisation;
grant(u.getEmail(), Group.ORGASSURER);
clearCaches();
u = User.getById(u.getId());
- Organisation o = new Organisation(Organisation.SELF_ORG_NAME, "NA", "NA", "NA", "contact@cacert.org", "", "", u);
+ Organisation o = new Organisation(Organisation.SELF_ORG_NAME, CountryCode.getCountryCode("DE", CountryCodeType.CODE_2_CHARS), "NA", "NA", "contact@cacert.org", "", "", u);
assertTrue(o.isSelfOrganisation());
KeyPair kp = generateKeypair();
String key1 = generatePEMCSR(kp, "EMAIL=cats@cacert.org");