From 4dedc3e14f4feed3fedab59b363787cc611e34ad Mon Sep 17 00:00:00 2001 From: Benny Baumann Date: Sun, 14 Aug 2016 17:39:21 +0200 Subject: [PATCH] chg: Refactor CountryCode class This allows both code length transformations as well as dropping of several unnecessary throws declarations. Change-Id: Iecab2181690907bc0bf9c0dc20d67b08cb929d68 --- .../cacert/gigi/dbObjects/CountryCode.java | 34 ++++++++++++++++--- .../cacert/gigi/output/CountrySelector.java | 14 +++++--- .../cacert/gigi/pages/orga/CreateOrgForm.java | 13 +++---- 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/src/org/cacert/gigi/dbObjects/CountryCode.java b/src/org/cacert/gigi/dbObjects/CountryCode.java index 47abc277..bd22dbc1 100644 --- a/src/org/cacert/gigi/dbObjects/CountryCode.java +++ b/src/org/cacert/gigi/dbObjects/CountryCode.java @@ -55,10 +55,13 @@ public class CountryCode { 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() { @@ -73,7 +76,11 @@ public class CountryCode { 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(); @@ -84,7 +91,7 @@ public class CountryCode { 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; } @@ -96,6 +103,7 @@ public class CountryCode { 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(); @@ -104,7 +112,10 @@ public class CountryCode { 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 { @@ -118,8 +129,23 @@ public class CountryCode { 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()); } + } diff --git a/src/org/cacert/gigi/output/CountrySelector.java b/src/org/cacert/gigi/output/CountrySelector.java index f35a8f29..95e373ba 100644 --- a/src/org/cacert/gigi/output/CountrySelector.java +++ b/src/org/cacert/gigi/output/CountrySelector.java @@ -24,20 +24,21 @@ public class CountrySelector implements Outputable { 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; @@ -45,8 +46,8 @@ public class CountrySelector implements Outputable { throw new GigiApiException("Country code required."); } } - selected = CountryCode.getCountryCode(vS, CountryCodeType.CODE_2_CHARS); + selected = CountryCode.getCountryCode(vS, CountryCodeType.CODE_2_CHARS); } @Override @@ -63,9 +64,12 @@ public class CountrySelector implements Outputable { vars.put("selected", ""); } } + }); + vars.put("optional", optional); vars.put("name", name); + t.output(out, l, vars); } diff --git a/src/org/cacert/gigi/pages/orga/CreateOrgForm.java b/src/org/cacert/gigi/pages/orga/CreateOrgForm.java index 2d57a3c4..e54eaecd 100644 --- a/src/org/cacert/gigi/pages/orga/CreateOrgForm.java +++ b/src/org/cacert/gigi/pages/orga/CreateOrgForm.java @@ -6,6 +6,7 @@ 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.Organisation; import org.cacert.gigi.email.EmailProvider; import org.cacert.gigi.localisation.Language; @@ -39,11 +40,7 @@ public class CreateOrgForm extends Form { 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) { @@ -51,11 +48,15 @@ public class CreateOrgForm extends Form { 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(); -- 2.39.2