Class DefaultCurrencyConversionService

java.lang.Object
com.broadleafcommerce.pricing.service.DefaultCurrencyConversionService
All Implemented Interfaces:
CurrencyConversionService

public class DefaultCurrencyConversionService extends Object implements 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 Details

  • Method Details

    • init

      @PostConstruct public void init()
    • canCovert

      public boolean canCovert(javax.money.CurrencyUnit from, javax.money.CurrencyUnit to)
      Description copied from interface: CurrencyConversionService
      Determine if conversion support is available based on a currencyCode from and to.
      Specified by:
      canCovert in interface CurrencyConversionService
      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 NullPointerException if the currencies cannot be converted. Call canConvert(from,to) before calling to ensure support for the convert operation.

      Specified by:
      convert in interface CurrencyConversionService
      Parameters:
      fromAmount - The original amount to convert
      toCurrency - The currency to which the fromAmount is 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: CurrencyConversionService
      Converts the currencies on the priceable fields on the priceInfos from the existing currency into the PriceInfoContext.getRequestedCurrency(). The converted currencies are stored on the PriceInfo.getConvertedPriceTypeDetails().
      Specified by:
      populateCurrencyConversionDetails in interface CurrencyConversionService
      Parameters:
      priceInfos - The priceInfos having the currencies to convert.
      priceInfoContext - The PriceInfoContext containing 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 a convertedPriceTypeDetail in the requestedCurrency from the existing PriceTypeDetail
      Parameters:
      priceTypeDetail - The existing PriceTypeDetail to convert
      requestedCurrency - The CurrencyUnit in which the existing PriceTypeDetail is to be converted to
      Returns:
      The converted PriceTypeDetail in the requestedCurrency
    • 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 - the CurrencyUnit from which to convert
      requestedCurrency - the CurrencyUnit to 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 the ExchangeRate when converting from currency to the to currency.
      Parameters:
      from - the currency from which to convert
      to - the currency to which to convert
      Returns:
      the ExchangeRate to convert from to to currency
    • getExchangeRateDetail

      protected Optional<com.broadleafcommerce.pricing.client.domain.ExchangeRateDetail> getExchangeRateDetail(@NonNull @NonNull javax.money.CurrencyUnit from, @NonNull @NonNull javax.money.CurrencyUnit to)
      Gets the ExchangeRateDetail when converting from currency to the to currency.
      Parameters:
      from - the currency from which to convert
      to - the currency to which to convert
      Returns:
      the ExchangeRateDetail to convert from to to currency
    • buildExchangeRateDetail

      protected com.broadleafcommerce.pricing.client.domain.ExchangeRateDetail buildExchangeRateDetail(@NonNull @NonNull javax.money.convert.ExchangeRate exchangeRate)
      Builds an instance of ExchangeRateDetail from the ExchangeRate
      Parameters:
      exchangeRate - the ExchangeRate to convert into ExchangeRateDetail
      Returns:
      the ExchangeRateDetail built from ExchangeRate
    • refreshExchangeRateData

      @Scheduled(fixedDelayString="${exchange.refresh.rate.in.milliseconds:86400000}") public void refreshExchangeRateData()
    • refreshExchangeRateDataByCurrency

      protected void refreshExchangeRateDataByCurrency(Optional<String> baseCurrency)
    • populateBaseExchangeRates

      protected void populateBaseExchangeRates(ExchangeRateInfo exchangeRateInfo, Map<javax.money.CurrencyUnit,javax.money.convert.ExchangeRate> rates)
    • populateOtherExchangeRatesFromBase

      protected void populateOtherExchangeRatesFromBase(javax.money.CurrencyUnit baseCurrency, Map<javax.money.CurrencyUnit,Map<javax.money.CurrencyUnit,javax.money.convert.ExchangeRate>> ratesMap)
    • buildExchangeRatesFromMap

      protected Map<javax.money.CurrencyUnit,javax.money.convert.ExchangeRate> buildExchangeRatesFromMap(javax.money.CurrencyUnit targetCurrency, Map<javax.money.CurrencyUnit,javax.money.convert.ExchangeRate> baseExchangeRates)
    • 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

      protected RoundingMode getDefaultRoundingMode()
    • getDefaultScale

      protected int getDefaultScale()
    • getDefaultCurrency

      public javax.money.CurrencyUnit getDefaultCurrency()
    • getExchangeRateInfoProvider

      public ExchangeRateInfoProvider getExchangeRateInfoProvider()
    • getExchangeMap

      public Map<javax.money.CurrencyUnit,Map<javax.money.CurrencyUnit,javax.money.convert.ExchangeRate>> getExchangeMap()
    • getBaseCurrencies

      public List<String> getBaseCurrencies()
    • isAllowMappingThroughBaseCurrency

      public boolean isAllowMappingThroughBaseCurrency()
    • setDefaultCurrency

      public void setDefaultCurrency(javax.money.CurrencyUnit defaultCurrency)
    • setExchangeRateInfoProvider

      public void setExchangeRateInfoProvider(ExchangeRateInfoProvider exchangeRateInfoProvider)
    • setExchangeMap

      public void setExchangeMap(Map<javax.money.CurrencyUnit,Map<javax.money.CurrencyUnit,javax.money.convert.ExchangeRate>> exchangeMap)
    • setBaseCurrencies

      public void setBaseCurrencies(List<String> baseCurrencies)
    • setAllowMappingThroughBaseCurrency

      public void setAllowMappingThroughBaseCurrency(boolean allowMappingThroughBaseCurrency)
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • canEqual

      protected boolean canEqual(Object other)
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getTypeFactory

      protected com.broadleafcommerce.common.extension.TypeFactory getTypeFactory()
    • setTypeFactory

      @Autowired public void setTypeFactory(com.broadleafcommerce.common.extension.TypeFactory typeFactory)