Skip to content

Commit

Permalink
Merge pull request #260 from sphereio/currencies-from-project
Browse files Browse the repository at this point in the history
Get currencies from CTP project first
  • Loading branch information
lauraluiz committed Jan 20, 2016
2 parents 55e3b0e + a672aca commit fc87238
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 29 deletions.
39 changes: 12 additions & 27 deletions app/inject/ProjectContextProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,40 +51,25 @@ public ProjectContext get() {
}

private List<Locale> getLanguages(final Project project) {
final List<Locale> 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<CountryCode> getCountries(final Project project) {
final List<CountryCode> countriesFromConfig = getCountriesFromConfig();
final List<CountryCode> 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<CurrencyUnit> getCurrencies(final Project project) {
final List<CurrencyUnit> 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<CountryCode> getCountriesFromConfig() {
return configuration.getStringList(CONFIG_COUNTRIES, emptyList()).stream()
.map(CountryCode::valueOf)
private <T> List<T> getValues(final String configKey, final Function<String, T> mapper, final List<T> fallbackValues) {
final List<T> valuesFromConfig = configuration.getStringList(configKey, emptyList()).stream()
.map(mapper)
.collect(toList());
final List<T> values = valuesFromConfig.isEmpty() ? fallbackValues : valuesFromConfig;
if (values.isEmpty()) {
throw new SunriseInitializationException("No '" + configKey + "' defined and CTP project information was empty");
}
return values;
}
}
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down

0 comments on commit fc87238

Please sign in to comment.