public List<CurrencyDTO> getCurrenciesToDate(Integer languageId, Integer entityId, Date to)
      throws NamingException, SQLException {

    String cacheKey = getCacheKey(languageId, entityId, to);
    List<CurrencyDTO> cachedCurrencies =
        (List<CurrencyDTO>) cache.getFromCache(cacheKey, cacheModel);

    if (cachedCurrencies != null && !cachedCurrencies.isEmpty()) {
      LOG.debug("Cache hit for %s", cacheKey);
      return cachedCurrencies;
    }

    List<CurrencyDTO> currencies = new CurrencyDAS().findAll();

    for (CurrencyDTO currency : currencies) {
      set(currency.getId());
      currency.setName(this.currency.getDescription(languageId));

      // find system rate
      if (currency.getId() == SYSTEM_CURRENCY_ID.intValue()) {
        currency.setSysRate(SYSTEM_CURRENCY_RATE_DEFAULT);
      } else {
        final CurrencyExchangeDTO exchangeRateForDate =
            findExchange(SYSTEM_RATE_ENTITY_ID, currency.getId(), to);
        currency.setSysRate(exchangeRateForDate.getRate());
      }

      // find entity specific rate
      CurrencyExchangeDTO exchange =
          exchangeDas.getExchangeRateForDate(entityId, currency.getId(), to);
      if (exchange != null) {
        currency.setRate(exchange.getRate().toString());
      }

      // set in-use flag
      currency.setInUse(entityHasCurrency(entityId, currency.getId()));
    }

    cache.putInCache(cacheKey, cacheModel, currencies);

    return currencies;
  }
  private CurrencyExchangeDTO findExchange(Integer entityId, Integer currencyId, Date toDate)
      throws SessionInternalError {

    // check for system currency exchange
    if (SYSTEM_CURRENCY_ID.equals(currencyId)) {
      return new CurrencyExchangeDTO(
          0, currency, entityId, SYSTEM_CURRENCY_RATE_DEFAULT, new Date());
    }
    LOG.debug("Get exchange rate for %s for entity %s for date %s", currencyId, entityId, toDate);

    CurrencyExchangeDTO exchange = exchangeDas.getExchangeRateForDate(entityId, currencyId, toDate);
    if (exchange == null) {
      // this entity doesn't have this exchange defined
      // 0 is the default, don't try to use null, it won't work
      exchange = exchangeDas.findExchange(SYSTEM_RATE_ENTITY_ID, currencyId);
      if (exchange == null) {
        throw new SessionInternalError(
            "Currency " + currencyId + " doesn't have a default exchange");
      }
    }
    LOG.debug("Exchange found %s", exchange.getId());
    return exchange;
  }