11package de .rwth .idsg .steve .utils ;
22
3+ import com .neovisionaries .i18n .CountryCode ;
34import de .rwth .idsg .steve .web .dto .Address ;
45import jooq .steve .db .tables .records .AddressRecord ;
56
7+ import java .util .Arrays ;
68import java .util .HashMap ;
9+ import java .util .LinkedHashMap ;
710import java .util .List ;
811import 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}
0 commit comments