]> WPIA git - gigi.git/commitdiff
chg: Refactor CountryCode class
authorBenny Baumann <BenBE1987@gmx.net>
Sun, 14 Aug 2016 15:39:21 +0000 (17:39 +0200)
committerBenny Baumann <BenBE1987@gmx.net>
Sun, 14 Aug 2016 16:29:15 +0000 (18:29 +0200)
This allows both code length transformations as well as dropping of several unnecessary throws declarations.

Change-Id: Iecab2181690907bc0bf9c0dc20d67b08cb929d68

src/org/cacert/gigi/dbObjects/CountryCode.java
src/org/cacert/gigi/output/CountrySelector.java
src/org/cacert/gigi/pages/orga/CreateOrgForm.java

index 47abc2776e818bcb5e375986ab626fddf8beb760..bd22dbc1df746729d6f4d6a1ff278a13316833b9 100644 (file)
@@ -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());
     }
+
 }
index f35a8f299b486a06a7d8adb7457d96aa3f8a1bd6..95e373bacf266cafcd2bc0748452de80bd1dcfaf 100644 (file)
@@ -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);
     }
 
index 2d57a3c4af642c98a1e51df48b0ba1a4919cb4a4..e54eaecdf8355f17405728cc93d76cbfe7d5ddc6 100644 (file)
@@ -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();