Skip to content

Commit 72d5f42

Browse files
committed
store/reference countries according to ISO 3166-1
and use a country dropdown menu rather than free text field
1 parent 88a2d78 commit 72d5f42

7 files changed

Lines changed: 66 additions & 7 deletions

File tree

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,5 +610,13 @@
610610
<scope>test</scope>
611611
</dependency>
612612

613+
<!-- Codes according to ISO standards
614+
https://github.com/TakahikoKawasaki/nv-i18n -->
615+
<dependency>
616+
<groupId>com.neovisionaries</groupId>
617+
<artifactId>nv-i18n</artifactId>
618+
<version>1.18</version>
619+
</dependency>
620+
613621
</dependencies>
614622
</project>

src/main/java/de/rwth/idsg/steve/repository/impl/AddressRepositoryImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private Integer insert(DSLContext ctx, Address ad) {
8181
.set(ADDRESS.HOUSE_NUMBER, ad.getHouseNumber())
8282
.set(ADDRESS.ZIP_CODE, ad.getZipCode())
8383
.set(ADDRESS.CITY, ad.getCity())
84-
.set(ADDRESS.COUNTRY, ad.getCountry())
84+
.set(ADDRESS.COUNTRY, ad.getCountryAlpha2OrNull())
8585
.execute();
8686

8787
if (count != 1) {
@@ -99,7 +99,7 @@ private void update(DSLContext ctx, Address ad) {
9999
.set(ADDRESS.HOUSE_NUMBER, ad.getHouseNumber())
100100
.set(ADDRESS.ZIP_CODE, ad.getZipCode())
101101
.set(ADDRESS.CITY, ad.getCity())
102-
.set(ADDRESS.COUNTRY, ad.getCountry())
102+
.set(ADDRESS.COUNTRY, ad.getCountryAlpha2OrNull())
103103
.where(ADDRESS.ADDRESS_PK.eq(ad.getAddressPk()))
104104
.execute();
105105

src/main/java/de/rwth/idsg/steve/utils/ControllerHelper.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package de.rwth.idsg.steve.utils;
22

3+
import com.neovisionaries.i18n.CountryCode;
34
import de.rwth.idsg.steve.web.dto.Address;
45
import jooq.steve.db.tables.records.AddressRecord;
56

7+
import java.util.Arrays;
68
import java.util.HashMap;
9+
import java.util.LinkedHashMap;
710
import java.util.List;
811
import java.util.Map;
912

@@ -16,6 +19,8 @@ private ControllerHelper() { }
1619

1720
public static final String EMPTY_OPTION = "-- Empty --";
1821

22+
public static final Map<String, String> COUNTRY_DROPDOWN = populateCountryCodes();
23+
1924
public static Address recordToDto(AddressRecord record) {
2025
Address address = new Address();
2126
if (record != null) {
@@ -24,7 +29,7 @@ public static Address recordToDto(AddressRecord record) {
2429
address.setHouseNumber(record.getHouseNumber());
2530
address.setZipCode(record.getZipCode());
2631
address.setCity(record.getCity());
27-
address.setCountry(record.getCountry());
32+
address.setCountry(CountryCode.getByCode(record.getCountry()));
2833
}
2934
return address;
3035
}
@@ -38,4 +43,28 @@ public static Map<String, String> idTagEnhancer(List<String> idTagList) {
3843
}
3944
return map;
4045
}
46+
47+
private static Map<String, String> populateCountryCodes() {
48+
CountryCode[] codes = CountryCode.values();
49+
Arrays.sort(codes, (o1, o2) -> o1.getName().compareTo(o2.getName()));
50+
51+
Map<String, String> map = new LinkedHashMap<>(codes.length + 1);
52+
map.put("", EMPTY_OPTION);
53+
54+
for (CountryCode c : codes) {
55+
if (shouldInclude(c)) {
56+
map.put(c.getAlpha2(), c.getName());
57+
}
58+
}
59+
return map;
60+
}
61+
62+
/**
63+
* There are some invalid codes like {@link CountryCode#UNDEFINED} and {@link CountryCode#EU},
64+
* or some countries are listed twice {@link CountryCode#FI} - {@link CountryCode#SF} and
65+
* {@link CountryCode#GB} - {@link CountryCode#UK} which are confusing. We filter these out.
66+
*/
67+
private static boolean shouldInclude(CountryCode c) {
68+
return c.getAssignment() == CountryCode.Assignment.OFFICIALLY_ASSIGNED;
69+
}
4170
}

src/main/java/de/rwth/idsg/steve/web/controller/ChargePointsController.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,23 @@ public String getDetails(@PathVariable("chargeBoxPk") int chargeBoxPk, Model mod
7777

7878
model.addAttribute("chargePointForm", form);
7979
model.addAttribute("cp", cp);
80+
addCountryCodes(model);
81+
8082
return "data-man/chargepointDetails";
8183
}
8284

8385
@RequestMapping(value = ADD_PATH, method = RequestMethod.GET)
8486
public String addGet(Model model) {
8587
model.addAttribute("chargePointForm", new ChargePointForm());
88+
addCountryCodes(model);
8689
return "data-man/chargepointAdd";
8790
}
8891

8992
@RequestMapping(params = "add", value = ADD_PATH, method = RequestMethod.POST)
9093
public String addPost(@Valid @ModelAttribute("chargePointForm") ChargePointForm chargePointForm,
91-
BindingResult result) {
94+
Model model, BindingResult result) {
9295
if (result.hasErrors()) {
96+
addCountryCodes(model);
9397
return "data-man/chargepointAdd";
9498
}
9599

@@ -99,8 +103,9 @@ public String addPost(@Valid @ModelAttribute("chargePointForm") ChargePointForm
99103

100104
@RequestMapping(params = "update", value = UPDATE_PATH, method = RequestMethod.POST)
101105
public String update(@Valid @ModelAttribute("chargePointForm") ChargePointForm chargePointForm,
102-
BindingResult result) {
106+
Model model, BindingResult result) {
103107
if (result.hasErrors()) {
108+
addCountryCodes(model);
104109
return "data-man/chargepointDetails";
105110
}
106111

@@ -114,6 +119,10 @@ public String delete(@PathVariable("chargeBoxPk") int chargeBoxPk) {
114119
return toOverview();
115120
}
116121

122+
private void addCountryCodes(Model model) {
123+
model.addAttribute("countryCodes", ControllerHelper.COUNTRY_DROPDOWN);
124+
}
125+
117126
// -------------------------------------------------------------------------
118127
// Back to Overview
119128
// -------------------------------------------------------------------------

src/main/java/de/rwth/idsg/steve/web/controller/UsersController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ public String delete(@PathVariable("userPk") int userPk) {
132132
}
133133

134134
private void setTags(Model model) {
135+
model.addAttribute("countryCodes", ControllerHelper.COUNTRY_DROPDOWN);
135136
model.addAttribute("idTagList", ControllerHelper.idTagEnhancer(ocppTagRepository.getIdTags()));
136137
}
137138

src/main/java/de/rwth/idsg/steve/web/dto/Address.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package de.rwth.idsg.steve.web.dto;
22

3+
import com.neovisionaries.i18n.CountryCode;
34
import lombok.Getter;
45
import lombok.Setter;
56

@@ -18,7 +19,7 @@ public class Address {
1819
private String houseNumber;
1920
private String zipCode;
2021
private String city;
21-
private String country;
22+
private CountryCode country;
2223

2324
public boolean isEmpty() {
2425
return addressPk == null
@@ -28,4 +29,15 @@ public boolean isEmpty() {
2829
&& city == null
2930
&& country == null;
3031
}
32+
33+
/**
34+
* Otherwise, if the country field is not set, we would get a NPE.
35+
*/
36+
public String getCountryAlpha2OrNull() {
37+
if (country == null) {
38+
return null;
39+
} else {
40+
return country.getAlpha2();
41+
}
42+
}
3143
}

src/main/resources/webapp/WEB-INF/views/data-man/00-address.jsp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
<tr><td>House Number:</td><td><form:input path="address.houseNumber"/></td></tr>
66
<tr><td>Zip code:</td><td><form:input path="address.zipCode"/></td></tr>
77
<tr><td>City:</td><td><form:input path="address.city"/></td></tr>
8-
<tr><td>Country:</td><td><form:input path="address.country"/></td></tr>
8+
<tr><td>Country:</td><td><form:select path="address.country" items="${countryCodes}"/></td></tr>
99
</table>

0 commit comments

Comments
 (0)