コード例 #1
0
  public void updateDailyExchangeRateTable(
      CurrencyPair currencyPair, List<ExchangeRate> exchangeRates)
      throws CantSaveExchangeRateException {
    List<String> exchangeRateTimestampsInDatabase = new ArrayList<>();

    DatabaseTable table =
        this.database.getTable(
            BitcoinVenezuelaProviderDatabaseConstants.DAILY_EXCHANGE_RATES_TABLE_NAME);
    table.addStringFilter(
        BitcoinVenezuelaProviderDatabaseConstants.DAILY_EXCHANGE_RATES_FROM_CURRENCY_COLUMN_NAME,
        currencyPair.getFrom().getCode(),
        DatabaseFilterType.EQUAL);
    table.addStringFilter(
        BitcoinVenezuelaProviderDatabaseConstants.DAILY_EXCHANGE_RATES_TO_CURRENCY_COLUMN_NAME,
        currencyPair.getTo().getCode(),
        DatabaseFilterType.EQUAL);

    try {
      table.loadToMemory();

      for (DatabaseTableRecord record : table.getRecords()) {
        String timestamp =
            record.getStringValue(
                BitcoinVenezuelaProviderDatabaseConstants
                    .DAILY_EXCHANGE_RATES_TIMESTAMP_COLUMN_NAME);
        exchangeRateTimestampsInDatabase.add(timestamp);
      }
    } catch (CantLoadTableToMemoryException e) {
      throw new CantSaveExchangeRateException(
          CantSaveExchangeRateException.DEFAULT_MESSAGE,
          e,
          "Failed to get ExchangeRates in database for CurrencyPair: " + currencyPair.toString(),
          "Couldn't load table to memory");
    }

    for (ExchangeRate e : exchangeRates) {
      String currentTimestamp = String.valueOf(e.getTimestamp());
      if (!exchangeRateTimestampsInDatabase.contains(currentTimestamp)) {
        this.saveDailyExchangeRate(e); // TODO: improve this.. saving one by one..
      }
    }
  }
コード例 #2
0
  public List<ExchangeRate> getQueriedExchangeRateHistory(
      ExchangeRateType exchangeRateType, CurrencyPair currencyPair)
      throws CantGetExchangeRateException {
    List<ExchangeRate> exchangeRateList = new ArrayList<>();
    DatabaseTable table = null;

    switch (exchangeRateType) {
      case CURRENT:
        table =
            this.database.getTable(
                BitcoinVenezuelaProviderDatabaseConstants.CURRENT_EXCHANGE_RATES_TABLE_NAME);
        table.addStringFilter(
            BitcoinVenezuelaProviderDatabaseConstants
                .CURRENT_EXCHANGE_RATES_FROM_CURRENCY_COLUMN_NAME,
            currencyPair.getFrom().getCode(),
            DatabaseFilterType.EQUAL);
        table.addStringFilter(
            BitcoinVenezuelaProviderDatabaseConstants
                .CURRENT_EXCHANGE_RATES_TO_CURRENCY_COLUMN_NAME,
            currencyPair.getTo().getCode(),
            DatabaseFilterType.EQUAL);
        break;
      case DAILY:
        table =
            this.database.getTable(
                BitcoinVenezuelaProviderDatabaseConstants.DAILY_EXCHANGE_RATES_TABLE_NAME);
        table.addStringFilter(
            BitcoinVenezuelaProviderDatabaseConstants
                .DAILY_EXCHANGE_RATES_FROM_CURRENCY_COLUMN_NAME,
            currencyPair.getFrom().getCode(),
            DatabaseFilterType.EQUAL);
        table.addStringFilter(
            BitcoinVenezuelaProviderDatabaseConstants.DAILY_EXCHANGE_RATES_TO_CURRENCY_COLUMN_NAME,
            currencyPair.getTo().getCode(),
            DatabaseFilterType.EQUAL);
        break;
    }

    try {
      table.loadToMemory();

      for (DatabaseTableRecord record : table.getRecords()) {
        ExchangeRate exchangeRate = constructExchangeRateFromRecord(record);
        exchangeRateList.add(exchangeRate);
      }
    } catch (CantLoadTableToMemoryException e) {
      throw new CantGetExchangeRateException(
          CantGetExchangeRateException.DEFAULT_MESSAGE,
          e,
          "Failed to get History for currencyPair: " + currencyPair.toString(),
          "Couldn't load table to memory");
    } catch (CantCreateExchangeRateException e) {
      throw new CantGetExchangeRateException(
          CantGetExchangeRateException.DEFAULT_MESSAGE,
          e,
          "Failed to get History for currencyPair: " + currencyPair.toString(),
          "Couldn't create ExchangeRate object");
    }

    return exchangeRateList;
  }