From a672acaf0fe6940d5876ad731d8f55accd1208c7 Mon Sep 17 00:00:00 2001 From: Laura Luiz Date: Wed, 20 Jan 2016 17:58:24 +0100 Subject: [PATCH] Get currencies from CTP project first --- app/inject/ProjectContextProvider.java | 39 ++++++++------------------ build.sbt | 2 +- conf/application.conf | 2 +- 3 files changed, 14 insertions(+), 29 deletions(-) diff --git a/app/inject/ProjectContextProvider.java b/app/inject/ProjectContextProvider.java index 1e0df27c7..42b253cf2 100644 --- a/app/inject/ProjectContextProvider.java +++ b/app/inject/ProjectContextProvider.java @@ -12,10 +12,10 @@ import javax.inject.Inject; import javax.money.CurrencyUnit; import javax.money.Monetary; -import javax.money.MonetaryAmount; import java.util.List; import java.util.Locale; import java.util.concurrent.ExecutionException; +import java.util.function.Function; import static java.util.Collections.emptyList; import static java.util.stream.Collectors.toList; @@ -51,40 +51,25 @@ public ProjectContext get() { } private List getLanguages(final Project project) { - final List locales = configuration.getStringList(CONFIG_LANGUAGES, project.getLanguages()) - .stream() - .map(Locale::forLanguageTag) - .collect(toList()); - if (locales.isEmpty()) { - throw new SunriseInitializationException("No language defined, neither in project nor in configuration '" + CONFIG_LANGUAGES + "'"); - } - return locales; + return getValues(CONFIG_LANGUAGES, Locale::forLanguageTag, project.getLanguageLocales()); } private List getCountries(final Project project) { - final List countriesFromConfig = getCountriesFromConfig(); - final List countries = countriesFromConfig.isEmpty() ? project.getCountries() : countriesFromConfig; - if (countries.isEmpty()) { - throw new SunriseInitializationException("No country defined, neither in project nor in configuration '" + CONFIG_COUNTRIES + "'"); - } - return countries; + return getValues(CONFIG_COUNTRIES, CountryCode::getByCode, project.getCountries()); } private List getCurrencies(final Project project) { - final List currencies = configuration.getStringList(CONFIG_CURRENCIES, emptyList()) - .stream() - .map(Monetary::getCurrency) - .collect(toList()); - // TODO Fallback to CTP currencies - if (currencies.isEmpty()) { - throw new SunriseInitializationException("No currency defined in configuration '" + CONFIG_CURRENCIES + "'"); - } - return currencies; + return getValues(CONFIG_CURRENCIES, Monetary::getCurrency, project.getCurrencyUnits()); } - private List getCountriesFromConfig() { - return configuration.getStringList(CONFIG_COUNTRIES, emptyList()).stream() - .map(CountryCode::valueOf) + private List getValues(final String configKey, final Function mapper, final List fallbackValues) { + final List valuesFromConfig = configuration.getStringList(configKey, emptyList()).stream() + .map(mapper) .collect(toList()); + final List values = valuesFromConfig.isEmpty() ? fallbackValues : valuesFromConfig; + if (values.isEmpty()) { + throw new SunriseInitializationException("No '" + configKey + "' defined and CTP project information was empty"); + } + return values; } } diff --git a/build.sbt b/build.sbt index 227e6b56c..2ebe5f7e9 100644 --- a/build.sbt +++ b/build.sbt @@ -13,7 +13,7 @@ organization := "io.sphere" lazy val sunriseDesignVersion = "0.45.0" -lazy val sphereJvmSdkVersion = "1.0.0-M25" +lazy val sphereJvmSdkVersion = "1.0.0-M26" lazy val jacksonVersion = "2.6.0" diff --git a/conf/application.conf b/conf/application.conf index 73fa7315b..3787ad58a 100644 --- a/conf/application.conf +++ b/conf/application.conf @@ -18,7 +18,7 @@ play.modules.enabled += "inject.ApplicationProductionModule" # ~~~~~ application.settingsWidget.enabled = true #application.countries = ["DE", "US", "GB", "AT", "CH"] # uncomment to override countries from CTP project, default first -application.currencies = ["EUR"] # uncomment to override currencies from CTP project, default first +#application.currencies = ["EUR"] # uncomment to override currencies from CTP project, default first #application.i18n.languages = ["en", "de", "es"] # uncomment to override languages from CTP project, default first application.i18n.bundles = ["translations", "home", "catalog", "checkout"] application.i18n.resolverLoaders = [