Search in sources :

Example 1 with ExchangeRate

use of jgnash.engine.ExchangeRate in project jgnash by ccavanaugh.

the class JpaCommodityDAO method getExchangeNode.

/*
     * @see jgnash.engine.CommodityDAOInterface#getExchangeNode(java.lang.String)
     */
@Override
public ExchangeRate getExchangeNode(final String rateId) {
    ExchangeRate exchangeRate = null;
    try {
        Future<ExchangeRate> future = executorService.submit(() -> {
            emLock.lock();
            try {
                ExchangeRate exchangeRate1 = null;
                final CriteriaBuilder cb = em.getCriteriaBuilder();
                final CriteriaQuery<ExchangeRate> cq = cb.createQuery(ExchangeRate.class);
                final Root<ExchangeRate> root = cq.from(ExchangeRate.class);
                cq.select(root);
                final TypedQuery<ExchangeRate> q = em.createQuery(cq);
                for (final ExchangeRate rate : q.getResultList()) {
                    if (rate.getRateId().equals(rateId)) {
                        exchangeRate1 = rate;
                        break;
                    }
                }
                return exchangeRate1;
            } finally {
                emLock.unlock();
            }
        });
        exchangeRate = future.get();
    } catch (final InterruptedException | ExecutionException e) {
        logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
    }
    return exchangeRate;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ExchangeRate(jgnash.engine.ExchangeRate) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with ExchangeRate

use of jgnash.engine.ExchangeRate in project jgnash by ccavanaugh.

the class MessageBusClient method processRemoteMessage.

/**
     * Takes a remote message and forces remote updates before sending the message to the MessageBus to notify UI
     * components of changes.
     *
     * @param message Message to process and send
     */
private void processRemoteMessage(final Message message) {
    logger.fine("processing a remote message");
    final Engine engine = EngineFactory.getEngine(name);
    Objects.requireNonNull(engine);
    if (message.getChannel() == MessageChannel.ACCOUNT) {
        final Account account = message.getObject(MessageProperty.ACCOUNT);
        switch(message.getEvent()) {
            case ACCOUNT_ADD:
            case ACCOUNT_REMOVE:
                engine.refresh(account);
                message.setObject(MessageProperty.ACCOUNT, engine.getAccountByUuid(account.getUuid()));
                engine.refresh(account.getParent());
                break;
            case ACCOUNT_MODIFY:
            case ACCOUNT_SECURITY_ADD:
            case ACCOUNT_SECURITY_REMOVE:
            case ACCOUNT_VISIBILITY_CHANGE:
                engine.refresh(account);
                message.setObject(MessageProperty.ACCOUNT, engine.getAccountByUuid(account.getUuid()));
                break;
            default:
                break;
        }
    }
    if (message.getChannel() == MessageChannel.BUDGET) {
        final Budget budget = message.getObject(MessageProperty.BUDGET);
        switch(message.getEvent()) {
            case BUDGET_ADD:
            case BUDGET_UPDATE:
            case BUDGET_REMOVE:
            case BUDGET_GOAL_UPDATE:
                engine.refresh(budget);
                message.setObject(MessageProperty.BUDGET, engine.getBudgetByUuid(budget.getUuid()));
                break;
            default:
                break;
        }
    }
    if (message.getChannel() == MessageChannel.COMMODITY) {
        switch(message.getEvent()) {
            case CURRENCY_ADD:
            case CURRENCY_MODIFY:
                final CommodityNode currency = message.getObject(MessageProperty.COMMODITY);
                engine.refresh(currency);
                message.setObject(MessageProperty.COMMODITY, engine.getCurrencyNodeByUuid(currency.getUuid()));
                break;
            case SECURITY_ADD:
            case SECURITY_MODIFY:
            case SECURITY_HISTORY_ADD:
            case SECURITY_HISTORY_REMOVE:
                final CommodityNode node = message.getObject(MessageProperty.COMMODITY);
                engine.refresh(node);
                message.setObject(MessageProperty.COMMODITY, engine.getSecurityNodeByUuid(node.getUuid()));
                break;
            case EXCHANGE_RATE_ADD:
            case EXCHANGE_RATE_REMOVE:
                final ExchangeRate rate = message.getObject(MessageProperty.EXCHANGE_RATE);
                engine.refresh(rate);
                message.setObject(MessageProperty.EXCHANGE_RATE, engine.getExchangeRateByUuid(rate.getUuid()));
                break;
            default:
                break;
        }
    }
    if (message.getChannel() == MessageChannel.CONFIG) {
        switch(message.getEvent()) {
            case CONFIG_MODIFY:
                final Config config = message.getObject(MessageProperty.CONFIG);
                engine.refresh(config);
                message.setObject(MessageProperty.CONFIG, engine.getStoredObjectByUuid(Config.class, config.getUuid()));
                break;
            default:
                break;
        }
    }
    if (message.getChannel() == MessageChannel.REMINDER) {
        switch(message.getEvent()) {
            case REMINDER_ADD:
            case REMINDER_REMOVE:
                final Reminder reminder = message.getObject(MessageProperty.REMINDER);
                engine.refresh(reminder);
                message.setObject(MessageProperty.REMINDER, engine.getReminderByUuid(reminder.getUuid()));
                break;
            default:
                break;
        }
    }
    if (message.getChannel() == MessageChannel.TRANSACTION) {
        switch(message.getEvent()) {
            case TRANSACTION_ADD:
            case TRANSACTION_REMOVE:
                final Transaction transaction = message.getObject(MessageProperty.TRANSACTION);
                engine.refresh(transaction);
                message.setObject(MessageProperty.TRANSACTION, engine.getTransactionByUuid(transaction.getUuid()));
                final Account account = message.getObject(MessageProperty.ACCOUNT);
                engine.refresh(account);
                message.setObject(MessageProperty.ACCOUNT, engine.getAccountByUuid(account.getUuid()));
                break;
            default:
                break;
        }
    }
    /* Flag the message as remote */
    message.setRemote();
    logger.fine("fire remote message");
    MessageBus.getInstance(name).fireEvent(message);
}
Also used : Account(jgnash.engine.Account) ExchangeRate(jgnash.engine.ExchangeRate) Reminder(jgnash.engine.recurring.Reminder) Transaction(jgnash.engine.Transaction) Config(jgnash.engine.Config) CommodityNode(jgnash.engine.CommodityNode) Budget(jgnash.engine.budget.Budget) Engine(jgnash.engine.Engine)

Example 3 with ExchangeRate

use of jgnash.engine.ExchangeRate in project jgnash by ccavanaugh.

the class XStreamCommodityDAO method getExchangeRateByUuid.

@Override
public ExchangeRate getExchangeRateByUuid(final String uuid) {
    ExchangeRate exchangeRate = null;
    StoredObject o = container.get(uuid);
    if (o != null && o instanceof ExchangeRate) {
        exchangeRate = (ExchangeRate) o;
    }
    return exchangeRate;
}
Also used : ExchangeRate(jgnash.engine.ExchangeRate) StoredObject(jgnash.engine.StoredObject)

Example 4 with ExchangeRate

use of jgnash.engine.ExchangeRate in project jgnash by ccavanaugh.

the class JpaCommodityDAO method getExchangeRates.

@Override
public List<ExchangeRate> getExchangeRates() {
    List<ExchangeRate> exchangeRateList = Collections.emptyList();
    try {
        Future<List<ExchangeRate>> future = executorService.submit(() -> {
            emLock.lock();
            try {
                final CriteriaBuilder cb = em.getCriteriaBuilder();
                final CriteriaQuery<ExchangeRate> cq = cb.createQuery(ExchangeRate.class);
                final Root<ExchangeRate> root = cq.from(ExchangeRate.class);
                cq.select(root);
                final TypedQuery<ExchangeRate> q = em.createQuery(cq);
                return stripMarkedForRemoval(new ArrayList<>(q.getResultList()));
            } finally {
                emLock.unlock();
            }
        });
        exchangeRateList = future.get();
    } catch (final InterruptedException | ExecutionException e) {
        logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
    }
    return exchangeRateList;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ExchangeRate(jgnash.engine.ExchangeRate) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with ExchangeRate

use of jgnash.engine.ExchangeRate in project jgnash by ccavanaugh.

the class CurrencyExchangeDialog method removeExchangeRates.

private void removeExchangeRates() {
    ExchangeRate rate = getSelectedExchangeRate();
    if (rate != null) {
        int[] rows = table.getSelectedRows();
        List<ExchangeRateHistoryNode> history = rate.getHistory();
        /* Capture a list of references */
        ExchangeRateHistoryNode[] temp = new ExchangeRateHistoryNode[rows.length];
        for (int i = 0; i < rows.length; i++) {
            temp[i] = history.get(rows[i]);
        }
        final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
        if (engine != null) {
            for (int i = rows.length - 1; i >= 0; i--) {
                engine.removeExchangeRateHistory(rate, temp[i]);
            }
        }
    }
}
Also used : ExchangeRate(jgnash.engine.ExchangeRate) ExchangeRateHistoryNode(jgnash.engine.ExchangeRateHistoryNode) Engine(jgnash.engine.Engine)

Aggregations

ExchangeRate (jgnash.engine.ExchangeRate)6 ExecutionException (java.util.concurrent.ExecutionException)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 Engine (jgnash.engine.Engine)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Account (jgnash.engine.Account)1 CommodityNode (jgnash.engine.CommodityNode)1 Config (jgnash.engine.Config)1 CurrencyNode (jgnash.engine.CurrencyNode)1 ExchangeRateHistoryNode (jgnash.engine.ExchangeRateHistoryNode)1 StoredObject (jgnash.engine.StoredObject)1 Transaction (jgnash.engine.Transaction)1 Budget (jgnash.engine.budget.Budget)1 Reminder (jgnash.engine.recurring.Reminder)1