Class DefaultCurrencyConversionService
java.lang.Object
com.broadleafcommerce.pricing.service.DefaultCurrencyConversionService
- All Implemented Interfaces:
CurrencyConversionService
Implementation of
CurrencyConversionService that relies on an
implementation of ExchangeRateService and provides a mapping from and to for each item included
in the ExchangeRateService- Author:
- Brian Polster (bpolster)
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected com.broadleafcommerce.pricing.client.domain.PriceTypeDetailbuildConvertedPriceTypeDetail(@NonNull com.broadleafcommerce.pricing.client.domain.PriceTypeDetail priceTypeDetail, @NonNull javax.money.CurrencyUnit requestedCurrency) Builds aconvertedPriceTypeDetailin therequestedCurrencyfrom the existingPriceTypeDetailprotected com.broadleafcommerce.pricing.client.domain.ExchangeRateDetailbuildExchangeRateDetail(@NonNull javax.money.convert.ExchangeRate exchangeRate) Builds an instance ofExchangeRateDetailfrom theExchangeRateprotected Map<javax.money.CurrencyUnit,javax.money.convert.ExchangeRate> buildExchangeRatesFromMap(javax.money.CurrencyUnit targetCurrency, Map<javax.money.CurrencyUnit, javax.money.convert.ExchangeRate> baseExchangeRates) booleancanCovert(javax.money.CurrencyUnit from, javax.money.CurrencyUnit to) Determine if conversion support is available based on a currencyCodefromandto.protected booleanjavax.money.MonetaryAmountconvert(javax.money.MonetaryAmount fromAmount, javax.money.CurrencyUnit toCurrency) Converts the passed in MonetaryAmount to the requested currency.protected javax.money.convert.ExchangeRateconvertExchangeRate(javax.money.convert.ExchangeRate fromBaseToSrc, javax.money.convert.ExchangeRate fromBaseToTarget, javax.money.NumberValue newFactor) booleanprotected javax.money.convert.ConversionContextjavax.money.CurrencyUnitprotected RoundingModeprotected intprotected Optional<javax.money.convert.ExchangeRate>getExchangeRate(@NonNull javax.money.CurrencyUnit from, @NonNull javax.money.CurrencyUnit to) Gets theExchangeRatewhen convertingfromcurrency to thetocurrency.protected Optional<com.broadleafcommerce.pricing.client.domain.ExchangeRateDetail>getExchangeRateDetail(@NonNull javax.money.CurrencyUnit from, @NonNull javax.money.CurrencyUnit to) Gets theExchangeRateDetailwhen convertingfromcurrency to thetocurrency.protected com.broadleafcommerce.common.extension.TypeFactoryinthashCode()voidinit()protected javax.money.convert.ExchangeRateinvertExchangeRate(javax.money.convert.ExchangeRate origRate, javax.money.NumberValue newFactor) booleanprotected voidpopulateBaseExchangeRates(ExchangeRateInfo exchangeRateInfo, Map<javax.money.CurrencyUnit, javax.money.convert.ExchangeRate> rates) <P extends com.broadleafcommerce.pricing.client.domain.PriceInfo>
voidpopulateCurrencyConversionDetails(@NonNull Collection<P> priceInfos, @NonNull com.broadleafcommerce.pricing.client.domain.context.PriceInfoContext priceInfoContext, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Converts the currencies on the priceable fields on thepriceInfosfrom the existing currency into thePriceInfoContext.getRequestedCurrency().protected voidpopulateOtherExchangeRatesFromBase(javax.money.CurrencyUnit baseCurrency, Map<javax.money.CurrencyUnit, Map<javax.money.CurrencyUnit, javax.money.convert.ExchangeRate>> ratesMap) voidprotected voidrefreshExchangeRateDataByCurrency(Optional<String> baseCurrency) voidsetAllowMappingThroughBaseCurrency(boolean allowMappingThroughBaseCurrency) voidsetBaseCurrencies(List<String> baseCurrencies) voidsetDefaultCurrency(javax.money.CurrencyUnit defaultCurrency) voidsetExchangeMap(Map<javax.money.CurrencyUnit, Map<javax.money.CurrencyUnit, javax.money.convert.ExchangeRate>> exchangeMap) voidsetExchangeRateInfoProvider(ExchangeRateInfoProvider exchangeRateInfoProvider) voidsetTypeFactory(com.broadleafcommerce.common.extension.TypeFactory typeFactory) protected booleanshouldConvertCurrency(@NonNull javax.money.CurrencyUnit existingCurrency, @NonNull javax.money.CurrencyUnit requestedCurrency) Whether the currency conversion is supported from the existing currency to the requested currency.toString()
-
Constructor Details
-
DefaultCurrencyConversionService
-
-
Method Details
-
init
@PostConstruct public void init() -
canCovert
public boolean canCovert(javax.money.CurrencyUnit from, javax.money.CurrencyUnit to) Description copied from interface:CurrencyConversionServiceDetermine if conversion support is available based on a currencyCodefromandto.- Specified by:
canCovertin interfaceCurrencyConversionService- Parameters:
from- The currency to convert from.to- The currency to convert to.- Returns:
- whether the currency can be converted
-
convert
public javax.money.MonetaryAmount convert(javax.money.MonetaryAmount fromAmount, javax.money.CurrencyUnit toCurrency) Converts the passed in MonetaryAmount to the requested currency. The value will be rounded to the scale appropriate for the currency and as such, this method should not be used in intermediary calculations.Throws
NullPointerExceptionif the currencies cannot be converted. Call canConvert(from,to) before calling to ensure support for the convert operation.- Specified by:
convertin interfaceCurrencyConversionService- Parameters:
fromAmount- The original amount to converttoCurrency- The currency to which thefromAmountis to be converted to- Returns:
- The original amount in the converted currency
-
populateCurrencyConversionDetails
public <P extends com.broadleafcommerce.pricing.client.domain.PriceInfo> void populateCurrencyConversionDetails(@NonNull @NonNull Collection<P> priceInfos, @NonNull @NonNull com.broadleafcommerce.pricing.client.domain.context.PriceInfoContext priceInfoContext, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Description copied from interface:CurrencyConversionServiceConverts the currencies on the priceable fields on thepriceInfosfrom the existing currency into thePriceInfoContext.getRequestedCurrency(). The converted currencies are stored on thePriceInfo.getConvertedPriceTypeDetails().- Specified by:
populateCurrencyConversionDetailsin interfaceCurrencyConversionService- Parameters:
priceInfos- ThepriceInfoshaving the currencies to convert.priceInfoContext- ThePriceInfoContextcontaining the requested currency of the cart.contextInfo- Context information regarding the current API request
-
buildConvertedPriceTypeDetail
protected com.broadleafcommerce.pricing.client.domain.PriceTypeDetail buildConvertedPriceTypeDetail(@NonNull @NonNull com.broadleafcommerce.pricing.client.domain.PriceTypeDetail priceTypeDetail, @NonNull @NonNull javax.money.CurrencyUnit requestedCurrency) Builds aconvertedPriceTypeDetailin therequestedCurrencyfrom the existingPriceTypeDetail- Parameters:
priceTypeDetail- The existingPriceTypeDetailto convertrequestedCurrency- TheCurrencyUnitin which the existing PriceTypeDetail is to be converted to- Returns:
- The converted
PriceTypeDetailin therequestedCurrency
-
shouldConvertCurrency
protected boolean shouldConvertCurrency(@NonNull @NonNull javax.money.CurrencyUnit existingCurrency, @NonNull @NonNull javax.money.CurrencyUnit requestedCurrency) Whether the currency conversion is supported from the existing currency to the requested currency.- Parameters:
existingCurrency- theCurrencyUnitfrom which to convertrequestedCurrency- theCurrencyUnitto which to convert- Returns:
- whether the currency conversion is supported.
-
getExchangeRate
protected Optional<javax.money.convert.ExchangeRate> getExchangeRate(@NonNull @NonNull javax.money.CurrencyUnit from, @NonNull @NonNull javax.money.CurrencyUnit to) Gets theExchangeRatewhen convertingfromcurrency to thetocurrency.- Parameters:
from- the currency from which to convertto- the currency to which to convert- Returns:
- the
ExchangeRateto convertfromtotocurrency
-
getExchangeRateDetail
protected Optional<com.broadleafcommerce.pricing.client.domain.ExchangeRateDetail> getExchangeRateDetail(@NonNull @NonNull javax.money.CurrencyUnit from, @NonNull @NonNull javax.money.CurrencyUnit to) Gets theExchangeRateDetailwhen convertingfromcurrency to thetocurrency.- Parameters:
from- the currency from which to convertto- the currency to which to convert- Returns:
- the
ExchangeRateDetailto convertfromtotocurrency
-
buildExchangeRateDetail
protected com.broadleafcommerce.pricing.client.domain.ExchangeRateDetail buildExchangeRateDetail(@NonNull @NonNull javax.money.convert.ExchangeRate exchangeRate) Builds an instance ofExchangeRateDetailfrom theExchangeRate- Parameters:
exchangeRate- theExchangeRateto convert intoExchangeRateDetail- Returns:
- the
ExchangeRateDetailbuilt fromExchangeRate
-
refreshExchangeRateData
@Scheduled(fixedDelayString="${exchange.refresh.rate.in.milliseconds:86400000}") public void refreshExchangeRateData() -
refreshExchangeRateDataByCurrency
-
populateBaseExchangeRates
protected void populateBaseExchangeRates(ExchangeRateInfo exchangeRateInfo, Map<javax.money.CurrencyUnit, javax.money.convert.ExchangeRate> rates) -
populateOtherExchangeRatesFromBase
-
buildExchangeRatesFromMap
-
invertExchangeRate
protected javax.money.convert.ExchangeRate invertExchangeRate(javax.money.convert.ExchangeRate origRate, javax.money.NumberValue newFactor) -
getDefaultContext
protected javax.money.convert.ConversionContext getDefaultContext() -
convertExchangeRate
protected javax.money.convert.ExchangeRate convertExchangeRate(javax.money.convert.ExchangeRate fromBaseToSrc, javax.money.convert.ExchangeRate fromBaseToTarget, javax.money.NumberValue newFactor) -
getDefaultRoundingMode
-
getDefaultScale
protected int getDefaultScale() -
getDefaultCurrency
public javax.money.CurrencyUnit getDefaultCurrency() -
getExchangeRateInfoProvider
-
getExchangeMap
-
getBaseCurrencies
-
isAllowMappingThroughBaseCurrency
public boolean isAllowMappingThroughBaseCurrency() -
setDefaultCurrency
public void setDefaultCurrency(javax.money.CurrencyUnit defaultCurrency) -
setExchangeRateInfoProvider
-
setExchangeMap
-
setBaseCurrencies
-
setAllowMappingThroughBaseCurrency
public void setAllowMappingThroughBaseCurrency(boolean allowMappingThroughBaseCurrency) -
equals
-
canEqual
-
hashCode
public int hashCode() -
toString
-
getTypeFactory
protected com.broadleafcommerce.common.extension.TypeFactory getTypeFactory() -
setTypeFactory
@Autowired public void setTypeFactory(com.broadleafcommerce.common.extension.TypeFactory typeFactory)
-