private final String countryCode;
- public CountryCode(int id, String country, String countryCode) {
+ private final CountryCodeType ctype;
+
+ private CountryCode(int id, String country, String countryCode, CountryCodeType ctype) {
this.id = id;
this.country = country;
this.countryCode = countryCode;
+ this.ctype = ctype;
}
public int getId() {
return countryCode;
}
- public static CountryCode[] getCountryCodes(CountryCodeType clength) throws GigiApiException {
+ public CountryCodeType getCountryCodeType() {
+ return ctype;
+ }
+
+ public static CountryCode[] getCountryCodes(CountryCodeType clength) {
try (GigiPreparedStatement ps = new GigiPreparedStatement(clength.getListQuery(), true)) {
GigiResultSet rs = ps.executeQuery();
CountryCode[] finalResult = new CountryCode[totalCount];
while (rs.next()) {
- finalResult[i] = new CountryCode(rs.getInt("id"), rs.getString("country"), rs.getString("countrycode"));
+ finalResult[i] = new CountryCode(rs.getInt("id"), rs.getString("country"), rs.getString("countrycode"), clength);
i += 1;
}
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();
throw new GigiApiException(SprintfCommand.createSimple("Country code {0} is not available in database", countrycode.toUpperCase()));
}
}
+ }
+ public static CountryCode getCountryCode(String countrycode) throws GigiApiException {
+ return getCountryCode(countrycode, CountryCodeType.CODE_2_CHARS);
}
public static CountryCode getCountryCode(String countrycode, CountryCodeType cType) throws GigiApiException {
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"));
+ return new CountryCode(rs.getInt("id"), rs.getString("country"), rs.getString("countrycode"), cType);
+ }
+ }
+
+ public CountryCode convertToCountryCodeType(CountryCodeType ctype) {
+ if (this.ctype.equals(ctype)) {
+ return this;
+ }
+
+ CountryCode[] cclist = getCountryCodes(ctype);
+ for (CountryCode cc : cclist) {
+ if (cc.getId() == this.getId()) {
+ return cc;
+ }
}
+ throw new RuntimeException("Internal Error: CountryCode for country not found" + this.getCountry());
}
+
}
private boolean optional;
- public CountrySelector(String name, boolean optional) throws GigiApiException {
+ public CountrySelector(String name, boolean optional) {
this.name = name;
this.optional = optional;
}
- public CountrySelector(String name, boolean optional, String state) throws GigiApiException {
+ public CountrySelector(String name, boolean optional, CountryCode state) {
this(name, optional);
- selected = CountryCode.getCountryCode(state, CountryCodeType.CODE_2_CHARS);
-
+ selected = state == null ? null : state.convertToCountryCodeType(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;
throw new GigiApiException("Country code required.");
}
}
- selected = CountryCode.getCountryCode(vS, CountryCodeType.CODE_2_CHARS);
+ selected = CountryCode.getCountryCode(vS, CountryCodeType.CODE_2_CHARS);
}
@Override
vars.put("selected", "");
}
}
+
});
+
vars.put("optional", optional);
vars.put("name", name);
+
t.output(out, l, vars);
}
import javax.servlet.http.HttpServletRequest;
import org.cacert.gigi.GigiApiException;
+import org.cacert.gigi.dbObjects.CountryCode;
import org.cacert.gigi.dbObjects.Organisation;
import org.cacert.gigi.email.EmailProvider;
import org.cacert.gigi.localisation.Language;
public CreateOrgForm(HttpServletRequest hsr) {
super(hsr);
- try {
- cs = new CountrySelector("C", false);
- } catch (GigiApiException e) {
- throw new Error(e); // should not happen
- }
+ cs = new CountrySelector("C", false);
}
public CreateOrgForm(HttpServletRequest hsr, Organisation t) {
isEdit = true;
result = t;
o = t.getName();
+
+ CountryCode orgState = null;
try {
- cs = new CountrySelector("C", false, t.getState());
+ orgState = CountryCode.getCountryCode(t.getState());
} catch (GigiApiException e) {
throw new Error(e);
}
+ cs = new CountrySelector("C", false, orgState);
+
st = t.getProvince();
l = t.getCity();
email = t.getContactEmail();