public void saveDailyExchangeRate(ExchangeRate e) throws CantSaveExchangeRateException { // Create new exchangeRate with standarized daily timestamp e = new ExchangeRateImpl( e.getFromCurrency(), e.getToCurrency(), e.getSalePrice(), e.getPurchasePrice(), DateHelper.getStandarizedTimestampFromTimestamp(e.getTimestamp())); // If it exists, return if (this.dailyExchangeRateExists(e)) return; // Else, save it DatabaseTable table = this.database.getTable( BitcoinVenezuelaProviderDatabaseConstants.DAILY_EXCHANGE_RATES_TABLE_NAME); DatabaseTableRecord newRecord = table.getEmptyRecord(); constructRecordFromExchangeRate(newRecord, e); try { table.insertRecord(newRecord); } catch (CantInsertRecordException ex) { throw new CantSaveExchangeRateException( CantSaveExchangeRateException.DEFAULT_MESSAGE, ex, "BitcoinVenezuela provider plugin", "Cant save new record in DAILY_EXCHANGE_RATES_TABLE"); } }
public boolean dailyExchangeRateExists(ExchangeRate e) { try { getDailyExchangeRateFromDate( new CurrencyPairImpl(e.getFromCurrency(), e.getToCurrency()), e.getTimestamp()); return true; } catch (CantGetExchangeRateException ex) { return false; } }
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.. } } }
private void constructRecordFromExchangeRate( DatabaseTableRecord newRecord, ExchangeRate exchangeRate) { newRecord.setUUIDValue( BitcoinVenezuelaProviderDatabaseConstants.DAILY_EXCHANGE_RATES_ID_COLUMN_NAME, UUID.randomUUID()); newRecord.setStringValue( BitcoinVenezuelaProviderDatabaseConstants.DAILY_EXCHANGE_RATES_FROM_CURRENCY_COLUMN_NAME, exchangeRate.getFromCurrency().getCode()); newRecord.setStringValue( BitcoinVenezuelaProviderDatabaseConstants.DAILY_EXCHANGE_RATES_TO_CURRENCY_COLUMN_NAME, exchangeRate.getToCurrency().getCode()); newRecord.setStringValue( BitcoinVenezuelaProviderDatabaseConstants.DAILY_EXCHANGE_RATES_SALE_PRICE_COLUMN_NAME, String.valueOf(exchangeRate.getSalePrice())); newRecord.setStringValue( BitcoinVenezuelaProviderDatabaseConstants.DAILY_EXCHANGE_RATES_PURCHASE_PRICE_COLUMN_NAME, String.valueOf(exchangeRate.getPurchasePrice())); newRecord.setLongValue( BitcoinVenezuelaProviderDatabaseConstants.DAILY_EXCHANGE_RATES_TIMESTAMP_COLUMN_NAME, exchangeRate.getTimestamp()); }